dialog
import "drab/dialog/define";
<div class="grid gap-4">
<drab-dialog click-outside-close>
<button data-trigger type="button">Right</button>
<dialog
data-content
class="bg-background backdrop:bg-muted/75 my-0 mr-0 ml-auto h-full max-h-screen w-full max-w-screen border-l p-6 backdrop:backdrop-blur sm:max-w-96"
>
<div class="mb-4 flex items-center justify-between">
<h2 class="my-0">Dialog</h2>
<button data-trigger class="ghost icon" aria-label="Close">
<span class="icon-[lucide--x]"></span>
</button>
</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur.
</p>
</dialog>
</drab-dialog>
<drab-dialog remove-body-scroll>
<button data-trigger type="button">Center</button>
<dialog
data-content
class="bg-background backdrop:bg-muted/75 m-auto rounded-md border p-6 opacity-0 transition-[display,opacity] transition-discrete duration-500 backdrop:opacity-0 backdrop:backdrop-blur-lg backdrop:transition-[display,opacity] backdrop:transition-discrete backdrop:duration-500 open:opacity-100 open:backdrop:opacity-100 sm:max-w-96 starting:open:opacity-0 starting:open:backdrop:opacity-0"
>
<div class="mb-4 flex items-center justify-between">
<h2 class="my-0">Dialog</h2>
<button data-trigger class="ghost icon" aria-label="Close">
<span class="icon-[lucide--x]"></span>
</button>
</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur.
</p>
</dialog>
</drab-dialog>
<drab-dialog remove-body-scroll click-outside-close>
<button data-trigger type="button">Cover</button>
<dialog
data-content
class="bg-muted/75 h-full min-h-full w-full min-w-full p-6 backdrop-blur-lg"
>
<article class="bg-background h-full w-full rounded border p-6">
<div class="mb-4 flex items-center justify-between">
<h2 class="my-0">Dialog</h2>
<button data-trigger class="ghost icon" aria-label="Close">
<span class="icon-[lucide--x]"></span>
</button>
</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur.
</p>
</article>
</dialog>
</drab-dialog>
</div>
Overview
Defined in: dialog/index.ts:36
Provides triggers for the HTMLDialogElement
.
Attributes
click-outside-close
By default, the HTMLDialogElement
doesn’t close if the user clicks outside of it.
Add a click-outside-close
attribute to close when the user clicks outside.
If the dialog covers the full viewport and this attribute is present, the first child of the dialog will be assumed to be the content of the dialog and the dialog will close if there is a click outside of the first child element.
remove-body-scroll
Add the remove-body-scroll
attribute to remove the scroll from document.body
when the dialog
is open.
Constructors
Constructor
new Dialog():
Dialog
Defined in: dialog/index.ts:37
Returns
Dialog
Overrides
Lifecycle(Trigger(Content())).constructor
Accessors
event
Get Signature
get event(): keyof
HTMLElementEventMap
Defined in: base/index.ts:120
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.
Default
"click";
Returns
keyof HTMLElementEventMap
Set Signature
set event(
value
):void
Defined in: base/index.ts:126
Parameters
value
keyof HTMLElementEventMap
Returns
void
Inherited from
Lifecycle(Trigger(Content())).event
Methods
close()
close():
void
Defined in: dialog/index.ts:69
Wraps HTMLDialogElement.close()
.
Returns
void
connectedCallback()
connectedCallback():
void
Defined in: base/index.ts:76
Called when custom element is added to the page.
Returns
void
Inherited from
Lifecycle(Trigger(Content())).connectedCallback
content()
Call Signature
content<
T
>(instance
):T
Defined in: base/index.ts:215
Type Parameters
T
T
extends HTMLElement
Parameters
instance
Constructor
<T
>
The instance of the desired element to validate against,
ex: HTMLDialogElement
. Defaults to HTMLElement
.
Returns
T
The element that matches the content
selector.
Default
this.querySelector("[data-content]");
Inherited from
Lifecycle(Trigger(Content())).content
Call Signature
content():
HTMLElement
Defined in: base/index.ts:216
Returns
HTMLElement
The element that matches the content
selector.
Default
this.querySelector("[data-content]");
Inherited from
Lifecycle(Trigger(Content())).content
destroy()
destroy():
void
Defined in: base/index.ts:83
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
Lifecycle(Trigger(Content())).destroy
disconnectedCallback()
disconnectedCallback():
void
Defined in: base/index.ts:86
Called when custom element is removed from the page.
Returns
void
Inherited from
Lifecycle(Trigger(Content())).disconnectedCallback
listener()
Call Signature
listener<
T
>(listener
,options?
):void
Defined in: base/index.ts:152
Type Parameters
T
T
extends keyof HTMLElementEventMap
Parameters
listener
Listener
<T
>
Listener to attach to all of the trigger
elements.
options?
AddEventListenerOptions
Returns
void
Inherited from
Lifecycle(Trigger(Content())).listener
Call Signature
listener<
T
>(type
,listener
,options?
):void
Defined in: base/index.ts:161
Type Parameters
T
T
extends keyof HTMLElementEventMap
Parameters
type
T
Event type.
listener
Listener
<T
>
Listener to attach to all of the trigger
elements.
options?
AddEventListenerOptions
Returns
void
Inherited from
Lifecycle(Trigger(Content())).listener
mount()
mount():
void
Defined in: dialog/index.ts:80
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
Lifecycle(Trigger(Content())).mount
safeListener()
Call Signature
safeListener<
T
>(type
,listener
,element?
,options?
):void
Defined in: base/index.ts:35
Wrapper around addEventListener
that ensures when the element is
removed from the DOM, these event listeners are cleaned up.
Type Parameters
T
T
extends keyof HTMLElementEventMap
Parameters
type
T
Event listener type - ex: "keydown"
listener
(this
, event
) => any
Listener to add to the target.
element?
HTMLElement
options?
AddEventListenerOptions
Other options sans signal
.
Returns
void
Inherited from
Lifecycle(Trigger(Content())).safeListener
Call Signature
safeListener<
T
>(type
,listener
,document
,options?
):void
Defined in: base/index.ts:41
Wrapper around addEventListener
that ensures when the element is
removed from the DOM, these event listeners are cleaned up.
Type Parameters
T
T
extends keyof DocumentEventMap
Parameters
type
T
Event listener type - ex: "keydown"
listener
(this
, event
) => any
Listener to add to the target.
document
Document
options?
AddEventListenerOptions
Other options sans signal
.
Returns
void
Inherited from
Lifecycle(Trigger(Content())).safeListener
Call Signature
safeListener<
T
>(type
,listener
,window
,options?
):void
Defined in: base/index.ts:47
Wrapper around addEventListener
that ensures when the element is
removed from the DOM, these event listeners are cleaned up.
Type Parameters
T
T
extends keyof WindowEventMap
Parameters
type
T
Event listener type - ex: "keydown"
listener
(this
, event
) => any
Listener to add to the target.
window
Window
options?
AddEventListenerOptions
Other options sans signal
.
Returns
void
Inherited from
Lifecycle(Trigger(Content())).safeListener
show()
show():
void
Defined in: dialog/index.ts:63
Wraps HTMLDialogElement.showModal()
.
Returns
void
swap()
swap(
revert
):void
Defined in: base/index.ts:231
Finds the HTMLElement | HTMLTemplateElement
via the swap
selector and
swaps this.content()
with the content of the element found.
Parameters
revert
Wait time (ms) before swapping back, set to false
to not revert.
default: 800
number
| false
Returns
void
Inherited from
Lifecycle(Trigger(Content())).swap
toggle()
toggle():
void
Defined in: dialog/index.ts:75
show
or close
depending on the dialog’s open
attribute.
Returns
void
triggers()
Call Signature
triggers<
T
>(instance
):NodeListOf
<T
>
Defined in: base/index.ts:136
Type Parameters
T
T
extends HTMLElement
Parameters
instance
Constructor
<T
>
The instance of the desired element to validate against,
ex: HTMLButtonElement
. Defaults to HTMLElement
.
Returns
NodeListOf
<T
>
All of the elements that match the trigger
selector.
Default
this.querySelectorAll("[data-trigger]");
Inherited from
Lifecycle(Trigger(Content())).triggers
Call Signature
triggers():
NodeListOf
<HTMLElement
>
Defined in: base/index.ts:137
Returns
NodeListOf
<HTMLElement
>
All of the elements that match the trigger
selector.
Default
this.querySelectorAll("[data-trigger]");
Inherited from
Lifecycle(Trigger(Content())).triggers