-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
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) | ||
} | ||
|
||
disconnectedCallback() { | ||
this.#abortController?.abort() | ||
} | ||
|
||
handleEvent(event) { | ||
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / eslint
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / stylelint
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (7.1.1, 3.2)
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (7.1.1, 3.2)
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (7.0.3, 3.0)
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (main, 3.1)
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / npm / Canary
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (main, 3.1)
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (main, 3.2)
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (main, 3.2)
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (7.0.3, 3.0)
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (6.1.1, 2.7)
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (6.1.1, 2.7)
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / CSS coverage
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / System
Check failure on line 46 in app/components/primer/dialog_helpers.ts GitHub Actions / Visual Regressions
|
||
const target = event.target as HTMLElement | ||
const dialog = this.dialog | ||
if (!dialog.open) return | ||
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / eslint
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / stylelint
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (7.1.1, 3.2)
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (7.1.1, 3.2)
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (7.0.3, 3.0)
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (main, 3.1)
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / npm / Canary
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (main, 3.1)
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (main, 3.2)
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (main, 3.2)
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (7.0.3, 3.0)
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (6.1.1, 2.7)
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (6.1.1, 2.7)
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / CSS coverage
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / System
Check failure on line 49 in app/components/primer/dialog_helpers.ts GitHub Actions / Visual Regressions
|
||
if (target?.closest('dialog') !== dialog) return | ||
|
||
const rect = dialog.getBoundingClientRect() | ||
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / eslint
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / stylelint
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (7.1.1, 3.2)
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (7.1.1, 3.2)
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (7.0.3, 3.0)
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (main, 3.1)
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / npm / Canary
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (main, 3.1)
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (main, 3.2)
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (main, 3.2)
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (7.0.3, 3.0)
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (6.1.1, 2.7)
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (6.1.1, 2.7)
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / CSS coverage
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / System
Check failure on line 52 in app/components/primer/dialog_helpers.ts GitHub Actions / Visual Regressions
|
||
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() | ||
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / eslint
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / stylelint
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (7.1.1, 3.2)
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (7.1.1, 3.2)
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (7.0.3, 3.0)
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (main, 3.1)
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / npm / Canary
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (main, 3.1)
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (main, 3.2)
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (main, 3.2)
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (7.0.3, 3.0)
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (6.1.1, 2.7)
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (6.1.1, 2.7)
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / CSS coverage
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / System
Check failure on line 60 in app/components/primer/dialog_helpers.ts GitHub Actions / Visual Regressions
|
||
} | ||
} | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
DialogHelperElement: typeof DialogHelperElement | ||
} | ||
interface HTMLElementTagNameMap { | ||
'dialog-helper': DialogHelperElement | ||
} | ||
} | ||
|
||
if (!window.customElements.get('dialog-helper')) { | ||
window.ModalDialogElement = DialogHelperElement | ||
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / eslint
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / stylelint
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (7.1.1, 3.2)
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (7.1.1, 3.2)
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (7.0.3, 3.0)
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (main, 3.1)
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / npm / Canary
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (main, 3.1)
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (main, 3.2)
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (main, 3.2)
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (7.0.3, 3.0)
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Components (6.1.1, 2.7)
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Lib (6.1.1, 2.7)
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / CSS coverage
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / System
Check failure on line 75 in app/components/primer/dialog_helpers.ts GitHub Actions / Visual Regressions
|
||
window.customElements.define('dialog-helper', DialogHelperElement) | ||
} |