shoelace/src/components/drawer/drawer.ts

384 wiersze
13 KiB
TypeScript
Czysty Zwykły widok Historia

2021-07-10 00:45:44 +00:00
import { LitElement, html } from 'lit';
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';
2021-07-10 00:45:44 +00:00
import styles from './drawer.styles';
import '~/components/icon-button/icon-button';
import { animateTo, stopAnimations } from '~/internal/animate';
import { emit, waitForEvent } from '~/internal/event';
import Modal from '~/internal/modal';
import { lockBodyScrolling, unlockBodyScrolling } from '~/internal/scroll';
import { HasSlotController } from '~/internal/slot';
import { uppercaseFirstLetter } from '~/internal/string';
import { isPreventScrollSupported } from '~/internal/support';
import { watch } from '~/internal/watch';
import { setDefaultAnimation, getAnimation } from '~/utilities/animation-registry';
2021-07-12 14:36:06 +00:00
2021-01-08 15:25:29 +00:00
const hasPreventScroll = isPreventScrollSupported();
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 drawer's content.
* @slot label - The drawer's label. Alternatively, you can use the label prop.
* @slot footer - The drawer's footer, usually one or more buttons representing various options.
2020-07-15 21:30:37 +00:00
*
2021-06-25 20:25:46 +00:00
* @event sl-show - Emitted when the drawer opens.
2021-09-27 21:58:14 +00:00
* @event sl-after-show - Emitted after the drawer opens and all animations are complete.
2021-06-25 20:25:46 +00:00
* @event sl-hide - Emitted when the drawer closes.
2021-09-27 21:58:14 +00:00
* @event sl-after-hide - Emitted after the drawer closes and all animations are complete.
2021-06-25 20:25:46 +00:00
* @event sl-initial-focus - Emitted when the drawer opens and the panel gains focus. Calling `event.preventDefault()` will
* prevent focus and allow you to set it on a different element in the drawer, such as an input or button.
2021-06-25 20:25:46 +00:00
* @event sl-request-close - Emitted when the user attempts to close the drawer by clicking the close button, clicking the
* overlay, or pressing the escape key. Calling `event.preventDefault()` will prevent the drawer from closing. Avoid
* using this unless closing the drawer will result in destructive behavior such as data loss.
*
2021-06-25 20:25:46 +00:00
* @csspart base - The component's base wrapper.
* @csspart overlay - The overlay.
* @csspart panel - The drawer panel (where the drawer and its content is rendered).
* @csspart header - The drawer header.
* @csspart title - The drawer title.
* @csspart close-button - The close button.
* @csspart body - The drawer body.
* @csspart footer - The drawer footer.
*
2021-06-25 20:25:46 +00:00
* @cssproperty --size - The preferred size of the drawer. This will be applied to the drawer's width or height
2021-05-26 11:32:16 +00:00
* depending on its `placement`. Note that the drawer will shrink to accommodate smaller screens.
2021-06-25 20:25:46 +00:00
* @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-05-26 11:32:16 +00:00
*
2021-06-25 20:25:46 +00:00
* @animation drawer.showTop - The animation to use when showing a drawer with `top` placement.
* @animation drawer.showEnd - The animation to use when showing a drawer with `end` placement.
* @animation drawer.showBottom - The animation to use when showing a drawer with `bottom` placement.
* @animation drawer.showStart - The animation to use when showing a drawer with `start` placement.
* @animation drawer.hideTop - The animation to use when hiding a drawer with `top` placement.
* @animation drawer.hideEnd - The animation to use when hiding a drawer with `end` placement.
* @animation drawer.hideBottom - The animation to use when hiding a drawer with `bottom` placement.
* @animation drawer.hideStart - The animation to use when hiding a drawer with `start` placement.
* @animation drawer.denyClose - The animation to use when a request to close the drawer is denied.
* @animation drawer.overlay.show - The animation to use when showing the drawer's overlay.
* @animation drawer.overlay.hide - The animation to use when hiding the drawer's overlay.
2020-07-15 21:30:37 +00:00
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-drawer')
2021-03-09 00:14:32 +00:00
export default class SlDrawer extends LitElement {
2021-07-10 00:45:44 +00:00
static styles = styles;
2021-03-06 17:01:39 +00:00
@query('.drawer') drawer: HTMLElement;
@query('.drawer__panel') panel: HTMLElement;
2021-05-26 11:32:16 +00:00
@query('.drawer__overlay') overlay: HTMLElement;
2021-02-26 14:09:13 +00:00
private readonly hasSlotController = new HasSlotController(this, 'footer');
2021-02-26 14:09:13 +00:00
private modal: Modal;
private originalTrigger: HTMLElement | null;
2020-07-15 21:30:37 +00:00
/** Indicates whether or not the drawer 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
/**
* The drawer'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.
*/
2021-07-01 00:04:46 +00:00
@property({ reflect: true }) label = '';
2020-07-15 21:30:37 +00:00
/** The direction from which the drawer will open. */
2021-05-26 11:32:16 +00:00
@property({ reflect: true }) placement: 'top' | 'end' | 'bottom' | 'start' = 'end';
2020-07-15 21:30:37 +00:00
/**
* By default, the drawer slides out of its containing block (usually the viewport). To make the drawer slide out of
* its parent element, set this prop and add `position: relative` to the parent.
*/
2021-07-01 00:04:46 +00:00
@property({ type: Boolean, reflect: true }) contained = false;
2020-07-15 21:30:37 +00:00
/**
* Removes the header. This will also remove the default close button, so please ensure you provide an easy,
* accessible way for users to dismiss the drawer.
*/
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();
this.modal = new Modal(this);
2020-07-15 21:30:37 +00:00
}
2021-06-02 12:47:55 +00:00
firstUpdated() {
2021-05-26 11:32:16 +00:00
this.drawer.hidden = !this.open;
2021-06-28 20:49:56 +00:00
if (this.open && !this.contained) {
this.modal.activate();
lockBodyScrolling(this);
}
2021-05-26 11:32:16 +00:00
}
2021-03-06 17:01:39 +00:00
disconnectedCallback() {
super.disconnectedCallback();
2021-02-26 14:09:13 +00:00
unlockBodyScrolling(this);
2020-07-15 21:30:37 +00:00
}
2021-05-27 20:29:10 +00:00
/** Shows the drawer. */
2021-05-26 11:32:16 +00:00
async show() {
2021-05-27 20:29:10 +00:00
if (this.open) {
return undefined;
2020-08-13 14:29:31 +00:00
}
2020-07-15 21:30:37 +00:00
2020-08-13 14:29:31 +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 drawer */
2021-05-26 11:32:16 +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
}
2021-06-21 13:40:11 +00:00
private requestClose() {
const slRequestClose = emit(this, 'sl-request-close', { cancelable: true });
2021-06-21 13:40:11 +00:00
if (slRequestClose.defaultPrevented) {
const animation = getAnimation(this, 'drawer.denyClose');
void animateTo(this.panel, animation.keyframes, animation.options);
2021-06-21 13:40:11 +00:00
return;
}
void this.hide();
2020-07-15 21:30:37 +00:00
}
handleKeyDown(event: KeyboardEvent) {
if (event.key === 'Escape') {
event.stopPropagation();
2021-06-21 13:40:11 +00:00
this.requestClose();
2020-07-15 21:30:37 +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
emit(this, 'sl-show');
2021-05-27 20:29:10 +00:00
this.originalTrigger = document.activeElement as HTMLElement;
// Lock body scrolling only if the drawer isn't contained
if (!this.contained) {
this.modal.activate();
lockBodyScrolling(this);
}
await Promise.all([stopAnimations(this.drawer), stopAnimations(this.overlay)]);
this.drawer.hidden = false;
// Browsers that support el.focus({ preventScroll }) can set initial focus immediately
if (hasPreventScroll) {
const slInitialFocus = emit(this, 'sl-initial-focus', { cancelable: true });
2021-05-27 20:29:10 +00:00
if (!slInitialFocus.defaultPrevented) {
this.panel.focus({ preventScroll: true });
}
}
const panelAnimation = getAnimation(this, `drawer.show${uppercaseFirstLetter(this.placement)}`);
const overlayAnimation = getAnimation(this, 'drawer.overlay.show');
await Promise.all([
animateTo(this.panel, panelAnimation.keyframes, panelAnimation.options),
animateTo(this.overlay, overlayAnimation.keyframes, overlayAnimation.options)
]);
// Browsers that don't support el.focus({ preventScroll }) have to wait for the animation to finish before initial
// focus to prevent scrolling issues. See: https://caniuse.com/mdn-api_htmlelement_focus_preventscroll_option
if (!hasPreventScroll) {
const slInitialFocus = emit(this, 'sl-initial-focus', { cancelable: true });
2021-05-27 20:29:10 +00:00
if (!slInitialFocus.defaultPrevented) {
this.panel.focus({ preventScroll: true });
}
}
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
this.modal.deactivate();
unlockBodyScrolling(this);
await Promise.all([stopAnimations(this.drawer), stopAnimations(this.overlay)]);
const panelAnimation = getAnimation(this, `drawer.hide${uppercaseFirstLetter(this.placement)}`);
const overlayAnimation = getAnimation(this, 'drawer.overlay.hide');
await Promise.all([
animateTo(this.panel, panelAnimation.keyframes, panelAnimation.options),
animateTo(this.overlay, overlayAnimation.keyframes, overlayAnimation.options)
]);
this.drawer.hidden = true;
// Restore focus to the original trigger
const trigger = this.originalTrigger;
if (typeof trigger?.focus === 'function') {
setTimeout(() => {
trigger.focus();
});
2021-05-27 20:29:10 +00:00
}
emit(this, 'sl-after-hide');
2021-05-27 20:29:10 +00:00
}
2021-03-30 12:33:08 +00:00
}
2020-07-15 21:30:37 +00:00
render() {
2021-02-26 14:09:13 +00:00
return html`
2020-07-15 21:30:37 +00:00
<div
part="base"
2021-02-26 14:09:13 +00:00
class=${classMap({
2020-07-15 21:30:37 +00:00
drawer: true,
'drawer--open': this.open,
'drawer--top': this.placement === 'top',
2021-05-26 11:32:16 +00:00
'drawer--end': this.placement === 'end',
2020-07-15 21:30:37 +00:00
'drawer--bottom': this.placement === 'bottom',
2021-05-26 11:32:16 +00:00
'drawer--start': this.placement === 'start',
2020-07-15 21:30:37 +00:00
'drawer--contained': this.contained,
'drawer--fixed': !this.contained,
'drawer--has-footer': this.hasSlotController.test('footer')
2021-02-26 14:09:13 +00:00
})}
2021-03-06 17:01:39 +00:00
@keydown=${this.handleKeyDown}
2020-07-15 21:30:37 +00:00
>
<div
part="overlay"
class="drawer__overlay"
@click=${this.requestClose}
@keydown=${this.handleKeyDown}
tabindex="-1"
></div>
2020-07-15 21:30:37 +00:00
<div
part="panel"
class="drawer__panel"
role="dialog"
aria-modal="true"
2021-02-26 14:09:13 +00:00
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"
2020-07-15 21:30:37 +00:00
>
2021-02-26 14:09:13 +00:00
${!this.noHeader
? html`
<header part="header" class="drawer__header">
2021-12-30 17:14:39 +00:00
<span part="title" class="drawer__title" id="title">
<!-- If there's no label, use an invisible character to prevent the header from collapsing -->
<slot name="label"> ${this.label.length > 0 ? this.label : String.fromCharCode(65279)} </slot>
2021-02-26 14:09:13 +00:00
</span>
<sl-icon-button
exportparts="base:close-button"
class="drawer__close"
name="x"
library="system"
2021-06-21 13:40:11 +00:00
@click=${this.requestClose}
2021-03-06 17:01:39 +00:00
></sl-icon-button>
2021-02-26 14:09:13 +00:00
</header>
`
: ''}
2020-07-15 21:30:37 +00:00
<div part="body" class="drawer__body">
2021-03-06 17:01:39 +00:00
<slot></slot>
2020-07-15 21:30:37 +00:00
</div>
<footer part="footer" class="drawer__footer">
<slot name="footer"></slot>
</footer>
2020-07-15 21:30:37 +00:00
</div>
</div>
2021-02-26 14:09:13 +00:00
`;
2020-07-15 21:30:37 +00:00
}
}
2021-05-26 11:32:16 +00:00
// Top
setDefaultAnimation('drawer.showTop', {
keyframes: [
{ opacity: 0, transform: 'translateY(-100%)' },
{ opacity: 1, transform: 'translateY(0)' }
],
options: { duration: 250, easing: 'ease' }
});
setDefaultAnimation('drawer.hideTop', {
keyframes: [
{ opacity: 1, transform: 'translateY(0)' },
{ opacity: 0, transform: 'translateY(-100%)' }
],
options: { duration: 250, easing: 'ease' }
});
// End
setDefaultAnimation('drawer.showEnd', {
keyframes: [
{ opacity: 0, transform: 'translateX(100%)' },
{ opacity: 1, transform: 'translateX(0)' }
],
options: { duration: 250, easing: 'ease' }
});
setDefaultAnimation('drawer.hideEnd', {
keyframes: [
{ opacity: 1, transform: 'translateX(0)' },
{ opacity: 0, transform: 'translateX(100%)' }
],
options: { duration: 250, easing: 'ease' }
});
// Bottom
setDefaultAnimation('drawer.showBottom', {
keyframes: [
{ opacity: 0, transform: 'translateY(100%)' },
{ opacity: 1, transform: 'translateY(0)' }
],
options: { duration: 250, easing: 'ease' }
});
setDefaultAnimation('drawer.hideBottom', {
keyframes: [
{ opacity: 1, transform: 'translateY(0)' },
{ opacity: 0, transform: 'translateY(100%)' }
],
options: { duration: 250, easing: 'ease' }
});
// Start
setDefaultAnimation('drawer.showStart', {
keyframes: [
{ opacity: 0, transform: 'translateX(-100%)' },
{ opacity: 1, transform: 'translateX(0)' }
],
options: { duration: 250, easing: 'ease' }
});
setDefaultAnimation('drawer.hideStart', {
keyframes: [
{ opacity: 1, transform: 'translateX(0)' },
{ opacity: 0, transform: 'translateX(-100%)' }
],
options: { duration: 250, easing: 'ease' }
});
2021-06-21 13:40:11 +00:00
// Deny close
setDefaultAnimation('drawer.denyClose', {
keyframes: [{ transform: 'scale(1)' }, { transform: 'scale(1.01)' }, { transform: 'scale(1)' }],
options: { duration: 250 }
});
2021-05-26 11:32:16 +00:00
// Overlay
setDefaultAnimation('drawer.overlay.show', {
keyframes: [{ opacity: 0 }, { opacity: 1 }],
options: { duration: 250 }
});
setDefaultAnimation('drawer.overlay.hide', {
keyframes: [{ opacity: 1 }, { opacity: 0 }],
options: { duration: 250 }
});
2021-03-12 14:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-drawer': SlDrawer;
}
}