Skip to content
Shaken UI
Sign in

Select

Save

A styled Base UI Select with an Input-like trigger, groups, separators, item icons, and a check on the selected item.

Category
Forms
Version
0.1.0
Frameworks
Next.js, Vite + React
Styling
Tailwind CSS v4

Preview

Select preview
Model
Data region

Installation

npx @shakenui/cli add select

Review the plan first. A dry run lists every file and package without changing anything:

npx @shakenui/cli add select --dry-run

Usage

select-example.tsx
"use client";

import {
  Select,
  SelectContent,
  SelectGroup,
  SelectGroupLabel,
  SelectItem,
  SelectLabel,
  SelectSeparator,
  SelectTrigger,
} from "@/components/shaken/ui/select";

const models = [
  { value: "atlas-large", label: "Atlas Large" },
  { value: "atlas-small", label: "Atlas Small" },
  { value: "orbit-code", label: "Orbit Code" },
  { value: "orbit-vision", label: "Orbit Vision" },
  { value: "local-llama", label: "Local model" },
];

const regions = [
  { value: "eu-central", label: "Frankfurt (eu-central)" },
  { value: "us-east", label: "Virginia (us-east)" },
  { value: "ap-south", label: "Mumbai (ap-south)" },
];

function SparkIcon() {
  return (
    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round">
      <path d="M8 2l1.4 4.6L14 8l-4.6 1.4L8 14l-1.4-4.6L2 8l4.6-1.4L8 2z" />
    </svg>
  );
}

function CodeIcon() {
  return (
    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M5.5 4.5L2 8l3.5 3.5M10.5 4.5L14 8l-3.5 3.5" />
    </svg>
  );
}

function EyeIcon() {
  return (
    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M1.75 8S4 3.75 8 3.75 14.25 8 14.25 8 12 12.25 8 12.25 1.75 8 1.75 8z" />
      <circle cx="8" cy="8" r="2" />
    </svg>
  );
}

function ChipIcon() {
  return (
    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5">
      <rect x="3" y="3" width="10" height="10" rx="2" />
      <rect x="6" y="6" width="4" height="4" rx="0.5" />
    </svg>
  );
}

export default function SelectExample() {
  return (
    <div className="grid w-full max-w-xs gap-6">
      <Select items={models} defaultValue="atlas-large" name="model">
        <div className="grid gap-1.5">
          <SelectLabel>Model</SelectLabel>
          <SelectTrigger />
        </div>
        <SelectContent>
          <SelectGroup>
            <SelectGroupLabel>General</SelectGroupLabel>
            <SelectItem value="atlas-large" icon={<SparkIcon />}>
              Atlas Large
            </SelectItem>
            <SelectItem value="atlas-small" icon={<SparkIcon />}>
              Atlas Small
            </SelectItem>
          </SelectGroup>
          <SelectSeparator />
          <SelectGroup>
            <SelectGroupLabel>Specialized</SelectGroupLabel>
            <SelectItem value="orbit-code" icon={<CodeIcon />}>
              Orbit Code
            </SelectItem>
            <SelectItem value="orbit-vision" icon={<EyeIcon />}>
              Orbit Vision
            </SelectItem>
          </SelectGroup>
          <SelectSeparator />
          <SelectItem value="local-llama" icon={<ChipIcon />} disabled>
            Local model
          </SelectItem>
        </SelectContent>
      </Select>

      <Select items={regions} name="region">
        <div className="grid gap-1.5">
          <SelectLabel>Data region</SelectLabel>
          <SelectTrigger placeholder="Choose a region" />
        </div>
        <SelectContent>
          {regions.map((region) => (
            <SelectItem key={region.value} value={region.value}>
              {region.label}
            </SelectItem>
          ))}
        </SelectContent>
      </Select>
    </div>
  );
}

API

PropTypeDefaultDescription
items{ value, label }[]—Select: lets the trigger show the selected value's label.
value / defaultValueValue—Select: the selected value.
onValueChange(value) => void—Select: called when the selection changes.
multiplebooleanfalseSelect: allow several values.
placeholderReactNode—SelectTrigger: text shown before a choice is made.
size"sm" | "md""md"SelectTrigger: 32px or 36px tall.
alignItemWithTriggerbooleanfalseSelectContent: place the list over the trigger, aligned to the selected item.
iconReactNode—SelectItem: a 16px icon before the text.

Accessibility

  • The trigger is a button with role="combobox" and the list a role="listbox" of role="option" items, managed by Base UI.
  • SelectLabel names the trigger; clicking it focuses the trigger without opening the list.
  • Arrow keys move the highlight, typing jumps to a matching item, Enter or Space selects, Escape closes and returns focus to the trigger.
  • The selected item carries a check mark as well as stronger text, so it does not rely on color.
  • The list fades and scales from the trigger in 160ms; under reduced motion it appears without the transition.

Files

Installed into src/components/shaken, or components/shaken when the project has no src directory.

  • src/components/shaken/ui/select.tsxselect
  • npm packages: @base-ui/react@^1.9.0React and Tailwind CSS v4 required

Source

The exact source the CLI installs, from /r/items/react/select/0.1.0.json.

ui/select.tsxShow
ui/select.tsx
"use client";

import { Select as BaseSelect } from "@base-ui/react/select";
import type { ComponentProps, ReactNode } from "react";

type ClassName<State> = string | ((state: State) => string | undefined) | undefined;

function cx(...classes: (string | false | null | undefined)[]) {
  return classes.filter(Boolean).join(" ");
}

/** Prepends base classes while keeping Base UI's state-function form of className working. */
function withBase<State>(base: string, className: ClassName<State>) {
  return typeof className === "function" ? (state: State) => cx(base, className(state)) : cx(base, className);
}

/**
 * Groups the select parts and holds its value. Pass `items` (an array of `{ value, label }`) so
 * the trigger can show the label of the selected value, plus `defaultValue` or `value` +
 * `onValueChange`. Add `multiple` to select several values.
 */
export const Select = BaseSelect.Root;

/** A visible label for the trigger. Clicking it focuses the trigger without opening the list. */
export function SelectLabel({ className, ...props }: BaseSelect.Label.Props) {
  return (
    <BaseSelect.Label
      className={withBase("text-sm leading-5 font-medium text-zinc-950 select-none dark:text-white", className)}
      {...props}
    />
  );
}

const triggerSizes = {
  sm: "h-8 pr-1.5 pl-2.5",
  md: "h-9 pr-2 pl-3",
} as const;

export interface SelectTriggerProps extends BaseSelect.Trigger.Props {
  /** Text shown until a value is chosen. */
  placeholder?: ReactNode;
  /** Matches the Input height at `md`; `sm` for toolbars and dense forms. */
  size?: keyof typeof triggerSizes;
}

/**
 * The button that opens the list, styled like Input. Shows the selected item's label, or the
 * placeholder in the tertiary color. Pass `children` to render the value yourself.
 */
export function SelectTrigger({ placeholder, size = "md", className, children, ...props }: SelectTriggerProps) {
  return (
    <BaseSelect.Trigger
      className={withBase(
        cx(
          "group/trigger inline-flex w-full min-w-0 cursor-default items-center justify-between gap-2 rounded-lg border text-left text-sm select-none",
          triggerSizes[size],
          "border-zinc-300 bg-white text-zinc-900 shadow-sm hover:border-zinc-400",
          "dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100 dark:hover:border-zinc-600",
          "data-[popup-open]:border-zinc-400 dark:data-[popup-open]:border-zinc-600",
          "transition-[border-color,box-shadow] duration-150 ease-out motion-reduce:transition-none",
          "focus-visible:border-sky-500 focus-visible:ring-3 focus-visible:ring-sky-500/20 focus-visible:outline-none",
          "data-[invalid]:border-red-400 dark:data-[invalid]:border-red-500/60",
          "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-60",
        ),
        className,
      )}
      {...props}
    >
      {children ?? (
        <BaseSelect.Value
          placeholder={placeholder}
          className="min-w-0 truncate data-[placeholder]:text-zinc-400 dark:data-[placeholder]:text-zinc-500"
        />
      )}
      <BaseSelect.Icon className="flex shrink-0 text-zinc-400 transition-colors duration-150 group-hover/trigger:text-zinc-600 motion-reduce:transition-none dark:text-zinc-500 dark:group-hover/trigger:text-zinc-300">
        <svg aria-hidden="true" viewBox="0 0 16 16" fill="none" className="size-4">
          <path d="M5.5 6.25L8 3.75l2.5 2.5M5.5 9.75L8 12.25l2.5-2.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
        </svg>
      </BaseSelect.Icon>
    </BaseSelect.Trigger>
  );
}

type PositionerProps = BaseSelect.Positioner.Props;

export interface SelectContentProps extends BaseSelect.Popup.Props {
  /**
   * Places the list over the trigger so the selected item lines up with the trigger's text, as
   * native menus do. Off by default: the list opens below the trigger like a dropdown menu.
   */
  alignItemWithTrigger?: PositionerProps["alignItemWithTrigger"];
  /** Side of the trigger to place the list on. Flips when there is not enough room. */
  side?: PositionerProps["side"];
  /** Alignment against the trigger on the chosen side. */
  align?: PositionerProps["align"];
  /** Gap between the trigger and the list, in pixels. */
  sideOffset?: PositionerProps["sideOffset"];
  /** Classes for the positioner wrapper, for example to change the z-index. */
  positionerClassName?: string;
  /** Element to portal into. Defaults to `document.body`. */
  container?: BaseSelect.Portal.Props["container"];
}

/**
 * The list panel, rendered in a portal. It is at least as wide as the trigger and scrolls when
 * tall, with arrows at the edges for pointer users.
 */
export function SelectContent({
  alignItemWithTrigger = false,
  side = "bottom",
  align = "start",
  sideOffset = 6,
  positionerClassName,
  container,
  className,
  children,
  ...props
}: SelectContentProps) {
  return (
    <BaseSelect.Portal container={container}>
      <BaseSelect.Positioner
        alignItemWithTrigger={alignItemWithTrigger}
        side={side}
        align={align}
        sideOffset={sideOffset}
        className={cx("z-50 outline-none select-none", positionerClassName)}
      >
        <BaseSelect.Popup
          className={withBase(
            cx(
              "relative max-w-(--available-width) min-w-(--anchor-width) origin-(--transform-origin) overflow-hidden rounded-xl outline-none",
              "border border-zinc-950/10 bg-white text-zinc-950 shadow-[0_12px_32px_-8px_rgb(0_0_0/0.18)]",
              "dark:border-white/15 dark:bg-black dark:text-white dark:shadow-[0_16px_48px_-12px_rgb(0_0_0/1),inset_0_1px_0_rgb(255_255_255/0.08)]",
              // Enter: 160ms from 96% at the trigger. Exit: 110ms, fades with a smaller scale.
              "transition-[opacity,scale] duration-160 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none",
              "data-[starting-style]:opacity-0 motion-safe:data-[starting-style]:scale-96",
              "data-[ending-style]:opacity-0 data-[ending-style]:duration-110 data-[ending-style]:ease-out motion-safe:data-[ending-style]:scale-98",
              // Aligned over the trigger, the list only fades: growing from the trigger's middle reads as a jump.
              "data-[side=none]:data-[starting-style]:scale-100 data-[side=none]:data-[ending-style]:scale-100",
            ),
            className,
          )}
          {...props}
        >
          <SelectScrollArrow direction="up" />
          <BaseSelect.List className="max-h-[min(var(--available-height),22rem)] scroll-py-1.5 overflow-y-auto overscroll-contain p-1.5 outline-none">
            {children}
          </BaseSelect.List>
          <SelectScrollArrow direction="down" />
        </BaseSelect.Popup>
      </BaseSelect.Positioner>
    </BaseSelect.Portal>
  );
}

function SelectScrollArrow({ direction }: { direction: "up" | "down" }) {
  const Arrow = direction === "up" ? BaseSelect.ScrollUpArrow : BaseSelect.ScrollDownArrow;
  return (
    <Arrow
      className={cx(
        "absolute inset-x-0 z-10 flex h-6 cursor-default items-center justify-center text-zinc-400 dark:text-zinc-500",
        direction === "up"
          ? "top-0 bg-linear-to-b from-white from-50% to-transparent dark:from-black"
          : "bottom-0 bg-linear-to-t from-white from-50% to-transparent dark:from-black",
      )}
    >
      <svg aria-hidden="true" viewBox="0 0 16 16" fill="none" className={cx("size-3.5", direction === "down" && "rotate-180")}>
        <path d="M4.5 10L8 6.5l3.5 3.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
      </svg>
    </Arrow>
  );
}

export interface SelectItemProps extends BaseSelect.Item.Props {
  /** A 16px icon before the text. */
  icon?: ReactNode;
}

/**
 * An option. The selected one carries a check at the end. The highlight follows the pointer
 * and arrow keys instantly; lists are used too often to animate it.
 */
export function SelectItem({ icon, className, children, ...props }: SelectItemProps) {
  return (
    <BaseSelect.Item
      className={withBase(
        cx(
          "relative flex h-8 cursor-default items-center gap-2.5 rounded-md pr-8 pl-2 text-sm outline-none select-none",
          "text-zinc-700 data-[highlighted]:bg-zinc-950/[0.05] data-[highlighted]:text-zinc-950",
          "dark:text-zinc-300 dark:data-[highlighted]:bg-white/[0.08] dark:data-[highlighted]:text-white",
          "data-[selected]:text-zinc-950 dark:data-[selected]:text-white",
          "data-[disabled]:pointer-events-none data-[disabled]:opacity-45",
          "[&_svg]:size-4 [&_svg]:shrink-0",
        ),
        className,
      )}
      {...props}
    >
      {icon != null ? (
        <span aria-hidden="true" className="flex text-zinc-500 in-data-[highlighted]:text-zinc-950 dark:text-zinc-500 dark:in-data-[highlighted]:text-white">
          {icon}
        </span>
      ) : null}
      <BaseSelect.ItemText className="min-w-0 flex-1 truncate">{children}</BaseSelect.ItemText>
      <BaseSelect.ItemIndicator className="absolute right-2 flex text-zinc-950 dark:text-white">
        <svg aria-hidden="true" viewBox="0 0 16 16" fill="none">
          <path d="M3.75 8.25l2.75 2.75 5.75-6" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
        </svg>
      </BaseSelect.ItemIndicator>
    </BaseSelect.Item>
  );
}

/** Groups related options. Put a SelectGroupLabel inside to name the group. */
export function SelectGroup({ className, ...props }: BaseSelect.Group.Props) {
  return <BaseSelect.Group className={withBase("not-first:mt-1", className)} {...props} />;
}

/** Names a SelectGroup. Must be rendered inside the group it labels. */
export function SelectGroupLabel({ className, ...props }: BaseSelect.GroupLabel.Props) {
  return (
    <BaseSelect.GroupLabel
      className={withBase("px-2 pt-2 pb-1 text-xs font-medium text-zinc-500 select-none dark:text-zinc-500", className)}
      {...props}
    />
  );
}

/** A divider between groups of options. */
export function SelectSeparator({ className, ...props }: ComponentProps<typeof BaseSelect.Separator>) {
  return (
    <BaseSelect.Separator
      className={withBase("-mx-1.5 my-1.5 h-px bg-zinc-950/[0.07] dark:bg-white/[0.08]", className)}
      {...props}
    />
  );
}