shoelace/src/components/alert/alert.ts

242 wiersze
6.9 KiB
TypeScript
Czysty Zwykły widok Historia

import { html, LitElement } from 'lit';
2021-05-27 21:00:43 +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';
2022-03-24 11:48:03 +00:00
import '~/components/icon-button/icon-button';
import { animateTo, stopAnimations } from '~/internal/animate';
import { emit, waitForEvent } from '~/internal/event';
import { HasSlotController } from '~/internal/slot';
import { watch } from '~/internal/watch';
import { getAnimation, setDefaultAnimation } from '~/utilities/animation-registry';
2021-07-10 00:45:44 +00:00
import styles from './alert.styles';
2020-07-15 21:30:37 +00:00
2020-09-18 13:40:21 +00:00
const toastStack = Object.assign(document.createElement('div'), { className: 'sl-toast-stack' });
2020-09-16 21:00:48 +00:00
2020-07-15 21:30:37 +00:00
/**
2020-07-17 10:09:10 +00:00
* @since 2.0
2020-07-15 21:30:37 +00:00
* @status stable
*
2021-02-26 14:09:13 +00:00
* @dependency sl-icon-button
*
2021-06-25 20:25:46 +00:00
* @slot - The alert's content.
* @slot icon - An icon to show in the alert.
2020-07-15 21:30:37 +00:00
*
2021-06-25 20:25:46 +00:00
* @event sl-show - Emitted when the alert opens.
2021-09-27 21:58:14 +00:00
* @event sl-after-show - Emitted after the alert opens and all animations are complete.
2021-06-25 20:25:46 +00:00
* @event sl-hide - Emitted when the alert closes.
2021-09-27 21:58:14 +00:00
* @event sl-after-hide - Emitted after the alert closes and all animations are complete.
*
2022-03-09 20:54:18 +00:00
* @csspart base - The component's internal wrapper.
2021-06-25 20:25:46 +00:00
* @csspart icon - The container that wraps the alert icon.
* @csspart message - The alert message.
* @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
* @cssproperty --box-shadow - The alert's box shadow.
*
2021-06-25 20:25:46 +00:00
* @animation alert.show - The animation to use when showing the alert.
* @animation alert.hide - The animation to use when hiding the alert.
2020-07-15 21:30:37 +00:00
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-alert')
2021-03-09 00:14:32 +00:00
export default class SlAlert extends LitElement {
2021-07-10 00:45:44 +00:00
static styles = styles;
2020-07-15 21:30:37 +00:00
private autoHideTimeout: number;
2022-03-09 20:54:18 +00:00
private readonly hasSlotController = new HasSlotController(this, 'icon', 'suffix');
2021-03-06 17:01:39 +00:00
2021-05-19 17:46:18 +00:00
@query('[part="base"]') base: HTMLElement;
2020-10-13 16:41:57 +00:00
2020-07-15 21:30:37 +00:00
/** Indicates whether or not the alert 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;
2020-07-15 21:30:37 +00:00
2021-02-26 14:09:13 +00:00
/** Makes the alert closable. */
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) closable = false;
2020-07-15 21:30:37 +00:00
2021-12-13 22:38:40 +00:00
/** The alert's variant. */
@property({ reflect: true }) variant: 'primary' | 'success' | 'neutral' | 'warning' | 'danger' = 'primary';
2020-07-15 21:30:37 +00:00
/**
2021-02-26 14:09:13 +00:00
* The length of time, in milliseconds, the alert will show before closing itself. If the user interacts with
* the alert before it closes (e.g. moves the mouse over it), the timer will restart. Defaults to `Infinity`.
*/
@property({ type: Number }) duration = Infinity;
2021-03-06 17:01:39 +00:00
2021-06-02 12:47:55 +00:00
firstUpdated() {
2021-05-19 17:46:18 +00:00
this.base.hidden = !this.open;
2020-07-15 21:30:37 +00:00
}
/** Shows the alert. */
2021-05-19 17:46:18 +00:00
async show() {
2021-05-27 20:29:10 +00:00
if (this.open) {
return undefined;
2020-07-15 21:30:37 +00:00
}
this.open = true;
2021-05-27 20:29:10 +00:00
return waitForEvent(this, 'sl-after-show');
2020-07-15 21:30:37 +00:00
}
/** Hides the alert */
2021-05-19 17:46:18 +00:00
async hide() {
2021-05-27 20:29:10 +00:00
if (!this.open) {
return undefined;
2020-07-15 21:30:37 +00:00
}
this.open = false;
2021-05-27 20:29:10 +00:00
return waitForEvent(this, 'sl-after-hide');
2020-07-15 21:30:37 +00:00
}
2020-09-17 20:27:11 +00:00
/**
2021-03-22 15:03:24 +00:00
* Displays the alert as a toast notification. This will move the alert out of its position in the DOM and, when
* dismissed, it will be removed from the DOM completely. By storing a reference to the alert, you can reuse it by
* calling this method again. The returned promise will resolve after the alert is hidden.
2020-09-17 20:27:11 +00:00
*/
async toast() {
2021-01-04 19:11:23 +00:00
return new Promise<void>(resolve => {
if (toastStack.parentElement === null) {
2020-09-18 13:40:21 +00:00
document.body.append(toastStack);
2020-09-17 20:27:11 +00:00
}
2021-02-26 14:09:13 +00:00
toastStack.appendChild(this);
2021-03-22 15:03:24 +00:00
// Wait for the toast stack to render
requestAnimationFrame(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions -- force a reflow for the initial transition
this.clientWidth;
this.show();
2021-03-22 15:03:24 +00:00
});
2020-09-17 20:27:11 +00:00
2021-02-26 14:09:13 +00:00
this.addEventListener(
2020-10-09 21:45:16 +00:00
'sl-after-hide',
2020-09-17 20:27:11 +00:00
() => {
2021-02-26 14:09:13 +00:00
toastStack.removeChild(this);
2020-09-17 20:27:11 +00:00
resolve();
2020-09-18 13:40:21 +00:00
// Remove the toast stack from the DOM when there are no more alerts
if (toastStack.querySelector('sl-alert') === null) {
2020-09-18 13:40:21 +00:00
toastStack.remove();
2020-09-17 20:27:11 +00:00
}
},
{ once: true }
);
});
}
2021-02-26 14:09:13 +00:00
restartAutoHide() {
clearTimeout(this.autoHideTimeout);
if (this.open && this.duration < Infinity) {
this.autoHideTimeout = window.setTimeout(() => this.hide(), this.duration);
2021-02-26 14:09:13 +00:00
}
}
2020-07-15 21:30:37 +00:00
handleCloseClick() {
this.hide();
2020-07-15 21:30:37 +00:00
}
handleMouseMove() {
this.restartAutoHide();
}
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
emit(this, 'sl-show');
2021-05-27 20:29:10 +00:00
if (this.duration < Infinity) {
this.restartAutoHide();
}
await stopAnimations(this.base);
this.base.hidden = false;
const { keyframes, options } = getAnimation(this, 'alert.show');
await animateTo(this.base, keyframes, options);
emit(this, 'sl-after-show');
2021-05-27 20:29:10 +00:00
} else {
// Hide
emit(this, 'sl-hide');
2021-05-27 20:29:10 +00:00
clearTimeout(this.autoHideTimeout);
await stopAnimations(this.base);
const { keyframes, options } = getAnimation(this, 'alert.hide');
await animateTo(this.base, keyframes, options);
this.base.hidden = true;
emit(this, 'sl-after-hide');
2021-05-27 20:29:10 +00:00
}
2021-02-26 14:09:13 +00:00
}
@watch('duration')
2021-03-11 17:56:45 +00:00
handleDurationChange() {
2021-02-26 14:09:13 +00:00
this.restartAutoHide();
}
2020-07-15 21:30:37 +00:00
render() {
2021-02-26 14:09:13 +00:00
return html`
2020-10-13 16:41:57 +00:00
<div
part="base"
2021-02-26 14:09:13 +00:00
class=${classMap({
2020-10-13 16:41:57 +00:00
alert: true,
'alert--open': this.open,
'alert--closable': this.closable,
2022-03-09 20:54:18 +00:00
'alert--has-icon': this.hasSlotController.test('icon'),
2021-12-13 22:38:40 +00:00
'alert--primary': this.variant === 'primary',
'alert--success': this.variant === 'success',
'alert--neutral': this.variant === 'neutral',
'alert--warning': this.variant === 'warning',
'alert--danger': this.variant === 'danger'
2021-02-26 14:09:13 +00:00
})}
2020-10-13 16:41:57 +00:00
role="alert"
aria-live="assertive"
aria-atomic="true"
2021-02-26 14:09:13 +00:00
aria-hidden=${this.open ? 'false' : 'true'}
2021-07-07 12:13:07 +00:00
@mousemove=${this.handleMouseMove}
2020-10-13 16:41:57 +00:00
>
<span part="icon" class="alert__icon">
2021-03-06 17:01:39 +00:00
<slot name="icon"></slot>
2020-10-13 16:41:57 +00:00
</span>
<span part="message" class="alert__message">
2021-03-06 17:01:39 +00:00
<slot></slot>
2020-10-13 16:41:57 +00:00
</span>
2021-02-26 14:09:13 +00:00
${this.closable
? html`
2022-03-09 20:54:18 +00:00
<sl-icon-button
part="close-button"
exportparts="base:close-button__base"
class="alert__close-button"
name="x"
library="system"
@click=${this.handleCloseClick}
></sl-icon-button>
2021-02-26 14:09:13 +00:00
`
: ''}
2020-10-13 16:41:57 +00:00
</div>
2021-02-26 14:09:13 +00:00
`;
2020-07-15 21:30:37 +00:00
}
}
setDefaultAnimation('alert.show', {
keyframes: [
{ opacity: 0, transform: 'scale(0.8)' },
{ opacity: 1, transform: 'scale(1)' }
],
2021-05-26 11:29:59 +00:00
options: { duration: 250, easing: 'ease' }
});
setDefaultAnimation('alert.hide', {
keyframes: [
{ opacity: 1, transform: 'scale(1)' },
{ opacity: 0, transform: 'scale(0.8)' }
],
2021-05-26 11:29:59 +00:00
options: { duration: 250, easing: 'ease' }
});
2021-03-12 14:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-alert': SlAlert;
}
}