Motion selects
Five selects that keep one listbox anatomy and vary only the open and close motion: a slide, a pop, a plain fade, a loose spring and a vertical morph. Each is self-contained (no radix, no native select), sized, token-driven, and closes on outside click or Escape.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/select-motionDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/select-motion.tsx"use client"
import * as React from "react"
import { Check, ChevronDown } from "lucide-react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Motion select family: 5 self-contained single-selects. They all share the same
listbox anatomy; the only difference is the option panel's ENTER/EXIT motion
(slide, pop, fade, spring, morph). NO radix, NO native <select>, NO portal.
The panel pins to its own trigger (a relative wrapper + an absolute panel), so 25
examples side by side on a docs page are all positioned correctly. Keyboard:
ArrowUp/Down moves the highlight, Enter selects, Escape closes and returns focus
to the trigger. Color comes ONLY from tokens, via alpha color-mix. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const triggerHeight: Record<StyledSize, string> = {
sm: "h-8 text-sm",
md: "h-9 text-sm",
lg: "h-10 text-base",
xl: "h-12 text-base",
}
const panelWidth: Record<StyledSize, string> = {
sm: "w-48",
md: "w-56",
lg: "w-64",
xl: "w-72",
}
export interface StyledSelectOption {
value: string
label: string
}
export interface StyledSelectProps {
className?: string
size?: StyledSize
placeholder?: string
options?: StyledSelectOption[]
value?: string
defaultValue?: string
onValueChange?: (v: string) => void
}
const defaultOptions: StyledSelectOption[] = [
{ value: "light", label: "Light" },
{ value: "dark", label: "Dark" },
{ value: "system", label: "System" },
{ value: "auto", label: "Automatic" },
]
const triggerBase =
"inline-flex w-full shrink-0 select-none items-center justify-between gap-2 whitespace-nowrap rounded-lg border border-field-border bg-background px-3 font-medium text-foreground outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
const panelBase =
"absolute left-0 top-full z-50 mt-1 max-h-72 min-w-48 overflow-auto rounded-xl border border-border bg-popover p-1.5 text-sm text-popover-foreground shadow-lg outline-none"
const optionBase =
"flex w-full cursor-default select-none items-center gap-2.5 rounded-md px-3 py-2 text-left text-sm text-popover-foreground outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-selected:bg-accent aria-selected:text-accent-foreground [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
interface PanelMotion {
initial: Record<string, number>
animate: Record<string, number>
exit: Record<string, number>
transition: Record<string, unknown>
origin: string
}
/* Open state plus selected value plus highlight index. The state lives in the ROOT component, because the panel unmounts when it closes. */
function useSelectState(
list: StyledSelectOption[],
value: string | undefined,
defaultValue: string | undefined,
onValueChange: ((v: string) => void) | undefined,
) {
const [open, setOpen] = React.useState(false)
const [active, setActive] = React.useState(0)
const [internal, setInternal] = React.useState<string | undefined>(defaultValue)
const wrapperRef = React.useRef<HTMLDivElement>(null)
const triggerRef = React.useRef<HTMLButtonElement>(null)
const isControlled = value !== undefined
const current = isControlled ? value : internal
const close = React.useCallback(() => {
setOpen(false)
triggerRef.current?.focus()
}, [])
const select = React.useCallback(
(next: string) => {
if (!isControlled) setInternal(next)
onValueChange?.(next)
setOpen(false)
triggerRef.current?.focus()
},
[isControlled, onValueChange],
)
React.useEffect(() => {
if (!open) return
const onPointer = (e: PointerEvent) => {
const node = wrapperRef.current
if (node && e.target instanceof Node && !node.contains(e.target)) setOpen(false)
}
window.addEventListener("pointerdown", onPointer)
return () => window.removeEventListener("pointerdown", onPointer)
}, [open])
const indexOfCurrent = React.useCallback(() => {
const i = list.findIndex((o) => o.value === current)
return i < 0 ? 0 : i
}, [list, current])
const onKeyDown = React.useCallback(
(e: React.KeyboardEvent) => {
if (e.key === "Escape") {
if (!open) return
e.preventDefault()
close()
return
}
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
e.preventDefault()
if (!open) {
setOpen(true)
setActive(indexOfCurrent())
return
}
const dir = e.key === "ArrowDown" ? 1 : -1
setActive((i) => (i + dir + list.length) % list.length)
return
}
if (e.key === "Enter" || e.key === " ") {
e.preventDefault()
if (!open) {
setOpen(true)
setActive(indexOfCurrent())
return
}
const opt = list[active]
if (opt) select(opt.value)
}
},
[open, close, indexOfCurrent, list, active, select],
)
return { open, setOpen, active, setActive, current, select, close, onKeyDown, wrapperRef, triggerRef }
}
function BaseMotionSelect({
className,
size = "md",
placeholder = "Select...",
options,
value,
defaultValue,
onValueChange,
panelMotion,
}: StyledSelectProps & { panelMotion: PanelMotion }) {
const list = options ?? defaultOptions
const reduce = useReducedMotion()
const uid = React.useId()
const listId = `${uid}-listbox`
const optionId = (i: number) => `${uid}-option-${i}`
const { open, setOpen, active, setActive, current, select, onKeyDown, wrapperRef, triggerRef } =
useSelectState(list, value, defaultValue, onValueChange)
const selectedLabel = list.find((o) => o.value === current)?.label
const fade = {
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
}
const anim = reduce ? fade : panelMotion
return (
<div
ref={wrapperRef}
data-slot="styled-select"
className={cn("relative inline-flex w-56 max-w-full", panelWidth[size], className)}
>
<button
ref={triggerRef}
type="button"
data-slot="styled-select-trigger"
role="combobox"
aria-expanded={open}
aria-haspopup="listbox"
aria-controls={listId}
aria-activedescendant={open ? optionId(active) : undefined}
className={cn(triggerBase, triggerHeight[size])}
onClick={() => {
setActive(Math.max(0, list.findIndex((o) => o.value === current)))
setOpen(!open)
}}
onKeyDown={onKeyDown}
>
<span className={cn("truncate", selectedLabel === undefined && "text-muted-foreground")}>
{selectedLabel ?? placeholder}
</span>
<ChevronDown
className={cn("shrink-0 opacity-70 transition-transform duration-200", open && "rotate-180")}
/>
</button>
<AnimatePresence>
{open ? (
<motion.div
id={listId}
role="listbox"
data-slot="styled-select-content"
className={cn(panelBase, panelWidth[size], !reduce && panelMotion.origin)}
initial={anim.initial}
animate={anim.animate}
exit={anim.exit}
transition={reduce ? { duration: 0.14 } : panelMotion.transition}
>
<div className="flex flex-col">
{list.map((option, i) => {
const isSelected = option.value === current
return (
<button
key={option.value}
id={optionId(i)}
type="button"
role="option"
tabIndex={-1}
aria-selected={isSelected}
data-active={i === active ? "" : undefined}
className={cn(
optionBase,
i === active && "bg-accent text-accent-foreground",
)}
onPointerEnter={() => setActive(i)}
onClick={() => select(option.value)}
>
<span className="flex size-4 shrink-0 items-center justify-center text-primary">
{isSelected ? <Check /> : null}
</span>
<span className="truncate">{option.label}</span>
</button>
)
})}
</div>
</motion.div>
) : null}
</AnimatePresence>
</div>
)
}
/* Slide: the panel slides downwards from under the trigger (tween). */
export function SlideSelect(props: StyledSelectProps) {
return (
<BaseMotionSelect
{...props}
panelMotion={{
origin: "origin-top",
initial: { opacity: 0, y: -10 },
animate: { opacity: 1, y: 0 },
exit: { opacity: 0, y: -10 },
transition: { duration: 0.18, ease: "easeOut" },
}}
/>
)
}
/* Pop: it opens by bursting from small (a stiff spring, origin-top). */
export function PopSelect(props: StyledSelectProps) {
return (
<BaseMotionSelect
{...props}
panelMotion={{
origin: "origin-top",
initial: { opacity: 0, scale: 0.85 },
animate: { opacity: 1, scale: 1 },
exit: { opacity: 0, scale: 0.85 },
transition: { type: "spring" as const, stiffness: 520, damping: 24 },
}}
/>
)
}
/* Fade: no movement, opacity only. */
export function FadeSelect(props: StyledSelectProps) {
return (
<BaseMotionSelect
{...props}
panelMotion={{
origin: "origin-top",
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: { duration: 0.2, ease: "easeOut" },
}}
/>
)
}
/* Spring: dusuk damping ile yayli, hafif zipla. */
export function SpringSelect(props: StyledSelectProps) {
return (
<BaseMotionSelect
{...props}
panelMotion={{
origin: "origin-top",
initial: { opacity: 0, y: -14, scale: 0.96 },
animate: { opacity: 1, y: 0, scale: 1 },
exit: { opacity: 0, y: -14, scale: 0.96 },
transition: { type: "spring" as const, stiffness: 320, damping: 14 },
}}
/>
)
}
/* Morph: it opens out of a vertical squash (a scaleY morph, origin-top). */
export function MorphSelect(props: StyledSelectProps) {
return (
<BaseMotionSelect
{...props}
panelMotion={{
origin: "origin-top",
initial: { opacity: 0, scaleY: 0.55, scaleX: 0.94 },
animate: { opacity: 1, scaleY: 1, scaleX: 1 },
exit: { opacity: 0, scaleY: 0.55, scaleX: 0.94 },
transition: { duration: 0.24, ease: "easeOut" },
}}
/>
)
}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.
Slide
The panel slides down from the trigger.
import { SlideSelect } from "@/components/ui/select-motion"
<SlideSelect />Pop
The panel pops open with a snappy spring.
import { PopSelect } from "@/components/ui/select-motion"
<PopSelect />Fade
The panel fades in with no movement.
import { FadeSelect } from "@/components/ui/select-motion"
<FadeSelect />Spring
The panel settles with a loose, bouncy spring.
import { SpringSelect } from "@/components/ui/select-motion"
<SpringSelect />Morph
The panel unfolds from a squashed state.
import { MorphSelect } from "@/components/ui/select-motion"
<MorphSelect />ai2 Motion selects: 5 styled variations on the token system
The ai2 Motion selects are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around single-choice selects that differ in panel motion. 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 drives the panel enter and exit from the trigger. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the transforms are skipped and the panel fades instantly.
What is in the ai2 Motion selects?
5 exports in one file: Slide, Pop, Fade, Spring and Morph. 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 drives the panel enter and exit from the trigger.
- Reduced-motion aware: Under prefers-reduced-motion, the transforms are skipped and the panel fades instantly.
- 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 Motion selects 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.