Skip to content
Primitives

Collapsible

Single-section expand/collapse with Motion-driven height — Accordion's lighter cousin.

Motionheight + opacity on tween.quick (respects reduced motion)

Install

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

bash
npm install kineto
tsx
import {
  Collapsible,
  CollapsibleTrigger,
  CollapsibleContent,
} from "kineto/collapsible";

Preview

Yes — every interactive primitive ships with a `"use client"` boundary so it drops into the App Router cleanly.
tsx
"use client";

import * as React from "react";
import * as CollapsiblePrimitive from "@radix-ui/react-collapsible";
import { motion, useReducedMotion } from "motion/react";

import { cn } from "@/lib/cn";
import { tween } from "@/lib/motion-presets";

/* Mirrors Radix open state so content height can be driven by Motion rather than CSS. */
const CollapsibleOpenContext = React.createContext(false);

function useControllableOpen({
  open,
  defaultOpen = false,
  onOpenChange,
}: {
  open?: boolean;
  defaultOpen?: boolean;
  onOpenChange?: (open: boolean) => void;
}): [boolean, (next: boolean) => void] {
  const [uncontrolled, setUncontrolled] = React.useState(defaultOpen);
  const isControlled = open !== undefined;
  const value = isControlled ? (open as boolean) : uncontrolled;
  const setValue = React.useCallback(
    (next: boolean) => {
      if (!isControlled) setUncontrolled(next);
      onOpenChange?.(next);
    },
    [isControlled, onOpenChange]
  );
  return [value, setValue];
}

const Collapsible = ({
  open: openProp,
  defaultOpen,
  onOpenChange,
  children,
  ...props
}: React.ComponentProps<typeof CollapsiblePrimitive.Root>) => {
  const [open, setOpen] = useControllableOpen({
    open: openProp,
    defaultOpen,
    onOpenChange,
  });

  return (
    <CollapsibleOpenContext.Provider value={open}>
      <CollapsiblePrimitive.Root
        open={open}
        onOpenChange={setOpen}
        {...props}
      >
        {children}
      </CollapsiblePrimitive.Root>
    </CollapsibleOpenContext.Provider>
  );
};
Collapsible.displayName = "Collapsible";

const CollapsibleTrigger = CollapsiblePrimitive.CollapsibleTrigger;

const CollapsibleContent = React.forwardRef<
  React.ElementRef<typeof CollapsiblePrimitive.CollapsibleContent>,
  React.ComponentPropsWithoutRef<typeof CollapsiblePrimitive.CollapsibleContent>
>(({ className, children, ...props }, ref) => {
  const open = React.useContext(CollapsibleOpenContext);
  const reduced = useReducedMotion();

  return (
    <CollapsiblePrimitive.CollapsibleContent asChild forceMount ref={ref} {...props}>
      <motion.div
        className="overflow-hidden"
        initial={false}
        animate={open ? "open" : "closed"}
        variants={
          reduced
            ? { open: { opacity: 1 }, closed: { opacity: 0 } }
            : {
                open: { height: "auto", opacity: 1 },
                closed: { height: 0, opacity: 0 },
              }
        }
        transition={reduced ? { duration: 0 } : tween.quick}
      >
        <div className={cn(className)}>{children}</div>
      </motion.div>
    </CollapsiblePrimitive.CollapsibleContent>
  );
});
CollapsibleContent.displayName = "CollapsibleContent";

export { Collapsible, CollapsibleTrigger, CollapsibleContent };

src/components/ui/collapsible.tsx

Props

PropTypeDefaultDescription
open / defaultOpenboolean—Controlled or uncontrolled open state.
onOpenChange(open: boolean) => void—Fires when the section toggles.