Underline collapsibles
Five minimal collapsibles whose trigger grows an animated underline as it opens: sweep, center, grow, double and dashed. 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-underlineDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/collapsible-underline.tsx"use client"
import * as React from "react"
import { ChevronDown } from "lucide-react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Underline collapsible family: 5 minimal collapsibles with an animated underline
that glides beneath the trigger while open. Each export is a complete
collapsible: a single trigger + a single expanding body, a button with
aria-expanded, and a framer height auto animation. Works controlled
(open/onOpenChange) and uncontrolled (defaultOpen). The difference is on the
UNDERLINE: how it appears changes (Sweep, Center, Grow, Double, Dashed). Color
comes ONLY from tokens. Alpha via color-mix. No transform under reduced-motion.
Size = padding + text scale. */
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-1 py-2.5 text-sm",
md: "px-1 py-3 text-sm",
lg: "px-1 py-4 text-base",
xl: "px-1 py-5 text-lg",
}
const bodyPad: Record<StyledSize, string> = {
sm: "px-1 pb-2.5 pt-2 text-sm",
md: "px-1 pb-3 pt-2 text-sm",
lg: "px-1 pb-4 pt-3 text-base",
xl: "px-1 pb-5 pt-3 text-base",
}
const iconSize: Record<StyledSize, string> = {
sm: "size-4",
md: "size-4",
lg: "size-5",
xl: "size-5",
}
const headerBase =
"relative 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 Chevron({ isOpen, size }: { isOpen: boolean; size: StyledSize }) {
const reduce = useReducedMotion()
return (
<motion.span
aria-hidden
className="shrink-0 text-muted-foreground"
animate={reduce ? undefined : { rotate: isOpen ? 180 : 0 }}
transition={reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 300, damping: 24 }}
>
<ChevronDown className={iconSize[size]} />
</motion.span>
)
}
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={{ 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>
)
}
/* Shared shell: a minimal trigger with a static track underneath and a primary line sweeping in when open. lineClass and origin carry each variant's animation technique. */
function UnderlineShell({
props,
origin,
lineClass,
secondLine,
grow,
}: {
props: StyledCollapsibleProps
origin: string
lineClass: string
secondLine?: boolean
grow?: boolean
}) {
const { className, size = "md", title = defaultTitle, children = defaultChildren } = props
const { isOpen, toggle } = useControllableOpen(props)
const reduce = useReducedMotion()
const target = reduce
? { scaleX: isOpen ? 1 : 0, opacity: isOpen ? 1 : 0 }
: grow
? { scaleX: isOpen ? 1 : 0, scaleY: isOpen ? 1 : 0.4, opacity: isOpen ? 1 : 0 }
: { scaleX: isOpen ? 1 : 0, opacity: isOpen ? 1 : 0 }
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])}
>
{title}
<Chevron isOpen={isOpen} size={size} />
<span
aria-hidden
className="pointer-events-none absolute inset-x-0 bottom-0 h-px bg-[color-mix(in_oklab,var(--color-foreground)_14%,transparent)]"
/>
<motion.span
aria-hidden
className={cn("pointer-events-none absolute inset-x-0 bottom-0", origin, lineClass)}
initial={false}
animate={target}
transition={reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 240, damping: 28 }}
/>
{secondLine ? (
<motion.span
aria-hidden
className={cn(
"pointer-events-none absolute inset-x-0 bottom-1 h-px",
origin,
"bg-[color-mix(in_oklab,var(--color-primary)_45%,transparent)]"
)}
initial={false}
animate={{ scaleX: isOpen ? 1 : 0, opacity: isOpen ? 1 : 0 }}
transition={reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 240, damping: 28 }}
/>
) : null}
</button>
<Panel isOpen={isOpen} size={size}>
{children}
</Panel>
</div>
)
}
/* Sweep: a primary line sweeping in from the left. */
export function SweepCollapsible(props: StyledCollapsibleProps) {
return (
<UnderlineShell props={props} origin="origin-left" lineClass="h-0.5 bg-primary" />
)
}
/* Center: a line opening outwards from the centre. */
export function CenterCollapsible(props: StyledCollapsibleProps) {
return (
<UnderlineShell props={props} origin="origin-center" lineClass="h-0.5 bg-primary" />
)
}
/* Grow: a line thickening as it opens from the left. */
export function GrowCollapsible(props: StyledCollapsibleProps) {
return (
<UnderlineShell props={props} origin="origin-left" lineClass="h-1 rounded-full bg-primary" grow />
)
}
/* Double: a two-layer double line. */
export function DoubleCollapsible(props: StyledCollapsibleProps) {
return (
<UnderlineShell props={props} origin="origin-left" lineClass="h-0.5 bg-primary" secondLine />
)
}
/* Dashed: a dashed primary line. */
export function DashedCollapsible(props: StyledCollapsibleProps) {
return (
<UnderlineShell
props={props}
origin="origin-left"
lineClass="h-0 border-b-2 border-dashed border-primary"
/>
)
}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.
Sweep
A primary line sweeps left to right when open.
import { SweepCollapsible } from "@/components/ui/collapsible-underline"
<SweepCollapsible />Center
The line opens outward from the center.
import { CenterCollapsible } from "@/components/ui/collapsible-underline"
<CenterCollapsible />Grow
The line thickens as it sweeps in from the left.
import { GrowCollapsible } from "@/components/ui/collapsible-underline"
<GrowCollapsible />Double
Two stacked lines sweep in together.
import { DoubleCollapsible } from "@/components/ui/collapsible-underline"
<DoubleCollapsible />Dashed
A dashed primary line sweeps in on open.
import { DashedCollapsible } from "@/components/ui/collapsible-underline"
<DashedCollapsible />ai2 Underline collapsibles: 5 styled variations on the token system
The ai2 Underline collapsibles are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around minimal disclosures with an animated sweeping underline. 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 springs the underline in and animates the panel height. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the underline and panel change state instantly with no transforms.
What is in the ai2 Underline collapsibles?
5 exports in one file: Sweep, Center, Grow, Double and Dashed. 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 springs the underline in and animates the panel height.
- Reduced-motion aware: Under prefers-reduced-motion, the underline and panel change state instantly with no transforms.
- 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 Underline collapsibles 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.