Skip to content

attach listeners to data-modal when they become visible#206

Merged
plegner merged 3 commits intomathigon:masterfrom
zachicecreamcohn:zach/data-modal-conditional-fix
Oct 23, 2025
Merged

attach listeners to data-modal when they become visible#206
plegner merged 3 commits intomathigon:masterfrom
zachicecreamcohn:zach/data-modal-conditional-fix

Conversation

@zachicecreamcohn
Copy link
Copy Markdown
Contributor

Currently, listeners are only attached to elements with the data-modal attribute if they're are visible on load or when navigating.

This means that if you have an element with data-modal that is initially hidden with :if, listeners will not be attached to that element when it becomes visible.

This PR fixes that

@zachicecreamcohn zachicecreamcohn marked this pull request as ready for review October 17, 2025 16:15
Comment thread src/components/modal.ts
* Attaches a click listener to an element with data-modal attribute, if there isn't one already.
*/
attachListener($button: any) {
if (elementsWithModalListeners.has($button._el)) return;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think in case with :if we need to remove listeners from element and browser and do not keep them because of sense of this listener, that is why i think this line creates space for memory leak when elements will be cut from DOM but we keep listeners for them in memory.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I just pushed changes which remove the listeners when the element is detached from the dom.

Comment thread src/components/modal.ts
removeListener($button: ElementView) {
if (!elementsWithModalListeners.has($button._el)) return;

$button.off('click');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, now it should remove all click events, not just modal component, i think it is fine

@swarty
Copy link
Copy Markdown
Contributor

swarty commented Oct 23, 2025

@zachicecreamcohn you also need ask review Philip because he only has permissions to merge PRs into boost

@plegner plegner merged commit f9e23ba into mathigon:master Oct 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants