Preview
"use client";
import { ArrowUpRight, Check } from "lucide-react";
import {
CenterMorphModal,
CenterMorphModalContent,
CenterMorphModalTrigger,
} from "@/components/motion/center-morph-modal";
export function CenterMorphModalPreview() {
return (
<div className="flex min-h-[420px] w-full items-center justify-center">
<CenterMorphModal>
<CenterMorphModalTrigger>
<button
type="button"
className="inline-flex h-10 items-center justify-center rounded-full bg-primary px-5 text-sm font-medium text-primary-foreground press focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
>
Open modal
</button>
</CenterMorphModalTrigger>
<CenterMorphModalContent
ariaLabel="beUI Pro"
ariaDescribedBy="center-morph-pro-description"
>
<div className="p-7 sm:p-8">
<p className="text-sm font-medium text-muted-foreground">
beUI Pro
</p>
<h2 className="mt-5 max-w-xs pr-8 text-2xl font-medium tracking-tight text-foreground">
Ship the whole experience.
</h2>
<p
id="center-morph-pro-description"
className="mt-3 text-sm leading-relaxed text-muted-foreground"
>
Go beyond individual components with premium animated sections
and complete Next.js templates.
</p>
<div className="mt-7 space-y-3 border-y border-border py-5">
{[
"Premium animated sections",
"Complete Next.js templates",
"Editable source and private registry",
].map((feature) => (
<div
key={feature}
className="flex items-center gap-3 text-sm text-foreground"
>
<Check
className="h-4 w-4 text-muted-foreground"
aria-hidden="true"
/>
<span>{feature}</span>
</div>
))}
</div>
<a
href="https://pro.beui.dev/?utm_source=beui&utm_medium=component_preview&utm_campaign=center_morph_modal"
target="_blank"
rel="noreferrer"
className="mt-7 inline-flex h-11 w-full items-center justify-center gap-2 rounded-full bg-primary px-5 text-sm font-medium text-primary-foreground press focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
>
Explore beUI Pro
<ArrowUpRight className="h-4 w-4" aria-hidden="true" />
</a>
</div>
</CenterMorphModalContent>
</CenterMorphModal>
</div>
);
}
components/motion/center-morph-modal.tsx
359 lines"use client";
// beui.dev/components/motion/center-morph-modal
import { X } from "lucide-react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import {
cloneElement,
createContext,
isValidElement,
type ReactElement,
type ReactNode,
useCallback,
useContext,
useEffect,
useId,
useMemo,
useRef,
useState,
} from "react";
import { createPortal } from "react-dom";
import { Tooltip } from "@/components/motion/tooltip";
import { EASE_OUT } from "@/lib/ease";
import { useFocusTrap } from "@/lib/hooks/use-focus-trap";
import { useMessages } from "@/lib/i18n";
import { PresenceGate } from "@/lib/presence-gate";
import { useScrollLock } from "@/lib/scroll-lock";
import { cn } from "@/lib/utils";
/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */
export const centerMorphModalMessages = {
close: "Close modal",
dismiss: "Dismiss modal",
};
export type CenterMorphModalMessages = typeof centerMorphModalMessages;
// Kept apart from the state context, whose identity drives the focus effect,
// so inline `labels` never re-run focus handling.
const CenterMorphModalLabelsContext = createContext<
Partial<CenterMorphModalMessages> | undefined
>(undefined);
type CenterMorphModalContextValue = {
open: boolean;
setOpen: (open: boolean) => void;
triggerId: string;
contentId: string;
};
const CenterMorphModalContext =
createContext<CenterMorphModalContextValue | null>(null);
function useCenterMorphModalContext(component: string) {
const context = useContext(CenterMorphModalContext);
if (!context) {
throw new Error(`${component} must be used within <CenterMorphModal>`);
}
return context;
}
export interface CenterMorphModalProps {
children: ReactNode;
/** Controlled open state. */
open?: boolean;
/** Initial state when used uncontrolled. */
defaultOpen?: boolean;
/** Called with the new open state when the modal opens or closes. */
onOpenChange?: (open: boolean) => void;
/** Override any built-in string. */
labels?: Partial<CenterMorphModalMessages>;
}
/**
* A modal whose full-size surface unfolds outward from its exact center.
* Supports controlled and uncontrolled state through composable primitives.
*/
export function CenterMorphModal({
children,
open: controlledOpen,
defaultOpen = false,
onOpenChange,
labels,
}: CenterMorphModalProps) {
const id = useId();
const [internalOpen, setInternalOpen] = useState(defaultOpen);
const controlled = controlledOpen !== undefined;
const open = controlled ? controlledOpen : internalOpen;
const setOpen = useCallback(
(next: boolean) => {
if (!controlled) setInternalOpen(next);
onOpenChange?.(next);
},
[controlled, onOpenChange],
);
const value = useMemo<CenterMorphModalContextValue>(
() => ({
open,
setOpen,
triggerId: `${id}-trigger`,
contentId: `${id}-content`,
}),
[id, open, setOpen],
);
return (
<CenterMorphModalContext.Provider value={value}>
<CenterMorphModalLabelsContext.Provider value={labels}>
{children}
</CenterMorphModalLabelsContext.Provider>
</CenterMorphModalContext.Provider>
);
}
export interface CenterMorphModalTriggerProps {
children: ReactElement;
}
/** Wraps one interactive element and opens or closes the modal. */
export function CenterMorphModalTrigger({
children,
}: CenterMorphModalTriggerProps) {
const context = useCenterMorphModalContext("CenterMorphModalTrigger");
if (!isValidElement(children)) return children;
const child = children as ReactElement<Record<string, unknown>>;
const childOnClick = child.props.onClick as
| ((event: React.MouseEvent<HTMLElement>) => void)
| undefined;
return cloneElement(child, {
id: context.triggerId,
onClick: (event: React.MouseEvent<HTMLElement>) => {
childOnClick?.(event);
if (!event.defaultPrevented) context.setOpen(!context.open);
},
"aria-haspopup": "dialog",
"aria-expanded": context.open,
"aria-controls": context.open ? context.contentId : undefined,
});
}
export interface CenterMorphModalCloseProps {
children: ReactElement;
}
/** Wraps one interactive element and closes the modal. */
export function CenterMorphModalClose({
children,
}: CenterMorphModalCloseProps) {
const context = useCenterMorphModalContext("CenterMorphModalClose");
if (!isValidElement(children)) return children;
const child = children as ReactElement<Record<string, unknown>>;
const childOnClick = child.props.onClick as
| ((event: React.MouseEvent<HTMLElement>) => void)
| undefined;
return cloneElement(child, {
onClick: (event: React.MouseEvent<HTMLElement>) => {
childOnClick?.(event);
if (!event.defaultPrevented) context.setOpen(false);
},
});
}
export interface CenterMorphModalContentProps {
children: ReactNode;
/** Accessible name announced by screen readers. */
ariaLabel: string;
/** Optional id of descriptive content inside the modal. */
ariaDescribedBy?: string;
/** Close on Escape or backdrop press. Default true. */
dismissible?: boolean;
/** Render the close control inside the panel's top-right corner. Default true. */
showCloseButton?: boolean;
/** Accessible name and tooltip for the close button. Default: "Close modal" */
closeButtonLabel?: string;
/** Classes merged onto the modal panel. */
className?: string;
/** Classes merged onto the backdrop. */
backdropClassName?: string;
}
const CENTER_FOLDED_CLIP =
"inset(48% 48% 48% 48% round 16px)";
const CENTER_OPEN_CLIP = "inset(0% 0% 0% 0% round 16px)";
// Complex clip-path strings can snap when a spring resolves its final distance.
// Keep the radius constant so the whole duration reads as surface unfolding,
// rather than finishing early and spending its last frames rounding corners.
const CENTER_UNFOLD_EASE = [0.2, 0, 0.2, 1] as const;
const CENTER_UNFOLD_TRANSITION = {
duration: 0.43,
ease: CENTER_UNFOLD_EASE,
} as const;
// Folding back is faster than unfolding: the user is done with it.
const CENTER_FOLD_TRANSITION = {
duration: 0.3,
ease: CENTER_UNFOLD_EASE,
} as const;
export function CenterMorphModalContent({
children,
ariaLabel,
ariaDescribedBy,
dismissible = true,
showCloseButton = true,
closeButtonLabel,
className,
backdropClassName,
}: CenterMorphModalContentProps) {
const context = useCenterMorphModalContext("CenterMorphModalContent");
const t = useMessages(
"centerMorphModal",
centerMorphModalMessages,
useContext(CenterMorphModalLabelsContext),
);
const reduce = useReducedMotion() ?? false;
const [mounted, setMounted] = useState(false);
const panelRef = useRef<HTMLDivElement>(null);
useEffect(() => setMounted(true), []);
// Tab stays in the panel, Escape closes when dismissible, and focus returns
// to the trigger that opened it.
const { open, setOpen } = context;
const close = useCallback(() => setOpen(false), [setOpen]);
useFocusTrap(open && mounted, panelRef, {
onEscape: dismissible ? close : undefined,
});
useScrollLock(open);
if (!mounted) return null;
return createPortal(
<AnimatePresence>
{context.open ? (
<PresenceGate>
{({ isPresent, gate }) => (
<>
<motion.button
type="button"
aria-label={t("dismiss")}
tabIndex={-1}
disabled={!dismissible}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
// The scrim clears faster than it settles, like the panel.
exit={{
opacity: 0,
transition: { duration: reduce ? 0.1 : 0.2, ease: EASE_OUT },
}}
{...gate}
transition={{
duration: reduce ? 0.1 : 0.28,
ease: EASE_OUT,
}}
onClick={() => context.setOpen(false)}
className={cn(
"pointer-events-auto fixed inset-0 z-[100] h-full w-full cursor-default bg-background/10 backdrop-blur-sm",
backdropClassName,
)}
/>
{/* `inset-4` rather than `inset-0 p-4`: same content box, but the
layer stays off the viewport edges. It never takes pointer
events, so it carries `inert` alone. */}
<div
inert={!isPresent}
className="pointer-events-none fixed inset-4 z-[100] flex items-center justify-center overflow-y-auto drop-shadow-2xl"
>
{/* Drop-shadow reads the clipped child's alpha, so depth follows the
unfolding silhouette without introducing another panel layer. */}
<div className="flex w-full flex-col items-center py-8">
<motion.div
ref={panelRef}
id={context.contentId}
role="dialog"
aria-modal="true"
aria-label={ariaLabel}
aria-describedby={ariaDescribedBy}
tabIndex={-1}
initial={
reduce
? { opacity: 0, clipPath: CENTER_OPEN_CLIP }
: { opacity: 1, clipPath: CENTER_FOLDED_CLIP }
}
animate={{
opacity: 1,
clipPath: CENTER_OPEN_CLIP,
}}
exit={
reduce
? {
opacity: 0,
clipPath: CENTER_OPEN_CLIP,
}
: {
opacity: 1,
clipPath: CENTER_FOLDED_CLIP,
transition: CENTER_FOLD_TRANSITION,
}
}
{...gate}
transition={
reduce
? { duration: 0.14, ease: EASE_OUT }
: CENTER_UNFOLD_TRANSITION
}
className={cn(
"pointer-events-auto relative w-full max-w-[26rem] origin-center overflow-hidden rounded-2xl border border-border bg-background will-change-[clip-path]",
className,
)}
>
{children}
{showCloseButton ? (
<Tooltip content={closeButtonLabel ?? t("close")} side="left">
<motion.button
type="button"
aria-label={closeButtonLabel ?? t("close")}
onClick={() => context.setOpen(false)}
initial={
reduce
? { opacity: 0 }
: { opacity: 0, scale: 0.8 }
}
animate={{ opacity: 1, scale: 1 }}
whileTap={reduce ? undefined : { scale: 0.95 }}
exit={{
opacity: 0,
scale: reduce ? 1 : 0.88,
transition: { duration: 0.1, ease: EASE_OUT },
}}
transition={{
delay: reduce ? 0 : 0.16,
duration: reduce ? 0.12 : 0.2,
ease: EASE_OUT,
}}
className="absolute right-4 top-4 inline-flex h-8 w-8 items-center justify-center rounded-full bg-foreground/[0.05] text-muted-foreground transition-colors hover:bg-foreground/[0.08] hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
>
<X className="h-4 w-4" aria-hidden="true" />
</motion.button>
</Tooltip>
) : null}
</motion.div>
</div>
</div>
</>
)}
</PresenceGate>
) : null}
</AnimatePresence>,
document.body,
);
}
Add it with the shadcn CLI, or copy the source manually.
npx shadcn add https://beui.dev/r/theme.json. Theme setupInstall dependencies
npm i @floating-ui/dom clsx lucide-react motion tailwind-mergeAdd 19 files
Put each file at the same path in your project. Read or copy them one at a time in the Code tab above.
- components/motion/center-morph-modal.tsx359
- components/motion/tooltip.tsx766
- lib/ease.ts68
- lib/hooks/use-focus-trap.ts81
- lib/i18n.tsx90
- lib/presence-gate.tsx53
- lib/scroll-lock.ts72
- lib/utils.ts7
- components/motion/tooltip/grace.ts92
- components/motion/tooltip/positioner.tsx70
- components/motion/tooltip/use-position.ts384
- components/motion/tooltip-surface.tsx71
- lib/hooks/use-dismiss.ts161
- lib/hooks/use-hover-gesture.ts62
- lib/hooks/use-tap-gesture.ts67
- lib/merge-refs.ts15
- lib/focusable.ts49
- lib/layers.ts77
- lib/touch.ts118
open?booleanControlled open state.
defaultOpen?booleanInitial state when used uncontrolled.
falseonOpenChange?((open: boolean) => void)Called with the new open state when the modal opens or closes.
labels?Partial<{ close: string; dismiss: string; }>Override any built-in string.
ariaLabelstringAccessible name announced by screen readers.
ariaDescribedBy?stringOptional id of descriptive content inside the modal.
dismissible?booleanClose on Escape or backdrop press. Default true.
trueshowCloseButton?booleanRender the close control inside the panel's top-right corner. Default true.
truecloseButtonLabel?stringAccessible name and tooltip for the close button. Default: "Close modal"
className?stringClasses merged onto the modal panel.
backdropClassName?stringClasses merged onto the backdrop.
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.
Select
Composable select primitives whose panel unfolds out of the trigger on one spring and pulls away into its own pill, plus a Morph variant where the trigger grows into the panel via shared layout.
Dropdown Menu
Composable dropdown menu that opens from its trigger, with a gliding highlight, submenus that stay open on a diagonal path, checkbox and radio items, shortcuts and full keyboard support.
Updated