Application
Drawer
An animated panel that slides in from any edge with a blurred overlay.
Motionpanel slide on spring.soft + overlay fade
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import {
Drawer,
DrawerTrigger,
DrawerClose,
DrawerContent,
DrawerHeader,
DrawerFooter,
DrawerTitle,
DrawerDescription,
} from "kineto/drawer";Preview
tsx
"use client";
import * as React from "react";
import * as DialogPrimitive from "@radix-ui/react-dialog";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { X } from "lucide-react";
import { cn } from "@/lib/cn";
import { spring, tween } from "@/lib/motion-presets";
function useControllableState<T>({
prop,
defaultProp,
onChange,
}: {
prop: T | undefined;
defaultProp: T;
onChange?: (value: T) => void;
}): [T, (next: T) => void] {
const [uncontrolled, setUncontrolled] = React.useState(defaultProp);
const isControlled = prop !== undefined;
const value = isControlled ? (prop as T) : uncontrolled;
const setValue = React.useCallback(
(next: T) => {
if (!isControlled) setUncontrolled(next);
onChange?.(next);
},
[isControlled, onChange]
);
return [value, setValue];
}
const DrawerOpenContext = React.createContext(false);
const Drawer = ({
open,
defaultOpen = false,
onOpenChange,
children,
...props
}: DialogPrimitive.DialogProps) => {
const [isOpen, setIsOpen] = useControllableState({
prop: open,
defaultProp: defaultOpen,
onChange: onOpenChange,
});
return (
<DialogPrimitive.Root open={isOpen} onOpenChange={setIsOpen} {...props}>
<DrawerOpenContext.Provider value={isOpen}>
{children}
</DrawerOpenContext.Provider>
</DialogPrimitive.Root>
);
};
Drawer.displayName = "Drawer";
const DrawerTrigger = DialogPrimitive.Trigger;
const DrawerClose = DialogPrimitive.Close;
const drawerSides = {
right: { className: "inset-y-0 right-0 w-full max-w-md border-l", offset: { x: "100%" } },
left: { className: "inset-y-0 left-0 w-full max-w-md border-r", offset: { x: "-100%" } },
top: { className: "inset-x-0 top-0 max-h-dvh border-b", offset: { y: "-100%" } },
bottom: { className: "inset-x-0 bottom-0 max-h-dvh border-t", offset: { y: "100%" } },
} as const;
export interface DrawerContentProps
extends React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> {
side?: keyof typeof drawerSides;
}
const DrawerContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
DrawerContentProps
>(({ className, children, side = "right", ...props }, ref) => {
const open = React.useContext(DrawerOpenContext);
const reduced = useReducedMotion();
const { className: sideClassName, offset } = drawerSides[side];
return (
<AnimatePresence>
{open ? (
<DialogPrimitive.Portal forceMount>
<DialogPrimitive.Overlay asChild forceMount>
<motion.div
className="fixed inset-0 z-50 bg-foreground/40 backdrop-blur-sm"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={reduced ? { duration: 0 } : tween.quick}
/>
</DialogPrimitive.Overlay>
<DialogPrimitive.Content asChild forceMount ref={ref} {...props}>
<motion.div
className={cn(
"fixed z-50 flex flex-col gap-4 border-border bg-popover p-6 text-popover-foreground shadow-lift",
sideClassName,
className
)}
initial={reduced ? false : offset}
animate={{ x: 0, y: 0 }}
exit={reduced ? undefined : offset}
transition={reduced ? { duration: 0 } : spring.soft}
>
{children}
<DialogPrimitive.Close className="absolute right-4 top-4 inline-flex size-8 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
<X className="size-4" />
<span className="sr-only">Close</span>
</DialogPrimitive.Close>
</motion.div>
</DialogPrimitive.Content>
</DialogPrimitive.Portal>
) : null}
</AnimatePresence>
);
});
DrawerContent.displayName = "DrawerContent";
const DrawerHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
<div className={cn("flex flex-col gap-1.5 pr-8", className)} {...props} />
);
DrawerHeader.displayName = "DrawerHeader";
const DrawerFooter = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
<div className={cn("mt-auto flex flex-col gap-2 sm:flex-row sm:justify-end", className)} {...props} />
);
DrawerFooter.displayName = "DrawerFooter";
const DrawerTitle = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Title
ref={ref}
className={cn("font-display text-lg font-semibold tracking-tight", className)}
{...props}
/>
));
DrawerTitle.displayName = "DrawerTitle";
const DrawerDescription = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
));
DrawerDescription.displayName = "DrawerDescription";
export {
Drawer,
DrawerTrigger,
DrawerClose,
DrawerContent,
DrawerHeader,
DrawerFooter,
DrawerTitle,
DrawerDescription,
};src/components/ui/drawer.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| side | "right" | "left" | "top" | "bottom" | "right" | Edge it slides from. |
| open / onOpenChange | boolean / fn | — | Controlled or uncontrolled. |