-
Notifications
You must be signed in to change notification settings - Fork 0
Patch show() and showModal() methods to emit a "show" Event and prevent page scroll; allow subclassing of the component #2
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
Open
polarbirke
wants to merge
8
commits into
main
Choose a base branch
from
enhance-ux-add-show-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d76cebc
Polyfill show() and showModal() methods to emit a "show" Event
polarbirke d7760fd
Improve UX by preventing page scroll when a dialog is opened as a modal
polarbirke ad45888
Register event listeners before methods run, i.e. handleAutoopen()
polarbirke 3b113f7
Allow subclassing the component
polarbirke cf46bc4
Bump version
polarbirke 42661d2
Document new features in README
polarbirke 0ba30c1
Fix typo in README
polarbirke 730f066
Clean up method naming and documenting comments
polarbirke 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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class DialogUtils extends HTMLElement { | ||
export class DialogUtils extends HTMLElement { | ||
static get observedAttributes() { | ||
return ["autofocus-target", "autoopen"]; | ||
} | ||
|
@@ -28,8 +28,12 @@ class DialogUtils extends HTMLElement { | |
} | ||
|
||
this.dialog = this.querySelector('dialog'); | ||
|
||
this.dialog.addEventListener('show', this.onShow.bind(this)); | ||
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. Event listeners are added in Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
this.dialog.addEventListener('close', this.onClose.bind(this)); | ||
|
||
this.monkeyPatchShowMethod(); | ||
this.monkeyPatchShowModalMethod(); | ||
this.polyfillClosedByAny(); | ||
this.polyfillInvokerCommands(); | ||
this.handleAutofocus(); | ||
|
@@ -39,10 +43,52 @@ class DialogUtils extends HTMLElement { | |
if (this._observer) this._observer.disconnect(); | ||
} | ||
|
||
onShow(e) { | ||
if (e.detail.isModal) { | ||
this.disablePageScroll(); | ||
} | ||
} | ||
|
||
onClose() { | ||
this.resetIframes(); | ||
this.enablePageScroll(); | ||
} | ||
|
||
/** | ||
* Patch showModal() to emit a 'show' event, indicate that the dialog is modal | ||
*/ | ||
monkeyPatchShowModalMethod() { | ||
const origShowModal = this.dialog.showModal.bind(this.dialog); | ||
this.dialog.showModal = (...args) => { | ||
const result = origShowModal(...args); | ||
this.dialog.dispatchEvent(new CustomEvent('show', { | ||
detail: { | ||
isModal: true, | ||
}, | ||
bubbles: true, | ||
})); | ||
return result; | ||
}; | ||
} | ||
|
||
/** | ||
* Patch show() to emit a 'show' event, indicate that the dialog is non-modal | ||
*/ | ||
monkeyPatchShowMethod() { | ||
const origShow = this.dialog.show.bind(this.dialog); | ||
this.dialog.show = (...args) => { | ||
const result = origShow(...args); | ||
this.dialog.dispatchEvent(new CustomEvent('show', { | ||
detail: { | ||
isModal: false, | ||
}, | ||
bubbles: true, | ||
})); | ||
return result; | ||
}; | ||
} | ||
|
||
|
||
/** | ||
* Polyfill for light dismissal via closedby="any" | ||
* | ||
|
@@ -122,6 +168,19 @@ class DialogUtils extends HTMLElement { | |
} | ||
} | ||
|
||
enablePageScroll() { | ||
if (this.origBodyOverflow) { | ||
document.body.style.overflow = this.origBodyOverflow; | ||
this.origBodyOverflow = null; | ||
} | ||
} | ||
|
||
disablePageScroll() { | ||
this.origBodyOverflow = document.body.style.overflow; | ||
|
||
document.body.style.overflow = 'hidden'; | ||
} | ||
|
||
/** | ||
* Ensure that any interactive iframe content (i.e. embedded video) stops playing when the dialog is closed | ||
*/ | ||
|
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.
You assume a
<dialog>
is always present; add a null check or guard so that missing dialogs don’t cause runtime errors.Copilot uses AI. Check for mistakes.