Skip to content
Primitives

Dialog

Radix dialog with a blurred animated overlay and a spring scale/fade panel.

Motionoverlay tween.quick fade + content spring.soft scale/fade

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import {
  Dialog,
  DialogTrigger,
  DialogClose,
  DialogContent,
  DialogHeader,
  DialogFooter,
  DialogTitle,
  DialogDescription,
} from "kineto/dialog";

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";

/* Minimal controllable-state hook so the component can mirror Radix's open state and
   drive AnimatePresence (needed to play an *exit* animation before unmount). */
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 DialogOpenContext = React.createContext(false);

const Dialog = ({
  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}>
      <DialogOpenContext.Provider value={isOpen}>
        {children}
      </DialogOpenContext.Provider>
    </DialogPrimitive.Root>
  );
};
Dialog.displayName = "Dialog";

const DialogTrigger = DialogPrimitive.Trigger;
const DialogClose = DialogPrimitive.Close;

const DialogContent = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => {
  const open = React.useContext(DialogOpenContext);
  const reduced = useReducedMotion();

  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 left-1/2 top-1/2 z-50 grid w-full max-w-lg gap-4 rounded-xl border border-border bg-popover p-6 text-popover-foreground shadow-lift",
                className
              )}
              initial={
                reduced
                  ? { opacity: 0, x: "-50%", y: "-50%" }
                  : { opacity: 0, scale: 0.95, x: "-50%", y: "-50%" }
              }
              animate={{ opacity: 1, scale: 1, x: "-50%", y: "-50%" }}
              exit={
                reduced
                  ? { opacity: 0, x: "-50%", y: "-50%" }
                  : { opacity: 0, scale: 0.97, x: "-50%", y: "-50%" }
              }
              transition={reduced ? { duration: 0 } : spring.soft}
            >
              {children}
              <DialogPrimitive.Close className="absolute right-4 top-4 rounded-md p-1 text-muted-foreground opacity-70 ring-offset-background transition-opacity hover:opacity-100 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>
  );
});
DialogContent.displayName = "DialogContent";

const DialogHeader = ({
  className,
  ...props
}: React.HTMLAttributes<HTMLDivElement>) => (
  <div
    className={cn("flex flex-col gap-1.5 text-center sm:text-left", className)}
    {...props}
  />
);
DialogHeader.displayName = "DialogHeader";

const DialogFooter = ({
  className,
  ...props
}: React.HTMLAttributes<HTMLDivElement>) => (
  <div
    className={cn(
      "flex flex-col-reverse gap-2 sm:flex-row sm:justify-end",
      className
    )}
    {...props}
  />
);
DialogFooter.displayName = "DialogFooter";

const DialogTitle = 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 leading-none tracking-tight",
      className
    )}
    {...props}
  />
));
DialogTitle.displayName = "DialogTitle";

const DialogDescription = 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}
  />
));
DialogDescription.displayName = "DialogDescription";

export {
  Dialog,
  DialogTrigger,
  DialogClose,
  DialogContent,
  DialogHeader,
  DialogFooter,
  DialogTitle,
  DialogDescription,
};

src/components/ui/dialog.tsx

Props

PropTypeDefaultDescription
open / defaultOpenboolean—Controlled or uncontrolled (Radix props).
onOpenChange(open: boolean) => void—Fires when open state changes.