-
Notifications
You must be signed in to change notification settings - Fork 10
attach listeners to data-modal when they become visible
#206
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
Merged
plegner
merged 3 commits into
mathigon:master
from
zachicecreamcohn:zach/data-modal-conditional-fix
Oct 23, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ let $openModal: Modal|undefined = undefined; | |
| let lastFocusElement: HTMLElement|undefined = undefined; | ||
|
|
||
| const TITLE_ID = 'boost-modal-title'; | ||
| const elementsWithModalListeners = new WeakSet(); | ||
|
|
||
| function tryClose() { | ||
| if ($openModal && $openModal.canClose) $openModal.close(); | ||
|
|
@@ -51,12 +52,12 @@ export class Modal extends CustomElementView { | |
| this.$video = this.$('video') as MediaView|undefined; | ||
|
|
||
| const $buttons = $$(`[data-modal=${this.id}]`); | ||
| for (const $b of $buttons) $b.on('click', () => this.open()); | ||
| for (const $b of $buttons) this.attachListener($b); | ||
|
|
||
| // Look for new modals to open, after browser navigation. | ||
| Router.on('afterChange', ({$viewport}) => { | ||
| const $buttons = $viewport.$$(`[data-modal=${this.id}]`); | ||
| for (const $b of $buttons) $b.on('click', () => this.open()); | ||
| for (const $b of $buttons) this.attachListener($b); | ||
| }); | ||
|
|
||
| // Open modals that are shown on pageload | ||
|
|
@@ -90,6 +91,26 @@ export class Modal extends CustomElementView { | |
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Attaches a click listener to an element with data-modal attribute, if there isn't one already. | ||
| */ | ||
| attachListener($button: ElementView) { | ||
| if (elementsWithModalListeners.has($button._el)) return; | ||
|
|
||
| $button.on('click', () => this.open()); | ||
| elementsWithModalListeners.add($button._el); | ||
| } | ||
|
|
||
| /** | ||
| * Removes the click listener from an element | ||
| */ | ||
| removeListener($button: ElementView) { | ||
| if (!elementsWithModalListeners.has($button._el)) return; | ||
|
|
||
| $button.off('click'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| elementsWithModalListeners.delete($button._el); | ||
| } | ||
|
|
||
| open(noAnimation = false) { | ||
| if (this.isOpen) return; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.