Skip to content
2k
On This Page

Select

Displays a list of options for the user to pick from—triggered by a button.

import { For } from "solid-js"

import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectLabel,
  SelectTrigger,
  SelectValue
} from "~/components/ui/select"

type Fruit = {
  label: string
  value: string
}

const fruits: Fruit[] = [
  { label: "Apple", value: "apple" },
  { label: "Banana", value: "banana" },
  { label: "Blueberry", value: "blueberry" },
  { label: "Grapes", value: "grapes" },
  { label: "Pineapple", value: "pineapple" }
]

export default function SelectDemo() {
  return (
    <Select<Fruit>
      itemToLabel={(option) => String(option["label"])}
      itemToValue={(option) => String(option["value"])}

      placeholder="Select a fruit"
    >
      <SelectTrigger aria-label="Select a fruit" class="w-full max-w-48">
        <SelectValue />
      </SelectTrigger>
      <SelectContent>
        <SelectGroup>
          <SelectLabel>Fruits</SelectLabel>
          <For each={fruits}>
            {(option) => (
              <SelectItem value={option} label={option["label"]}>
                {option.label}
              </SelectItem>
            )}
          </For>
        </SelectGroup>
      </SelectContent>
    </Select>
  )
}

Installation

pnpm dlx solidui-cli@latest add select

Install the following dependencies:

pnpm add @ui-sdk/solid @ui-sdk/core

Copy and paste the following code into your project.

components/ui/select.tsx
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import type { ComponentProps, JSX } from "@solidjs/web"
import {
  Select as SdkSelect,
  SelectTrigger as SdkSelectTrigger,
  SelectValue as SdkSelectValue,
  SelectContent as SdkSelectContent,
  SelectList as SdkSelectList,
  SelectGroup as SdkSelectGroup,
  SelectGroupLabel as SdkSelectGroupLabel,
  SelectItem as SdkSelectItem,
  SelectItemText as SdkSelectItemText,
  SelectItemIndicator as SdkSelectItemIndicator,
  SelectSeparator as SdkSelectSeparator
} from "@ui-sdk/solid/select"
import { omit } from "solid-js"

import { cn } from "~/lib/utils"
const Select = SdkSelect
const SelectGroup = (
  props: Omit<ComponentProps<typeof SdkSelectGroup>, "class" | "children"> & {
    class?: string
    children?: JSX.Element
  }
) => <SdkSelectGroup {...props} class={cn("scroll-my-1 p-1", props.class)} />
const SelectLabel = (
  props: Omit<
    ComponentProps<typeof SdkSelectGroupLabel>,
    "class" | "children"
  > & {
    class?: string
    children?: JSX.Element
  }
) => (
  <SdkSelectGroupLabel {...props} class={cn("px-1.5 py-1 text-xs text-muted-foreground", props.class)} />
)
const SelectValue = (
  props: Omit<ComponentProps<typeof SdkSelectValue>, "class"> & {
    class?: string
  }
) => <SdkSelectValue {...props} class={cn("flex flex-1 text-left", props.class)} />
type SelectTriggerProps = Omit<
  ComponentProps<typeof SdkSelectTrigger>,
  "class"
> & {
  class?: string
  size?: "sm" | "default"
}
const SelectTrigger = (props: SelectTriggerProps) => {
  const rest = omit(props, "class", "size", "children")
  return (
    <SdkSelectTrigger
      {...rest}
      data-size={props.size ?? "default"}
      class={cn(
        "gap-1.5 rounded-lg border border-input bg-transparent py-2 pr-2 pl-2.5 text-sm transition-colors select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-placeholder:text-muted-foreground data-[size=default]:h-8 data-[size=sm]:h-7 data-[size=sm]:rounded-[min(var(--radius-md),10px)] *:data-[slot=select-value]:flex *:data-[slot=select-value]:gap-1.5 dark:bg-input/30 dark:hover:bg-input/50 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg:not([class*='size-'])]:size-4 flex w-fit items-center justify-between whitespace-nowrap outline-none disabled:cursor-not-allowed disabled:opacity-50 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center [&_svg]:pointer-events-none [&_svg]:shrink-0",
        props.class
      )}
    >
      {props.children}
      <svg
        width="24"
        height="24"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
        class="size-4 text-muted-foreground pointer-events-none"
      >
        <path d="m6 9 6 6 6-6" />
      </svg>
    </SdkSelectTrigger>
  )
}
const SelectContent = (
  props: Omit<ComponentProps<typeof SdkSelectContent>, "class" | "children"> & {
    class?: string
    children?: JSX.Element
  }
) => {
  const rest = omit(props, "class", "children")
  return (
    <SdkSelectContent
      sideOffset={4}
      align="center"
      {...rest}
      class={cn(
        "min-w-36 rounded-lg bg-popover text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 data-ending-style:animate-out data-ending-style:fade-out-0 data-ending-style:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 relative z-50 max-h-(--available-height) w-(--anchor-width) origin-(--transform-origin) overflow-x-hidden overflow-y-auto",
        props.class
      )}
    >
      <SdkSelectList class="has-[>[data-slot=select-item]]:p-1">{props.children}</SdkSelectList>
    </SdkSelectContent>
  )
}
const SelectItem = (
  props: Omit<ComponentProps<typeof SdkSelectItem>, "class" | "children"> & {
    class?: string
    children?: JSX.Element
  }
) => {
  const rest = omit(props, "class", "children")
  return (
    <SdkSelectItem
      {...rest}
      class={cn(
        "gap-1.5 rounded-md py-1 pr-8 pl-1.5 text-sm focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2 relative flex w-full cursor-default items-center outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0",
        props.class
      )}
    >
      <SdkSelectItemText class="flex flex-1 gap-2 shrink-0 whitespace-nowrap">
        {props.children}
      </SdkSelectItemText>
      <SdkSelectItemIndicator class="pointer-events-none absolute right-2 flex size-4 items-center justify-center">
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          class="pointer-events-none"
        >
          <path d="M20 6 9 17l-5-5" />
        </svg>
      </SdkSelectItemIndicator>
    </SdkSelectItem>
  )
}
const SelectSeparator = (
  props: Omit<
    ComponentProps<typeof SdkSelectSeparator>,
    "class" | "children"
  > & {
    class?: string
    children?: JSX.Element
  }
) => (
  <SdkSelectSeparator
    {...props}
    class={cn("-mx-1 my-1 h-px bg-border pointer-events-none", props.class)}
  />
)
export {
  Select,
  SelectGroup,
  SelectLabel,
  SelectValue,
  SelectTrigger,
  SelectContent,
  SelectItem,
  SelectSeparator
}

Update the import paths to match your project setup.

Usage

import {
  Select,
  SelectContent,
  SelectGroup,
  SelectLabel,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "~/components/ui/select"
 
;<Select placeholder="Select a fruit">
  <SelectTrigger aria-label="Fruit">
    <SelectValue />
  </SelectTrigger>
  <SelectContent>
    <SelectGroup>
      <SelectLabel>Fruits</SelectLabel>
      <SelectItem value="apple">Apple</SelectItem>
      <SelectItem value="banana">Banana</SelectItem>
    </SelectGroup>
  </SelectContent>
</Select>

Use value and onValueChange for a controlled selection. Object values use itemToValue and itemToLabel. Compose items inside SelectContent; groups use SelectGroup and SelectLabel.

Composition

Use the following composition to build a Select:

Select
├── SelectTrigger
│   └── SelectValue
└── SelectContent
    ├── SelectGroup
    │   ├── SelectLabel
    │   ├── SelectItem
    │   └── SelectItem
    ├── SelectSeparator
    └── SelectGroup
        ├── SelectLabel
        ├── SelectItem
        └── SelectItem

Align Item With Trigger

Use the position prop on SelectContent to control alignment. When position="item-aligned" (default), the popup positions so the selected item appears over the trigger. When position="popper", the popup aligns to the trigger edge.

Toggle to align the item with the trigger.

import { createSignal } from "solid-js"

import {
  Field,
  FieldContent,
  FieldDescription,
  FieldGroup,
  FieldLabel
} from "~/components/ui/field"
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "~/components/ui/select"
import { Switch } from "~/components/ui/switch"

export default function SelectAlignItem() {
  const [alignItemWithTrigger, setAlignItemWithTrigger] = createSignal(true)

  return (
    <FieldGroup class="w-full max-w-xs">
      <Field orientation="horizontal">
        <FieldContent>
          <FieldLabel for="align-item">Align Item</FieldLabel>
          <FieldDescription>
            Toggle to align the item with the trigger.
          </FieldDescription>
        </FieldContent>
        <Switch
          id="align-item"
          checked={alignItemWithTrigger()}
          onCheckedChange={setAlignItemWithTrigger}
        />
      </Field>
      <Field>
        <Select defaultValue="banana">
          <SelectTrigger>
            <SelectValue />
          </SelectTrigger>
          <SelectContent alignItemWithTrigger={alignItemWithTrigger()}>
            <SelectGroup>
              <SelectItem value="apple">Apple</SelectItem>
              <SelectItem value="banana">Banana</SelectItem>
              <SelectItem value="blueberry">Blueberry</SelectItem>
              <SelectItem value="grapes">Grapes</SelectItem>
              <SelectItem value="pineapple">Pineapple</SelectItem>
            </SelectGroup>
          </SelectContent>
        </Select>
      </Field>
    </FieldGroup>
  )
}

Groups

Use SelectGroup, SelectLabel, and SelectSeparator to organize items.

import { For } from "solid-js"
import { Show } from "solid-js"

import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectLabel,
  SelectSeparator,
  SelectTrigger,
  SelectValue
} from "~/components/ui/select"

type FoodOption = {
  label: string
  value: string
}

type FoodGroup = {
  label: string
  options: FoodOption[]
}

const foods: FoodGroup[] = [
  {
    label: "Fruits",
    options: [
      { label: "Apple", value: "apple" },
      { label: "Banana", value: "banana" },
      { label: "Blueberry", value: "blueberry" }
    ]
  },
  {
    label: "Vegetables",
    options: [
      { label: "Carrot", value: "carrot" },
      { label: "Broccoli", value: "broccoli" },
      { label: "Spinach", value: "spinach" }
    ]
  }
]

export default function SelectGroups() {
  return (
    <Select<FoodOption>
      itemToLabel={(option) => String(option["label"])}
      itemToValue={(option) => String(option["value"])}

      placeholder="Select a fruit"
    >
      <SelectTrigger aria-label="Select a fruit" class="w-full max-w-52">
        <SelectValue />
      </SelectTrigger>
      <SelectContent>
        <For each={foods}>
          {(group, groupIndex) => (
            <>
              <Show when={groupIndex() !== 0}>
                <SelectSeparator />
              </Show>
              <SelectGroup>
                <SelectLabel>{group.label}</SelectLabel>
                <For each={group["options"]}>
                  {(option) => (
                    <SelectItem value={option} label={option["label"]}>
                      {option.label}
                    </SelectItem>
                  )}
                </For>
              </SelectGroup>
            </>
          )}
        </For>
      </SelectContent>
    </Select>
  )
}

Scrollable

A select with many items that scrolls.

import { For } from "solid-js"
import { Show } from "solid-js"

import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectLabel,
  SelectSeparator,
  SelectTrigger,
  SelectValue
} from "~/components/ui/select"

type TimezoneOption = {
  label: string
  value: string
}

type TimezoneGroup = {
  label: string
  options: TimezoneOption[]
}

const timezones: TimezoneGroup[] = [
  {
    label: "North America",
    options: [
      { label: "Eastern Standard Time", value: "est" },
      { label: "Central Standard Time", value: "cst" },
      { label: "Mountain Standard Time", value: "mst" },
      { label: "Pacific Standard Time", value: "pst" },
      { label: "Alaska Standard Time", value: "akst" },
      { label: "Hawaii Standard Time", value: "hst" }
    ]
  },
  {
    label: "Europe & Africa",
    options: [
      { label: "Greenwich Mean Time", value: "gmt" },
      { label: "Central European Time", value: "cet" },
      { label: "Eastern European Time", value: "eet" },
      { label: "Western European Summer Time", value: "west" },
      { label: "Central Africa Time", value: "cat" },
      { label: "East Africa Time", value: "eat" }
    ]
  },
  {
    label: "Asia",
    options: [
      { label: "Moscow Time", value: "msk" },
      { label: "India Standard Time", value: "ist" },
      { label: "China Standard Time", value: "cst_china" },
      { label: "Japan Standard Time", value: "jst" },
      { label: "Korea Standard Time", value: "kst" },
      { label: "Indonesia Central Standard Time", value: "icst" }
    ]
  },
  {
    label: "Australia & Pacific",
    options: [
      { label: "Australian Western Standard Time", value: "awst" },
      { label: "Australian Central Standard Time", value: "acst" },
      { label: "Australian Eastern Standard Time", value: "aest" },
      { label: "New Zealand Standard Time", value: "nzst" },
      { label: "Fiji Time", value: "fjt" }
    ]
  },
  {
    label: "South America",
    options: [
      { label: "Argentina Time", value: "art" },
      { label: "Bolivia Time", value: "bot" },
      { label: "Brasilia Time", value: "brt" },
      { label: "Chile Standard Time", value: "clt" }
    ]
  }
]

export default function SelectScrollable() {
  return (
    <Select<TimezoneOption>
      itemToLabel={(option) => String(option["label"])}
      itemToValue={(option) => String(option["value"])}

      placeholder="Select a timezone"
    >
      <SelectTrigger aria-label="Select a timezone" class="w-full max-w-64">
        <SelectValue />
      </SelectTrigger>
      <SelectContent>
        <For each={timezones}>
          {(group, groupIndex) => (
            <>
              <Show when={groupIndex() !== 0}>
                <SelectSeparator />
              </Show>
              <SelectGroup>
                <SelectLabel>{group.label}</SelectLabel>
                <For each={group["options"]}>
                  {(option) => (
                    <SelectItem value={option} label={option["label"]}>
                      {option.label}
                    </SelectItem>
                  )}
                </For>
              </SelectGroup>
            </>
          )}
        </For>
      </SelectContent>
    </Select>
  )
}

Disabled

import { For } from "solid-js"

import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "~/components/ui/select"

type Fruit = {
  label: string
  value: string
  disabled?: boolean
}

const fruits: Fruit[] = [
  { label: "Apple", value: "apple" },
  { label: "Banana", value: "banana" },
  { label: "Blueberry", value: "blueberry" },
  { label: "Grapes", value: "grapes", disabled: true },
  { label: "Pineapple", value: "pineapple" }
]

export default function SelectDisabled() {
  return (
    <Select<Fruit>
      itemToLabel={(option) => String(option["label"])}
      itemToValue={(option) => String(option["value"])}
      disabled

      placeholder="Select a fruit"
    >
      <SelectTrigger aria-label="Select a fruit" class="w-full max-w-48">
        <SelectValue />
      </SelectTrigger>
      <SelectContent>
        <For each={fruits}>
          {(option) => (
            <SelectItem
              value={option}
              label={option["label"]}
              disabled={option["disabled"] ?? false}
            >
              {option.label}
            </SelectItem>
          )}
        </For>
      </SelectContent>
    </Select>
  )
}

Invalid

Set data-invalid on Field and aria-invalid on SelectTrigger to style an invalid selection. Use FieldError to explain the error.

import { For } from "solid-js"

import { Field, FieldError, FieldLabel } from "~/components/ui/field"
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "~/components/ui/select"

type Fruit = {
  label: string
  value: string
}

const fruits: Fruit[] = [
  { label: "Apple", value: "apple" },
  { label: "Banana", value: "banana" },
  { label: "Blueberry", value: "blueberry" },
  { label: "Grapes", value: "grapes" },
  { label: "Pineapple", value: "pineapple" }
]

export default function SelectInvalid() {
  return (
    <Field class="max-w-48" data-invalid>
      <FieldLabel for="select-invalid-fruit">Fruit</FieldLabel>
      <Select<Fruit>
        itemToLabel={(option) => String(option["label"])}
        itemToValue={(option) => String(option["value"])}

        placeholder="Select a fruit"
      >
        <SelectTrigger
          aria-label="Select a fruit"
          aria-invalid="true"
          class="w-full"
          id="select-invalid-fruit"
        >
          <SelectValue />
        </SelectTrigger>
        <SelectContent>
          <For each={fruits}>
            {(option) => (
              <SelectItem value={option} label={option["label"]}>
                {option.label}
              </SelectItem>
            )}
          </For>
        </SelectContent>
      </Select>
      <FieldError>Please select a fruit.</FieldError>
    </Field>
  )
}

API Reference

See the UI SDK Select API for the full API reference.