-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
182 additions
and
166 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,87 @@ | ||
export default class WebUIModal extends HTMLElement { | ||
private dialog: HTMLDialogElement | null; | ||
private modalContent: HTMLElement | null; | ||
private btnModalOpen: HTMLButtonElement | HTMLAnchorElement | null; | ||
private btnsModalClose: NodeListOf<HTMLElement>; | ||
private classNameModalOpen: string; | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.dialog = this.querySelector('dialog'); | ||
this.modalContent = this.querySelector('[data-content]'); | ||
this.btnModalOpen = this.querySelector('[data-open]'); | ||
this.btnsModalClose = this.querySelectorAll('[data-close]'); | ||
this.classNameModalOpen = 'has-modal-open'; | ||
|
||
this.init(); | ||
|
||
this.btnModalOpen?.addEventListener('click', this); | ||
this.btnsModalClose.forEach((btnModalClose) => { | ||
btnModalClose?.addEventListener('click', this); | ||
}); | ||
} | ||
|
||
private init(): void { | ||
if (!this.btnModalOpen || !this.dialog) return; | ||
} | ||
|
||
private handleOpen(): void { | ||
if (!this.dialog?.open) { | ||
this.dialog?.showModal(); | ||
// Set focus on content rather than the 'close' button. | ||
this.modalContent?.setAttribute('tabIndex', '-1'); | ||
this.modalContent?.focus(); | ||
|
||
document.body.classList.add(this.classNameModalOpen); | ||
} | ||
} | ||
|
||
private handleClose(): void { | ||
this.dialog?.close(); | ||
// Set focus back on button that opened modal. | ||
this.btnModalOpen?.focus(); | ||
|
||
document.body.classList.remove(this.classNameModalOpen); | ||
} | ||
|
||
private handleGlobalClick(e: MouseEvent): void { | ||
const target = e.target as HTMLElement; | ||
if ( | ||
this.dialog?.open && | ||
!this.dialog.contains(target) && | ||
!this.btnModalOpen?.contains(target) | ||
) { | ||
this.handleClose(); | ||
} | ||
} | ||
|
||
// Handle web component events from constructor(). | ||
handleEvent(e: MouseEvent) { | ||
const target = e.currentTarget as HTMLButtonElement; | ||
|
||
// Click 'open' button. | ||
if (target?.dataset.open === '') { | ||
// Prevent default behaviour on any links that open a modal. | ||
e.preventDefault(); | ||
|
||
this.handleOpen(); | ||
} | ||
|
||
// Click 'close' button. | ||
if (target?.dataset.close === '') { | ||
this.handleClose(); | ||
} | ||
} | ||
|
||
// Add/remove other (global) event listeners which are not part of this web component. | ||
connectedCallback() { | ||
window.addEventListener('click', (e: MouseEvent) => | ||
this.handleGlobalClick(e), | ||
); | ||
} | ||
|
||
disconnectedCallback() { | ||
window.removeEventListener('click', this.handleGlobalClick); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
@use 'webui-disclosure'; | ||
@use 'webui-make-clickable'; | ||
@use 'webui-modal'; | ||
@use 'webui-share'; |
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 |
---|---|---|
@@ -1,25 +1,6 @@ | ||
import { Meta, Canvas } from '@storybook/blocks'; | ||
import * as Modal from './Modal.stories'; | ||
import { Meta } from '@storybook/blocks'; | ||
|
||
<Meta of={Modal} /> | ||
<Meta title="Components/Modal" /> | ||
|
||
# Modal | ||
- Uses the native HTML `<dialog>` element. See [dialog](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog) docs on MDN. | ||
|
||
## Accessibility considerations | ||
- There are ongoing discussions about the [initial focus behaviour](https://github.com/whatwg/html/wiki/dialog--initial-focus,-a-proposal) for `<dialog>`. | ||
- Keyboard `focus` is currently being set on the `data-modal-content` using `tabindex="-1"` to ensure that content gets announced first, before the `close` button. | ||
- When modal is closed, focus is set back to the button that opened the modal. | ||
- Modal can be closed with the `ESC` key (default browser behaviour), and by clicking outside the modal (JavaScript enhancement). | ||
|
||
### Useful articles | ||
- [Is dialog ready to be used?](https://www.scottohara.me/blog/2019/03/05/open-dialog.html) | ||
- [Defending dialog](https://whistlr.info/2021/in-defence-of-dialog/) by the official polyfill maintainer. | ||
|
||
<Canvas of={Modal.Modal} /> | ||
|
||
## Modal with overflowing content | ||
<Canvas of={Modal.ModalOverflow} /> | ||
|
||
## Modal with multiple close buttons | ||
<Canvas of={Modal.ModalMultipleCloseButtons} /> | ||
- See the [`<webui-modal>`](/docs/web-components-or-custom-elements-webui-modal--docs) custom element. |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.