nercone.devaboutlinkscontactsitemap
aboutlinkssitemap82ffc6419

Overlays

Popup and toast. Both are drawn above the page.

Popup

Applies to <dialog>. A button with commandfor="ID" command="show-modal" opens the dialog with that ID as a modal, and one with command="close" closes it. Esc also closes it.

These are HTML invoker commands, so they work without JavaScript. In browsers that do not support them, popup.js runs show-modal, close, and request-close in their place.

Return value: -

This is a basic popup.

With closedby="any", clicking outside the popup closes it. In browsers that do not support the attribute, popup.js does the same. Keep a close button as well: without JavaScript, those browsers can close it only with Esc.

This cannot be undone.

When the content does not fit on the screen, only the inside of the popup scrolls.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

<button type="button" commandfor="confirm" command="show-modal">Delete</button>

<dialog id="confirm" closedby="any" aria-labelledby="confirm-title">
    <h2 id="confirm-title">Delete this item?</h2>
    <div class="popup-actions">
        <button type="button" commandfor="confirm" command="close" value="cancel">Cancel</button>
        <button class="button-error" type="button" commandfor="confirm" command="close" value="delete">Delete</button>
    </div>
</dialog>

popup-actions lines up buttons at the end of the popup. Headings in a popup have no border.

Return value

The value of a command="close" button becomes the returnValue of the dialog, which can be read in its close event. In a <form method="dialog">, the value of the submit button does the same.

document.getElementById('confirm').addEventListener('close', (event) => {
    if (event.target.returnValue === 'delete') { … }
});

Script

Each method takes an ID or a <dialog>, and returns the dialog, or null when nothing changed.

const popup = window.nercone.get('popup');
popup.open('confirm');
popup.close('confirm', 'cancel');
popup.toggle(document.getElementById('confirm'));

popup.js also opens a dialog when an element with data-popup-open="ID" is clicked, and closes the dialog around an element with data-popup-close="VALUE". They need JavaScript, so prefer commandfor and command. Open popups are closed when view-transition.js moves to another page.

Customization

Change the width with --popup-width (default 480px), the padding with --popup-padding (default 24px), the blur behind it with --popup-backdrop-blur (default 4px), and the colors with --color-popup-*.

--popup-width: 640px; --popup-padding: 48px; --popup-backdrop-blur: 16px; --color-popup-border: var(--color-information-bright)

Toast

A short notification at the edge of the screen that closes after a while. With toast.js, clicking an element with data-toast="Message" shows one; scripts can show them as well. A toast does not close while the pointer or focus is on it, and toasts stay above popups.

Toasts need JavaScript, so do not use them for anything the page needs. Give buttons that only show toasts the script-only class, which hides them without JavaScript (see Layout).

Variants

Set data-toast-variant to information, success, warning, or error. warning and error get role="alert", so assistive technology announces them at once; the others are announced when the reader is idle.

<button class="script-only" type="button" data-toast="Saved" data-toast-variant="success">Save</button>

Script

The toast container is kept in the top layer, so toasts are drawn above modal popups.

const toast = window.nercone.get('toast');
toast.show('Saved', { variant: 'success' });
toast.show('Stays until closed', { duration: 0 });
toast.show('No close button', { dismissible: false });

const element = toast.show('Working');
toast.pause(element);   // Stops the timer
toast.resume(element);  // Continues from where it stopped
toast.close(element);
toast.clear();          // Closes every toast

show() returns the toast element. The message can be a string or a Node; a string is shown as text, not as HTML. When there are more toasts than the limit, the oldest are closed. Toasts stay when view-transition.js moves to another page.

Customization

The close button (×) is drawn in the color of the toast text at full opacity, so it has the same contrast as the message. On hover it gets a 1px line inside instead of a fill, so that the color of the toast behind it, a JIS safety color in the UD themes, does not change.

Change the width of the container with --toast-width (default 360px, including --toast-offset on both sides, so toasts are 312px wide), the distance from the edge of the screen with --toast-offset (default 24px), the padding with --toast-padding (default 12px 16px), and the colors with --color-toast-*. The container is created directly under <body>, so set them on :root or body.

The options of toast.js, such as the position and the duration, are on Scripts.

window.nercone.register('toast', new window.nercone.Toast({
    position: 'top-center',
    duration: 3000,
    limit:    3
}));