Drawer
Preview
"use client";
import { useState } from "react";
import { Drawer } from "@/components/motion/drawer";
export function DrawerPreview() {
const [open, setOpen] = useState(false);
const [side, setSide] = useState<"left" | "right">("right");
const openWith = (s: "left" | "right") => {
setSide(s);
setOpen(true);
};
return (
<div className="flex items-center gap-3">
<button
type="button"
onClick={() => openWith("left")}
className="inline-flex h-10 items-center rounded-full border border-border bg-card px-5 text-sm font-medium text-foreground transition-colors hover:bg-card/70"
>
Open left
</button>
<button
type="button"
onClick={() => openWith("right")}
className="inline-flex h-10 items-center rounded-full bg-primary px-5 text-sm font-medium text-primary-foreground transition-colors hover:bg-primary/90"
>
Open right
</button>
<Drawer
open={open}
onOpenChange={setOpen}
side={side}
ariaLabel="Demo drawer"
className="gap-4 p-6"
>
<h2 className="text-sm font-semibold text-foreground">Drawer</h2>
<p className="text-sm text-muted-foreground">
Slides in from the {side}. Press Esc or click outside to close.
</p>
<button
type="button"
onClick={() => setOpen(false)}
className="mt-auto inline-flex h-10 items-center justify-center rounded-full border border-border bg-card px-5 text-sm font-medium text-foreground transition-colors hover:bg-card/70"
>
Close
</button>
</Drawer>
</div>
);
}
"use client";
// beui.dev/components/motion/drawer
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { type ReactNode, useEffect, useRef } from "react";
import { EASE_OUT, SPRING_PANEL } from "@/lib/ease";
import { useFocusTrap } from "@/lib/hooks/use-focus-trap";
import { useMessages } from "@/lib/i18n";
import { PresenceGate } from "@/lib/presence-gate";
import { cn } from "@/lib/utils";
/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */
export const drawerMessages = {
close: "Close",
};
export type DrawerMessages = typeof drawerMessages;
export interface DrawerProps {
open: boolean;
onOpenChange: (open: boolean) => void;
side?: "left" | "right";
children: ReactNode;
/** Class for the panel surface. */
className?: string;
/** Class for the backdrop. */
backdropClassName?: string;
ariaLabel?: string;
/** Close when the backdrop is clicked or Escape is pressed. Default true. */
dismissable?: boolean;
/** Override any built-in string. */
labels?: Partial<DrawerMessages>;
}
export function Drawer({
open,
onOpenChange,
side = "right",
children,
className,
backdropClassName,
ariaLabel,
dismissable = true,
labels,
}: DrawerProps) {
const t = useMessages("drawer", drawerMessages, labels);
const reduce = useReducedMotion();
const panelRef = useRef<HTMLDivElement>(null);
// Focus moves into the panel on open, Tab stays inside it, and focus returns
// to the opener on close.
useFocusTrap(open, panelRef, {
onEscape: dismissable ? () => onOpenChange(false) : undefined,
});
useEffect(() => {
if (!open) return;
const prevOverflow = document.body.style.overflow;
document.body.style.overflow = "hidden";
return () => {
document.body.style.overflow = prevOverflow;
};
}, [open]);
const offscreen = side === "right" ? "100%" : "-100%";
// Two fixed siblings, no wrapper: the backdrop spans the viewport edges but
// paints the scrim, and the panel is inset off one side and paints its own
// surface, so neither is a transparent edge-spanning layer. Both hang off
// `PresenceGate`, so interaction releases in the same commit that starts the
// exit rather than when it ends.
return (
<AnimatePresence>
{open ? (
<PresenceGate key="backdrop">
{({ gate }) => (
<motion.button
type="button"
aria-label={t("close")}
// Outside the panel, so the focus trap never lands on it; it is
// a pointer target only.
tabIndex={-1}
onClick={() => dismissable && onOpenChange(false)}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.25, ease: EASE_OUT }}
{...gate}
className={cn(
"fixed inset-0 z-50 h-full w-full cursor-default bg-black/40 backdrop-blur-sm",
backdropClassName,
)}
/>
)}
</PresenceGate>
) : null}
{open ? (
<PresenceGate key="panel">
{({ gate }) => (
<motion.div
ref={panelRef}
tabIndex={-1}
role="dialog"
aria-modal="true"
aria-label={ariaLabel}
initial={reduce ? { opacity: 0 } : { x: offscreen }}
animate={reduce ? { opacity: 1 } : { x: 0 }}
exit={reduce ? { opacity: 0 } : { x: offscreen }}
transition={
reduce ? { duration: 0.2, ease: EASE_OUT } : SPRING_PANEL
}
{...gate}
className={cn(
"fixed inset-y-0 z-50 flex w-80 max-w-[85vw] flex-col bg-background shadow-2xl outline-none",
side === "right"
? "right-0 border-l border-border"
: "left-0 border-r border-border",
className,
)}
>
{children}
</motion.div>
)}
</PresenceGate>
) : null}
</AnimatePresence>
);
}
Install
Add it with the shadcn CLI, or copy the source manually.
shadcn init? You are set. Theme setupInstall dependencies
npm i clsx motion tailwind-mergeAdd util files
// Shared motion tokens. Easing curves mirror the CSS custom properties in
// globals.css; springs are the canonical physics used across components.
// Strong custom variants — defaults like `ease-in`/`ease-out` feel weak.
export const EASE_OUT = [0.16, 1, 0.3, 1] as const;
/** Accelerating curve for things that fall (a dropped ball, a toppled block). Not for UI entrances. */
export const EASE_IN = [0.55, 0, 1, 0.45] as const;
export const EASE_IN_OUT = [0.77, 0, 0.175, 1] as const;
export const EASE_DRAWER = [0.32, 0.72, 0, 1] as const;
/** CSS string form of EASE_OUT for inline style transitions. */
export const EASE_OUT_CSS = "cubic-bezier(0.16, 1, 0.3, 1)";
/** Press feedback on buttons and other tappable surfaces. */
export const SPRING_PRESS = {
type: "spring",
stiffness: 500,
damping: 30,
mass: 0.6,
} as const;
/** Content swaps — label/icon slots trading places inside a control. */
export const SPRING_SWAP = {
type: "spring",
stiffness: 460,
damping: 30,
mass: 0.55,
} as const;
/** Overlay panel entrances — modals and sheets summoned by pointer. */
export const SPRING_PANEL = {
type: "spring",
stiffness: 420,
damping: 40,
mass: 0.5,
} as const;
/** Shared-layout glides — pills, indicators and panels morphing between positions. */
export const SPRING_LAYOUT = {
type: "spring",
stiffness: 360,
damping: 32,
mass: 0.6,
} as const;
/** Cursor-follow physics for decorative mouse tracking (magnetic, tilt, dock). */
export const SPRING_MOUSE = {
stiffness: 200,
damping: 15,
mass: 0.3,
} as const;
/** Dragged handles and fills (sliders) — critically damped `useSpring` config,
* so the value follows the pointer butterily and never rebounds off an end. */
export const SPRING_GLIDE = {
stiffness: 700,
damping: 50,
mass: 0.5,
} as const;
"use client";
import { type RefObject, useEffect, useLayoutEffect, useRef } from "react";
const FOCUSABLE =
'button:not(:disabled), a[href], input:not(:disabled), select:not(:disabled), textarea:not(:disabled), [tabindex]:not([tabindex="-1"])';
/** Elements inside `container` that Tab can reach, in DOM order. */
export function focusablesIn(container: Element | null) {
return Array.from(
container?.querySelectorAll<HTMLElement>(FOCUSABLE) ?? [],
).filter((element) => element.tabIndex >= 0 && !element.closest("[inert]"));
}
/**
* Open traps, innermost last. Only the innermost one answers Tab, Escape and
* stray focus: a sheet opened from inside a drawer portals outside the drawer,
* and a drawer that still pulled focus home would fight the sheet for it.
*/
const traps: object[] = [];
export interface FocusTrapOptions {
/** Called on Escape while this is the innermost open trap. */
onEscape?: () => void;
}
/**
* Moves focus into `panel` when `open` turns true, keeps Tab cycling inside it,
* pulls focus back if it lands outside, and returns focus to whatever held it
* before opening once `open` turns false.
*
* For overlays rendered in place, where `useModalScope` cannot inert the rest
* of the page. Pass `open` rather than presence: an exiting panel is already
* inert through `PresenceGate`, so the trap releases with the same commit.
* Give the panel `tabIndex={-1}` so it can take focus when it holds nothing
* focusable.
*/
export function useFocusTrap(
open: boolean,
panel: RefObject<HTMLElement | null>,
{ onEscape }: FocusTrapOptions = {},
) {
const escapeRef = useRef(onEscape);
useLayoutEffect(() => {
escapeRef.current = onEscape;
});
useEffect(() => {
if (!open || !panel.current) return;
const self = {};
traps.push(self);
const innermost = () => traps[traps.length - 1] === self;
const returnFocus =
document.activeElement instanceof HTMLElement
? document.activeElement
: null;
const focusables = () => focusablesIn(panel.current); const focusFirst = () =>
(focusables()[0] ?? panel.current)?.focus({ preventScroll: true });
if (!panel.current.contains(document.activeElement)) focusFirst();
const onFocus = (event: FocusEvent) => {
if (!innermost() || !panel.current || panel.current.closest("[inert]"))
return;
if (event.target instanceof Node && !panel.current.contains(event.target))
focusFirst();
};
const onKey = (event: KeyboardEvent) => {
if (!innermost() || event.defaultPrevented) return;
if (event.key === "Escape" && escapeRef.current) {
event.preventDefault();
escapeRef.current();
return;
}
if (event.key !== "Tab") return;
const elements = focusables();
const first = elements[0];
const last = elements[elements.length - 1];
const active = document.activeElement;
if (!first || !last) {
event.preventDefault();
panel.current?.focus({ preventScroll: true });
} else if (event.shiftKey && (active === first || active === panel.current)) {
event.preventDefault();
last.focus();
} else if (!event.shiftKey && active === last) {
event.preventDefault();
first.focus();
}
};
document.addEventListener("focusin", onFocus);
document.addEventListener("keydown", onKey);
return () => {
document.removeEventListener("focusin", onFocus);
document.removeEventListener("keydown", onKey);
traps.splice(traps.indexOf(self), 1);
if (returnFocus?.isConnected && !returnFocus.closest("[inert]"))
returnFocus.focus({ preventScroll: true });
};
}, [open, panel]);
}
"use client";
import {
createContext,
type ReactNode,
useCallback,
useContext,
useMemo,
} from "react";
/**
* A component's messages. Plain strings with `{name}` placeholders, so a
* dictionary stays JSON-serializable and can be passed from a server layout.
* There are no plural forms: write messages that read correctly for any count.
*/
export type Messages = Record<string, string>;
/** A language file: component namespace → any subset of that component's messages. */
export type LocaleMessages = Record<string, Partial<Messages>>;
type LocaleContextValue = {
locale?: string;
messages: LocaleMessages;
};
const NO_MESSAGES: LocaleMessages = {};
const LocaleContext = createContext<LocaleContextValue>({ messages: NO_MESSAGES });
/** Without a locale, digits stay plain so server and client render the same text. */
export function formatNumber(value: number, locale?: string) {
return locale ? new Intl.NumberFormat(locale).format(value) : String(value);
}
export interface LocaleProviderProps {
/** BCP 47 tag, e.g. `de`, `pt-BR`. Drives `Intl` number formatting. */
locale: string;
/**
* Component namespace → translated strings. Missing keys fall back to English.
* Pass a stable object (a module constant, imported JSON or a memoized value):
* a new object each render gives every translated component a new `t`.
*/
messages?: LocaleMessages;
children: ReactNode;
}
export function LocaleProvider({ locale, messages = NO_MESSAGES, children }: LocaleProviderProps) {
const value = useMemo(() => ({ locale, messages }), [locale, messages]);
return <LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>;
}
export function useLocale(): { locale?: string } {
return { locale: useContext(LocaleContext).locale };
}
/**
* Resolves a component's strings: `labels` prop, then the provider's
* messages for `namespace`, then the component's English `defaults`.
* Numbers passed as values are formatted for the provider's locale.
*/
export function useMessages<T extends Messages>(
namespace: string,
defaults: T,
labels?: Partial<T>,
) {
const { locale, messages } = useContext(LocaleContext);
const provided = messages[namespace];
// Inline `labels={{ … }}` is a new object every render. Keying it by content
// keeps `t` one identity until a label changes, so effects and memos that
// depend on `t` don't re-run per render, and render-time reads never lag.
const labelsKey = labels ? JSON.stringify(labels) : "";
const stableLabels = useMemo(
() => (labelsKey ? (JSON.parse(labelsKey) as Partial<T>) : undefined),
[labelsKey],
);
return useCallback(
(key: keyof T & string, values?: Record<string, string | number>) => {
const template = stableLabels?.[key] ?? provided?.[key] ?? defaults[key];
if (!values) return template;
return template.replace(/\{(\w+)\}/g, (match, name: string) => {
const value = values[name];
if (value === undefined) return match;
return typeof value === "number" ? formatNumber(value, locale) : value;
});
},
[defaults, stableLabels, locale, provided],
);
}
"use client";
import { useIsPresent } from "motion/react";
import type { ReactNode } from "react";
export interface PresenceGateRenderProps {
/**
* False from the render that starts the exit animation onward. An overlay
* kept in the tree by `AnimatePresence` is still the topmost thing on the
* page, so anything it decides from `open` alone stays true for the whole
* exit — this is the boolean that already knows the overlay is leaving.
*/
isPresent: boolean;
/**
* Spread onto every layer that takes pointer events while the overlay is
* open. Interaction releases in the same commit that starts the exit while
* the visual exit keeps playing: pointer events stop landing, and `inert`
* drops the subtree from focus order, from tab order and from the
* accessibility tree — an exiting dialog is not a dialog you can still type
* into. A layer that never takes pointer events (a wrapper that only centres
* the panel) takes `inert={!isPresent}` alone, so its own
* `pointer-events-none` is not overwritten.
*/
gate: {
inert: boolean;
style: { pointerEvents: "auto" | "none" };
};
}
export interface PresenceGateProps {
children: (props: PresenceGateRenderProps) => ReactNode;
}
/**
* Reads the presence of the subtree it renders and hands it down.
*
* `useIsPresent` only answers inside the `AnimatePresence` subtree, and the
* components that own an overlay render the `AnimatePresence` themselves, so
* the boolean has to be read one component further down: this is that
* component, and the render prop is how it reaches the layers.
*/
export function PresenceGate({ children }: PresenceGateProps) {
const isPresent = useIsPresent();
return children({
isPresent,
gate: {
inert: !isPresent,
style: { pointerEvents: isPresent ? "auto" : "none" },
},
});
}
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
Copy the source code
"use client";
// beui.dev/components/motion/drawer
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { type ReactNode, useEffect, useRef } from "react";
import { EASE_OUT, SPRING_PANEL } from "@/lib/ease";
import { useFocusTrap } from "@/lib/hooks/use-focus-trap";
import { useMessages } from "@/lib/i18n";
import { PresenceGate } from "@/lib/presence-gate";
import { cn } from "@/lib/utils";
/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */
export const drawerMessages = {
close: "Close",
};
export type DrawerMessages = typeof drawerMessages;
export interface DrawerProps {
open: boolean;
onOpenChange: (open: boolean) => void;
side?: "left" | "right";
children: ReactNode;
/** Class for the panel surface. */
className?: string;
/** Class for the backdrop. */
backdropClassName?: string;
ariaLabel?: string;
/** Close when the backdrop is clicked or Escape is pressed. Default true. */
dismissable?: boolean;
/** Override any built-in string. */
labels?: Partial<DrawerMessages>;
}
export function Drawer({
open,
onOpenChange,
side = "right",
children,
className,
backdropClassName,
ariaLabel,
dismissable = true,
labels,
}: DrawerProps) {
const t = useMessages("drawer", drawerMessages, labels);
const reduce = useReducedMotion();
const panelRef = useRef<HTMLDivElement>(null);
// Focus moves into the panel on open, Tab stays inside it, and focus returns
// to the opener on close.
useFocusTrap(open, panelRef, {
onEscape: dismissable ? () => onOpenChange(false) : undefined,
});
useEffect(() => {
if (!open) return;
const prevOverflow = document.body.style.overflow;
document.body.style.overflow = "hidden";
return () => {
document.body.style.overflow = prevOverflow;
};
}, [open]);
const offscreen = side === "right" ? "100%" : "-100%";
// Two fixed siblings, no wrapper: the backdrop spans the viewport edges but
// paints the scrim, and the panel is inset off one side and paints its own
// surface, so neither is a transparent edge-spanning layer. Both hang off
// `PresenceGate`, so interaction releases in the same commit that starts the
// exit rather than when it ends.
return (
<AnimatePresence>
{open ? (
<PresenceGate key="backdrop">
{({ gate }) => (
<motion.button
type="button"
aria-label={t("close")}
// Outside the panel, so the focus trap never lands on it; it is
// a pointer target only.
tabIndex={-1}
onClick={() => dismissable && onOpenChange(false)}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.25, ease: EASE_OUT }}
{...gate}
className={cn(
"fixed inset-0 z-50 h-full w-full cursor-default bg-black/40 backdrop-blur-sm",
backdropClassName,
)}
/>
)}
</PresenceGate>
) : null}
{open ? (
<PresenceGate key="panel">
{({ gate }) => (
<motion.div
ref={panelRef}
tabIndex={-1}
role="dialog"
aria-modal="true"
aria-label={ariaLabel}
initial={reduce ? { opacity: 0 } : { x: offscreen }}
animate={reduce ? { opacity: 1 } : { x: 0 }}
exit={reduce ? { opacity: 0 } : { x: offscreen }}
transition={
reduce ? { duration: 0.2, ease: EASE_OUT } : SPRING_PANEL
}
{...gate}
className={cn(
"fixed inset-y-0 z-50 flex w-80 max-w-[85vw] flex-col bg-background shadow-2xl outline-none",
side === "right"
? "right-0 border-l border-border"
: "left-0 border-r border-border",
className,
)}
>
{children}
</motion.div>
)}
</PresenceGate>
) : null}
</AnimatePresence>
);
}
API Reference
openboolean—onOpenChange(open: boolean) => void—side?"right" | "left"rightclassName?stringClass for the panel surface.
—backdropClassName?stringClass for the backdrop.
—ariaLabel?string—dismissable?booleanClose when the backdrop is clicked or Escape is pressed. Default true.
truelabels?Partial<{ close: string; }>Override any built-in string.
—Related components
Popover
Gooey popover whose panel oozes out of the trigger through an SVG goo filter with a liquid neck that stretches and pinches, then crisp content fades in on top. The Morph variant reveals its full-size panel from the corner nearest the trigger on one synchronized spring clip. Click or hover trigger, controlled or uncontrolled.
Morphing Modal
Family-app-style modal. A single panel that morphs its height as you navigate between inner views, with blur cross-fade on content.
Tooltip
Composable hover, focus and tap tooltip with a subtle scale and fade, collision-aware side and alignment, an optional arrow, a pointer corridor onto hoverable content, one label at a time, keyboard-only focus reveal and optional cursor tracking.
Updated