--- package: "@svenskaspel/ui" version: "4.4.0" document_type: "component-readme" source: "@svenskaspel/ui/docs/drawer.md" canonical_url: "/components/drawer/code" --- # `Drawer` Drawer displays a swipeable panel from the bottom, top, left, or right of the viewport. It uses `@base-ui/react/drawer`, following shadcn's Base UI composition with Nova's existing Drawer colors, dimensions, spacing, and typography. Use Drawer for Sport & Casino, Tur, and other services or apps that must work on mobile. Sheet is best suited to desktop-only applications. BottomSheet is deprecated. See the [migration skill](https://github.com/SvenskaSpelAB/nova/blob/main/.agents/skills/nova-drawer-migration/SKILL.md) for BottomSheet, Sheet, and the previous Vaul Drawer. ## Setup On iOS Safari, Base UI positions the backdrop absolutely. Nova's `@svenskaspel/ui/tailwind/base.css` includes `body { position: relative; }` in the base layer so the backdrop covers the viewport when the page is scrolled. Import the base stylesheet as part of your theme setup; no additional Drawer-specific body rule is required. Keep any intentional application override positioned rather than static. ## Usage ```tsx import { Button } from '@svenskaspel/ui/react/button'; import { Drawer, DrawerClose, DrawerContent, DrawerBody, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger, } from '@svenskaspel/ui/react/drawer'; export function AccountDrawer() { return ( }>Open account Account Manage your account settings. Account details }>Close ); } ``` ## Components | Component | Purpose | | -------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | | `Drawer` | Base UI Root with context for direction, modal mode, snap points, and the swipe indicator. | | `DrawerTrigger` | Opens the drawer; use `render` to compose a Button. | | `DrawerContent` | Composes Portal, modal Overlay, Viewport, Popup, and the internal Content. Props, styles, and refs target the outer Popup. | | `DrawerPortal` | Base UI Portal, available for custom composition. | | `DrawerOverlay` | Styled Base UI Backdrop using Nova's scrim. | | `DrawerSwipeHandle` | Bottom-only visual swipe indicator. Hiding it does not disable swiping. | | `DrawerHeader`, `DrawerBody`, `DrawerFooter` | Layout wrappers with Nova spacing. | | `DrawerTitle`, `DrawerDescription` | Accessible title and description. | | `DrawerClose` | Closes the drawer; use `render` to compose a Button. | ## Actions and centered content `DrawerContent` provides the same close and back actions as BottomSheet: | Prop | Default | Behavior | | ----------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `closeAction` | `true` | Displays a top-right close icon labelled "Stäng". Use `false` to hide it. | | `backAction` | — | Displays a top-left arrow and calls `(event: React.MouseEvent) => void`. The application controls navigation; it does not close the drawer. | | `backActionLabel` | `'Tillbaka'` | Accessible label for the back button. | ```tsx }>Open details setView('overview')} backActionLabel="Back to overview"> Details Details content ``` The drag indicator is supported only for bottom drawers. The drag indicator uses Nova's `bg-layer-neutral` token. See the **Without Drag Handle**, **Back Action**, and **Without Close Action** Storybook examples. `showSwipeHandle={false}` hides the indicator without disabling swipe dismissal. Bottom drawers retain its layout space so the header and action buttons do not move when it is hidden. The special top-corner radii on the close and back buttons apply only to bottom drawers. Vertical drawers keep a full-width panel. Like BottomSheet, their Header, Body and Footer use `mx-auto` and `w-full`, with a minimum width of `theme(screens.sm)` and maximum width of `theme(screens.md)` from the `sm` breakpoint. The content is centered on wider screens. Header text is centered for top and bottom drawers. Top/bottom headers reserve equal horizontal padding when a close or back action is present. Left/right headers reserve padding on each occupied side: left for a back action and right for a close action, keeping longer titles clear of the buttons. Sides without an action retain their standard padding. Horizontal drawers retain their narrower panel and do not apply those content-width limits. Use `DrawerBody` for scrollable content; a plain application-owned `div` does not receive these layout styles. ## Root props Drawer accepts Base UI Root props, plus `showSwipeHandle` and `dismissible`: | Prop | Default | Description | | -------------------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `swipeDirection` | `'down'` | `'down'`, `'up'`, `'left'`, or `'right'`. This is the dismissal direction. | | `dismissible` | `true` | Set to `false` to prevent scrim clicks, Escape, swipe, focus-out and platform back dismissal. Explicit Close actions and application-controlled closing remain available. | | `modal` | `true` | `true` traps focus, locks page scrolling, and blocks background interaction; `false` allows page interaction; `'trap-focus'` traps focus without blocking page scrolling or pointer interaction. | | `showSwipeHandle` | `true` for `'down'`, otherwise `false` | Displays the indicator for bottom drawers only; ignored for other directions. The default retains the existing bottom Drawer's appearance. | | `open`, `defaultOpen` | — / `false` | Controlled or initial open state. | | `onOpenChange` | — | `(open, details) => void`; `details.reason` identifies the action and `details.cancel()` cancels it. | | `onOpenChangeComplete` | — | `(open) => void` after opening or closing animations complete. | | `disablePointerDismissal` | `false` | Prevents outside-press dismissal and, in non-modal mode, focus-out dismissal. It does not disable Escape or swipe. | | `snapPoints` | — | Vertical snap heights as viewport fractions or `px`/`rem` strings. | | `snapPoint`, `onSnapPointChange` | — | Controlled snap point and callback. | | `snapToSequentialPoints` | `false` | Prevents velocity-based snap-point skipping. | ## Content and composition `DrawerContent` accepts Base UI Popup props, including `initialFocus`, `finalFocus`, `className`, `style`, and `render`. Styled Base UI parts support state-based `className` callbacks. Trigger and Close support `render`, not `asChild`. Set `nativeButton={false}` when rendering a non-button trigger. Bottom panels use `rounded-t-3xl` and top panels use `rounded-b-3xl`. Vertical panels are capped at `calc(100svh - 36px)`. A bottom drawer leaves at least 36 CSS pixels of scrim above it; a top drawer leaves the gap below it. This also applies to the fully expanded snap point. `svh` uses the viewport with mobile browser toolbars expanded, so the gap can be larger when those toolbars retract. Unlike `dvh`, it does not resize as the bars animate. See [the viewport-unit explanation](https://web.dev/blog/viewport-units). Horizontal panels remain full-height and are `75%` wide, capped at `24rem` on larger screens. Custom `--drawer-content-max-height` values can make a vertical drawer smaller; the component clamps larger values to the 36px scrim limit. Override the sizing variables for smaller custom heights: ```tsx {/* ... */} ``` ```tsx }>Open snap drawer {/* The full snap point still leaves the 36px scrim gap. */} ``` For forms with a software keyboard, opt in with `DrawerVirtualKeyboardProvider` (Nova's export of Base UI's `Drawer.VirtualKeyboardProvider`). Keep the Header and Footer outside a plain scrollable `DrawerBody`. The provider manages focus and scroll alignment when the software keyboard opens; drawers without it are unaffected. ```tsx import { DrawerVirtualKeyboardProvider } from '@svenskaspel/ui/react/drawer'; import { Input } from '@svenskaspel/ui/react/input'; import { Label } from '@svenskaspel/ui/react/label'; }>Edit details Edit details Update your contact information. }>Done ; ``` `initialFocus={false}` avoids opening the keyboard automatically; users focus a field themselves. Try the **Virtual Keyboard** Storybook example on an actual iOS or Android device, including fields near the bottom and switching fields while the keyboard is open. Desktop emulation does not reproduce a software keyboard. See the [Base UI documentation](https://base-ui.com/react/components/drawer#virtual-keyboard-aware) for the full API and additional composition for inputs pinned in a footer. Base UI controls focus and dismissal, while CSS controls transitions and stacking. Nested drawers can use another `Drawer` inside the parent content. Keep Select and Dialog roots inside the drawer's React tree when they belong to that drawer, even when their popups render in portals. Separately managed global dialogs also need integration testing. ## ScrollArea The **Maximum Height** and **Snap Points** stories use Nova's ScrollArea. Its viewport needs a definite height; flex sizing alone can let it grow to the full list height instead of scrolling. Keep Header and Footer outside the scroll area and constrain it with a positioned wrapper: ```tsx import { ScrollArea } from '@svenskaspel/ui/react/scroll-area'; Settings
Scrollable settings
}>Close
; ``` The explicit frame height is capped by the 36px scrim rule. Snap-point drawers already provide a frame height, so their example does not need the extra height class. The wrapper lets ScrollArea adapt to the available body height without JavaScript measurements or changing the ScrollArea component itself. ## References - [shadcn Base UI Drawer](https://ui.shadcn.com/docs/components/base/drawer) - [Base UI Drawer](https://base-ui.com/react/components/drawer) - [Migration skill](https://github.com/SvenskaSpelAB/nova/blob/main/.agents/skills/nova-drawer-migration/SKILL.md) ## Mandatory flows Use `dismissible={false}` to require an explicit action. For a mandatory modal flow, also hide the default close action and drag indicator: ```tsx }>Open mandatory drawer Complete this step Choose Continue when you are ready. {/* Required content */} }>Continue ``` The default `modal={true}` traps focus and blocks background interaction. `dismissible={false}` blocks incidental dismissal before calling `onOpenChange` and disables swipe gestures, including snap-point gestures. Explicit Close buttons, imperative close actions, and setting controlled `open` to `false` still work. For forms, only close after successful validation. See the **Mandatory** story.