base
Selected using data attributes by default.
Selected via a CSS selector.
<drab-base>
<button data-trigger class="button button-primary">Trigger Example</button>
<p data-content>Selected using data attributes by default.</p>
</drab-base>
<drab-base content="p">
<p>Selected via a CSS selector.</p>
</drab-base>
Overview
Each element in the library extends the Base
class. It provides methods for selecting elements via HTML attributes along with other helpers.
By default, trigger
s and content
will be selected via the data-trigger
and data-content
attributes. Alternatively, you can set the trigger
or content
attribute to a CSS selector to change the default selector from [data-trigger]
or [data-content]
to a selector of your choosing. This can be useful if you have multiple elements within one another.
Each element can have multiple trigger
s, but will only have one content
.
Hierarchy
HTMLElement
↳
Base
↳↳
Animate
↳↳
Breakpoint
↳↳
Editor
↳↳
Fullscreen
↳↳
Intersect
↳↳
Prefetch
↳↳
TableSort
↳↳
WakeLock
↳↳
YouTube
Constructors
constructor
• new Base(): Base
Returns
Overrides
HTMLElement.constructor
Defined in
Properties
#listenerController
• Private
#listenerController: AbortController
To clean up event listeners added to document
when the element is removed.
Defined in
Accessors
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";
Defined in
• set
event(value
): void
Parameters
Name | Type |
---|---|
value | keyof HTMLElementEventMap |
Returns
void
Defined in
Methods
connectedCallback
▸ connectedCallback(): void
Returns
void
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
Defined in
disconnectedCallback
▸ disconnectedCallback(): void
Returns
void
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]");
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]");
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
Defined in
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
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
Defined in
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