Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Paboda Hettiarachchi
Paboda Hettiarachchi

Posted on • Edited on

     

Custom popup for header click - Magento 2

I was trying to implement a custom popup when a header link was clicked. Tried a few options.

1) Magento Modal
2) A JS file with toggleClass
3) DropdownDialog widget - So far the best option

1) Magento Modal
Issues noticed was the overlay was unwanted and the positioning the popup was difficult.

2) A JS file with toggleClass
Issues noticed here was the click away did not close the popup. The same header link had to be clicked to close the popup.

<?php/** @var \Vendor\Module\Block\Link $block *//* @var \Magento\Framework\Escaper $escaper */?><li data-mage-init='{"Vendor_Module/js/header-subscribe": {}}'>    <div></div>    <div>        <form              novalidate              action="<?= $escaper->escapeUrl($block->getFormActionUrl()) ?>"              method="post"              data-mage-init='{"validation": {"errorClass": "mage-error"}}'             >            <div>                <div>                    <label for="newsletter">                        <input name="email"                               type="email"                                                             placeholder="<?= $escaper->escapeHtml(__('Your email')) ?>"                               data-mage-init='{"mage/trim-input":{}}'                               data-validate="{required:true, 'validate-email':true}"                        />                    </label>                </div>            </div>            <div>                <button                        title="<?= $escaper->escapeHtmlAttr(__('Sign Up')) ?>"                        type="submit"                        aria-label="Subscribe">                    <span><?= $escaper->escapeHtml(__('Sign Up')) ?></span>                </button>            </div>        </form>    </div></li>
Enter fullscreen modeExit fullscreen mode
define([    'jquery'], function($) {    return function(config, element) {        $('.subscribe-link', $(element)).click(function(){            $('.content', $(element)).toggleClass('active');        });    };});
Enter fullscreen modeExit fullscreen mode
  • Some CSS work is required

3) DropdownDialog widget

<li data-block="dropdown">    <div data-trigger="trigger"></div><div     data-mage-init='{        "dropdownDialog": {            "appendTo": "[data-block=dropdown]",            "triggerTarget":"[data-trigger=trigger]",            "timeout": "2000",            "closeOnMouseLeave": false,            "closeOnEscape": true,            "autoOpen": false,            "triggerClass": "",            "parentClass": "active",            "buttons": []        }     }'>    <div>        <form              novalidate              action="<?= $escaper->escapeUrl($block->getFormActionUrl()) ?>"              method="post"              data-mage-init='{"validation": {"errorClass": "mage-error"}}'             >            <div>                <div>                    <label for="newsletter">                        <input name="email"                               type="email"                                                             placeholder="<?= $escaper->escapeHtml(__('Your email')) ?>"                               data-mage-init='{"mage/trim-input":{}}'                               data-validate="{required:true, 'validate-email':true}"                        />                    </label>                </div>            </div>            <div>                <button                        title="<?= $escaper->escapeHtmlAttr(__('Sign Up')) ?>"                        type="submit"                        aria-label="Subscribe">                    <span><?= $escaper->escapeHtml(__('Sign Up')) ?></span>                </button>            </div>        </form>    </div></div></li>
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a Magento programer. Like to travel and have work life balance so love to work remotely.
  • Joined

More fromPaboda Hettiarachchi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp