details
Does it work without JavaScript?
HTMLDetailsElement
element.<drab-details
animation-keyframe-from-grid-template-rows="0fr"
animation-keyframe-to-grid-template-rows="1fr"
>
<details class="group overflow-hidden border-b px-4 pb-2 pt-4">
<summary
data-trigger
class="link flex list-none items-center justify-between gap-8 py-2"
>
<span>Does it work without JavaScript?</span>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="size-4 transition group-[[open]]:rotate-180"
>
<path
fill-rule="evenodd"
d="M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z"
clip-rule="evenodd"
/>
</svg>
</summary>
<div data-content class="grid">
<div class="overflow-hidden">
Yes, this element is a progressive enhancement on the native
<code>HTMLDetailsElement</code>
element.
</div>
</div>
</details>
</drab-details>
<style>
/* for safari */
summary::-webkit-details-marker {
display: none;
}
</style>
Overview
This element provides a progressive enhancement on top of the HTMLDetailsElement
to animate it with the Web Animations API.
The best way I’ve found to animate the details element is using CSS grid from this Kevin Powell video. The example demonstrates this animation. If you know the exact height of the content, you could animate height
instead.
Hierarchy
↳
Details
Constructors
constructor
• new Details(): Details
Returns
Overrides
Defined in
src/package/details/index.ts:16
Properties
#listenerController
• Private
#listenerController: AbortController
To clean up event listeners added to document
when the element is removed.
Inherited from
Defined in
Accessors
animationOptions
• get
animationOptions(): KeyframeAnimationOptions
Returns
KeyframeAnimationOptions
An object containing the values of each animation-option
attribute
Inherited from
Animate.animationOptions
Defined in
src/package/animate/index.ts:46
details
• get
details(): HTMLDetailsElement
Returns
HTMLDetailsElement
Defined in
src/package/details/index.ts:20
event
• get
event(): keyof HTMLElementEventMap
Event for the trigger
to execute.
For example, set to "mouseover"
to execute the event when the user hovers the mouse over the trigger
, instead of when they click it.
Returns
keyof HTMLElementEventMap
Default
"click";
Inherited from
Animate.event
Defined in
• set
event(value
): void
Parameters
Name | Type |
---|---|
value | keyof HTMLElementEventMap |
Returns
void
Inherited from
Animate.event
Defined in
keyframes
• get
keyframes(): Keyframe
[]
Returns
Keyframe
[]
Inherited from
Animate.keyframes
Defined in
src/package/animate/index.ts:128
Methods
animateElement
▸ animateElement(animateOptions?
): Promise
<void
>
Parameters
Name | Type | Description |
---|---|---|
animateOptions | Object | animates this.content() by default |
animateOptions.element? | HTMLElement | - |
animateOptions.options? | KeyframeAnimationOptions | - |
Returns
Promise
<void
>
Description
Animates a particular element using the web animations API.
- Disables animation if the user prefers reduced motion.
- Sets default options
- Uses the keyframes provided from
this.keyframes
- Waits for the animation to complete
- Sets the start and end styles based on the first and last keyframe
Inherited from
Defined in
src/package/animate/index.ts:76
close
▸ close(): Promise
<void
>
Closes details with animation.
Returns
Promise
<void
>
Defined in
src/package/details/index.ts:33
connectedCallback
▸ connectedCallback(): void
Returns
void
Inherited from
Defined in
destroy
▸ destroy(): void
Passed into disconnectedCallback
, since Base
needs to run disconnectedCallback
as well. It is overridden in each element that needs to run disconnectedCallback
.
Returns
void
Inherited from
Defined in
disconnectedCallback
▸ disconnectedCallback(): void
Returns
void
Inherited from
Defined in
getContent
▸ getContent<T
>(instance?
): T
Type parameters
Name | Type |
---|---|
T | extends HTMLElement = HTMLElement |
Parameters
Name | Type | Description |
---|---|---|
instance | () => T | The instance of the desired element, ex: HTMLDialogElement . Defaults to HTMLElement . |
Returns
T
The element that matches the content
selector.
Default
this.querySelector("[data-content]");
Inherited from
Defined in
getTrigger
▸ getTrigger<T
>(): NodeListOf
<T
>
Type parameters
Name | Type |
---|---|
T | extends HTMLElement = HTMLElement |
Returns
NodeListOf
<T
>
All of the elements that match the trigger
selector.
Default
this.querySelectorAll("[data-trigger]");
Inherited from
Defined in
mount
▸ mount(): void
Passed into queueMicrotask
in connectedCallback
. It is overridden in each component that needs to run connectedCallback
.
The reason for this is to make these elements work better with frameworks like Svelte. For SSR this isn’t necessary, but when client side rendering, the HTML within the custom element isn’t available before connectedCallback
is called. By waiting until the next microtask, the HTML content is available—then for example, listeners can be attached to elements inside.
Returns
void
Overrides
Defined in
src/package/details/index.ts:48
open
▸ open(): Promise
<void
>
Returns
Promise
<void
>
Defined in
src/package/details/index.ts:27
safeListener
▸ safeListener<K
, T
>(type
, listener
, element?
, options?
): void
Wrapper around document.body.addEventListener
that ensures when the element is removed from the DOM, these event listeners are cleaned up.
Type parameters
Name | Type |
---|---|
K | extends keyof DocumentEventMap |
T | extends Window | Document | HTMLElement = HTMLElement |
Parameters
Name | Type |
---|---|
type | K |
listener | (this : T , ev : DocumentEventMap [K ]) => any |
element | T |
options | AddEventListenerOptions |
Returns
void
Inherited from
Defined in
swapContent
▸ swapContent(revert?
, delay?
): void
Finds the HTMLElement | HTMLTemplateElement
via the swap
selector and swaps this.content()
with the content of the element found.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
revert | boolean | true | Swap back to old content |
delay | number | 800 | Wait time before swapping back |
Returns
void
Inherited from
Defined in
toggle
▸ toggle(): void
Returns
void
Defined in
src/package/details/index.ts:40
triggerListener
▸ triggerListener<T
, K
>(listener
, type?
, options?
): void
Type parameters
Name | Type |
---|---|
T | extends HTMLElement |
K | extends keyof HTMLElementEventMap |
Parameters
Name | Type | Description |
---|---|---|
listener | (this : T , e : HTMLElementEventMap [K ]) => any | Listener to attach to all of the trigger elements. |
type | K | - |
options? | AddEventListenerOptions | - |
Returns
void