Skip to content

Commit

Permalink
refactor Dialog to use <dialog> internally
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Nov 9, 2023
1 parent 2c5b0b0 commit 21748de
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 205 deletions.
4 changes: 2 additions & 2 deletions app/components/primer/alpha/dialog.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= show_button %>
<div class="Overlay--hidden <%= @backdrop_classes %>" data-modal-dialog-overlay>
<dialog-helper>
<%= render Primer::BaseComponent.new(**@system_arguments) do %>
<%= header %>
<% if content.present? %>
Expand All @@ -9,4 +9,4 @@
<%= footer %>
<% end %>
<% end %>
</div>
</dialog-helper>
3 changes: 1 addition & 2 deletions app/components/primer/alpha/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ def initialize(
@position_narrow = position_narrow
@visually_hide_title = visually_hide_title

@system_arguments[:tag] = "modal-dialog"
@system_arguments[:role] = "dialog"
@system_arguments[:tag] = "dialog"
@system_arguments[:id] = @id
@system_arguments[:aria] = { modal: true }
@system_arguments[:aria] = merge_aria(
Expand Down
199 changes: 0 additions & 199 deletions app/components/primer/alpha/modal_dialog.ts

This file was deleted.

7 changes: 6 additions & 1 deletion app/components/primer/alpha/overlay.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ anchored-position[popover] {

.Overlay {
display: flex;
border-width: 0;
}

anchored-position.not-anchored::backdrop {
anchored-position.not-anchored::backdrop, dialog::backdrop {
background-color: var(--overlay-backdrop-bgColor, var(--color-neutral-muted));
}

dialog.Overlay:not([open]) {
display: none;
}
77 changes: 77 additions & 0 deletions app/components/primer/dialog_helpers.ts
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

View workflow job for this annotation

GitHub Actions / eslint

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / stylelint

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (6.1.1, 2.7)

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (7.1.1, 3.2)

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.1)

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.2)

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (7.0.3, 3.0)

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (7.0.3, 3.0)

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / npm / Canary

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.1)

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (7.1.1, 3.2)

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.2)

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (6.1.1, 2.7)

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / System

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / CSS coverage

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Accessibility

Parameter 'event' implicitly has an 'any' type.

Check failure on line 46 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Visual Regressions

Parameter 'event' implicitly has an 'any' type.
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

View workflow job for this annotation

GitHub Actions / eslint

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / stylelint

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (6.1.1, 2.7)

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (7.1.1, 3.2)

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.1)

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.2)

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (7.0.3, 3.0)

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (7.0.3, 3.0)

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / npm / Canary

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.1)

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (7.1.1, 3.2)

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.2)

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (6.1.1, 2.7)

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / System

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / CSS coverage

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Accessibility

'dialog' is possibly 'null'.

Check failure on line 49 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Visual Regressions

'dialog' is possibly 'null'.
if (target?.closest('dialog') !== dialog) return

const rect = dialog.getBoundingClientRect()

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / eslint

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / stylelint

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (6.1.1, 2.7)

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (7.1.1, 3.2)

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.1)

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.2)

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (7.0.3, 3.0)

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (7.0.3, 3.0)

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / npm / Canary

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.1)

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (7.1.1, 3.2)

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.2)

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (6.1.1, 2.7)

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / System

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / CSS coverage

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Accessibility

'dialog' is possibly 'null'.

Check failure on line 52 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Visual Regressions

'dialog' is possibly 'null'.
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

View workflow job for this annotation

GitHub Actions / eslint

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / stylelint

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (6.1.1, 2.7)

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (7.1.1, 3.2)

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.1)

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.2)

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (7.0.3, 3.0)

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (7.0.3, 3.0)

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / npm / Canary

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.1)

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (7.1.1, 3.2)

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.2)

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (6.1.1, 2.7)

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / System

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / CSS coverage

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Accessibility

'dialog' is possibly 'null'.

Check failure on line 60 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Visual Regressions

'dialog' is possibly 'null'.
}
}
}

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

View workflow job for this annotation

GitHub Actions / eslint

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / stylelint

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (6.1.1, 2.7)

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (7.1.1, 3.2)

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.1)

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.2)

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (7.0.3, 3.0)

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (7.0.3, 3.0)

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / npm / Canary

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.1)

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (7.1.1, 3.2)

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.2)

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Lib (6.1.1, 2.7)

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / System

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / CSS coverage

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Accessibility

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?

Check failure on line 75 in app/components/primer/dialog_helpers.ts

View workflow job for this annotation

GitHub Actions / Visual Regressions

Property 'ModalDialogElement' does not exist on type 'Window & typeof globalThis'. Did you mean 'HTMLDialogElement'?
window.customElements.define('dialog-helper', DialogHelperElement)
}
2 changes: 1 addition & 1 deletion app/components/primer/primer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import '@github/include-fragment-element'
import './alpha/action_bar_element'
import './alpha/dropdown'
import './anchored_position'
import './dialog_helpers'
import './focus_group'
import './alpha/image_crop'
import './alpha/modal_dialog'
import './beta/nav_list'
import './alpha/segmented_control'
import './alpha/toggle_switch'
Expand Down

0 comments on commit 21748de

Please sign in to comment.