Rich selects
Five selects with richer option rows: an initials avatar, a description line, a trailing badge, an inline SVG media tile and a split label and meta column. Every surface is painted with tokens and every glyph is inline SVG, so no remote image is fetched. Each is self-contained (no radix, no native select), sized, 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-richDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/select-rich.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"
/* Rich select family: 5 self-contained single-selects whose option rows are richer
than plain text (an initial-based avatar, a description line, a badge, an inline
SVG media square, meta on the right). There are NO remote image URLs: every
surface is painted with tokens and the glyphs are inline SVG. NO radix, NO portal.
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-56",
md: "w-64",
lg: "w-72",
xl: "w-80",
}
export interface StyledRichOption {
value: string
label: string
/** The second description line (the description variant). */
description?: string
/** Sag tarafta kucuk rozet metni (badge varyanti). */
badge?: string
/** The initials in the avatar square; derived from the label when not given. */
initials?: string
/** Sag tarafta ikincil meta metni (split varyanti). */
meta?: string
}
export interface StyledSelectProps {
className?: string
size?: StyledSize
placeholder?: string
options?: StyledRichOption[]
value?: string
defaultValue?: string
onValueChange?: (v: string) => void
}
const defaultOptions: StyledRichOption[] = [
{ value: "ada", label: "Ada Lovelace", description: "Owner", badge: "Admin", meta: "Owner", initials: "AL" },
{ value: "grace", label: "Grace Hopper", description: "Can edit and publish", badge: "Editor", meta: "Editor", initials: "GH" },
{ value: "alan", label: "Alan Turing", description: "Can comment on drafts", badge: "Viewer", meta: "Viewer", initials: "AT" },
{ value: "katherine", label: "Katherine Johnson", description: "Read-only access", badge: "Guest", meta: "Guest", initials: "KJ" },
]
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-80 min-w-56 origin-top 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-3 rounded-md px-2.5 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"
const badgeBase =
"ml-auto inline-flex shrink-0 items-center rounded-full border border-[color-mix(in_oklab,var(--color-border)_80%,transparent)] bg-secondary px-2 py-0.5 text-xs font-medium text-secondary-foreground"
const panelMotion = {
initial: { opacity: 0, scale: 0.96, y: -6 },
animate: { opacity: 1, scale: 1, y: 0 },
exit: { opacity: 0, scale: 0.96, y: -6 },
transition: { type: "spring" as const, stiffness: 340, damping: 26 },
}
/* Deterministic initials from the label (nothing random). */
function initialsOf(option: StyledRichOption) {
if (option.initials) return option.initials
return option.label
.split(" ")
.filter(Boolean)
.slice(0, 2)
.map((w) => w[0]?.toUpperCase() ?? "")
.join("")
}
/* A token-coloured inline SVG media square. No remote URLs. */
function MediaTile({ seed }: { seed: number }) {
const bars = [6, 11, 8, 13]
return (
<span
aria-hidden="true"
className="flex size-8 shrink-0 items-center justify-center rounded-md border border-[color-mix(in_oklab,var(--color-border)_70%,transparent)] bg-[color-mix(in_oklab,var(--color-primary)_12%,transparent)] text-primary"
>
<svg viewBox="0 0 20 20" className="size-4" fill="none" aria-hidden="true">
{bars.map((h, i) => (
<rect
key={i}
x={2 + i * 4.5}
y={17 - ((h + seed * 2) % 12) - 3}
width="3"
height={((h + seed * 2) % 12) + 3}
rx="1"
fill="currentColor"
/>
))}
</svg>
</span>
)
}
type RichKind = "avatar" | "description" | "badge" | "media" | "split"
function BaseRichSelect({
className,
size = "md",
placeholder = "Select...",
options,
value,
defaultValue,
onValueChange,
kind,
}: StyledSelectProps & { kind: RichKind }) {
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] = 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 selected = list.find((o) => o.value === current)
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 = () => {
const i = list.findIndex((o) => o.value === current)
return i < 0 ? 0 : i
}
const onKeyDown = (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)
}
}
const fade = { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 } }
const anim = reduce ? fade : panelMotion
const triggerContent = (
<span className="flex min-w-0 items-center gap-2">
{kind === "avatar" && selected ? (
<span
aria-hidden="true"
className="flex size-5 shrink-0 items-center justify-center rounded-full bg-[color-mix(in_oklab,var(--color-primary)_18%,transparent)] text-[10px] font-semibold text-primary"
>
{initialsOf(selected)}
</span>
) : null}
<span className={cn("truncate", selected === undefined && "text-muted-foreground")}>
{selected?.label ?? placeholder}
</span>
{kind === "split" && selected?.meta ? (
<span className="shrink-0 text-xs text-muted-foreground">{selected.meta}</span>
) : null}
</span>
)
return (
<div
ref={wrapperRef}
data-slot="styled-select"
className={cn("relative inline-flex w-64 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(indexOfCurrent())
setOpen(!open)
}}
onKeyDown={onKeyDown}
>
{triggerContent}
<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])}
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}
className={cn(optionBase, i === active && "bg-accent text-accent-foreground")}
onPointerEnter={() => setActive(i)}
onClick={() => select(option.value)}
>
{kind === "avatar" ? (
<span
aria-hidden="true"
className="flex size-8 shrink-0 items-center justify-center rounded-full bg-[color-mix(in_oklab,var(--color-primary)_18%,transparent)] text-xs font-semibold text-primary"
>
{initialsOf(option)}
</span>
) : null}
{kind === "media" ? <MediaTile seed={i} /> : null}
<span className="flex min-w-0 flex-1 flex-col">
<span className="truncate font-medium">{option.label}</span>
{(kind === "description" || kind === "avatar" || kind === "media") &&
option.description ? (
<span className="truncate text-xs text-muted-foreground">
{option.description}
</span>
) : null}
</span>
{kind === "badge" && option.badge ? (
<span className={badgeBase}>{option.badge}</span>
) : null}
{kind === "split" && option.meta ? (
<span className="ml-auto shrink-0 text-xs text-muted-foreground">
{option.meta}
</span>
) : null}
<span className="flex size-4 shrink-0 items-center justify-center text-primary">
{isSelected ? <Check /> : null}
</span>
</button>
)
})}
</div>
</motion.div>
) : null}
</AnimatePresence>
</div>
)
}
/* Avatar: her satirda token yuzeyli, bas harfli yuvarlak avatar. */
export function AvatarSelect(props: StyledSelectProps) {
return <BaseRichSelect {...props} kind="avatar" />
}
/* Description: a second description line under the label. */
export function DescriptionSelect(props: StyledSelectProps) {
return <BaseRichSelect {...props} kind="description" />
}
/* Badge: satirin sagina rol/durum rozeti dusar. */
export function BadgeSelect(props: StyledSelectProps) {
return <BaseRichSelect {...props} kind="badge" />
}
/* Media: a media square carrying an inline SVG glyph at the start of the row. */
export function MediaSelect(props: StyledSelectProps) {
return <BaseRichSelect {...props} kind="media" />
}
/* Split: label solda, meta metni sagda; trigger da bolunmus okunur. */
export function SplitSelect(props: StyledSelectProps) {
return <BaseRichSelect {...props} kind="split" />
}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.
Avatar
Rows lead with an initials avatar on a token surface.
import { AvatarSelect } from "@/components/ui/select-rich"
<AvatarSelect />Description
A second line explains each option.
import { DescriptionSelect } from "@/components/ui/select-rich"
<DescriptionSelect />Badge
A role badge sits at the end of each row.
import { BadgeSelect } from "@/components/ui/select-rich"
<BadgeSelect />Media
Rows lead with an inline SVG media tile.
import { MediaSelect } from "@/components/ui/select-rich"
<MediaSelect />Split
Label on the left, meta text on the right.
import { SplitSelect } from "@/components/ui/select-rich"
<SplitSelect />ai2 Rich selects: 5 styled variations on the token system
The ai2 Rich selects are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around single-choice selects with richer option rows. 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 fades and scales the panel in 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 scale is skipped and the panel appears instantly.
What is in the ai2 Rich selects?
5 exports in one file: Avatar, Description, Badge, Media and Split. 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 fades and scales the panel in from the trigger.
- Reduced-motion aware: Under prefers-reduced-motion, the scale is skipped and the panel appears 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 Rich 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.