Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Web Component with progressive enhancements for the HTML <dialog> element

NotificationsYou must be signed in to change notification settings

webfactory/dialog-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Web Component with progressive enhancements for the HTML <dialog> element

Installation

npm install @webfactoryde/dialog-utils

Usage

The<dialog-utils> Web Component is meant to be a lightweight wrapper and progressive enhancement for<dialog> elements that are used as modal dialogs.

The enhancements are:

  1. autoopen: The Web Component supports opening a dialog immediately if anautoopen attribute is present on<dialog-utils>.
  2. commandfor andcommand: The Web Component polyfills theInvoker Commands API to enable future-friendly, declarative linkup of buttons to show and close the dialog even in browsers that don't support the API yet. You can opt out of this if you prefer to write your own JavaScript logic to handle this functionality.
  3. closedby="any": The Web Component polyfills thedeclarative attribute for light dismissal of the dialog by a click outside of it.
  4. Playing media: If the dialog contains an iframe, the Web Component will ensure that any media stops playing when the dialog is closed.
  5. Focus behaviour: You should use the declarativeautofocus attribute to indicate whether the dialog itself or a specific interactive child element should receive focus when the dialog is shown. If this is not an option, the Web Component accepts aautofocus-target attribute with a valid DOM selector string as its value. The WC will then try to set theautofocus attribute on the first element that matches the selector.
  6. Page scroll: If the dialog is opened as a modal, scrolling is disabled on the<body> and re-enabled onclose.
  7. The Web Component emits a customopen event when the dialog is opened.

Steps to implement:

  1. The JS file "dialog-utils.js" must be loaded. Depending on browser support requirements, transpilation for older browsers is recommended.
  2. Wrap your<dialog> with<dialog-utils>.
  3. Add a trigger and close<button> with your desired markup (e.g. nested icon, attributes, translated text, etc.). The buttons need to be made identifiable withcommandfor andcommand as per thespecification, if you want to benefit from the ease-of-use polyfills. The Web Component leaves positioning and aesthetics of the buttons to the outside context.

Basic example

<button commandfor="my-dialog" command="show-modal">Open my dialog</button><dialog-utils>    <dialogmy-dialog" command="close">Close</button>        <h1>My modal</h1>        <p>Some content</p>    </dialog></dialog-utils>

Events

open

The component emits aopen event on the<dialog> element that includes information about whether the dialog is displayed as a modal via(bool) event.detail.isModal.

Caveat: This requires support for thetoggle event which isBaseline 2023 but with aknown regression for dialog in Safari 18 (fixed in Safari 26).

Extending the component

The component is exported to allow subclassing and extending its methods.

Example

import {DialogUtils} from '@webfactoryde/dialog-utils';class MyCustomDialogUtils extends DialogUtils {    onOpen(event) {        // call the parent method        super.onOpen?.(event);        // do your custom thing    }}customElements.define('my-custom-dialog-utils', MyCustomDialogUtils);

About

Web Component with progressive enhancements for the HTML <dialog> element

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp