Use the Drawer component (or “off-canvas”) to show a fixed element relative to the document page from any side for navigation, contact forms, informational purposes or other user actions.
Set up
<script>
import { Drawer, Button } from '.';
</script> Default drawer
Since Drawer component extend Svelte's HTMLAttributes<HTMLDivElement>, you can set the id, aria-labelledby=id and aria-controls=id to the drawer component.
<script>
import { Drawer, Button } from '.';
let hidden1 = $state(true);
</script>
<div class="text-center">
<Button onclick={() => (hidden1 = false)}>Show drawer</Button>
</div>
<Drawer
bind:hidden={hidden1}
id="sidebar1"
aria-controls="sidebar1"
aria-labelledby="sidebar1"
title="Info"
>
<p class="mb-6 text-sm text-gray-500 dark:text-gray-400">
Supercharge your hiring by taking advantage of our <a
href="/"
class="text-primary-600 underline hover:no-underline dark:text-primary-500"
>limited-time sale</a
>
for Bitblue Docs + Job Board. Unlimited access to over 190K top-ranked candidates and the #1 design
job board.
</p>
<div class="grid grid-cols-2 gap-4">
<Button color="light" href="/">Learn more</Button>
<Button href="/" class="px-4">Get access <ArrowRightOutline class="ms-2 h-5 w-5" /></Button>
</div>
</Drawer> Contact form with
Use this example to show a contact form inside the drawer component.
<script>
import { Drawer, Button, Label, Input } from '.';
let hidden3 = $state(true);
</script>
<div class="text-center">
<Button onclick={() => (hidden3 = false)}>Show contact form</Button>
</div>
<Drawer
bind:hidden={hidden3}
form
title="Contact us"
onaction={handleAction}
onsubmit={handleSubmit}
placement="right"
>
<div class="mb-6">
<Label for="email" class="mb-2 block">Your email</Label>
<Input id="email" name="email" required placeholder="name@company.com" />
</div>
<div class="mb-6">
<Label for="subject" class="mb-2 block">Subject</Label>
<Input id="subject" name="subject" required placeholder="Let us know how we can help you" />
</div>
{#snippet footer()}
<Button type="submit" class="w-full" value="send">Send message</Button>
{/snippet}
</Drawer>> A11y
Use Tab and Shift+Tab to navigate between buttons or links in the drawer. Press ESC to close it on a small screen.
Placement
Use the placement prop to position the drawer component either on the top, right, bottom, or left side of the document page. This can be done using the placement='left|right|top|bottom' where the default value is “left”.
Left drawer
<script>
import { Drawer, Button, CloseButton, Label, Textarea } from '.';
let hidden5 = $state(true);
</script>
<div class="text-center">
<Button onclick={() => (hidden5 = false)}>Show drawer</Button>
</div>
<Drawer placement="left" bind:hidden={hidden5} title="Info">
<p class="mb-6 text-sm text-gray-500 dark:text-gray-400">
Supercharge your hiring by taking advantage of our <a
href="/"
class="text-primary-600 underline hover:no-underline dark:text-primary-500"
>limited-time sale</a
>
for Bitblue Docs + Job Board. Unlimited access to over 190K top-ranked candidates and the #1 design
job board.
</p>
<div class="grid grid-cols-2 gap-4">
<Button color="light" href="/">Learn more</Button>
<Button href="/" class="px-4">Get access</Button>
</div>
</Drawer> Right drawer
Use this example to show the drawer component on the right side of the page.
Set the transitionParams variable to new variables.
<script>
import { Drawer, Button, CloseButton } from '.';
import { sineIn } from 'svelte/easing';
let hidden6 = $state(true);
let transitionParamsRight = {
x: 320,
duration: 200,
easing: sineIn
};
</script>
<div class="text-center">
<Button onclick={() => (hidden6 = false)}>Show drawer</Button>
</div>
<Drawer
placement="right"
transitionParams={transitionParamsRight}
bind:hidden={hidden6}
title="Info"
>
<p class="mb-6 text-sm text-gray-500 dark:text-gray-400">
Supercharge your hiring by taking advantage of our <a
href="/"
class="text-primary-600 underline hover:no-underline dark:text-primary-500"
>limited-time sale</a
>
for Bitblue Docs + Job Board. Unlimited access to over 190K top-ranked candidates and the #1 design
job board.
</p>
<div class="grid grid-cols-2 gap-4">
<Button color="light" href="/">Learn more</Button>
<Button href="/" class="px-4">Get access</Button>
</div>
</Drawer> Backdrop
The backdrop element can be used to dim out the background elements when the drawer is visible and also automatically hide the component when clicking outside of it.
Use the backdrop="false|true" prop where you can disable or enable the backdrop element.
Disabled
Use the backdrop={false} prop to disable the backdrop element when the drawer is shown.
<script>
import { Drawer, Button} from '.';
let hiddenBackdropFalse = $state(true);
</script>
<div class="text-center">
<Button onclick={() => (hiddenBackdropFalse = false)}>Show drawer</Button>
</div>
<Drawer backdrop={false} bind:hidden={hiddenBackdropFalse} title="Info">
<p class="mb-6 text-sm text-gray-500 dark:text-gray-400">
Supercharge your hiring by taking advantage of our <a
href="/"
class="text-primary-600 underline hover:no-underline dark:text-primary-500"
>limited-time sale</a
>
for Bitblue Docs + Job Board. Unlimited access to over 190K top-ranked candidates and the #1 design
job board.
</p>
<div class="grid grid-cols-2 gap-4">
<Button color="light" href="/">Learn more</Button>
<Button href="/" class="px-4">Get access </Button>
</div>
</Drawer> Disabling outside click
<script>
import { Drawer, Button } from '.';
let hiddenDisablingOnlyOutsideClick = $state(true);
</script>
<div class="text-center">
<Button onclick={() => (hiddenDisablingOnlyOutsideClick = false)}>Show drawer</Button>
</div>
<Drawer activateClickOutside={false} bind:hidden={hiddenDisablingOnlyOutsideClick}>
<div class="flex items-center justify-between">
<h5
id="drawer-label"
class="mb-4 inline-flex items-center text-base font-semibold text-gray-500 dark:text-gray-400"
>
<InfoCircleSolid class="me-2.5 h-5 w-5" />Info
</h5>
<CloseButton class="mb-4 dark:text-white" />
</div>
<p class="mb-6 text-sm text-gray-500 dark:text-gray-400">
Supercharge your hiring by taking advantage of our <a
href="/"
class="text-primary-600 underline hover:no-underline dark:text-primary-500"
>limited-time sale</a
>
for Bitblue Docs + Job Board. Unlimited access to over 190K top-ranked candidates and the #1 design
job board.
</p>
<div class="grid grid-cols-2 gap-4">
<Button color="light" href="/">Learn more</Button>
<Button href="/" class="px-4">Get access <ArrowRightOutline class="ms-2 h-5 w-5" /></Button>
</div>
</Drawer> Body scrolling and custom width
By default, body scrolling is disabled when the drawer is visible, however, you can choose to enable it using the bodyScrolling={true|false} prop.
Limitation: When bodyScrolling=true, clicking the backdrop to close the drawer is disabled. Use the Escape key or provide a close button inside the drawer content instead.
<script>
import { Drawer, Button } from '.';
let drawerHidden = $state(true);
</script>
<div class="text-center">
<Button onclick={() => (drawerHidden = false)}>Show drawer</Button>
</div>
<Drawer bind:hidden={drawerHidden} bodyScrolling={true} title="Info" class="w-[700px]">
<p class="mb-6 text-sm text-gray-500 dark:text-gray-400">
Supercharge your hiring by taking advantage of our <a
href="/"
class="text-primary-600 underline hover:no-underline dark:text-primary-500"
>limited-time sale</a
>
for Bitblue Docs + Job Board. Unlimited access to over 190K top-ranked candidates and the #1 design
job board. Lorem ipsum dolor sit, amet consectetur adipisicing elit. At vel quidem sed numquam repellendus.
Nihil earum veritatis quas non quisquam nisi hic ad, quo ullam iusto aliquid architecto saepe asperiores.
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Amet ratione vero repudiandae ex? Possimus
doloremque culpa excepturi, corporis natus hic perferendis dolorem dignissimos fugit nostrum ipsam
sapiente voluptate eos ea! Lorem ipsum dolor sit, amet consectetur adipisicing elit. At vel quidem
sed numquam repellendus. Nihil earum veritatis quas non quisquam nisi hic ad, quo ullam iusto aliquid
architecto saepe asperiores. Lorem, ipsum dolor sit amet consectetur adipisicing elit. Amet ratione
vero repudiandae ex? Possimus doloremque culpa excepturi, corporis natus hic perferendis dolorem
dignissimos fugit nostrum ipsam sapiente voluptate eos ea! Lorem ipsum dolor sit, amet consectetur
adipisicing elit. At vel quidem sed numquam repellendus. Nihil earum veritatis quas non quisquam
nisi hic ad, quo ullam iusto aliquid architecto saepe asperiores. Lorem, ipsum dolor sit amet consectetur
adipisicing elit. Amet ratione vero repudiandae ex? Possimus doloremque culpa excepturi, corporis
natus hic perferendis dolorem dignissimos fugit nostrum ipsam sapiente voluptate eos ea! Lorem ipsum
dolor sit, amet consectetur adipisicing elit. At vel quidem sed numquam repellendus. Nihil earum
veritatis quas non quisquam nisi hic ad, quo ullam iusto aliquid architecto saepe asperiores. Lorem,
ipsum dolor sit amet consectetur adipisicing elit. Amet ratione vero repudiandae ex? Possimus doloremque
culpa excepturi, corporis natus hic perferendis dolorem dignissimos fugit nostrum ipsam sapiente
voluptate eos ea! Lorem ipsum dolor sit, amet consectetur adipisicing elit. At vel quidem sed numquam
repellendus. Nihil earum veritatis quas non quisquam nisi hic ad, quo ullam iusto aliquid architecto
saepe asperiores. Lorem, ipsum dolor sit amet consectetur adipisicing elit. Amet ratione vero repudiandae
ex? Possimus doloremque culpa excepturi, corporis natus hic perferendis dolorem dignissimos fugit
nostrum ipsam sapiente voluptate eos ea!.
</p>
{#snippet footer()}
<div class="grid grid-cols-2 gap-4 w-full">
<Button color="light" href="/">Learn more</Button>
<Button href="/" class="px-4">Get access</Button>
</div>
{/snippet}
</Drawer> Accessibility
Drawer is implemented as a dialog surface:
- Root uses
role="dialog"andaria-modal="true" - When
titleis set,aria-labelledbypoints at the title element focustrapdefaults totrue— Tab cycles within the drawer while open- Escape closes the drawer unless
permanentis set - Backdrop click closes when
activateClickOutsideis true (and not permanent) - Prefer pairing the trigger with
aria-controls/ matchingidon the drawer
<script lang="ts">
import { Button, Drawer } from '.';
let hidden = $state(true);
</script>
<Button onclick={() => (hidden = false)} aria-controls="info-drawer">Open</Button>
<Drawer bind:hidden id="info-drawer" title="Info" focustrap>
<p>Focus stays inside while open. Esc or backdrop dismisses.</p>
</Drawer> Composition notes
- Bind
hidden(inverted visibility) from a trigger button. - Use
header/footersnippets for structured chrome;titlerenders a labelled heading. formwraps the body in<form method="dialog">and routes submit throughonaction.- Placement (
left|right|top|bottom) +size/widthcontrol geometry.
Props
DrawerProps
| Prop | Type | Default | Description |
|---|---|---|---|
hidden | boolean | — | Bindable; true when closed |
title | string | — | Header title + labelledby |
placement | left | right | top | bottom | left | Edge the drawer slides from |
position | fixed | absolute | fixed | CSS positioning mode |
size | sm | md | lg | xl | 2xl | 3xl | md | Preset width |
width | default | full | half | default | Width variant |
backdrop | boolean | true | Dimmed overlay |
backdropClass | ClassValue | — | Backdrop classes (prefer classes.backdrop) |
activateClickOutside | boolean | true | Backdrop click closes |
bodyScrolling | boolean | false | Allow page scroll while open |
dismissable | boolean | true | Show close button |
permanent | boolean | false | Block Esc / backdrop dismiss |
focustrap | boolean | true | Trap Tab inside drawer |
autoclose | boolean | false | Close on inner button click |
form | boolean | false | Wrap body in dialog form |
onaction | ({ action, data }) => unknown | — | Form action handler |
closeDrawer | () => void | — | Optional external close callback |
transitionType | TransitionFunc | fly | Enter/exit transition |
transitionParams | ParamsType | — | Transition params |
header | Snippet | — | Custom header content |
footer | Snippet | — | Footer actions |
headerClass | ClassValue | — | Header classes |
bodyClass | ClassValue | — | Body classes |
footerClass | ClassValue | — | Footer classes |
closeBtnClass | ClassValue | — | Close button classes |
classes | slot class map | — | Theme slot overrides |
children | Snippet | — | Drawer body |
class | ClassValue | — | Root classes |
…rest | div attrs | — | Forwarded to the dialog root |
Snippets
| Snippet | Description |
|---|---|
children | Main body content |
header | Replaces / extends header region |
footer | Sticky footer actions |
Events
| Event / callback | Description |
|---|---|
onaction | Fired from dialog form submit with { action, data } |
oncancel | Cancel / dismiss path |
onsubmit | Native form submit |