Styled collapsible
Five collapsibles: simple, card, ghost, plus and preview. Each toggles a token content panel with an accessible trigger and is sized.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/collapsible-styledDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/collapsible-styled.tsx"use client"
import * as React from "react"
import { ChevronDown, Plus } from "lucide-react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Collapsible family: 5 decorative single-section collapsibles. Every export is a complete collapsible: one trigger plus one expanding body, a button with aria-expanded, a framer height auto animation. Works controlled (open/onOpenChange) and uncontrolled (defaultOpen). Colour comes ONLY from tokens. Alpha via color-mix. Opening and closing are instant under reduced-motion. Size = padding plus the text scale. Renders without props too. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
interface StyledCollapsibleProps {
className?: string
size?: StyledSize
title?: React.ReactNode
children?: React.ReactNode
open?: boolean
defaultOpen?: boolean
onOpenChange?: (o: boolean) => void
}
const headerPad: Record<StyledSize, string> = {
sm: "px-3 py-2.5 text-sm",
md: "px-4 py-3 text-sm",
lg: "px-5 py-4 text-base",
xl: "px-6 py-5 text-lg",
}
const bodyPad: Record<StyledSize, string> = {
sm: "px-3 pb-2.5 text-sm",
md: "px-4 pb-3 text-sm",
lg: "px-5 pb-4 text-base",
xl: "px-6 pb-5 text-base",
}
const iconSize: Record<StyledSize, string> = {
sm: "size-4",
md: "size-4",
lg: "size-5",
xl: "size-5",
}
const hoverTint =
"hover:bg-[color-mix(in_oklab,var(--color-foreground)_5%,transparent)]"
const headerBase =
"flex w-full items-center justify-between gap-3 text-left font-medium outline-none transition-colors focus-visible:ring-[3px] focus-visible:ring-ring/50"
const defaultTitle = "What is included?"
const defaultChildren =
"Every plan ships the full component library, semantic tokens, and lifetime updates for the tier you own. Add any component with a single CLI command and keep the files fully editable."
/* Controlled and uncontrolled open state in a single hook. */
function useControllableOpen({
open,
defaultOpen,
onOpenChange,
}: {
open?: boolean
defaultOpen?: boolean
onOpenChange?: (o: boolean) => void
}) {
const isControlled = open !== undefined
const [internal, setInternal] = React.useState<boolean>(defaultOpen ?? false)
const current = isControlled ? (open as boolean) : internal
const toggle = React.useCallback(() => {
const next = !current
if (!isControlled) setInternal(next)
onOpenChange?.(next)
}, [current, isControlled, onOpenChange])
return { isOpen: current, toggle }
}
function Panel({
isOpen,
size,
children,
}: {
isOpen: boolean
size: StyledSize
children: React.ReactNode
}) {
const reduce = useReducedMotion()
return (
<AnimatePresence initial={false}>
{isOpen && (
<motion.div
key="panel"
initial={reduce ? false : { height: 0, opacity: 0 }}
animate={{ height: "auto", opacity: 1 }}
exit={reduce ? { height: 0, opacity: 0 } : { height: 0, opacity: 0 }}
transition={
reduce ? { duration: 0 } : { duration: 0.25, ease: [0.4, 0, 0.2, 1] }
}
className="overflow-hidden"
>
<div className={cn(bodyPad[size], "text-muted-foreground")}>
{children}
</div>
</motion.div>
)}
</AnimatePresence>
)
}
function Chevron({ isOpen, size }: { isOpen: boolean; size: StyledSize }) {
const reduce = useReducedMotion()
return (
<motion.span
aria-hidden
className="shrink-0 text-muted-foreground"
animate={{ rotate: isOpen ? 180 : 0 }}
transition={reduce ? { duration: 0 } : { duration: 0.2 }}
>
<ChevronDown className={iconSize[size]} />
</motion.span>
)
}
/* Arti ikonu: acikken 45 derece doner ve x'e donusur. */
function PlusMark({ isOpen, size }: { isOpen: boolean; size: StyledSize }) {
const reduce = useReducedMotion()
return (
<motion.span
aria-hidden
className="shrink-0 text-muted-foreground"
animate={{ rotate: isOpen ? 45 : 0 }}
transition={reduce ? { duration: 0 } : { duration: 0.2 }}
>
<Plus className={iconSize[size]} />
</motion.span>
)
}
/* Simple: temiz cerceveli tek bolum, chevron doner. */
export function SimpleCollapsible({
className,
size = "md",
title = defaultTitle,
children = defaultChildren,
open,
defaultOpen,
onOpenChange,
}: StyledCollapsibleProps) {
const { isOpen, toggle } = useControllableOpen({ open, defaultOpen, onOpenChange })
return (
<div
data-slot="styled-collapsible"
className={cn(
"overflow-hidden rounded-xl border border-border bg-background",
className
)}
>
<button
type="button"
aria-expanded={isOpen}
onClick={toggle}
className={cn(headerBase, headerPad[size], hoverTint)}
>
{title}
<Chevron isOpen={isOpen} size={size} />
</button>
<Panel isOpen={isOpen} size={size}>
{children}
</Panel>
</div>
)
}
/* Card: token kart; baslik satiri + genisleyen govde, acikken hafif ayirici. */
export function CardCollapsible({
className,
size = "md",
title = defaultTitle,
children = defaultChildren,
open,
defaultOpen,
onOpenChange,
}: StyledCollapsibleProps) {
const { isOpen, toggle } = useControllableOpen({ open, defaultOpen, onOpenChange })
return (
<div
data-slot="styled-collapsible"
className={cn(
"overflow-hidden rounded-xl border border-border bg-surface-2 shadow-sm",
className
)}
>
<button
type="button"
aria-expanded={isOpen}
onClick={toggle}
className={cn(
headerBase,
headerPad[size],
"hover:bg-[color-mix(in_oklab,var(--color-foreground)_4%,transparent)]"
)}
>
{title}
<Chevron isOpen={isOpen} size={size} />
</button>
<div
className={cn(
"border-t border-border transition-opacity",
isOpen ? "opacity-100" : "opacity-0"
)}
aria-hidden={!isOpen}
/>
<Panel isOpen={isOpen} size={size}>
{children}
</Panel>
</div>
)
}
/* Ghost: no frame, minimal; only a chevron + a hover token tint. */
export function GhostCollapsible({
className,
size = "md",
title = defaultTitle,
children = defaultChildren,
open,
defaultOpen,
onOpenChange,
}: StyledCollapsibleProps) {
const { isOpen, toggle } = useControllableOpen({ open, defaultOpen, onOpenChange })
return (
<div data-slot="styled-collapsible" className={cn("flex flex-col", className)}>
<button
type="button"
aria-expanded={isOpen}
onClick={toggle}
className={cn(headerBase, headerPad[size], "rounded-lg", hoverTint)}
>
{title}
<Chevron isOpen={isOpen} size={size} />
</button>
<Panel isOpen={isOpen} size={size}>
{children}
</Panel>
</div>
)
}
/* Plus: token arti ikonu, acikken doner ve x'e donusur. */
export function PlusCollapsible({
className,
size = "md",
title = defaultTitle,
children = defaultChildren,
open,
defaultOpen,
onOpenChange,
}: StyledCollapsibleProps) {
const { isOpen, toggle } = useControllableOpen({ open, defaultOpen, onOpenChange })
return (
<div
data-slot="styled-collapsible"
className={cn(
"overflow-hidden rounded-xl border border-border bg-background",
className
)}
>
<button
type="button"
aria-expanded={isOpen}
onClick={toggle}
className={cn(headerBase, headerPad[size], hoverTint)}
>
{title}
<PlusMark isOpen={isOpen} size={size} />
</button>
<Panel isOpen={isOpen} size={size}>
{children}
</Panel>
</div>
)
}
/* Preview: it always shows a single-line truncated preview, "Show more" opens the
full content and "Show less" closes it. */
export function PreviewCollapsible({
className,
size = "md",
title = defaultTitle,
children = defaultChildren,
open,
defaultOpen,
onOpenChange,
}: StyledCollapsibleProps) {
const { isOpen, toggle } = useControllableOpen({ open, defaultOpen, onOpenChange })
return (
<div
data-slot="styled-collapsible"
className={cn(
"overflow-hidden rounded-xl border border-border bg-background",
className
)}
>
<div className={cn(headerPad[size], "font-medium")}>{title}</div>
{!isOpen && (
<div className={cn(bodyPad[size], "pt-0 truncate text-muted-foreground")}>
{children}
</div>
)}
<Panel isOpen={isOpen} size={size}>
{children}
</Panel>
<div className={cn(bodyPad[size], "pt-0")}>
<button
type="button"
aria-expanded={isOpen}
onClick={toggle}
className={cn(
"inline-flex items-center gap-1 rounded-md font-medium text-foreground outline-none transition-colors focus-visible:ring-[3px] focus-visible:ring-ring/50 hover:text-muted-foreground"
)}
>
{isOpen ? "Show less" : "Show more"}
<Chevron isOpen={isOpen} size={size} />
</button>
</div>
</div>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Simple
A chevron trigger over a token content panel.
import { SimpleCollapsible } from "@/components/ui/collapsible-styled"
<SimpleCollapsible />Card
The collapsible sits inside a bordered token card.
import { CardCollapsible } from "@/components/ui/collapsible-styled"
<CardCollapsible />Ghost
Borderless trigger with a soft-token hover.
import { GhostCollapsible } from "@/components/ui/collapsible-styled"
<GhostCollapsible />Plus
A plus icon that rotates into a minus when open.
import { PlusCollapsible } from "@/components/ui/collapsible-styled"
<PlusCollapsible />Preview
Shows a peek of the content, expands to reveal the rest.
import { PreviewCollapsible } from "@/components/ui/collapsible-styled"
<PreviewCollapsible />ai2 Styled collapsible: 5 styled variations on the token system
The ai2 Styled collapsible are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around expand and collapse disclosures. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion animates the panel height open and closed. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the panel shows and hides instantly with no height animation.
What is in the ai2 Styled collapsible?
5 exports in one file: Simple, Card, Ghost, Plus and Preview. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion animates the panel height open and closed.
- Reduced-motion aware: Under prefers-reduced-motion, the panel shows and hides instantly with no height animation.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Styled collapsible sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.