/** * Copyright Aquera Inc 2023 * * This source code is licensed under the BSD-3-Clause license found in the * LICENSE file in the root directory of this source tree. */ import {LitElement, CSSResultArray, TemplateResult} from 'lit'; import {styles} from './nile-dialog.css'; import '../nile-icon-button/nile-icon-button'; import { animateTo, stopAnimations } from '../internal/animate'; import { classMap } from 'lit/directives/class-map.js'; import { customElement, property, query } from 'lit/decorators.js'; import { getAnimation, setDefaultAnimation } from '../utilities/animation-registry'; import { HasSlotController } from '../internal/slot'; import { html,nothing } from 'lit'; import { ifDefined } from 'lit/directives/if-defined.js'; import { lockBodyScrolling, unlockBodyScrolling } from '../internal/scroll'; import { waitForEvent } from '../internal/event'; import { watch } from '../internal/watch'; import Modal from '../internal/modal'; import type { CSSResultGroup } from 'lit'; import NileElement from '../internal/nile-element'; /** * Nile icon component. * * @tag nile-modal * */ @customElement('nile-dialog') export class NileDialog extends NileElement { static styles: CSSResultGroup = styles; private readonly hasSlotController = new HasSlotController(this, 'footer','sublabel','label'); private modal: Modal; private originalTrigger: HTMLElement | null; @query('.dialog') dialog: HTMLElement; @query('.dialog__panel') panel: HTMLElement; @query('.dialog__overlay') overlay: HTMLElement; /** * Indicates whether or not the dialog is open. You can toggle this attribute to show and hide the dialog, or you can * use the `show()` and `hide()` methods and this attribute will reflect the dialog's open state. */ @property({ type: Boolean, reflect: true }) open = false; /** * The dialog's label as displayed in the header. You should always include a relevant label even when using * `no-header`, as it is required for proper accessibility. If you need to display HTML, use the `label` slot instead. */ @property({ reflect: true }) label = ''; @property({ reflect: true }) sublabel = ''; /** * 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. */ @property({ type: Boolean, reflect: true }) noHeader = false; @property({ type: Boolean, reflect: true }) preventOverlayClose = false; /** * Added to resolve conflicts between Angular Material's mat-drawer components. */ private drawerStyle = ` `; connectedCallback() { super.connectedCallback(); this.handleDocumentKeyDown = this.handleDocumentKeyDown.bind(this); this.modal = new Modal(this); } firstUpdated() { this.dialog.hidden = !this.open; if (this.open) { this.addOpenListeners(); this.modal.activate(); lockBodyScrolling(this); document.head.insertAdjacentHTML('beforeend', this.drawerStyle); } } disconnectedCallback() { super.disconnectedCallback(); unlockBodyScrolling(this); } private requestClose(source: 'close-button' | 'keyboard' | 'overlay') { const nileRequestClose = this.emit('nile-request-close', { cancelable: true, detail: { source }, }); if (source === 'close-button') { this.hide(); return; } if (nileRequestClose.defaultPrevented || this.preventOverlayClose) { const animation = getAnimation(this, 'dialog.denyClose', { dir: 'right', }); animateTo(this.panel, animation.keyframes, animation.options); return; } this.hide(); } private addOpenListeners() { document.addEventListener('keydown', this.handleDocumentKeyDown); } private removeOpenListeners() { document.removeEventListener('keydown', this.handleDocumentKeyDown); } private handleDocumentKeyDown(event: KeyboardEvent) { if (this.open && event.key === 'Escape') { event.stopPropagation(); this.requestClose('keyboard'); } } @watch('open', { waitUntilFirstUpdate: true }) async handleOpenChange() { if (this.open) { this.emit('nile-show'); this.addOpenListeners(); this.originalTrigger = document.activeElement as HTMLElement; this.modal.activate(); document.head.insertAdjacentHTML('beforeend', this.drawerStyle); lockBodyScrolling(this); const autoFocusTarget = this.querySelector('[autofocus]'); if (autoFocusTarget) { autoFocusTarget.removeAttribute('autofocus'); } await Promise.all([ stopAnimations(this.dialog), stopAnimations(this.overlay), ]); this.dialog.hidden = false; // Set initial focus requestAnimationFrame(() => { const nileInitialFocus = this.emit('nile-initial-focus', { cancelable: true, }); if (!nileInitialFocus.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 }); } } // Restore the autofocus attribute if (autoFocusTarget) { autoFocusTarget.setAttribute('autofocus', ''); } }); const panelAnimation = getAnimation(this, 'dialog.show', { dir: 'right', }); const overlayAnimation = getAnimation(this, 'dialog.overlay.show', { dir: 'right', }); await Promise.all([ animateTo(this.panel, panelAnimation.keyframes, panelAnimation.options), animateTo( this.overlay, overlayAnimation.keyframes, overlayAnimation.options ), ]); this.emit('nile-after-show'); } else { // Hide this.emit('nile-hide'); this.removeOpenListeners(); this.modal.deactivate(); await Promise.all([ stopAnimations(this.dialog), stopAnimations(this.overlay), ]); const panelAnimation = getAnimation(this, 'dialog.hide', { dir: 'right', }); const overlayAnimation = getAnimation(this, 'dialog.overlay.hide', { dir: 'right', }); const styleTag = document.getElementById('drawer-style'); if (styleTag) { document.head.removeChild(styleTag); } // 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. await Promise.all([ animateTo( this.overlay, overlayAnimation.keyframes, overlayAnimation.options ).then(() => { this.overlay.hidden = true; }), animateTo( this.panel, panelAnimation.keyframes, panelAnimation.options ).then(() => { this.panel.hidden = true; }), ]); this.dialog.hidden = true; // Now that the dialog is hidden, restore the overlay and panel for next time this.overlay.hidden = false; this.panel.hidden = false; unlockBodyScrolling(this); // Restore focus to the original trigger const trigger = this.originalTrigger; if (typeof trigger?.focus === 'function') { setTimeout(() => trigger.focus()); } this.emit('nile-after-hide'); } } /** Shows the dialog. */ async show() { if (this.open) { return undefined; } this.open = true; return waitForEvent(this, 'nile-after-show'); } /** Hides the dialog */ async hide() { if (!this.open) { return undefined; } this.open = false; return waitForEvent(this, 'nile-after-hide'); } render() { return html`