shoelace/src/components/details/details.ts

227 wiersze
6.8 KiB
TypeScript
Czysty Zwykły widok Historia

import { LitElement, html, unsafeCSS } from 'lit';
import { customElement, property, query } from 'lit/decorators';
2021-03-06 17:01:39 +00:00
import { classMap } from 'lit-html/directives/class-map';
2021-03-18 13:04:23 +00:00
import { event, EventEmitter, watch } from '../../internal/decorators';
2021-02-26 14:09:13 +00:00
import { focusVisible } from '../../internal/focus-visible';
2021-05-03 19:08:17 +00:00
import styles from 'sass:./details.scss';
2020-07-15 21:30:37 +00:00
let id = 0;
/**
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
*
2020-07-15 21:30:37 +00:00
* @slot - The details' content.
* @slot summary - The details' summary. Alternatively, you can use the summary prop.
*
* @part base - The component's base wrapper.
2020-08-02 10:54:17 +00:00
* @part header - The summary header.
2020-07-15 21:30:37 +00:00
* @part summary - The details summary.
* @part summary-icon - The expand/collapse summary icon.
* @part content - The details content.
*
* @customProperty --hide-duration - The length of the hide transition.
* @customProperty --hide-timing-function - The timing function (easing) to use for the hide transition.
* @customProperty --show-duration - The length of the show transition.
* @customProperty --show-timing-function - The timing function (easing) to use for the show transition.
2020-07-15 21:30:37 +00:00
*/
2021-03-18 13:04:23 +00:00
@customElement('sl-details')
2021-03-09 00:14:32 +00:00
export default class SlDetails extends LitElement {
2021-03-06 17:01:39 +00:00
static styles = unsafeCSS(styles);
@query('.details') details: HTMLElement;
@query('.details__header') header: HTMLElement;
@query('.details__body') body: HTMLElement;
2021-02-26 14:09:13 +00:00
private componentId = `details-${++id}`;
private isVisible = false;
2020-07-15 21:30:37 +00:00
/** Indicates whether or not the details is open. You can use this in lieu of the show/hide methods. */
2021-03-06 20:34:33 +00:00
@property({ type: Boolean, reflect: true }) open = false;
2020-07-15 21:30:37 +00:00
2020-08-03 16:54:47 +00:00
/** The summary to show in the details header. If you need to display HTML, use the `summary` slot instead. */
2021-04-02 11:47:25 +00:00
@property() summary: string;
2020-07-15 21:30:37 +00:00
2021-02-26 14:09:13 +00:00
/** Disables the details so it can't be toggled. */
2021-03-06 20:34:33 +00:00
@property({ type: Boolean, reflect: true }) disabled = false;
2021-03-06 17:01:39 +00:00
/** Emitted when the details opens. Calling `event.preventDefault()` will prevent it from being opened. */
@event('sl-show') slShow: EventEmitter<void>;
/** Emitted after the details opens and all transitions are complete. */
2021-04-01 12:50:32 +00:00
@event('sl-after-show') slAfterShow: EventEmitter<void>;
2020-07-15 21:30:37 +00:00
2021-03-06 17:01:39 +00:00
/** Emitted when the details closes. Calling `event.preventDefault()` will prevent it from being closed. */
@event('sl-hide') slHide: EventEmitter<void>;
/** Emitted after the details closes and all transitions are complete. */
2021-04-01 12:50:32 +00:00
@event('sl-after-hide') slAfterHide: EventEmitter<void>;
2021-03-06 17:01:39 +00:00
2021-03-30 13:30:00 +00:00
connectedCallback() {
super.connectedCallback();
this.isVisible = this.open;
}
2021-03-06 17:01:39 +00:00
firstUpdated() {
2020-07-15 21:30:37 +00:00
focusVisible.observe(this.details);
2021-02-05 21:09:05 +00:00
this.body.hidden = !this.open;
2021-03-30 13:30:00 +00:00
this.body.style.height = this.open ? 'auto' : '0';
2020-07-15 21:30:37 +00:00
}
2021-03-06 17:01:39 +00:00
disconnectedCallback() {
super.disconnectedCallback();
focusVisible.unobserve(this.details);
2020-07-15 21:30:37 +00:00
}
/** Shows the alert. */
2021-02-26 14:09:13 +00:00
show() {
2020-08-13 14:29:31 +00:00
// Prevent subsequent calls to the method, whether manually or triggered by the `open` watcher
2021-04-01 13:02:25 +00:00
if (this.isVisible || this.disabled) {
2020-08-13 14:29:31 +00:00
return;
}
2020-07-15 21:30:37 +00:00
2021-03-06 17:01:39 +00:00
const slShow = this.slShow.emit();
2020-07-15 21:30:37 +00:00
if (slShow.defaultPrevented) {
2020-08-13 14:29:31 +00:00
this.open = false;
return;
2020-07-15 21:30:37 +00:00
}
2021-02-05 21:09:05 +00:00
this.body.hidden = false;
2020-08-05 17:56:47 +00:00
if (this.body.scrollHeight === 0) {
// When the scroll height can't be measured, use auto. This prevents a borked open state when the details is open
2021-03-03 12:59:40 +00:00
// intitially, but not immediately visible (i.e. in a tab panel).
2020-08-05 17:56:47 +00:00
this.body.style.height = 'auto';
this.body.style.overflow = 'visible';
} else {
this.body.style.height = `${this.body.scrollHeight}px`;
this.body.style.overflow = 'hidden';
}
2020-07-15 21:30:37 +00:00
2020-10-13 12:53:44 +00:00
this.isVisible = true;
2020-07-15 21:30:37 +00:00
this.open = true;
}
/** Hides the alert */
2021-02-26 14:09:13 +00:00
hide() {
2020-08-13 14:29:31 +00:00
// Prevent subsequent calls to the method, whether manually or triggered by the `open` watcher
2021-04-01 13:02:25 +00:00
if (!this.isVisible || this.disabled) {
2020-08-13 14:29:31 +00:00
return;
}
2020-07-15 21:30:37 +00:00
2021-03-06 17:01:39 +00:00
const slHide = this.slHide.emit();
2020-07-15 21:30:37 +00:00
if (slHide.defaultPrevented) {
2020-08-13 14:29:31 +00:00
this.open = true;
return;
2020-07-15 21:30:37 +00:00
}
// We can't transition out of `height: auto`, so let's set it to the current height first
this.body.style.height = `${this.body.scrollHeight}px`;
this.body.style.overflow = 'hidden';
requestAnimationFrame(() => {
this.body.clientWidth; // force a reflow
this.body.style.height = '0';
});
2020-10-13 12:53:44 +00:00
this.isVisible = false;
2020-07-15 21:30:37 +00:00
this.open = false;
}
handleBodyTransitionEnd(event: TransitionEvent) {
const target = event.target as HTMLElement;
// Ensure we only emit one event when the target element is no longer visible
if (event.propertyName === 'height' && target.classList.contains('details__body')) {
this.body.style.overflow = this.open ? 'visible' : 'hidden';
this.body.style.height = this.open ? 'auto' : '0';
2021-03-06 17:01:39 +00:00
this.open ? this.slAfterShow.emit() : this.slAfterHide.emit();
2021-02-05 21:09:05 +00:00
this.body.hidden = !this.open;
2020-07-15 21:30:37 +00:00
}
}
handleSummaryClick() {
if (!this.disabled) {
this.open ? this.hide() : this.show();
this.header.focus();
}
}
handleSummaryKeyDown(event: KeyboardEvent) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
this.open ? this.hide() : this.show();
}
if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {
event.preventDefault();
this.hide();
}
if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
event.preventDefault();
this.show();
}
}
2021-03-06 20:09:12 +00:00
@watch('open')
handleOpenChange() {
this.open ? this.show() : this.hide();
2021-02-26 14:09:13 +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
details: true,
'details--open': this.open,
'details--disabled': this.disabled
2021-02-26 14:09:13 +00:00
})}
2020-07-15 21:30:37 +00:00
>
<header
2020-08-02 10:54:17 +00:00
part="header"
2021-02-26 14:09:13 +00:00
id=${`${this.componentId}-header`}
2020-07-15 21:30:37 +00:00
class="details__header"
role="button"
2021-02-26 14:09:13 +00:00
aria-expanded=${this.open ? 'true' : 'false'}
aria-controls=${`${this.componentId}-content`}
aria-disabled=${this.disabled ? 'true' : 'false'}
tabindex=${this.disabled ? '-1' : '0'}
2021-03-06 17:01:39 +00:00
@click=${this.handleSummaryClick}
@keydown=${this.handleSummaryKeyDown}
2020-07-15 21:30:37 +00:00
>
<div part="summary" class="details__summary">
2021-02-26 14:09:13 +00:00
<slot name="summary">${this.summary}</slot>
2020-07-15 21:30:37 +00:00
</div>
<span part="summary-icon" class="details__summary-icon">
<sl-icon name="chevron-right" library="system"></sl-icon>
2020-07-15 21:30:37 +00:00
</span>
</header>
2021-03-06 17:01:39 +00:00
<div class="details__body" @transitionend=${this.handleBodyTransitionEnd}>
2020-07-15 21:30:37 +00:00
<div
part="content"
2021-02-26 14:09:13 +00:00
id=${`${this.componentId}-content`}
2020-07-15 21:30:37 +00:00
class="details__content"
role="region"
2021-02-26 14:09:13 +00:00
aria-labelledby=${`${this.componentId}-header`}
2020-07-15 21:30:37 +00:00
>
2021-03-06 17:01:39 +00:00
<slot></slot>
2020-07-15 21:30:37 +00:00
</div>
</div>
</div>
2021-02-26 14:09:13 +00:00
`;
2020-07-15 21:30:37 +00:00
}
}
2021-03-12 14:09:08 +00:00
declare global {
interface HTMLElementTagNameMap {
'sl-details': SlDetails;
}
}