-
theoctaneagency
Hello,
I’m trying to trigger a modal (based on BasicLightbox) from my website header.
My code works fine if triggered from page content (e.g. a Custom HTML block) but breaks if added to the header. I’m wondering if the issue is caused by any of the JS GeneratePress uses. If so, do you have a recommended work around?
Here is a simplified version of the code:
<a href="#" id="advanced-search">Search</a> <template id="advanced-search-form"> <div class="modal"> <p>Test modal.</p> </div> </template> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/basiclightbox/dist/basicLightbox.min.css?ver=6.3.1" media="all"> <script src="https://cdn.jsdelivr.net/npm/basiclightbox/dist/basicLightbox.min.js?ver=6.3.1"></script> <script> document.addEventListener("DOMContentLoaded", function() { const instance = basicLightbox.create( document.querySelector("template#advanced-search-form") ); document.getElementById("advanced-search").onclick = instance.show; }); </script>
Additional Details
I was trying to add the code to the header using the GP docs here:
https://docs.generatepress.com/article/generate_menu_bar_items/add_action( 'generate_menu_bar_items', function() { ?> <span class="menu-bar-item"> <a href="#">Your icon HTML</a> </span> <?php } );
-
Fernando
Hi there,
At first glance, your code doesn’t seem to have any conflict with ours.
What I can think of to easily resolve this is to use the Search Modal of GP to create your custom Header Modal.
If you’re open to this, there’s a Block Element – Search Modal feature that would allow you to fully alter the output of this modal. With this, you can add anything of your liking.
-
theoctaneagency
I actually tried the Search modal first. We had a few issues with default behavior that made it not a good options.
The big ones are the search modal automatically focuses the 1st field (which triggers a date select pop-up that covers most of the form) and the search modal content isn’t scrollable (our form can get pretty tall on some screens).
-
Hi there,
can we see the issue on your site ? Might be able to diagnose what the issue with that script is.
-
theoctaneagency
Sure David. I’ve added a staging site URL to my Private Information box. Look for the “Test Modal Trigger” link in the header.
-
Fernando
I see. For testing purposes, can you also add another “Test Modal Trigger” in the content so we can observe how it works as well live on the site?
- You must be logged in to reply to this topic.