Primitives
Accordion
Radix accordion whose content reveals via a Motion height animation.
Motionheight/opacity on tween.quick; chevron rotate on spring.snappy
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import {
Accordion,
AccordionItem,
AccordionTrigger,
AccordionContent,
} from "kineto/accordion";Preview
No. You copy the source into your project and own it outright.
With Motion, not CSS keyframes — one engine for the whole library.
Yes — it is built on Radix primitives.
tsx
"use client";
import * as React from "react";
import * as AccordionPrimitive from "@radix-ui/react-accordion";
import { motion, useReducedMotion } from "motion/react";
import { ChevronDown } from "lucide-react";
import { cn } from "@/lib/cn";
import { spring, tween } from "@/lib/motion-presets";
/* Tracks which items are open so the trigger chevron and the content height
reveal can be driven by Motion rather than CSS keyframes. */
const AccordionOpenContext = React.createContext<string[]>([]);
const AccordionItemContext = React.createContext<string>("");
function toArray(value: string | string[] | undefined): string[] {
if (value == null) return [];
return Array.isArray(value) ? value : [value];
}
const Accordion = ({
className,
children,
...props
}: React.ComponentProps<typeof AccordionPrimitive.Root>) => {
// Radix's value type is a `single | multiple` union; cast through `any` to read it
// uniformly while keeping the public prop types intact for consumers.
const radixProps = props as {
value?: string | string[];
defaultValue?: string | string[];
onValueChange?: (value: string | string[]) => void;
};
const [open, setOpen] = React.useState<string[]>(() =>
toArray(radixProps.defaultValue ?? radixProps.value)
);
React.useEffect(() => {
if (radixProps.value !== undefined) setOpen(toArray(radixProps.value));
}, [radixProps.value]);
const handleValueChange = (value: string | string[]) => {
setOpen(toArray(value));
radixProps.onValueChange?.(value);
};
return (
<AccordionOpenContext.Provider value={open}>
<AccordionPrimitive.Root
{...(props as React.ComponentProps<typeof AccordionPrimitive.Root>)}
onValueChange={handleValueChange as never}
className={cn("w-full", className)}
>
{children}
</AccordionPrimitive.Root>
</AccordionOpenContext.Provider>
);
};
Accordion.displayName = "Accordion";
const AccordionItem = React.forwardRef<
React.ElementRef<typeof AccordionPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Item>
>(({ className, value, ...props }, ref) => (
<AccordionItemContext.Provider value={value}>
<AccordionPrimitive.Item
ref={ref}
value={value}
className={cn("border-b border-border", className)}
{...props}
/>
</AccordionItemContext.Provider>
));
AccordionItem.displayName = "AccordionItem";
const AccordionTrigger = React.forwardRef<
React.ElementRef<typeof AccordionPrimitive.Trigger>,
React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Trigger>
>(({ className, children, ...props }, ref) => {
const open = React.useContext(AccordionOpenContext);
const itemValue = React.useContext(AccordionItemContext);
const reduced = useReducedMotion();
const isOpen = open.includes(itemValue);
return (
<AccordionPrimitive.Header className="flex">
<AccordionPrimitive.Trigger
ref={ref}
className={cn(
"flex flex-1 items-center justify-between gap-4 py-4 text-left text-base font-medium transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
className
)}
{...props}
>
{children}
<motion.span
aria-hidden
className="shrink-0 text-muted-foreground"
animate={{ rotate: isOpen ? 180 : 0 }}
transition={reduced ? { duration: 0 } : spring.snappy}
>
<ChevronDown className="size-4" />
</motion.span>
</AccordionPrimitive.Trigger>
</AccordionPrimitive.Header>
);
});
AccordionTrigger.displayName = "AccordionTrigger";
const AccordionContent = React.forwardRef<
React.ElementRef<typeof AccordionPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Content>
>(({ className, children, ...props }, ref) => {
const open = React.useContext(AccordionOpenContext);
const itemValue = React.useContext(AccordionItemContext);
const reduced = useReducedMotion();
const isOpen = open.includes(itemValue);
return (
<AccordionPrimitive.Content asChild forceMount ref={ref} {...props}>
<motion.div
className="overflow-hidden"
initial={false}
animate={isOpen ? "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("pb-4 pt-0 text-muted-foreground", className)}>
{children}
</div>
</motion.div>
</AccordionPrimitive.Content>
);
});
AccordionContent.displayName = "AccordionContent";
export { Accordion, AccordionItem, AccordionTrigger, AccordionContent };src/components/ui/accordion.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| type | "single" | "multiple" | — | Whether one or many items open (Radix). |
| collapsible | boolean | — | Allow closing the open item in "single" mode. |