2022-08-17 15:37:37 +00:00
|
|
|
import { html } from 'lit';
|
2022-01-05 23:31:41 +00:00
|
|
|
import { customElement, property, query } from 'lit/decorators.js';
|
2021-09-29 12:40:26 +00:00
|
|
|
import { classMap } from 'lit/directives/class-map.js';
|
|
|
|
import { ifDefined } from 'lit/directives/if-defined.js';
|
2022-03-24 12:01:09 +00:00
|
|
|
import { animateTo, stopAnimations } from '../../internal/animate';
|
2022-09-16 20:21:40 +00:00
|
|
|
import { waitForEvent } from '../../internal/event';
|
2022-03-24 12:01:09 +00:00
|
|
|
import Modal from '../../internal/modal';
|
|
|
|
import { lockBodyScrolling, unlockBodyScrolling } from '../../internal/scroll';
|
2022-08-17 15:37:37 +00:00
|
|
|
import ShoelaceElement from '../../internal/shoelace-element';
|
2022-03-24 12:01:09 +00:00
|
|
|
import { HasSlotController } from '../../internal/slot';
|
|
|
|
import { watch } from '../../internal/watch';
|
|
|
|
import { getAnimation, setDefaultAnimation } from '../../utilities/animation-registry';
|
|
|
|
import { LocalizeController } from '../../utilities/localize';
|
2022-08-08 14:15:41 +00:00
|
|
|
import '../icon-button/icon-button';
|
2021-07-10 00:45:44 +00:00
|
|
|
import styles from './dialog.styles';
|
2022-07-19 12:27:39 +00:00
|
|
|
import type { CSSResultGroup } from 'lit';
|
2021-07-12 14:36:06 +00:00
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
/**
|
2022-10-21 13:56:35 +00:00
|
|
|
* @summary Dialogs, sometimes called "modals", appear above the page and require the user's immediate attention.
|
|
|
|
*
|
2021-02-26 14:09:13 +00:00
|
|
|
* @since 2.0
|
|
|
|
* @status stable
|
|
|
|
*
|
|
|
|
* @dependency sl-icon-button
|
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @slot - The dialog's content.
|
2022-08-03 15:06:52 +00:00
|
|
|
* @slot label - The dialog's label. Alternatively, you can use the `label` attribute.
|
2021-06-25 20:25:46 +00:00
|
|
|
* @slot footer - The dialog's footer, usually one or more buttons representing various options.
|
2021-02-26 14:09:13 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @event sl-show - Emitted when the dialog opens.
|
2021-09-27 21:58:14 +00:00
|
|
|
* @event sl-after-show - Emitted after the dialog opens and all animations are complete.
|
2021-06-25 20:25:46 +00:00
|
|
|
* @event sl-hide - Emitted when the dialog closes.
|
2021-09-27 21:58:14 +00:00
|
|
|
* @event sl-after-hide - Emitted after the dialog closes and all animations are complete.
|
2022-02-27 16:46:55 +00:00
|
|
|
* @event sl-initial-focus - Emitted when the dialog opens and is ready to receive focus. Calling
|
|
|
|
* `event.preventDefault()` will prevent focusing and allow you to set it on a different element, such as an input.
|
2022-02-10 15:34:22 +00:00
|
|
|
* @event {{ source: 'close-button' | 'keyboard' | 'overlay' }} sl-request-close - Emitted when the user attempts to
|
|
|
|
* close the dialog by clicking the close button, clicking the overlay, or pressing escape. Calling
|
|
|
|
* `event.preventDefault()` will keep the dialog open. Avoid using this unless closing the dialog will result in
|
|
|
|
* destructive behavior such as data loss.
|
2021-05-17 22:04:28 +00:00
|
|
|
*
|
2022-03-09 20:54:18 +00:00
|
|
|
* @csspart base - The component's internal wrapper.
|
2021-06-25 20:25:46 +00:00
|
|
|
* @csspart overlay - The overlay.
|
|
|
|
* @csspart panel - The dialog panel (where the dialog and its content is rendered).
|
|
|
|
* @csspart header - The dialog header.
|
2022-11-29 16:17:15 +00:00
|
|
|
* @csspart header-actions - Optional actions to add to the header. Works best with `<sl-icon-button>`.
|
2021-06-25 20:25:46 +00:00
|
|
|
* @csspart title - The dialog title.
|
|
|
|
* @csspart close-button - The close button.
|
2022-03-09 20:54:18 +00:00
|
|
|
* @csspart close-button__base - The close button's `base` part.
|
2021-06-25 20:25:46 +00:00
|
|
|
* @csspart body - The dialog body.
|
|
|
|
* @csspart footer - The dialog footer.
|
2021-05-21 21:53:53 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @cssproperty --width - The preferred width of the dialog. Note that the dialog will shrink to accommodate smaller screens.
|
|
|
|
* @cssproperty --header-spacing - The amount of padding to use for the header.
|
|
|
|
* @cssproperty --body-spacing - The amount of padding to use for the body.
|
|
|
|
* @cssproperty --footer-spacing - The amount of padding to use for the footer.
|
2021-06-24 22:24:54 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @animation dialog.show - The animation to use when showing the dialog.
|
|
|
|
* @animation dialog.hide - The animation to use when hiding the dialog.
|
|
|
|
* @animation dialog.denyClose - The animation to use when a request to close the dialog is denied.
|
|
|
|
* @animation dialog.overlay.show - The animation to use when showing the dialog's overlay.
|
|
|
|
* @animation dialog.overlay.hide - The animation to use when hiding the dialog's overlay.
|
2021-02-26 14:09:13 +00:00
|
|
|
*/
|
2021-03-18 13:04:23 +00:00
|
|
|
@customElement('sl-dialog')
|
2022-08-17 15:37:37 +00:00
|
|
|
export default class SlDialog extends ShoelaceElement {
|
2022-07-19 12:27:39 +00:00
|
|
|
static styles: CSSResultGroup = styles;
|
2021-03-06 17:01:39 +00:00
|
|
|
|
|
|
|
@query('.dialog') dialog: HTMLElement;
|
|
|
|
@query('.dialog__panel') panel: HTMLElement;
|
2021-05-21 21:53:53 +00:00
|
|
|
@query('.dialog__overlay') overlay: HTMLElement;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
2022-01-16 05:47:14 +00:00
|
|
|
private readonly hasSlotController = new HasSlotController(this, 'footer');
|
2022-01-24 22:27:24 +00:00
|
|
|
private readonly localize = new LocalizeController(this);
|
2021-02-26 14:09:13 +00:00
|
|
|
private modal: Modal;
|
2021-04-15 17:30:58 +00:00
|
|
|
private originalTrigger: HTMLElement | null;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
/** Indicates whether or not the dialog is open. You can use this in lieu of the show/hide methods. */
|
2021-07-01 00:04:46 +00:00
|
|
|
@property({ type: Boolean, reflect: true }) open = false;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The dialog's label as displayed in the header. You should always include a relevant label even when using
|
2022-08-03 15:06:52 +00:00
|
|
|
* `no-header`, as it is required for proper accessibility. If you need to display HTML, you can use the `label` slot
|
|
|
|
* instead.
|
2021-02-26 14:09:13 +00:00
|
|
|
*/
|
2021-07-01 00:04:46 +00:00
|
|
|
@property({ reflect: true }) label = '';
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables the header. This will also remove the default close button, so please ensure you provide an easy,
|
|
|
|
* accessible way for users to dismiss the dialog.
|
|
|
|
*/
|
2021-07-01 00:04:46 +00:00
|
|
|
@property({ attribute: 'no-header', type: Boolean, reflect: true }) noHeader = false;
|
2021-03-06 17:01:39 +00:00
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
super.connectedCallback();
|
2022-11-09 16:34:50 +00:00
|
|
|
this.handleDocumentKeyDown = this.handleDocumentKeyDown.bind(this);
|
2021-04-15 17:30:58 +00:00
|
|
|
this.modal = new Modal(this);
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 12:47:55 +00:00
|
|
|
firstUpdated() {
|
2021-05-21 21:53:53 +00:00
|
|
|
this.dialog.hidden = !this.open;
|
2021-06-28 20:49:56 +00:00
|
|
|
|
|
|
|
if (this.open) {
|
2022-11-09 16:34:50 +00:00
|
|
|
this.addOpenListeners();
|
2021-06-28 20:49:56 +00:00
|
|
|
this.modal.activate();
|
|
|
|
lockBodyScrolling(this);
|
|
|
|
}
|
2021-05-21 21:53:53 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 17:01:39 +00:00
|
|
|
disconnectedCallback() {
|
|
|
|
super.disconnectedCallback();
|
2021-02-26 14:09:13 +00:00
|
|
|
unlockBodyScrolling(this);
|
|
|
|
}
|
|
|
|
|
2021-05-27 20:29:10 +00:00
|
|
|
/** Shows the dialog. */
|
2021-05-21 21:53:53 +00:00
|
|
|
async show() {
|
2021-05-27 20:29:10 +00:00
|
|
|
if (this.open) {
|
2022-01-16 05:47:14 +00:00
|
|
|
return undefined;
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.open = true;
|
2021-05-27 20:29:10 +00:00
|
|
|
return waitForEvent(this, 'sl-after-show');
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Hides the dialog */
|
2021-05-21 21:53:53 +00:00
|
|
|
async hide() {
|
2021-05-27 20:29:10 +00:00
|
|
|
if (!this.open) {
|
2022-01-16 05:47:14 +00:00
|
|
|
return undefined;
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.open = false;
|
2021-05-27 20:29:10 +00:00
|
|
|
return waitForEvent(this, 'sl-after-hide');
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 15:34:22 +00:00
|
|
|
private requestClose(source: 'close-button' | 'keyboard' | 'overlay') {
|
2022-09-16 20:21:40 +00:00
|
|
|
const slRequestClose = this.emit('sl-request-close', {
|
2022-02-10 15:34:22 +00:00
|
|
|
cancelable: true,
|
|
|
|
detail: { source }
|
|
|
|
});
|
|
|
|
|
2021-06-21 13:40:11 +00:00
|
|
|
if (slRequestClose.defaultPrevented) {
|
2022-06-09 22:14:38 +00:00
|
|
|
const animation = getAnimation(this, 'dialog.denyClose', { dir: this.localize.dir() });
|
2022-01-17 04:44:10 +00:00
|
|
|
animateTo(this.panel, animation.keyframes, animation.options);
|
2021-06-21 13:40:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-17 04:44:10 +00:00
|
|
|
this.hide();
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 16:34:50 +00:00
|
|
|
addOpenListeners() {
|
|
|
|
document.addEventListener('keydown', this.handleDocumentKeyDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeOpenListeners() {
|
|
|
|
document.removeEventListener('keydown', this.handleDocumentKeyDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDocumentKeyDown(event: KeyboardEvent) {
|
|
|
|
if (this.open && event.key === 'Escape') {
|
2021-04-15 17:30:58 +00:00
|
|
|
event.stopPropagation();
|
2022-02-10 15:34:22 +00:00
|
|
|
this.requestClose('keyboard');
|
2021-02-26 14:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 13:26:35 +00:00
|
|
|
@watch('open', { waitUntilFirstUpdate: true })
|
2021-05-27 20:29:10 +00:00
|
|
|
async handleOpenChange() {
|
|
|
|
if (this.open) {
|
|
|
|
// Show
|
2022-09-16 20:21:40 +00:00
|
|
|
this.emit('sl-show');
|
2022-11-09 16:34:50 +00:00
|
|
|
this.addOpenListeners();
|
2021-05-27 20:29:10 +00:00
|
|
|
this.originalTrigger = document.activeElement as HTMLElement;
|
|
|
|
this.modal.activate();
|
|
|
|
|
|
|
|
lockBodyScrolling(this);
|
|
|
|
|
2022-02-27 16:46:55 +00:00
|
|
|
// When the dialog is shown, Safari will attempt to set focus on whatever element has autofocus. This can cause
|
|
|
|
// the dialogs's animation to jitter (if it starts offscreen), so we'll temporarily remove the attribute, call
|
|
|
|
// `focus({ preventScroll: true })` ourselves, and add the attribute back afterwards.
|
|
|
|
//
|
|
|
|
// Related: https://github.com/shoelace-style/shoelace/issues/693
|
|
|
|
//
|
|
|
|
const autoFocusTarget = this.querySelector('[autofocus]');
|
|
|
|
if (autoFocusTarget) {
|
|
|
|
autoFocusTarget.removeAttribute('autofocus');
|
|
|
|
}
|
|
|
|
|
2021-05-27 20:29:10 +00:00
|
|
|
await Promise.all([stopAnimations(this.dialog), stopAnimations(this.overlay)]);
|
|
|
|
this.dialog.hidden = false;
|
|
|
|
|
2022-02-27 16:46:55 +00:00
|
|
|
// Set initial focus
|
|
|
|
requestAnimationFrame(() => {
|
2022-09-16 20:21:40 +00:00
|
|
|
const slInitialFocus = this.emit('sl-initial-focus', { cancelable: true });
|
2022-02-27 16:46:55 +00:00
|
|
|
|
|
|
|
if (!slInitialFocus.defaultPrevented) {
|
|
|
|
// Set focus to the autofocus target and restore the attribute
|
|
|
|
if (autoFocusTarget) {
|
|
|
|
(autoFocusTarget as HTMLInputElement).focus({ preventScroll: true });
|
|
|
|
} else {
|
|
|
|
this.panel.focus({ preventScroll: true });
|
2022-02-21 02:18:26 +00:00
|
|
|
}
|
2022-02-27 16:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the autofocus attribute
|
|
|
|
if (autoFocusTarget) {
|
|
|
|
autoFocusTarget.setAttribute('autofocus', '');
|
|
|
|
}
|
|
|
|
});
|
2021-05-27 20:29:10 +00:00
|
|
|
|
2022-06-09 22:14:38 +00:00
|
|
|
const panelAnimation = getAnimation(this, 'dialog.show', { dir: this.localize.dir() });
|
|
|
|
const overlayAnimation = getAnimation(this, 'dialog.overlay.show', { dir: this.localize.dir() });
|
2021-05-27 20:29:10 +00:00
|
|
|
await Promise.all([
|
|
|
|
animateTo(this.panel, panelAnimation.keyframes, panelAnimation.options),
|
|
|
|
animateTo(this.overlay, overlayAnimation.keyframes, overlayAnimation.options)
|
|
|
|
]);
|
|
|
|
|
2022-09-16 20:21:40 +00:00
|
|
|
this.emit('sl-after-show');
|
2021-05-27 20:29:10 +00:00
|
|
|
} else {
|
|
|
|
// Hide
|
2022-09-16 20:21:40 +00:00
|
|
|
this.emit('sl-hide');
|
2022-11-09 16:34:50 +00:00
|
|
|
this.removeOpenListeners();
|
2021-05-27 20:29:10 +00:00
|
|
|
this.modal.deactivate();
|
|
|
|
|
|
|
|
await Promise.all([stopAnimations(this.dialog), stopAnimations(this.overlay)]);
|
2022-06-09 22:14:38 +00:00
|
|
|
const panelAnimation = getAnimation(this, 'dialog.hide', { dir: this.localize.dir() });
|
|
|
|
const overlayAnimation = getAnimation(this, 'dialog.overlay.hide', { dir: this.localize.dir() });
|
2022-08-23 14:55:49 +00:00
|
|
|
|
|
|
|
// Animate the overlay and the panel at the same time. Because animation durations might be different, we need to
|
|
|
|
// hide each one individually when the animation finishes, otherwise the first one that finishes will reappear
|
|
|
|
// unexpectedly. We'll unhide them after all animations have completed.
|
2021-05-27 20:29:10 +00:00
|
|
|
await Promise.all([
|
2022-08-23 14:55:49 +00:00
|
|
|
animateTo(this.overlay, overlayAnimation.keyframes, overlayAnimation.options).then(() => {
|
|
|
|
this.overlay.hidden = true;
|
|
|
|
}),
|
|
|
|
animateTo(this.panel, panelAnimation.keyframes, panelAnimation.options).then(() => {
|
|
|
|
this.panel.hidden = true;
|
|
|
|
})
|
2021-05-27 20:29:10 +00:00
|
|
|
]);
|
2022-08-23 14:55:49 +00:00
|
|
|
|
2021-05-27 20:29:10 +00:00
|
|
|
this.dialog.hidden = true;
|
|
|
|
|
2022-08-23 14:55:49 +00:00
|
|
|
// Now that the dialog is hidden, restore the overlay and panel for next time
|
|
|
|
this.overlay.hidden = false;
|
|
|
|
this.panel.hidden = false;
|
|
|
|
|
2021-05-27 20:29:10 +00:00
|
|
|
unlockBodyScrolling(this);
|
|
|
|
|
|
|
|
// Restore focus to the original trigger
|
|
|
|
const trigger = this.originalTrigger;
|
2022-01-16 05:47:14 +00:00
|
|
|
if (typeof trigger?.focus === 'function') {
|
2022-01-17 04:44:10 +00:00
|
|
|
setTimeout(() => trigger.focus());
|
2021-05-27 20:29:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 20:21:40 +00:00
|
|
|
this.emit('sl-after-hide');
|
2021-05-27 20:29:10 +00:00
|
|
|
}
|
2021-03-06 20:09:12 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
render() {
|
|
|
|
return html`
|
|
|
|
<div
|
|
|
|
part="base"
|
|
|
|
class=${classMap({
|
|
|
|
dialog: true,
|
|
|
|
'dialog--open': this.open,
|
2022-01-05 23:31:41 +00:00
|
|
|
'dialog--has-footer': this.hasSlotController.test('footer')
|
2021-02-26 14:09:13 +00:00
|
|
|
})}
|
|
|
|
>
|
2022-02-10 15:34:22 +00:00
|
|
|
<div part="overlay" class="dialog__overlay" @click=${() => this.requestClose('overlay')} tabindex="-1"></div>
|
2021-02-26 14:09:13 +00:00
|
|
|
|
|
|
|
<div
|
|
|
|
part="panel"
|
|
|
|
class="dialog__panel"
|
|
|
|
role="dialog"
|
|
|
|
aria-modal="true"
|
|
|
|
aria-hidden=${this.open ? 'false' : 'true'}
|
2021-03-15 11:56:15 +00:00
|
|
|
aria-label=${ifDefined(this.noHeader ? this.label : undefined)}
|
2021-12-30 17:14:39 +00:00
|
|
|
aria-labelledby=${ifDefined(!this.noHeader ? 'title' : undefined)}
|
2021-02-26 14:09:13 +00:00
|
|
|
tabindex="0"
|
|
|
|
>
|
|
|
|
${!this.noHeader
|
|
|
|
? html`
|
|
|
|
<header part="header" class="dialog__header">
|
2022-01-24 22:27:24 +00:00
|
|
|
<h2 part="title" class="dialog__title" id="title">
|
2022-01-16 05:47:14 +00:00
|
|
|
<slot name="label"> ${this.label.length > 0 ? this.label : String.fromCharCode(65279)} </slot>
|
2022-01-24 22:27:24 +00:00
|
|
|
</h2>
|
2022-11-29 16:17:15 +00:00
|
|
|
<div part="header-actions" class="dialog__header-actions">
|
|
|
|
<slot name="header-actions"></slot>
|
|
|
|
<sl-icon-button
|
|
|
|
part="close-button"
|
|
|
|
exportparts="base:close-button__base"
|
|
|
|
class="dialog__close"
|
|
|
|
name="x-lg"
|
|
|
|
label=${this.localize.term('close')}
|
|
|
|
library="system"
|
|
|
|
@click="${() => this.requestClose('close-button')}"
|
|
|
|
></sl-icon-button>
|
|
|
|
</div>
|
2021-02-26 14:09:13 +00:00
|
|
|
</header>
|
|
|
|
`
|
|
|
|
: ''}
|
|
|
|
|
|
|
|
<div part="body" class="dialog__body">
|
2021-03-06 17:01:39 +00:00
|
|
|
<slot></slot>
|
2021-02-26 14:09:13 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<footer part="footer" class="dialog__footer">
|
2022-01-05 23:31:41 +00:00
|
|
|
<slot name="footer"></slot>
|
2021-02-26 14:09:13 +00:00
|
|
|
</footer>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 14:07:38 +00:00
|
|
|
|
2021-05-21 21:53:53 +00:00
|
|
|
setDefaultAnimation('dialog.show', {
|
|
|
|
keyframes: [
|
|
|
|
{ opacity: 0, transform: 'scale(0.8)' },
|
|
|
|
{ opacity: 1, transform: 'scale(1)' }
|
|
|
|
],
|
2021-05-26 11:31:42 +00:00
|
|
|
options: { duration: 250, easing: 'ease' }
|
2021-05-21 21:53:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
setDefaultAnimation('dialog.hide', {
|
|
|
|
keyframes: [
|
|
|
|
{ opacity: 1, transform: 'scale(1)' },
|
|
|
|
{ opacity: 0, transform: 'scale(0.8)' }
|
|
|
|
],
|
2021-05-26 11:31:42 +00:00
|
|
|
options: { duration: 250, easing: 'ease' }
|
2021-05-21 21:53:53 +00:00
|
|
|
});
|
|
|
|
|
2021-06-21 13:40:11 +00:00
|
|
|
setDefaultAnimation('dialog.denyClose', {
|
|
|
|
keyframes: [{ transform: 'scale(1)' }, { transform: 'scale(1.02)' }, { transform: 'scale(1)' }],
|
|
|
|
options: { duration: 250 }
|
|
|
|
});
|
|
|
|
|
2021-05-21 21:53:53 +00:00
|
|
|
setDefaultAnimation('dialog.overlay.show', {
|
|
|
|
keyframes: [{ opacity: 0 }, { opacity: 1 }],
|
2021-05-26 11:31:42 +00:00
|
|
|
options: { duration: 250 }
|
2021-05-21 21:53:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
setDefaultAnimation('dialog.overlay.hide', {
|
|
|
|
keyframes: [{ opacity: 1 }, { opacity: 0 }],
|
2021-05-26 11:31:42 +00:00
|
|
|
options: { duration: 250 }
|
2021-05-21 21:53:53 +00:00
|
|
|
});
|
|
|
|
|
2021-03-12 14:09:08 +00:00
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'sl-dialog': SlDialog;
|
|
|
|
}
|
|
|
|
}
|