2022-02-26 15:48:56 +00:00
|
|
|
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/icon';
|
|
|
|
import { animateTo, shimKeyframesHeightAuto, stopAnimations } from '~/internal/animate';
|
|
|
|
import { emit, waitForEvent } from '~/internal/event';
|
|
|
|
import { watch } from '~/internal/watch';
|
|
|
|
import { getAnimation, setDefaultAnimation } from '~/utilities/animation-registry';
|
2021-07-10 00:45:44 +00:00
|
|
|
import styles from './details.styles';
|
2021-07-12 14:36:06 +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
|
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @slot - The details' content.
|
|
|
|
* @slot summary - The details' summary. Alternatively, you can use the summary prop.
|
2020-07-15 21:30:37 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @event sl-show - Emitted when the details opens.
|
2021-09-27 21:58:14 +00:00
|
|
|
* @event sl-after-show - Emitted after the details opens and all animations are complete.
|
2021-06-25 20:25:46 +00:00
|
|
|
* @event sl-hide - Emitted when the details closes.
|
2021-09-27 21:58:14 +00:00
|
|
|
* @event sl-after-hide - Emitted after the details closes and all animations are complete.
|
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 header - The summary header.
|
|
|
|
* @csspart summary - The details summary.
|
|
|
|
* @csspart summary-icon - The expand/collapse summary icon.
|
|
|
|
* @csspart content - The details content.
|
2021-06-24 22:24:54 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @animation details.show - The animation to use when showing details. You can use `height: auto` with this animation.
|
|
|
|
* @animation details.hide - The animation to use when hiding details. You can use `height: auto` with this animation.
|
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-07-10 00:45:44 +00:00
|
|
|
static styles = styles;
|
2021-03-06 17:01:39 +00:00
|
|
|
|
|
|
|
@query('.details') details: HTMLElement;
|
|
|
|
@query('.details__header') header: HTMLElement;
|
|
|
|
@query('.details__body') body: HTMLElement;
|
2021-02-26 14:09:13 +00:00
|
|
|
|
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-07-01 00:04:46 +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-07-01 00:04:46 +00:00
|
|
|
@property({ type: Boolean, reflect: true }) disabled = false;
|
2021-03-06 17:01:39 +00:00
|
|
|
|
2021-06-02 12:47:55 +00:00
|
|
|
firstUpdated() {
|
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-05-27 20:29:10 +00:00
|
|
|
/** Shows the details. */
|
2021-05-26 11:31:26 +00:00
|
|
|
async show() {
|
2022-02-01 14:19:15 +00:00
|
|
|
if (this.open || this.disabled) {
|
2022-01-16 05:47:14 +00:00
|
|
|
return undefined;
|
2020-08-13 14:29:31 +00:00
|
|
|
}
|
2020-07-15 21:30:37 +00:00
|
|
|
|
2021-05-26 11:31:26 +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
|
|
|
}
|
|
|
|
|
2021-05-27 20:29:10 +00:00
|
|
|
/** Hides the details */
|
2021-05-26 11:31:26 +00:00
|
|
|
async hide() {
|
2022-02-01 14:19:15 +00:00
|
|
|
if (!this.open || this.disabled) {
|
2022-01-16 05:47:14 +00:00
|
|
|
return undefined;
|
2020-08-13 14:29:31 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
handleSummaryClick() {
|
|
|
|
if (!this.disabled) {
|
2022-02-01 14:19:15 +00:00
|
|
|
if (this.open) {
|
|
|
|
this.hide();
|
|
|
|
} else {
|
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
|
2020-07-15 21:30:37 +00:00
|
|
|
this.header.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSummaryKeyDown(event: KeyboardEvent) {
|
|
|
|
if (event.key === 'Enter' || event.key === ' ') {
|
|
|
|
event.preventDefault();
|
2022-02-01 14:19:15 +00:00
|
|
|
|
|
|
|
if (this.open) {
|
|
|
|
this.hide();
|
|
|
|
} else {
|
|
|
|
this.show();
|
|
|
|
}
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {
|
|
|
|
event.preventDefault();
|
2022-01-17 04:44:10 +00:00
|
|
|
this.hide();
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
|
|
|
|
event.preventDefault();
|
2022-01-17 04:44:10 +00:00
|
|
|
this.show();
|
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
|
2021-06-24 22:24:54 +00:00
|
|
|
emit(this, 'sl-show');
|
2021-05-27 20:29:10 +00:00
|
|
|
|
2022-02-14 16:08:26 +00:00
|
|
|
await stopAnimations(this.body);
|
2021-05-27 20:29:10 +00:00
|
|
|
this.body.hidden = false;
|
|
|
|
|
|
|
|
const { keyframes, options } = getAnimation(this, 'details.show');
|
|
|
|
await animateTo(this.body, shimKeyframesHeightAuto(keyframes, this.body.scrollHeight), options);
|
|
|
|
this.body.style.height = 'auto';
|
|
|
|
|
2021-06-24 22:24:54 +00:00
|
|
|
emit(this, 'sl-after-show');
|
2021-05-27 20:29:10 +00:00
|
|
|
} else {
|
|
|
|
// Hide
|
2021-06-24 22:24:54 +00:00
|
|
|
emit(this, 'sl-hide');
|
2021-05-27 20:29:10 +00:00
|
|
|
|
2022-02-14 16:08:26 +00:00
|
|
|
await stopAnimations(this.body);
|
2021-05-27 20:29:10 +00:00
|
|
|
|
|
|
|
const { keyframes, options } = getAnimation(this, 'details.hide');
|
|
|
|
await animateTo(this.body, shimKeyframesHeightAuto(keyframes, this.body.scrollHeight), options);
|
|
|
|
this.body.hidden = true;
|
|
|
|
this.body.style.height = 'auto';
|
|
|
|
|
2021-06-24 22:24:54 +00:00
|
|
|
emit(this, 'sl-after-hide');
|
2021-05-27 20:29:10 +00:00
|
|
|
}
|
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-12-30 17:14:39 +00:00
|
|
|
id="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'}
|
2021-12-30 17:14:39 +00:00
|
|
|
aria-controls="content"
|
2021-02-26 14:09:13 +00:00
|
|
|
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">
|
2021-04-20 13:37:19 +00:00
|
|
|
<sl-icon name="chevron-right" library="system"></sl-icon>
|
2020-07-15 21:30:37 +00:00
|
|
|
</span>
|
|
|
|
</header>
|
|
|
|
|
2021-05-26 11:31:26 +00:00
|
|
|
<div class="details__body">
|
2021-12-30 17:14:39 +00:00
|
|
|
<div part="content" id="content" class="details__content" role="region" aria-labelledby="header">
|
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:07:38 +00:00
|
|
|
|
2021-05-26 11:31:26 +00:00
|
|
|
setDefaultAnimation('details.show', {
|
|
|
|
keyframes: [
|
|
|
|
{ height: '0', opacity: '0' },
|
|
|
|
{ height: 'auto', opacity: '1' }
|
|
|
|
],
|
|
|
|
options: { duration: 250, easing: 'linear' }
|
|
|
|
});
|
|
|
|
|
|
|
|
setDefaultAnimation('details.hide', {
|
|
|
|
keyframes: [
|
|
|
|
{ height: 'auto', opacity: '1' },
|
|
|
|
{ height: '0', opacity: '0' }
|
|
|
|
],
|
|
|
|
options: { duration: 250, easing: 'linear' }
|
|
|
|
});
|
|
|
|
|
2021-03-12 14:09:08 +00:00
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'sl-details': SlDetails;
|
|
|
|
}
|
|
|
|
}
|