Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroying instances #312

Open
rctneil opened this issue Apr 10, 2020 · 10 comments
Open

Destroying instances #312

rctneil opened this issue Apr 10, 2020 · 10 comments
Labels
multiple modals Related to multiple modals showing on the same page (including nested) needs more info User needs to provide more information before we can debug further

Comments

@rctneil
Copy link

rctneil commented Apr 10, 2020

Hi,

I've just added this to a local project i'm working on. It's great but I have a grid of items, on which some of them have a Micro Modal set up. These work perfectly but when I use Infinite Scroll to append more grid items to the bottom, Micro Modal does not work on those items which require it that have been appended.

What is the best way of destroying all the Micro Modal instances and recreating them once the new items have been added to the grid?

Thanks,
Neil

@ghosh
Copy link
Collaborator

ghosh commented Apr 11, 2020

@rctneil Thanks for reporting, we don't have a way of doing this yet. However, that is something we want to solve for our next release.

Meanwhile, if you are using the init method, them try calling Micromodal.init() after the items are appened.

@rctneil
Copy link
Author

rctneil commented Apr 11, 2020

Thanks, I did try that after appending items but it didn’t seem to make any difference and didn’t initialise the new modals.

@ghosh
Copy link
Collaborator

ghosh commented Apr 11, 2020

Noted. Will work on a fix.

@Edodums
Copy link

Edodums commented Apr 15, 2020

@ghosh , maybe something like this?
https://github.com/banthagroup/fslightbox/blob/c3470d0445a360e2bee47b4962d0b570a03fe48d/src/index.js

A part from that everything is literally perfect, great job 🧡

@ghosh
Copy link
Collaborator

ghosh commented Apr 16, 2020

@Edodums Yes, something like the refresh mothod.

@joshangehr
Copy link

I had this same issue. The below is what worked for me.

function refreshModals() {
    // Use whatever selector you're using for MicroModal triggers.
    const modalTrigger = 'data-micromodal-trigger';

    // Get all triggers. 
    const modalTriggers = document.querySelectorAll( `[${modalTrigger}]` );

    modalTriggers.forEach( trigger => {
        // Get the attribute to save.
        const triggerElement = trigger.getAttribute( modalTrigger );
        
        // Remove the attribute briefly to clear memory/existing modals.
        trigger.removeAttribute( modalTrigger );

        // Immediately add it back.
        trigger.setAttribute( modalTrigger, triggerElement );

        // Re-initialize.
        MicroModal.init();
    } );
}

// Call whenever you need (AJAX calls, infinite scrolling, etc).
refreshModals();

I have not tested thoroughly, but it has no side effects in my use case. This seems to be enough to flush the MicroModal memory and help a new initialization succeed. @ghosh If the above is at all reckless, let me know. Hope this helps.

@mrwweb
Copy link

mrwweb commented Jan 20, 2021

Ran into the need for a .destroy() method but in a different use case. We have multiple potential triggers but only want to show the modal once to a user on a page.

My solution was a little different in this case. Since MicroModal checks to be sure that the modal exists, I just remove the modal from the DOM when it's closed.

That said, it would be much nicer to have an upstream option to destroy a modal instance and/or make a way to only trigger the open event once.

const modalId = 'my-modal';
// remove the modal from the DOM
const modalClose = function() {
	const modal = document.getElementById(modalId);
	modal.parentNode.removeChild(modal);
}

MicroModal.init({
	onClose: modalClose,
});

@hasenov
Copy link

hasenov commented Jan 2, 2022

+1

@wplit
Copy link

wplit commented Feb 21, 2022

@mrwweb

Another way to show only once would be to have a variable that you set as false, like modalShown = false, then check if that is false before triggering the modal, then with the onShow event change the variable to true. Next time the modal tries to show, it will check the variable and this time it will fail the condition, so the modal is only shown once.

@dkniffin
Copy link
Contributor

I think this is a duplicate of #136 Can anyone confirm?

@dkniffin dkniffin added needs more info User needs to provide more information before we can debug further multiple modals Related to multiple modals showing on the same page (including nested) labels Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
multiple modals Related to multiple modals showing on the same page (including nested) needs more info User needs to provide more information before we can debug further
Projects
None yet
Development

No branches or pull requests

8 participants