Collapsible
An animated React collapsible built on shadcn/ui and Radix: the drop-in Collapsible eases its height open and fades content in, plus a spring-physics Motion collapsible with a rotating chevron and show-more blocks that expand clamped text and long lists smoothly.
- Drop-in shadcn API
- Radix Collapsible
- Height animation
- Show more
- prefers-reduced-motion
- Order confirmed
- Packed at the warehouse
- Handed to the courier
import { Check, Package } from "lucide-react";
import {
MotionCollapsible,
MotionCollapsibleContent,
MotionCollapsibleTrigger,
} from "@/components/dusk/collapsible/motion-collapsible";
const steps = ["Order confirmed", "Packed at the warehouse", "Handed to the courier"];
export default function MotionCollapsibleDemo() {
return (
<div className="flex w-full max-w-md flex-col gap-3">
<MotionCollapsible defaultOpen>
<MotionCollapsibleTrigger>
<span className="flex items-center gap-3">
<span className="flex size-8 items-center justify-center rounded-lg border border-white/10 text-muted-foreground">
<Package className="size-4" aria-hidden />
</span>
<span className="flex flex-col">
<span>Order #4821</span>
<span className="text-xs font-normal text-muted-foreground">Arrives Thursday</span>
</span>
</span>
</MotionCollapsibleTrigger>
<MotionCollapsibleContent>
<ol className="flex flex-col gap-2 border-t border-white/5 pt-3">
{steps.map((step) => (
<li key={step} className="flex items-center gap-2">
<Check className="size-3.5 text-muted-foreground" aria-hidden />
{step}
</li>
))}
</ol>
</MotionCollapsibleContent>
</MotionCollapsible>
<MotionCollapsible>
<MotionCollapsibleTrigger>Shipping & returns</MotionCollapsibleTrigger>
<MotionCollapsibleContent>
Free returns within 30 days. Print a label from your account and drop the parcel at any partner
location — refunds land within three business days of us receiving it.
</MotionCollapsibleContent>
</MotionCollapsible>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/collapsible.json1. Install the dependencies.
npm install @radix-ui/react-collapsible2. Copy the source into
components/ui/collapsible.tsx.collapsible.tsx "use client" import * as React from "react" import * as CollapsiblePrimitive from "@radix-ui/react-collapsible" import { cn } from "@/lib/utils" /** * DuskUI Collapsible — a drop-in upgrade of the shadcn/ui Collapsible. * * Same parts, props and data-slot attributes as shadcn (Collapsible, * CollapsibleTrigger, CollapsibleContent), plus: * - The content eases its height open (200ms ease-out), using Radix's * measured `--radix-collapsible-content-height`, and closes quicker than it * opens. * - Content fades in while it grows, and fades out as it closes. * - Nothing below the panel jumps: the height animates, so the layout * follows smoothly. * - Under `prefers-reduced-motion` the panel opens and closes instantly. * CSS only (tw-animate-css keyframes), powered by Radix for aria-expanded, * aria-controls and keyboard support. */ function Collapsible({ ...props }: React.ComponentProps<typeof CollapsiblePrimitive.Root>) { return <CollapsiblePrimitive.Root data-slot="collapsible" {...props} /> } /** The button that toggles the panel. Use `asChild` to render your own Button. */ function CollapsibleTrigger({ ...props }: React.ComponentProps<typeof CollapsiblePrimitive.CollapsibleTrigger>) { return ( <CollapsiblePrimitive.CollapsibleTrigger data-slot="collapsible-trigger" {...props} /> ) } /** The panel. Animates its height between 0 and its measured content height. */ function CollapsibleContent({ className, ...props }: React.ComponentProps<typeof CollapsiblePrimitive.CollapsibleContent>) { return ( <CollapsiblePrimitive.CollapsibleContent data-slot="collapsible-content" className={cn( "overflow-hidden", // Height (Radix CSS var) and a fade run as one animation list, so Radix // waits for both before unmounting the closed panel. "[--tw-enter-opacity:0] [--tw-enter-translate-y:-4px] [--tw-exit-opacity:0]", "data-[state=open]:animate-[collapsible-down_200ms_ease-out,enter_200ms_ease-out]", "data-[state=closed]:animate-[collapsible-up_150ms_ease-in,exit_150ms_ease-in]", "motion-reduce:animate-none", className )} {...props} /> ) } export { Collapsible, CollapsibleTrigger, CollapsibleContent }
Replaces components/ui/collapsible.tsx with the same exports. The height animation uses Radix's --radix-collapsible-content-height with the collapsible-down / collapsible-up keyframes from tw-animate-css, which shadcn projects already include.
Usage
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible"
export function Details() {
return (
<Collapsible>
<CollapsibleTrigger>Can I use this in my project?</CollapsibleTrigger>
<CollapsibleContent>Yes. Free for personal and commercial use.</CollapsibleContent>
</Collapsible>
)
}Examples
Default
A controlled Collapsible with a Button trigger (asChild). The extra rows grow in and fade as the panel opens, and close quicker than they open; nothing below them jumps.
@ada starred 4 repositories
"use client";
import * as React from "react";
import { ChevronsUpDown } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible";
const repos = ["duskui/registry", "duskui/icons", "duskui/docs"];
export default function CollapsibleBasicDemo() {
const [open, setOpen] = React.useState(false);
return (
<Collapsible open={open} onOpenChange={setOpen} className="flex w-full max-w-xs flex-col gap-2">
<div className="flex items-center justify-between gap-4 px-1">
<h4 className="text-sm font-semibold">@ada starred 4 repositories</h4>
<CollapsibleTrigger asChild>
<Button variant="ghost" size="icon-sm" aria-label={open ? "Hide repositories" : "Show all repositories"}>
<ChevronsUpDown />
</Button>
</CollapsibleTrigger>
</div>
<div className="rounded-md border px-4 py-2 font-mono text-sm">duskui/ui</div>
<CollapsibleContent className="flex flex-col gap-2">
{repos.map((repo) => (
<div key={repo} className="rounded-md border px-4 py-2 font-mono text-sm">
{repo}
</div>
))}
</CollapsibleContent>
</Collapsible>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Motion Collapsible
The panel eases its height open while the content fades in after it, and the chevron turns. Open panels get a slightly brighter outline. Closed content stays in the DOM as inert, so it's indexed by search engines but skipped by keyboard and screen readers.
- Order confirmed
- Packed at the warehouse
- Handed to the courier
import { Check, Package } from "lucide-react";
import {
MotionCollapsible,
MotionCollapsibleContent,
MotionCollapsibleTrigger,
} from "@/components/dusk/collapsible/motion-collapsible";
const steps = ["Order confirmed", "Packed at the warehouse", "Handed to the courier"];
export default function MotionCollapsibleDemo() {
return (
<div className="flex w-full max-w-md flex-col gap-3">
<MotionCollapsible defaultOpen>
<MotionCollapsibleTrigger>
<span className="flex items-center gap-3">
<span className="flex size-8 items-center justify-center rounded-lg border border-white/10 text-muted-foreground">
<Package className="size-4" aria-hidden />
</span>
<span className="flex flex-col">
<span>Order #4821</span>
<span className="text-xs font-normal text-muted-foreground">Arrives Thursday</span>
</span>
</span>
</MotionCollapsibleTrigger>
<MotionCollapsibleContent>
<ol className="flex flex-col gap-2 border-t border-white/5 pt-3">
{steps.map((step) => (
<li key={step} className="flex items-center gap-2">
<Check className="size-3.5 text-muted-foreground" aria-hidden />
{step}
</li>
))}
</ol>
</MotionCollapsibleContent>
</MotionCollapsible>
<MotionCollapsible>
<MotionCollapsibleTrigger>Shipping & returns</MotionCollapsibleTrigger>
<MotionCollapsibleContent>
Free returns within 30 days. Print a label from your account and drop the parcel at any partner
location — refunds land within three business days of us receiving it.
</MotionCollapsibleContent>
</MotionCollapsible>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/motion-collapsible.json1. Install the dependencies.
npm install motion @radix-ui/react-collapsible lucide-react2. Copy the source into
components/dusk/collapsible/motion-collapsible.tsx.motion-collapsible.tsx "use client"; import * as React from "react"; import * as CollapsiblePrimitive from "@radix-ui/react-collapsible"; import { motion, useReducedMotion, type Transition, type Variants } from "motion/react"; import { ChevronDownIcon } from "lucide-react"; import { cn } from "@/lib/utils"; type MotionCollapsibleContextValue = { open: boolean; reduceMotion: boolean }; const MotionCollapsibleContext = React.createContext<MotionCollapsibleContextValue | null>(null); function useMotionCollapsible() { const context = React.useContext(MotionCollapsibleContext); if (!context) throw new Error("MotionCollapsible parts must be used inside <MotionCollapsible>."); return context; } export interface MotionCollapsibleProps extends React.ComponentProps<typeof CollapsiblePrimitive.Root> { /** Outline the panel and brighten its border while open. */ bordered?: boolean; } /** * A collapsible with gentle spring physics: the panel eases its height open, * the content fades in after it, and the chevron turns. Closed content stays mounted but `inert`, so it is * indexed by search engines yet skipped by keyboard and screen readers. * Built on Radix Collapsible (aria-expanded, aria-controls). Under reduced * motion it opens and closes instantly. */ export function MotionCollapsible({ open: openProp, defaultOpen = false, onOpenChange, bordered = true, className, ...props }: MotionCollapsibleProps) { const reduceMotion = useReducedMotion() ?? false; const [uncontrolled, setUncontrolled] = React.useState(defaultOpen); const open = openProp ?? uncontrolled; const setOpen = (next: boolean) => { if (openProp === undefined) setUncontrolled(next); onOpenChange?.(next); }; return ( <MotionCollapsibleContext.Provider value={{ open, reduceMotion }}> <CollapsiblePrimitive.Root data-slot="collapsible" open={open} onOpenChange={setOpen} className={cn( "w-full", bordered && [ "rounded-xl border bg-white/[0.015]", "transition-[border-color,background-color] duration-200 ease-out motion-reduce:transition-none", "data-[state=open]:border-white/15 data-[state=open]:bg-white/[0.025]", ], className )} {...props} /> </MotionCollapsibleContext.Provider> ); } const chevronSpring: Transition = { type: "spring", bounce: 0, duration: 0.25 }; export interface MotionCollapsibleTriggerProps extends Omit<React.ComponentProps<typeof CollapsiblePrimitive.Trigger>, "asChild"> { /** Replaces the chevron; it still rotates. `null` hides it. */ icon?: React.ReactNode; } /** The full-width toggle row. Its chevron turns between down and up. */ export function MotionCollapsibleTrigger({ className, children, icon, ...props }: MotionCollapsibleTriggerProps) { const { open, reduceMotion } = useMotionCollapsible(); return ( <CollapsiblePrimitive.Trigger data-slot="collapsible-trigger" className={cn( "group/trigger flex w-full items-center justify-between gap-4 rounded-xl px-4 py-3 text-left text-sm font-medium outline-none", "transition-colors duration-150 hover:text-foreground motion-reduce:transition-none", "focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50", className )} {...props} > {children} {icon !== null && ( <motion.span aria-hidden className={cn( "flex size-6 shrink-0 items-center justify-center rounded-md transition-colors duration-200", open ? "text-foreground" : "text-muted-foreground group-hover/trigger:text-foreground" )} initial={false} animate={{ rotate: open ? 180 : 0 }} transition={reduceMotion ? { duration: 0 } : chevronSpring} > {icon ?? <ChevronDownIcon className="size-4" />} </motion.span> )} </CollapsiblePrimitive.Trigger> ); } const panel = (instant: boolean): Variants => ({ open: { height: "auto", transition: instant ? { duration: 0 } : { type: "spring", bounce: 0, duration: 0.3 }, }, closed: { height: 0, transition: instant ? { duration: 0 } : { type: "spring", bounce: 0, duration: 0.25 }, }, }); const inner = (instant: boolean): Variants => ({ open: { opacity: 1, y: 0, transition: instant ? { duration: 0 } : { duration: 0.2, delay: 0.05, ease: "easeOut" }, }, // Same closed values either way (they're hidden at height 0), so server and client markup match. closed: { opacity: 0, y: -4, transition: { duration: instant ? 0 : 0.14 } }, }); /** The panel. Animates its height and fades its content. `className` styles the inner wrapper. */ export function MotionCollapsibleContent({ className, children, ...props }: Omit<React.ComponentProps<typeof CollapsiblePrimitive.Content>, "forceMount" | "asChild">) { const { open, reduceMotion } = useMotionCollapsible(); const state = open ? "open" : "closed"; return ( <CollapsiblePrimitive.Content forceMount asChild {...props}> <motion.div data-slot="collapsible-content" initial={false} animate={state} variants={panel(reduceMotion)} // Closed content stays mounted for the animation; inert hides it from focus and assistive tech. inert={!open} className="overflow-hidden" > <motion.div variants={inner(reduceMotion)} className={cn("px-4 pb-4 text-sm leading-relaxed text-muted-foreground", className)} > {children} </motion.div> </motion.div> </CollapsiblePrimitive.Content> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| open / defaultOpen / onOpenChange | boolean | false | On MotionCollapsible: controlled or uncontrolled state. |
| bordered | boolean | true | On MotionCollapsible: card outline that brightens while open. |
| disabled | boolean | false | On MotionCollapsible: prevent toggling. |
| icon | ReactNode | null | — | On MotionCollapsibleTrigger: replaces the chevron (still rotates); null hides it. |
| className | string | — | On MotionCollapsibleContent: styles the inner padded wrapper. |
Show More
ShowMore clamps long text to a preview that fades out at the bottom. The height springs to auto and the fade mask dissolves as it opens; the toggle hides itself when the content already fits. The full text is always in the DOM, so it stays indexable.
Why we build at dusk
The best interfaces feel calm. They answer before you ask, move only when movement means something, and get out of the way the rest of the time.
DuskUI started as a set of internal components for a dark-first product. Every piece had to survive a keyboard-only pass, a screen reader pass and a reduced-motion pass before it shipped.
Springs replaced durations, because springs can be interrupted without a jump. Heights animate to their natural size, so nothing below them ever snaps into place.
import { ShowMore } from "@/components/dusk/collapsible/show-more";
export default function ShowMoreDemo() {
return (
<article className="w-full max-w-md space-y-2">
<h3 className="text-base font-semibold">Why we build at dusk</h3>
<ShowMore collapsedHeight={96}>
<p>
The best interfaces feel calm. They answer before you ask, move only when movement means something, and
get out of the way the rest of the time.
</p>
<p className="mt-3">
DuskUI started as a set of internal components for a dark-first product. Every piece had to survive a
keyboard-only pass, a screen reader pass and a reduced-motion pass before it shipped.
</p>
<p className="mt-3">
Springs replaced durations, because springs can be interrupted without a jump. Heights animate to their
natural size, so nothing below them ever snaps into place.
</p>
</ShowMore>
</article>
);
}$ npx shadcn@latest add https://duskui.com/r/show-more.json1. Install the dependencies.
npm install motion lucide-react2. Copy the source into
components/dusk/collapsible/show-more.tsx.show-more.tsx "use client"; import * as React from "react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { ChevronDownIcon } from "lucide-react"; import { cn } from "@/lib/utils"; const heightSpring = { type: "spring", bounce: 0, duration: 0.35 } as const; /** Fully opaque down to `stop`%, then fading to transparent. Same shape both ways so Motion can tween it. */ const fadeMask = (stop: number) => `linear-gradient(to bottom, rgba(0,0,0,1) ${stop}%, rgba(0,0,0,${stop >= 100 ? 1 : 0}) 100%)`; function ShowMoreButton({ expanded, controls, onClick, moreLabel, lessLabel, className, }: { expanded: boolean; controls: string; onClick: () => void; moreLabel: React.ReactNode; lessLabel: React.ReactNode; className?: string; }) { const reduceMotion = useReducedMotion(); return ( <button type="button" aria-expanded={expanded} aria-controls={controls} onClick={onClick} className={cn( "group/more inline-flex items-center gap-1.5 rounded-md text-sm font-medium text-foreground outline-none", "transition-[color,box-shadow] duration-150 hover:text-foreground/80 focus-visible:ring-[3px] focus-visible:ring-ring/50", className )} > <span className="relative grid"> {/* Both labels share one grid cell so the button never changes width. */} <span className={cn("col-start-1 row-start-1 transition-opacity duration-150", expanded && "opacity-0")} aria-hidden={expanded}> {moreLabel} </span> <span className={cn("col-start-1 row-start-1 transition-opacity duration-150", !expanded && "opacity-0")} aria-hidden={!expanded}> {lessLabel} </span> </span> <motion.span aria-hidden className="flex" initial={false} animate={{ rotate: expanded ? 180 : 0 }} transition={reduceMotion ? { duration: 0 } : { type: "spring", bounce: 0, duration: 0.25 }} > <ChevronDownIcon className="size-4" /> </motion.span> </button> ); } export interface ShowMoreProps extends Omit<React.ComponentProps<"div">, "children"> { children: React.ReactNode; /** Height of the clamped preview, in px. */ collapsedHeight?: number; /** Controlled state. */ expanded?: boolean; defaultExpanded?: boolean; onExpandedChange?: (expanded: boolean) => void; moreLabel?: React.ReactNode; lessLabel?: React.ReactNode; /** Classes for the content wrapper (typography, spacing). */ contentClassName?: string; } /** * Clamps long content to a preview that fades out at the bottom, with a * "Show more" toggle. The height springs between the preview and the full * content (Motion animates to `height: auto`, no measuring), and the fade * mask dissolves as it opens. The full text is always in the DOM, so it is * indexed and read by screen readers. The toggle hides itself when the * content already fits. Under reduced motion it snaps open and shut. */ export function ShowMore({ children, collapsedHeight = 120, expanded: expandedProp, defaultExpanded = false, onExpandedChange, moreLabel = "Show more", lessLabel = "Show less", className, contentClassName, ...props }: ShowMoreProps) { const id = React.useId(); const reduceMotion = useReducedMotion(); const contentRef = React.useRef<HTMLDivElement>(null); const [uncontrolled, setUncontrolled] = React.useState(defaultExpanded); // Assume it overflows until measured, so the toggle doesn't pop in after hydration. const [overflows, setOverflows] = React.useState(true); const expanded = expandedProp ?? uncontrolled; React.useEffect(() => { const node = contentRef.current; if (!node) return; const observer = new ResizeObserver(() => { setOverflows(node.scrollHeight > collapsedHeight + 1); }); observer.observe(node); return () => observer.disconnect(); }, [collapsedHeight]); const setExpanded = (next: boolean) => { if (expandedProp === undefined) setUncontrolled(next); onExpandedChange?.(next); }; const clamped = overflows && !expanded; return ( <div className={cn("flex flex-col items-start gap-2", className)} {...props}> <motion.div id={id} className="w-full overflow-hidden" initial={false} animate={{ height: clamped ? collapsedHeight : "auto", maskImage: fadeMask(clamped ? 45 : 100), }} transition={reduceMotion ? { duration: 0 } : { height: heightSpring, default: { duration: 0.35, ease: "easeOut" } }} > <div ref={contentRef} className={cn("text-sm leading-relaxed text-muted-foreground", contentClassName)}> {children} </div> </motion.div> {overflows && ( <ShowMoreButton expanded={expanded} controls={id} onClick={() => setExpanded(!expanded)} moreLabel={moreLabel} lessLabel={lessLabel} /> )} </div> ); } export interface ShowMoreListProps<T> extends Omit<React.ComponentProps<"div">, "children"> { items: T[]; /** Renders one item's contents (inside an `li`). */ renderItem: (item: T, index: number) => React.ReactNode; /** Stable key for an item. Defaults to its index. */ getKey?: (item: T, index: number) => React.Key; /** How many items show before expanding. */ initialCount?: number; expanded?: boolean; defaultExpanded?: boolean; onExpandedChange?: (expanded: boolean) => void; /** Label for the expand button; receives the hidden count. */ moreLabel?: (hidden: number) => React.ReactNode; lessLabel?: React.ReactNode; /** Classes for the `ul`. */ listClassName?: string; /** Classes for every `li`. */ itemClassName?: string; } /** * Shows the first `initialCount` items of a list and reveals the rest with * a staggered spring: each extra row grows in, fades and slides up, and they * fold away in reverse. One semantic `ul` throughout; the toggle reports how * many items are hidden. Under reduced motion rows appear without movement. */ export function ShowMoreList<T>({ items, renderItem, getKey, initialCount = 3, expanded: expandedProp, defaultExpanded = false, onExpandedChange, moreLabel = (hidden) => `Show ${hidden} more`, lessLabel = "Show less", className, listClassName, itemClassName, ...props }: ShowMoreListProps<T>) { const id = React.useId(); const reduceMotion = useReducedMotion(); const [uncontrolled, setUncontrolled] = React.useState(defaultExpanded); const expanded = expandedProp ?? uncontrolled; const hidden = Math.max(0, items.length - initialCount); const visible = expanded ? items : items.slice(0, initialCount); const setExpanded = (next: boolean) => { if (expandedProp === undefined) setUncontrolled(next); onExpandedChange?.(next); }; return ( <div className={cn("flex flex-col items-start gap-2", className)} {...props}> <ul id={id} className={cn("flex w-full flex-col", listClassName)}> <AnimatePresence initial={false}> {visible.map((item, index) => { const extra = index >= initialCount; const order = index - initialCount; return ( <motion.li key={getKey ? getKey(item, index) : index} className={cn("overflow-hidden", itemClassName)} initial={extra ? { height: 0, opacity: 0, y: reduceMotion ? 0 : 8 } : false} animate={{ height: "auto", opacity: 1, y: 0 }} exit={{ height: 0, opacity: 0, y: reduceMotion ? 0 : 4, transition: reduceMotion ? { duration: 0 } : { duration: 0.2, ease: "easeIn", delay: Math.max(0, hidden - 1 - order) * 0.025 }, }} transition={ reduceMotion ? { duration: 0.15 } : { height: { ...heightSpring, delay: order * 0.04 }, default: { duration: 0.3, delay: order * 0.04 + 0.04 } } } > {renderItem(item, index)} </motion.li> ); })} </AnimatePresence> </ul> {hidden > 0 && ( <ShowMoreButton expanded={expanded} controls={id} onClick={() => setExpanded(!expanded)} moreLabel={moreLabel(hidden)} lessLabel={lessLabel} /> )} </div> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| collapsedHeight | number | 120 | Height of the clamped preview, in px. |
| expanded / defaultExpanded / onExpandedChange | boolean | false | Controlled or uncontrolled state. |
| moreLabel / lessLabel | ReactNode | "Show more" / "Show less" | Button text. Both share one grid cell, so the button never resizes. |
| contentClassName | string | — | Classes for the content wrapper (typography, spacing). |
Show More List
ShowMoreList (same file) shows the first items of a list and reveals the rest with a staggered spring — each row grows in, fades and slides up, then folds away in reverse. It stays one semantic ul, and the button says how many items are hidden.
Changelog
- 2.4.0Menubar, Drawer and Chart primitives
- 2.3.2Resizable handles highlight while dragging
- 2.3.1Collapsible content fades as it opens
"use client";
import { ShowMoreList } from "@/components/dusk/collapsible/show-more";
const changes = [
{ version: "2.4.0", note: "Menubar, Drawer and Chart primitives" },
{ version: "2.3.2", note: "Resizable handles highlight while dragging" },
{ version: "2.3.1", note: "Collapsible content fades as it opens" },
{ version: "2.3.0", note: "Sheet closes faster than it opens" },
{ version: "2.2.4", note: "Reduced-motion audit across blocks" },
{ version: "2.2.3", note: "Dropdown sub-menus nudge their chevron" },
{ version: "2.2.0", note: "Dialog gets a blurred backdrop" },
];
export default function ShowMoreListDemo() {
return (
<div className="w-full max-w-sm">
<h3 className="mb-2 text-sm font-semibold">Changelog</h3>
<ShowMoreList
items={changes}
initialCount={3}
getKey={(item) => item.version}
listClassName="divide-y divide-white/5 rounded-xl border"
renderItem={(item) => (
<div className="flex items-center gap-3 px-3 py-2.5 text-sm">
<span className="rounded-md bg-white/[0.06] px-1.5 py-0.5 font-mono text-xs text-white/70">{item.version}</span>
<span className="truncate text-muted-foreground">{item.note}</span>
</div>
)}
/>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/show-more.json1. Install the dependencies.
npm install motion lucide-react2. Copy the source into
components/dusk/collapsible/show-more.tsx.show-more.tsx "use client"; import * as React from "react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { ChevronDownIcon } from "lucide-react"; import { cn } from "@/lib/utils"; const heightSpring = { type: "spring", bounce: 0, duration: 0.35 } as const; /** Fully opaque down to `stop`%, then fading to transparent. Same shape both ways so Motion can tween it. */ const fadeMask = (stop: number) => `linear-gradient(to bottom, rgba(0,0,0,1) ${stop}%, rgba(0,0,0,${stop >= 100 ? 1 : 0}) 100%)`; function ShowMoreButton({ expanded, controls, onClick, moreLabel, lessLabel, className, }: { expanded: boolean; controls: string; onClick: () => void; moreLabel: React.ReactNode; lessLabel: React.ReactNode; className?: string; }) { const reduceMotion = useReducedMotion(); return ( <button type="button" aria-expanded={expanded} aria-controls={controls} onClick={onClick} className={cn( "group/more inline-flex items-center gap-1.5 rounded-md text-sm font-medium text-foreground outline-none", "transition-[color,box-shadow] duration-150 hover:text-foreground/80 focus-visible:ring-[3px] focus-visible:ring-ring/50", className )} > <span className="relative grid"> {/* Both labels share one grid cell so the button never changes width. */} <span className={cn("col-start-1 row-start-1 transition-opacity duration-150", expanded && "opacity-0")} aria-hidden={expanded}> {moreLabel} </span> <span className={cn("col-start-1 row-start-1 transition-opacity duration-150", !expanded && "opacity-0")} aria-hidden={!expanded}> {lessLabel} </span> </span> <motion.span aria-hidden className="flex" initial={false} animate={{ rotate: expanded ? 180 : 0 }} transition={reduceMotion ? { duration: 0 } : { type: "spring", bounce: 0, duration: 0.25 }} > <ChevronDownIcon className="size-4" /> </motion.span> </button> ); } export interface ShowMoreProps extends Omit<React.ComponentProps<"div">, "children"> { children: React.ReactNode; /** Height of the clamped preview, in px. */ collapsedHeight?: number; /** Controlled state. */ expanded?: boolean; defaultExpanded?: boolean; onExpandedChange?: (expanded: boolean) => void; moreLabel?: React.ReactNode; lessLabel?: React.ReactNode; /** Classes for the content wrapper (typography, spacing). */ contentClassName?: string; } /** * Clamps long content to a preview that fades out at the bottom, with a * "Show more" toggle. The height springs between the preview and the full * content (Motion animates to `height: auto`, no measuring), and the fade * mask dissolves as it opens. The full text is always in the DOM, so it is * indexed and read by screen readers. The toggle hides itself when the * content already fits. Under reduced motion it snaps open and shut. */ export function ShowMore({ children, collapsedHeight = 120, expanded: expandedProp, defaultExpanded = false, onExpandedChange, moreLabel = "Show more", lessLabel = "Show less", className, contentClassName, ...props }: ShowMoreProps) { const id = React.useId(); const reduceMotion = useReducedMotion(); const contentRef = React.useRef<HTMLDivElement>(null); const [uncontrolled, setUncontrolled] = React.useState(defaultExpanded); // Assume it overflows until measured, so the toggle doesn't pop in after hydration. const [overflows, setOverflows] = React.useState(true); const expanded = expandedProp ?? uncontrolled; React.useEffect(() => { const node = contentRef.current; if (!node) return; const observer = new ResizeObserver(() => { setOverflows(node.scrollHeight > collapsedHeight + 1); }); observer.observe(node); return () => observer.disconnect(); }, [collapsedHeight]); const setExpanded = (next: boolean) => { if (expandedProp === undefined) setUncontrolled(next); onExpandedChange?.(next); }; const clamped = overflows && !expanded; return ( <div className={cn("flex flex-col items-start gap-2", className)} {...props}> <motion.div id={id} className="w-full overflow-hidden" initial={false} animate={{ height: clamped ? collapsedHeight : "auto", maskImage: fadeMask(clamped ? 45 : 100), }} transition={reduceMotion ? { duration: 0 } : { height: heightSpring, default: { duration: 0.35, ease: "easeOut" } }} > <div ref={contentRef} className={cn("text-sm leading-relaxed text-muted-foreground", contentClassName)}> {children} </div> </motion.div> {overflows && ( <ShowMoreButton expanded={expanded} controls={id} onClick={() => setExpanded(!expanded)} moreLabel={moreLabel} lessLabel={lessLabel} /> )} </div> ); } export interface ShowMoreListProps<T> extends Omit<React.ComponentProps<"div">, "children"> { items: T[]; /** Renders one item's contents (inside an `li`). */ renderItem: (item: T, index: number) => React.ReactNode; /** Stable key for an item. Defaults to its index. */ getKey?: (item: T, index: number) => React.Key; /** How many items show before expanding. */ initialCount?: number; expanded?: boolean; defaultExpanded?: boolean; onExpandedChange?: (expanded: boolean) => void; /** Label for the expand button; receives the hidden count. */ moreLabel?: (hidden: number) => React.ReactNode; lessLabel?: React.ReactNode; /** Classes for the `ul`. */ listClassName?: string; /** Classes for every `li`. */ itemClassName?: string; } /** * Shows the first `initialCount` items of a list and reveals the rest with * a staggered spring: each extra row grows in, fades and slides up, and they * fold away in reverse. One semantic `ul` throughout; the toggle reports how * many items are hidden. Under reduced motion rows appear without movement. */ export function ShowMoreList<T>({ items, renderItem, getKey, initialCount = 3, expanded: expandedProp, defaultExpanded = false, onExpandedChange, moreLabel = (hidden) => `Show ${hidden} more`, lessLabel = "Show less", className, listClassName, itemClassName, ...props }: ShowMoreListProps<T>) { const id = React.useId(); const reduceMotion = useReducedMotion(); const [uncontrolled, setUncontrolled] = React.useState(defaultExpanded); const expanded = expandedProp ?? uncontrolled; const hidden = Math.max(0, items.length - initialCount); const visible = expanded ? items : items.slice(0, initialCount); const setExpanded = (next: boolean) => { if (expandedProp === undefined) setUncontrolled(next); onExpandedChange?.(next); }; return ( <div className={cn("flex flex-col items-start gap-2", className)} {...props}> <ul id={id} className={cn("flex w-full flex-col", listClassName)}> <AnimatePresence initial={false}> {visible.map((item, index) => { const extra = index >= initialCount; const order = index - initialCount; return ( <motion.li key={getKey ? getKey(item, index) : index} className={cn("overflow-hidden", itemClassName)} initial={extra ? { height: 0, opacity: 0, y: reduceMotion ? 0 : 8 } : false} animate={{ height: "auto", opacity: 1, y: 0 }} exit={{ height: 0, opacity: 0, y: reduceMotion ? 0 : 4, transition: reduceMotion ? { duration: 0 } : { duration: 0.2, ease: "easeIn", delay: Math.max(0, hidden - 1 - order) * 0.025 }, }} transition={ reduceMotion ? { duration: 0.15 } : { height: { ...heightSpring, delay: order * 0.04 }, default: { duration: 0.3, delay: order * 0.04 + 0.04 } } } > {renderItem(item, index)} </motion.li> ); })} </AnimatePresence> </ul> {hidden > 0 && ( <ShowMoreButton expanded={expanded} controls={id} onClick={() => setExpanded(!expanded)} moreLabel={moreLabel(hidden)} lessLabel={lessLabel} /> )} </div> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| items / renderItem | T[] / (item: T, index: number) => ReactNode | — | The data and how to render one item inside its li. |
| getKey | (item: T, index: number) => Key | — | Stable key per item. Defaults to the index. |
| initialCount | number | 3 | Items visible before expanding. |
| expanded / defaultExpanded / onExpandedChange | boolean | false | Controlled or uncontrolled state. |
| moreLabel / lessLabel | (hidden: number) => ReactNode / ReactNode | "Show N more" / "Show less" | Button text. |
| listClassName / itemClassName | string | — | Classes for the ul and every li. |
API Reference
Same parts as shadcn/ui: Collapsible, CollapsibleTrigger and CollapsibleContent, each forwarding its Radix Collapsible props. className on CollapsibleContent lands on the animated element, like shadcn.
| Prop | Type | Default | Description |
|---|---|---|---|
| open / defaultOpen / onOpenChange | boolean | — | On Collapsible: controlled or uncontrolled state. |
| disabled | boolean | false | On Collapsible: prevent toggling. |
| asChild | boolean | false | On CollapsibleTrigger: render your own Button. |
| forceMount | true | — | On CollapsibleContent: keep it mounted while closed (for your own animations). |
Accessibility
- Built on Radix Collapsible: the trigger is a button with aria-expanded and aria-controls pointing at the content.
- The Motion block keeps closed content mounted but inert, so it can't be focused or read until opened.
- Show More buttons expose aria-expanded / aria-controls; ShowMoreList's label states how many items are hidden.
- Under prefers-reduced-motion panels open and close instantly, and list rows appear without sliding.