-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor Dialog to use <dialog> internally (#2364)
- Loading branch information
Showing
18 changed files
with
151 additions
and
245 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/view-components': minor | ||
--- | ||
|
||
Primer::Alpha::Dialog uses <dialog> internally |
This file contains 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
function dialogInvokerButtonHandler(event: Event) { | ||
const target = event.target as HTMLElement | ||
const button = target?.closest('button') | ||
|
||
if (!button || button.hasAttribute('disabled') || button.getAttribute('aria-disabled') === 'true') return | ||
|
||
// If the user is clicking a valid dialog trigger | ||
let dialogId = button?.getAttribute('data-show-dialog-id') | ||
if (dialogId) { | ||
event.stopPropagation() | ||
const dialog = document.getElementById(dialogId) | ||
if (dialog instanceof HTMLDialogElement) { | ||
dialog.showModal() | ||
// A buttons default behaviour in some browsers it to send a pointer event | ||
// If the behaviour is allowed through the dialog will be shown but then | ||
// quickly hidden- as if it were never shown. This prevents that. | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
dialogId = button.getAttribute('data-close-dialog-id') || button.getAttribute('data-submit-dialog-id') | ||
if (dialogId) { | ||
const dialog = document.getElementById(dialogId) | ||
if (dialog instanceof HTMLDialogElement && dialog.open) { | ||
dialog.close() | ||
} | ||
} | ||
} | ||
|
||
export class DialogHelperElement extends HTMLElement { | ||
get dialog() { | ||
return this.querySelector('dialog') | ||
} | ||
|
||
#abortController: AbortController | null = null | ||
connectedCallback() { | ||
const {signal} = (this.#abortController = new AbortController()) | ||
document.addEventListener('click', dialogInvokerButtonHandler) | ||
document.addEventListener('click', this, {signal}) | ||
} | ||
|
||
disconnectedCallback() { | ||
this.#abortController?.abort() | ||
} | ||
|
||
handleEvent(event: MouseEvent) { | ||
const target = event.target as HTMLElement | ||
const dialog = this.dialog | ||
if (!dialog?.open) return | ||
if (target?.closest('dialog') !== dialog) return | ||
|
||
const rect = dialog.getBoundingClientRect() | ||
const clickWasInsideDialog = | ||
rect.top <= event.clientY && | ||
event.clientY <= rect.top + rect.height && | ||
rect.left <= event.clientX && | ||
event.clientX <= rect.left + rect.width | ||
|
||
if (!clickWasInsideDialog) { | ||
dialog.close() | ||
} | ||
} | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
DialogHelperElement: typeof DialogHelperElement | ||
} | ||
interface HTMLElementTagNameMap { | ||
'dialog-helper': DialogHelperElement | ||
} | ||
} | ||
|
||
if (!window.customElements.get('dialog-helper')) { | ||
window.DialogHelperElement = DialogHelperElement | ||
window.customElements.define('dialog-helper', DialogHelperElement) | ||
} |
This file contains 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
Oops, something went wrong.