Drawer

A Drawer is a panel that slides in from the edge of the screen over the page. It holds a form, the details of an item or a set of actions: a task that is too heavy for a popover and that the user should do without leaving the page. It is drawn above the page and, with a mask, keeps the page behind it out of reach.

Import
import { Drawer } from 'svelte-ant-design';
Links
src/lib/Drawer Report an issue Component README

When to use

  • Use it to create or edit a set of information with a form, to show the details of a row or a card, or for a subtask that is too heavy for a popover and belongs to the task on the page.
  • A dialog that asks for an answer in the middle of the screen is a Modal. A short notice that needs no answer is a Message. A panel that sits beside the page and lets it work is a Drawer with mask: false.
  • A Drawer inside another Drawer is nested: the parent moves aside while the child is open. Do not stack more than two levels.

Examples

Basic

A Drawer written into the markup with bind:open. The close button, a press on the mask and Escape close it by themselves; onclose hears each of them first, and onafteropenchange says when the slide has ended.

Placement

placement is the side the panel comes from: top, right (the default), bottom or left. The sides are physical, so left stays on the left in right-to-left text.

Size

size is default (378 px), large (736 px), a number in px, or any CSS length. It is the width of a left or right panel and the height of a top or bottom one; the panel is never larger than the page.

Header

The header holds the close button (at the start by default), the title and the extra actions at the end. closable moves the button with placement: 'end', disables it, or removes it; closeIcon replaces the cross.

A form in a Drawer

A Form in the body and the buttons in the footer: the task stays in the context of the page. The footer asks the form to validate, and the Drawer stays open while a field is wrong.

a control of the page clicks: 0

Mask and keys

With a mask the Drawer is modal: the page is locked and out of reach. mask.closable: false keeps a press from closing it, keyboard: false turns Escape off, and mask: false makes a panel beside the page: it works while the panel is open.

push of the parent

Nested Drawers

A Drawer written inside another one is nested. While it is open the parent moves aside by its own push distance (180 px by default, false for none), and only for a child of the same side. Only the lowest layer dims the page: the nested Drawer keeps a clear mask that still takes a press outside it.

container={false}

Render in current DOM

a control of the page, outside the region clicks while the panel is open: 0

Render in place

container is an element, a function, a selector, or false to leave the Drawer where it is written. In place it covers its nearest positioned parent only and is a panel of that region, not a modal: the rest of the page works.

default: the content stays after a close
destroyOnHidden
forceRender its content is in the page before the first opening:

Keep or remove the content

By default the content stays in the page after a close. destroyOnHidden removes it on every close; forceRender puts it in the page, hidden, before the first opening.

Custom semantic styling

class and style go on the panel, rootClassName and rootStyle on the layer, and classNames and styles on each of the eleven parts. zIndex sets the level of the layer.

Custom content render

drawerRender gets the content of the panel (header, body, footer) as a snippet and may wrap it.

Focus

By default focus goes to the panel, Tab stays inside and focus goes back to the button that opened it. focusable turns the trap (trap) or the return (focusTriggerAfterClose) off.

A Select inside

The list of a Select opens above the Drawer and takes typing; Escape closes the list first and the Drawer on the second press.

A long body

A long body scrolls on its own: the header and the footer stay in place, and the page behind does not scroll or jump.

Resizable

resizable draws a strip on the inner edge: drag it, or focus it and press the arrow keys (10 px, Shift 50 px), Home and End. minSize and maxSize set the stops, and the callbacks report the size. The size stays after a close.

API

Props of Drawer

PropertyDescriptionTypeDefault
openWhether the Drawer is shown. bind:open works: a way to close sets it to false.booleanfalse
titleThe title; it names the panel. 0 is shown. With no title give the Drawer an aria-label.string | number | Snippetnone
extraThe actions at the end of the header. A header with an extra alone is drawn.string | number | Snippetnone
childrenThe body. It scrolls on its own.Snippetnone
footerThe footer, at the bottom of the panel. None without it.string | number | Snippetnone
placementThe side the panel comes from. The sides are physical.'top' | 'right' | 'bottom' | 'left''right'
sizeThe width of a left or right panel, the height of a top or bottom one. default is 378 px, large 736 px; a number is px; a string is a CSS length. It replaces the deprecated width and height.'default' | 'large' | number | string'default'
closableThe close button. false removes it; disabled shows it and it does nothing; placement is logical and mirrors in right-to-left; aria-label is its name (default Close).boolean | { closeIcon?: Snippet; disabled?: boolean; placement?: 'start' | 'end'; 'aria-label'?: string }true
closeIconReplaces the cross. To hide the button use closable: false.Snippeta cross
maskThe dimmed backdrop. Only the lowest of the open layers (Drawers and Modals) dims the page; a layer above keeps a clear mask that still takes the press. closable (default true): a press on it asks to close. With no mask the Drawer is not modal: the page is not locked, not out of reach, Tab is not trapped, aria-modal is false.boolean | { enabled?: boolean; closable?: boolean }true
keyboardEscape closes the Drawer. A modal Drawer takes Escape when it is the top layer; a non-modal one only while focus is inside it.booleantrue
pushHow this Drawer moves aside when a nested Drawer of the same side opens. false keeps it still; a number is px, a string a CSS length.boolean | { distance?: string | number }{ distance: 180 }
zIndexThe z-index of the layer (token --ads-drawer-z-index): the level of the Modal, under the popup layers.number1000
containerWhere the layer goes: an element, a function, a selector. false leaves the Drawer where it is written: it covers its nearest positioned parent only and is not modal.HTMLElement | (() => HTMLElement) | string | falsedocument.body
destroyOnHiddenRemove the content on every close.booleanfalse
forceRenderPut the Drawer in the page, hidden, before the first opening.booleanfalse
focusableFocus. trap: Tab and Shift+Tab stay inside (a modal Drawer). focusTriggerAfterClose: focus goes back to what had it before. On open focus goes to the panel itself.{ trap?: boolean; focusTriggerAfterClose?: boolean }{ trap: true, focusTriggerAfterClose: true }
resizableA strip on the inner edge that the pointer or the arrow keys move (a focusable separator). The callbacks report the size in px; a key press is one gesture. The size stays after a close and goes when size or placement changes.boolean | { onresizestart?: () => void; onresize?: (size: number) => void; onresizeend?: () => void; 'aria-label'?: string }false
maxSizeThe largest size, in px, while resizing. Never larger than the container.numberthe container
minSizeThe smallest size, in px, while resizing. Ours: React has none, and its panel shrank to 7 px.number100
drawerRenderGets the content of the panel (header, body, footer) as a snippet and may wrap it.Snippet<[Snippet]>none
classA class on the panel (the section).stringnone
styleA style on the panel.stringnone
rootClassNameA class on the layer that holds the mask and the panel.stringnone
rootStyleA style on the layer.stringnone
classNamesClasses for the parts: root, mask, wrapper, section, header, title, close, extra, body, footer, dragger.{ root?: string; mask?: string; wrapper?: string; section?: string; header?: string; title?: string; close?: string; extra?: string; body?: string; footer?: string; dragger?: string }none
stylesInline styles for the same parts.{ root?: SemanticStyle; mask?: SemanticStyle; wrapper?: SemanticStyle; section?: SemanticStyle; header?: SemanticStyle; title?: SemanticStyle; close?: SemanticStyle; extra?: SemanticStyle; body?: SemanticStyle; footer?: SemanticStyle; dragger?: SemanticStyle }none
oncloseThe close button, the mask or Escape asked to close. The Drawer closes after the call unless it calls event.preventDefault().(event: MouseEvent | KeyboardEvent) => voidnone
onafteropenchangeAfter the enter animation (true) or the leave animation (false) ends; at once under prefers-reduced-motion.(open: boolean) => voidnone
Differences from Ant Design: the close button, the mask and Escape close the Drawer unless onclose calls event.preventDefault() (React leaves closing to you); focus goes to the panel, which carries the role and the name; a Drawer with no mask or rendered in place is not modal; resizable has a keyboard and a minimum (minSize); size replaces the deprecated width and height. Not yet: loading and mask.blur. Not taken: a service that opens a Drawer from code (only Angular has one).

Design tokens

TokenDescriptionDefault
--ads-drawer-z-indexThe level of the layer.1000
--ads-drawer-mask-bgThe colour of the mask.rgba(0, 0, 0, 0.45)
--ads-drawer-bgThe background of the panel.#ffffff
--ads-drawer-sizeThe size default.378px
--ads-drawer-size-largeThe size large.736px
--ads-drawer-shadow-rightThe shadow of a right panel (cast to the left).-6px 0 16px 0 rgba(0, 0, 0, 0.08), -3px 0 6px -4px rgba(0, 0, 0, 0.12), -9px 0 28px 8px rgba(0, 0, 0, 0.05)
--ads-drawer-shadow-leftThe shadow of a left panel.6px 0 16px 0 rgba(0, 0, 0, 0.08), 3px 0 6px -4px rgba(0, 0, 0, 0.12), 9px 0 28px 8px rgba(0, 0, 0, 0.05)
--ads-drawer-shadow-topThe shadow of a top panel.0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05)
--ads-drawer-shadow-bottomThe shadow of a bottom panel.0 -6px 16px 0 rgba(0, 0, 0, 0.08), 0 -3px 6px -4px rgba(0, 0, 0, 0.12), 0 -9px 28px 8px rgba(0, 0, 0, 0.05)
--ads-drawer-header-paddingThe padding of the header.16px 24px
--ads-drawer-header-borderThe line under the header.rgba(5, 5, 5, 0.06)
--ads-drawer-title-colorThe colour of the title.rgba(0, 0, 0, 0.88)
--ads-drawer-title-font-sizeThe font size of the title.16px
--ads-drawer-title-line-heightThe line height of the title.24px
--ads-drawer-title-font-weightThe weight of the title.600
--ads-drawer-close-sizeThe size of the close button.24px
--ads-drawer-close-gapThe gap between the close button and the title.8px
--ads-drawer-close-colorThe colour of the close button.rgba(0, 0, 0, 0.45)
--ads-drawer-close-color-hoverThe colour of the close button on the hover.rgba(0, 0, 0, 0.88)
--ads-drawer-close-bg-hoverThe fill of the close button on the hover.rgba(0, 0, 0, 0.06)
--ads-drawer-body-colorThe colour of the text.rgba(0, 0, 0, 0.88)
--ads-drawer-body-paddingThe padding of the body.24px
--ads-drawer-footer-paddingThe padding of the footer.8px 16px
--ads-drawer-footer-borderThe line above the footer.rgba(5, 5, 5, 0.06)
--ads-drawer-motion-durationThe duration of the slide and of the fade of the mask.0.3s
--ads-drawer-motion-easeThe curve of the slide and of the fade.ease
--ads-drawer-dragger-sizeThe width of the visible line of the resize strip.4px
--ads-drawer-dragger-hitThe width of the area that takes a press, half of it outside the panel.8px
--ads-drawer-dragger-hover-bgThe colour of the strip on the hover, the focus and the drag.var(--ads-color-primary)

Accessibility

  • The panel is role="dialog" and is named by its title (aria-labelledby). With no title give it an aria-label (a warning says so in development). A modal Drawer has aria-modal="true"; one with no mask, or rendered in place, has aria-modal="false".
  • Focus moves to the panel itself (not to the close button, so the title is announced first). In a modal Drawer Tab and Shift+Tab stay inside: Tab on the last element goes to the first. When the Drawer closes, focus goes back to the button that opened it.
  • The page behind a modal Drawer is out of reach of the keyboard, the pointer and assistive technology (inert; aria-hidden where a browser does not know inert). A Drawer with no mask, or rendered in place, leaves the page reachable and does not trap Tab.
  • Escape closes the Drawer that is on top; with a Select open inside it, Escape closes the list first. A Drawer with no mask takes Escape only while focus is inside it. A key that ends an IME composition does not close anything. keyboard: false turns Escape off.
  • A modal Drawer keeps the page from scrolling and from jumping sideways when its scrollbar goes away, up to the end of the leave animation. Not handled: iOS Safari before 16 scrolls a locked page under a finger.
  • The resize strip is a focusable separator (the window splitter pattern) with its value, its limits and a name (Resize); the arrow keys, Home and End move it. React's strip has no role and no keyboard, and shrinks the panel to a few pixels; here minSize is 100 px by default.
  • Contrast: the title and the text are about 15:1 on the panel, the close icon 3:1 (non-text contrast). The strip takes the primary colour on the hover and the focus. In forced colors the panel has a border on its inner edge, and the close button and the strip stay visible. The slide stops under prefers-reduced-motion.
  • Known limit: a Drawer opened from inside a popup (for example from the list of a Select) is outside that popup, so a press in it closes the popup and takes the Drawer with it. Open it from the page, or give the popup's content a place of its own with container.
  • Not checked: a real screen reader other than Orca (NVDA, JAWS, VoiceOver, a phone), a real touch device, and a real Safari. Checked: Orca with Firefox (the document starts inside a modal Drawer, Tab goes around inside, the strip is read as a splitter with its value), and the keys, focus and the page behind in Chromium, Firefox and WebKit, a touch drag in an emulated Chromium, and the lowest supported browser versions.