Components

Dialog component

The kit:dialog component is a modal window based on the native HTML element <dialog>. Thanks to this foundation, it benefits from accessible and semantic features by default, such as focus management, closing with the Escape key or clicking outside the box (unless persistent is enabled).

It can be used to display temporary content above the main interface: messages, forms, confirmations, etc. It is controlled via the prop bindable open, can be customized in position, size, density and style, and adapts easily to all usage contexts.

dialog.svelte

Confirm action

Are you sure you want to continue? This action cannot be undone.

<script lang="ts">
	let open = $state(false);
</script>

<kit:btn onclick={() => (open = true)}>Open dialog</kit:btn>

<kit:dialog bind:open>
	<h3 style="margin: 0 0 0.5rem;">Confirm action</h3>
	<p style="margin: 0 0 1.25rem; opacity: 0.7;">
		Are you sure you want to continue? This action cannot be undone.
	</p>
	<div style="display: flex; justify-content: flex-end; gap: 0.5rem;">
		<kit:btn variant="text" onclick={() => (open = false)}>Cancel</kit:btn>
		<kit:btn onclick={() => (open = false)}>Confirm</kit:btn>
	</div>
</kit:dialog>

kit:dialog calls showModal() / close() on the native <dialog> element in reaction to open. The backdrop blocks page interaction and closes the dialog when clicked unless persistent is set. Scrolling the page while a dialog is open is disabled automatically.

open is bindable set it to true to open, false to close, or bind it to react to user-initiated closes.

Examples of dialog

position

Controls the vertical alignment of the dialog on the viewport.

  • position: 'top' | 'center' | 'bottom' = 'center'
dialog.svelte
<script lang="ts">
	let position = $state<'top' | 'center' | 'bottom'>('center');
	let open = $state(false);

	function show(p: 'top' | 'center' | 'bottom') {
		position = p;
		open = true;
	}
</script>

<div style="display: flex; gap: 0.5rem; flex-wrap: wrap;">
	{#each ['top', 'center', 'bottom'] as p (p)}
		<kit:btn variant="outline" onclick={() => show(p as 'top' | 'center' | 'bottom')}>
			{p}
		</kit:btn>
	{/each}
</div>

<kit:dialog bind:open {position}>
	<p style="margin: 0 0 1rem;">Dialog positioned at <strong>{position}</strong>.</p>
	<kit:btn onclick={() => (open = false)}>Close</kit:btn>
</kit:dialog>

size

Controls the max-width of the dialog content panel.

  • size: 'xs' | 'sm' | 'md' | 'lg' | 'xl' = 'md'
dialog.svelte
<script lang="ts">
	let size = $state<'xs' | 'sm' | 'md' | 'lg' | 'xl'>('md');
	let open = $state(false);

	function show(s: 'xs' | 'sm' | 'md' | 'lg' | 'xl') {
		size = s;
		open = true;
	}
</script>

<div style="display: flex; gap: 0.5rem; flex-wrap: wrap;">
	{#each ['xs', 'sm', 'md', 'lg', 'xl'] as s (s)}
		<kit:btn variant="outline" onclick={() => show(s as 'xs' | 'sm' | 'md' | 'lg' | 'xl')}>
			{s}
		</kit:btn>
	{/each}
</div>

<kit:dialog bind:open {size}>
	<p style="margin: 0 0 1rem;">Size: <strong>{size}</strong></p>
	<kit:btn onclick={() => (open = false)}>Close</kit:btn>
</kit:dialog>

persistent

When persistent is set, clicking the backdrop or pressing Escape does not close the dialog. The user must interact with the content to dismiss it.

  • persistent: boolean = false
dialog.svelte
<script lang="ts">
	let open = $state(false);
</script>

<kit:btn onclick={() => (open = true)}>Open persistent dialog</kit:btn>

<kit:dialog bind:open persistent>
	<h3 style="margin: 0 0 0.5rem;">Unsaved changes</h3>
	<p style="margin: 0 0 1.25rem; opacity: 0.7;">
		Clicking outside won't close this dialog. You must choose an action.
	</p>
	<div style="display: flex; justify-content: flex-end; gap: 0.5rem;">
		<kit:btn variant="text" onclick={() => (open = false)}>Discard</kit:btn>
		<kit:btn onclick={() => (open = false)}>Save</kit:btn>
	</div>
</kit:dialog>

Without preprocess Lapikit

If you aren’t using the Lapikit preprocessor in svelte.config.js, import the components directly:

import { Dialog } from 'lapikit/components';

API Reference

kit:dialog

propstypedescriptiondefault
ref $bindableHTMLElementBindable reference to the rendered DOM element.null
classstring | array | objectClasses of the <dialog>, with the rules of Svelte (clsx).
stylestringInline style of the <dialog>.
s-classstring | array | objectClasses of the <dialog>, merged with class.
s-styleobjectInline style of the <dialog> as an object of property / value pairs.
open $bindablebooleanControls visibility. Bindable.false
persistentbooleanPrevents closing on backdrop click or Escape.false
sizeenum'xs''sm''md''lg''xl'Max-width of the content panel.'md'
positionenum'top''center''bottom''top-left''top-right''center-left''center-right''bottom-left''botom-right'Orientation dialog on viewport.'center'
densityenum'dense''compact''default''comfortable''spacious'Padding of the content panel, from -4px to +4px.'default'
roundedenum0'xs''sm''md''lg''xl''full'Border radius of the content panel.'md'
classContentstring | array | objectClasses of the inner content panel (clsx).
colorstring | false | { base?, active? }Background of the content panel, the text follows its on- color or a contrasted color.
childrenSnippetDialog content.
elevationElevationStateenum'0''1''2''3''4''5'Add shadow effect.'2'

Remember to follow up with Modal or Popover to create complete user paths.