Crystal Modal
A glassmorphism modal dialog for React built on Radix Dialog, Motion and Tailwind CSS. The pane scales up out of a blur while the page behind it frosts over — and focus trapping, Escape to close, scroll lock and ARIA all come from Radix.
- Radix Dialog
- Enter & exit animations
- Frosted overlay
- Focus trap
- prefers-reduced-motion
import { Check } from "lucide-react";
import { Button } from "@/components/ui/button";
import { FrostButton } from "@/components/dusk/glass/frost-button";
import {
CrystalModal,
CrystalModalClose,
CrystalModalContent,
CrystalModalDescription,
CrystalModalFooter,
CrystalModalHeader,
CrystalModalTitle,
CrystalModalTrigger,
} from "@/components/dusk/glass/crystal-modal";
import { GlassBackdrop } from "@/components/demos/glass-card/glass-backdrop";
const perks = ["Unlimited projects", "Every animated block", "Figma kit & priority support"];
export default function CrystalModalDemo() {
return (
<>
<GlassBackdrop />
<CrystalModal>
<CrystalModalTrigger asChild>
<FrostButton size="lg" tone="primary">
Upgrade to Pro
</FrostButton>
</CrystalModalTrigger>
<CrystalModalContent>
<CrystalModalHeader>
<CrystalModalTitle>Upgrade to Dusk Pro</CrystalModalTitle>
<CrystalModalDescription>
Everything in Free, plus the tools teams use to ship polished interfaces faster.
</CrystalModalDescription>
</CrystalModalHeader>
<ul className="space-y-2.5 rounded-lg bg-white/5 p-4 text-sm">
{perks.map((perk) => (
<li key={perk} className="flex items-center gap-2.5">
<Check className="size-4 text-white/60" aria-hidden />
{perk}
</li>
))}
</ul>
<CrystalModalFooter>
<CrystalModalClose asChild>
<Button variant="ghost" className="text-white/80 hover:text-white">
Maybe later
</Button>
</CrystalModalClose>
<CrystalModalClose asChild>
<FrostButton tone="primary">Upgrade · $12/mo</FrostButton>
</CrystalModalClose>
</CrystalModalFooter>
</CrystalModalContent>
</CrystalModal>
</>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/crystal-modal.json1. Install the dependencies.
npm install motion @radix-ui/react-dialog lucide-react2. Copy the source into
components/dusk/glass/crystal-modal.tsx.crystal-modal.tsx "use client"; import * as React from "react"; import * as DialogPrimitive from "@radix-ui/react-dialog"; import { XIcon } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion, type Transition } from "motion/react"; import { cn } from "@/lib/utils"; const RING = "p-px [mask-image:linear-gradient(#000_0_0),linear-gradient(#000_0_0)] [mask-clip:content-box,border-box] [mask-composite:exclude]"; const sizes = { sm: "max-w-sm", md: "max-w-md", lg: "max-w-lg", xl: "max-w-2xl", } as const; const enter: Transition = { type: "spring", duration: 0.3, bounce: 0.1 }; const leave: Transition = { duration: 0.2, ease: [0.4, 0, 1, 1] }; const ModalContext = React.createContext<{ open: boolean }>({ open: false }); export type CrystalModalProps = React.ComponentProps<typeof DialogPrimitive.Root>; /** * A glass modal on Radix Dialog: focus trap, Escape to close, scroll lock and * aria wiring come from Radix; Motion adds the enter/exit — the pane scales up * out of a blur while the page behind it frosts over. * * Works controlled (`open` + `onOpenChange`) or uncontrolled (`defaultOpen`). * Compose it like shadcn's Dialog: Trigger, Content, Header, Title, * Description, Footer and Close. */ function CrystalModal({ open: openProp, defaultOpen = false, onOpenChange, ...props }: CrystalModalProps) { // Track open state here so the content can stay mounted for its exit animation. const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen); const open = openProp ?? uncontrolledOpen; const handleOpenChange = React.useCallback( (next: boolean) => { if (openProp === undefined) setUncontrolledOpen(next); onOpenChange?.(next); }, [openProp, onOpenChange] ); const context = React.useMemo(() => ({ open }), [open]); return ( <ModalContext.Provider value={context}> <DialogPrimitive.Root open={open} onOpenChange={handleOpenChange} {...props} /> </ModalContext.Provider> ); } function CrystalModalTrigger(props: React.ComponentProps<typeof DialogPrimitive.Trigger>) { return <DialogPrimitive.Trigger data-slot="crystal-modal-trigger" {...props} />; } function CrystalModalClose(props: React.ComponentProps<typeof DialogPrimitive.Close>) { return <DialogPrimitive.Close data-slot="crystal-modal-close" {...props} />; } export interface CrystalModalContentProps extends Omit< React.ComponentProps<typeof DialogPrimitive.Content>, "asChild" | "forceMount" > { /** Max width of the pane. */ size?: keyof typeof sizes; /** Render the close (×) button in the corner. */ showCloseButton?: boolean; /** Classes for the frosted overlay behind the pane. */ overlayClassName?: string; /** Portal container. Defaults to `document.body`. */ container?: HTMLElement | null; } function CrystalModalContent({ size = "md", showCloseButton = true, overlayClassName, container, className, children, ...props }: CrystalModalContentProps) { const { open } = React.useContext(ModalContext); const reduceMotion = useReducedMotion(); const hidden = reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.96, y: 8, filter: "blur(4px)" }; return ( <AnimatePresence> {open && ( <DialogPrimitive.Portal forceMount container={container}> <DialogPrimitive.Overlay asChild forceMount> <motion.div data-slot="crystal-modal-overlay" className={cn( "fixed inset-0 z-50 bg-black/60 data-[state=closed]:pointer-events-none", "supports-[backdrop-filter]:bg-black/30 supports-[backdrop-filter]:backdrop-blur-md supports-[backdrop-filter]:backdrop-saturate-125", overlayClassName )} initial={{ opacity: 0 }} animate={{ opacity: 1, transition: { duration: 0.25, ease: "easeOut" } }} exit={{ opacity: 0, transition: { duration: 0.2, ease: "easeIn" } }} /> </DialogPrimitive.Overlay> <DialogPrimitive.Content asChild forceMount {...props}> <motion.div data-slot="crystal-modal-content" className={cn( "fixed left-1/2 top-1/2 z-50 grid max-h-[calc(100dvh-2rem)] w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 gap-4 overflow-y-auto rounded-xl p-6 text-foreground outline-none", "isolate data-[state=closed]:pointer-events-none", // Fallback first: opaque pane when backdrop-filter isn't supported. "bg-[color-mix(in_oklab,var(--background)_94%,white_2%)]", "supports-[backdrop-filter]:bg-[color-mix(in_oklab,var(--background)_55%,transparent)] supports-[backdrop-filter]:backdrop-blur-2xl supports-[backdrop-filter]:backdrop-saturate-150", "shadow-[inset_0_1px_0_rgb(255_255_255/0.08),0_16px_40px_-12px_rgb(0_0_0/0.6)]", sizes[size], className )} initial={hidden} animate={{ opacity: 1, scale: 1, y: 0, filter: "blur(0px)", // Drop the filter at rest: it would blur nested glass and costs a layer. transitionEnd: { filter: "none" }, transition: enter, }} exit={{ ...hidden, transition: leave }} > {/* 1px hairline border. */} <span aria-hidden className={cn("pointer-events-none absolute inset-0 -z-10 rounded-[inherit] bg-white/12", RING)} /> {children} {showCloseButton && ( <DialogPrimitive.Close data-slot="crystal-modal-close" className={cn( "absolute right-3 top-3 grid size-8 place-items-center rounded-full text-white/60 outline-none", "transition-[color,background-color,box-shadow] duration-150 hover:bg-white/10 hover:text-white", "focus-visible:text-white focus-visible:ring-[3px] focus-visible:ring-ring/50", "[&_svg]:size-4" )} > <XIcon aria-hidden /> <span className="sr-only">Close</span> </DialogPrimitive.Close> )} </motion.div> </DialogPrimitive.Content> </DialogPrimitive.Portal> )} </AnimatePresence> ); } function CrystalModalHeader({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="crystal-modal-header" className={cn("flex flex-col gap-1.5 pr-8 text-center sm:text-left", className)} {...props} /> ); } function CrystalModalFooter({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="crystal-modal-footer" className={cn("mt-2 flex flex-col-reverse gap-2 sm:flex-row sm:justify-end", className)} {...props} /> ); } function CrystalModalTitle({ className, ...props }: React.ComponentProps<typeof DialogPrimitive.Title>) { return ( <DialogPrimitive.Title data-slot="crystal-modal-title" className={cn("text-lg font-semibold leading-tight tracking-tight text-white", className)} {...props} /> ); } function CrystalModalDescription({ className, ...props }: React.ComponentProps<typeof DialogPrimitive.Description>) { return ( <DialogPrimitive.Description data-slot="crystal-modal-description" className={cn("text-sm leading-relaxed text-white/70", className)} {...props} /> ); } export { CrystalModal, CrystalModalTrigger, CrystalModalContent, CrystalModalHeader, CrystalModalFooter, CrystalModalTitle, CrystalModalDescription, CrystalModalClose, };
Usage
import {
CrystalModal,
CrystalModalContent,
CrystalModalDescription,
CrystalModalHeader,
CrystalModalTitle,
CrystalModalTrigger,
} from "@/components/dusk/glass/crystal-modal"
import { Button } from "@/components/ui/button"
export function InviteDialog() {
return (
<CrystalModal>
<CrystalModalTrigger asChild>
<Button>Invite</Button>
</CrystalModalTrigger>
<CrystalModalContent>
<CrystalModalHeader>
<CrystalModalTitle>Invite your team</CrystalModalTitle>
<CrystalModalDescription>They'll get an email with a link.</CrystalModalDescription>
</CrystalModalHeader>
</CrystalModalContent>
</CrystalModal>
)
}Examples
Controlled form
Control it with open and onOpenChange to close after an async save. The exit animation still plays. Fields here are Ice Inputs and the submit is a loading Frost Button.
"use client";
import * as React from "react";
import { AtSign, User } from "lucide-react";
import { Button } from "@/components/ui/button";
import { FrostButton } from "@/components/dusk/glass/frost-button";
import { IceInput } from "@/components/dusk/glass/ice-input";
import {
CrystalModal,
CrystalModalClose,
CrystalModalContent,
CrystalModalDescription,
CrystalModalFooter,
CrystalModalHeader,
CrystalModalTitle,
CrystalModalTrigger,
} from "@/components/dusk/glass/crystal-modal";
import { GlassBackdrop } from "@/components/demos/glass-card/glass-backdrop";
export default function CrystalModalForm() {
const [open, setOpen] = React.useState(false);
const [saving, setSaving] = React.useState(false);
const onSubmit = (event: React.FormEvent) => {
event.preventDefault();
setSaving(true);
setTimeout(() => {
setSaving(false);
setOpen(false);
}, 1200);
};
return (
<>
<GlassBackdrop />
<CrystalModal open={open} onOpenChange={setOpen}>
<CrystalModalTrigger asChild>
<FrostButton size="lg" className="rounded-full">
Edit profile
</FrostButton>
</CrystalModalTrigger>
<CrystalModalContent size="sm">
<form onSubmit={onSubmit} className="grid gap-4">
<CrystalModalHeader>
<CrystalModalTitle>Edit profile</CrystalModalTitle>
<CrystalModalDescription>Changes are visible to your team.</CrystalModalDescription>
</CrystalModalHeader>
<div className="grid gap-1.5">
<label htmlFor="crystal-name" className="text-sm font-medium text-white/80">
Name
</label>
<IceInput id="crystal-name" icon={<User />} defaultValue="Ava Lindqvist" autoComplete="name" />
</div>
<div className="grid gap-1.5">
<label htmlFor="crystal-handle" className="text-sm font-medium text-white/80">
Username
</label>
<IceInput id="crystal-handle" icon={<AtSign />} defaultValue="ava" autoComplete="username" />
</div>
<CrystalModalFooter>
<CrystalModalClose asChild>
<Button type="button" variant="ghost" className="text-white/80 hover:text-white">
Cancel
</Button>
</CrystalModalClose>
<FrostButton type="submit" tone="primary" loading={saving} loadingText="Saving…">
Save changes
</FrostButton>
</CrystalModalFooter>
</form>
</CrystalModalContent>
</CrystalModal>
</>
);
}API Reference
CrystalModal takes Radix Dialog Root props (open, defaultOpen, onOpenChange, modal). Trigger, Close, Title and Description are the Radix parts with styling. The props below are for CrystalModalContent, which also accepts Radix Content props such as onEscapeKeyDown and onOpenAutoFocus.
| Prop | Type | Default | Description |
|---|---|---|---|
| size | "sm" | "md" | "lg" | "xl" | "md" | Max width of the pane. |
| showCloseButton | boolean | true | Render the × button in the corner. |
| overlayClassName | string | — | Classes for the frosted overlay. |
| container | HTMLElement | null | document.body | Portal container. |
Accessibility
- Built on Radix Dialog: focus is trapped inside and returned to the trigger on close, Escape closes, and the page behind is inert and scroll-locked.
- Always include a
CrystalModalTitle(it labels the dialog) and ideally aCrystalModalDescription. - The close button has an accessible name and a focus-visible ring.
- Browsers without backdrop-filter get an opaque pane and a darker overlay, so contrast never depends on the blur.
- With reduced motion the pane only fades — no scale, slide or blur.