Skip to Content

dialog

Dialog

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.

Dialog

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.

import "drab/dialog/define";
<div class="mb-4">
	<drab-dialog click-outside-close class="contents">
		<button data-trigger type="button">Right</button>
		<dialog
			data-content
			class="bg-background my-0 mr-0 ml-auto h-full max-h-screen w-full max-w-screen 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" aria-label="Close">
					<!-- x-mark -->
					<svg
						xmlns="http://www.w3.org/2000/svg"
						viewBox="0 0 20 20"
						fill="currentColor"
						class="size-5"
					>
						<path
							d="M6.28 5.22a.75.75 0 0 0-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 1 0 1.06 1.06L10 11.06l3.72 3.72a.75.75 0 1 0 1.06-1.06L11.06 10l3.72-3.72a.75.75 0 0 0-1.06-1.06L10 8.94 6.28 5.22Z"
						/>
					</svg>
				</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>
</div>

<drab-dialog remove-body-scroll class="contents">
	<button data-trigger type="button">Center</button>
	<dialog
		data-content
		class="bg-background m-auto rounded-md border p-6 opacity-0 transition-[display,opacity] transition-discrete duration-500 backdrop:opacity-0 backdrop:backdrop-blur 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" aria-label="Close">
				<!-- x-mark -->
				<svg
					xmlns="http://www.w3.org/2000/svg"
					viewBox="0 0 20 20"
					fill="currentColor"
					class="size-5"
				>
					<path
						d="M6.28 5.22a.75.75 0 0 0-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 1 0 1.06 1.06L10 11.06l3.72 3.72a.75.75 0 1 0 1.06-1.06L11.06 10l3.72-3.72a.75.75 0 0 0-1.06-1.06L10 8.94 6.28 5.22Z"
					/>
				</svg>
			</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>

Overview

Defined in: dialog/index.ts:21

Provides triggers for the HTMLDialogElement.

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.

remove-body-scroll

Add the remove-body-scroll attribute to remove the scroll from document.body when the dialog is open.

Extends

Constructors

new Dialog()

new Dialog(): Dialog

Defined in: dialog/index.ts:27

Returns

Dialog

Overrides

Base.constructor

Accessors

dialog

Get Signature

get dialog(): HTMLDialogElement

Defined in: dialog/index.ts:32

The HTMLDialogElement within the element.

Returns

HTMLDialogElement


event

Get Signature

get event(): keyof HTMLElementEventMap

Defined in: base/index.ts:42

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:46

Parameters
value

keyof HTMLElementEventMap

Returns

void

Inherited from

Base.event

Methods

announce()

announce(message): void

Defined in: base/index.ts:53

Parameters

message

string

message to announce to screen readers

Returns

void

Inherited from

Base.announce


close()

close(): Promise<void>

Defined in: dialog/index.ts:58

HTMLDialogElement.close() with animation.

Returns

Promise<void>


connectedCallback()

connectedCallback(): void

Defined in: base/index.ts:171

Called when custom element is added to the page.

Returns

void

Inherited from

Base.connectedCallback


destroy()

destroy(): void

Defined in: base/index.ts:178

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

Base.destroy


disconnectedCallback()

disconnectedCallback(): void

Defined in: base/index.ts:181

Called when custom element is removed from the page.

Returns

void

Inherited from

Base.disconnectedCallback


getContent()

getContent<T>(instance): T

Defined in: base/index.ts:74

Type Parameters

T extends HTMLElement = HTMLElement

Parameters

instance

() => 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

Base.getContent


getTrigger()

getTrigger<T>(): NodeListOf<T>

Defined in: base/index.ts:61

Type Parameters

T extends HTMLElement = HTMLElement

Returns

NodeListOf<T>

All of the elements that match the trigger selector.

Default

this.querySelectorAll("[data-trigger]");

Inherited from

Base.getTrigger


mount()

mount(): void

Defined in: dialog/index.ts:69

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

Base.mount


safeListener()

safeListener<K, T>(type, listener, element, options): void

Defined in: base/index.ts:136

Wrapper around document.body.addEventListener that ensures when the element is removed from the DOM, these event listeners are cleaned up.

Type Parameters

K extends keyof DocumentEventMap

T extends HTMLElement | Document | Window = HTMLElement

Parameters

type

K

listener

(this, ev) => any

element

T = ...

options

AddEventListenerOptions = {}

Returns

void

Inherited from

Base.safeListener


show()

show(): Promise<void>

Defined in: dialog/index.ts:52

HTMLDialogElement.showModal() with animation.

Returns

Promise<void>


swapContent()

swapContent(revert): void

Defined in: base/index.ts:93

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

Base.swapContent


toggle()

toggle(): Promise<void>

Defined in: dialog/index.ts:64

show or close depending on the dialog’s open attribute.

Returns

Promise<void>


triggerListener()

triggerListener<T, K>(listener, type, options?): void

Defined in: base/index.ts:153

Type Parameters

T extends HTMLElement

K extends keyof HTMLElementEventMap

Parameters

listener

(this, e) => any

Listener to attach to all of the trigger elements.

type

K = ...

options?

AddEventListenerOptions

Returns

void

Inherited from

Base.triggerListener