---
title: "Select"
description: "Select with accessible presentation and local state."
---

# Select

Select with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use Select from "@rightui/ui"
```

## Usage

Import Select from @rightui/ui. Store its value in state and update it through the provided action callback.

## Example

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, Select from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  state preset = "Studio"
  derive reduce = reduced
  action selectPreset(v: string)
    preset = v
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      Select(id="preset", label="Espace de travail", options=["Studio", "Personnel", "Team"], value=preset, disabled=false, reduced=reduce, change=selectPreset)
      text "Disabled" font-size="ui.text-sm" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      Select(id="variation-select-disabled", label="Espace de travail", options=["Studio", "Personnel", "Team"], value=preset, disabled=true, reduced=reduce, change=selectPreset)
      text "Scrollable options" font-size="ui.text-sm" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      Select(id="variation-select-long", label="Destination", options=["Workspace 1","Workspace 2","Workspace 3","Workspace 4","Workspace 5","Workspace 6","Workspace 7","Workspace 8","Workspace 9","Workspace 10","Workspace 11","Workspace 12","Workspace 13","Workspace 14","Workspace 15","Workspace 16","Workspace 17","Workspace 18","Workspace 19","Workspace 20","Workspace 21","Workspace 22","Workspace 23","Workspace 24"], value=preset, disabled=false, reduced=reduced, change=selectPreset)

```

## API Reference

| Property | Type |
| --- | --- |
| Select.id | string |
| Select.label | string |
| Select.options | string[] |
| Select.value | string |
| Select.disabled | bool |
| Select.reduced | bool |
| Select.change | action |

## Source

File: packages/ui/select.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use uiPopupIn, uiPopupOut from "./popup-motion.contract"
use Field, Pressable from "./styles.contract"
component Select
  props
    id: string
    label: string
    options: string[]
    value: string
    disabled: bool
    reduced: bool
    change: action
  inject
    uiWeights: FontWeights
    uiFont: string
  state expanded = false
  state openEpoch = 0
  state active = 0
  state width = 240
  action show(index: number)
    if not disabled and options.length > 0
      let box = frame(id)
      width = box.width
      active = index
      if not expanded
        openEpoch = openEpoch + 1
      expanded = true
      showPopover(id + "-list", source=id)
      focus(id + "-option-" + index.toString())
  action open
    if not disabled
      if expanded
        expanded = false
      else
        show(max(0, options.indexOf(value)))
  action toggled(epoch: number, e: ToggleEvent)
    if epoch == openEpoch
      expanded = e.newState == "open"

  action pick(i: number)
    if not disabled
      match options.at(i)
        case some(v)
          change(v)
          expanded = false
          focus(id)
        case none
          expanded = false
  action keys(k: string, e: KeyboardEvent)
    if not disabled and not e.ctrlKey and not e.metaKey
      if k == "Escape"
        preventDefault()
        expanded = false
        focus(id)
      else if k == "Tab" and expanded
        expanded = false
        focus(id)
      else if k == "ArrowDown" or k == "ArrowUp"
        preventDefault()
        let next = (not expanded ? max(0, options.indexOf(value)) : max(0, min(options.length - 1, active + (k == "ArrowDown" ? 1 : -1))))
        show(next)
      else if k == "Home" or k == "End"
        preventDefault()
        show(k == "Home" ? 0 : options.length - 1)
      else if k == "Enter" or k == " "
        preventDefault()
        if expanded
          pick(active)
        else
          open()
      else if k.length == 1 and k != " "
        let ordered = options.slice(active + 1).concat(options.slice(0, active + 1))
        match first(ordered.filter(option => option.toLowerCase().startsWith(k.toLowerCase())))
          case some(v)
            preventDefault()
            show(options.indexOf(v))
          case none
            active = active
  view
    column gap="ui.space-8" min-width="0px"
      text label id=(id + "-label") font-size="ui.text-sm" font-weight=uiWeights.medium color="ui.foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      button id=id testId=id class=Field font-weight=uiWeights.regular disabled=disabled press=open key=keys
        role="combobox" aria-expanded=expanded aria-controls=(id + "-list") aria-haspopup="listbox" aria-labelledby=(id + "-label")
        display="flex" justify-content="space-between" align-items="center" gap="ui.space-8"
        text value flex=1 min-width="0px" text-align="left" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        svg width="ui.space-16" height="ui.space-16" flex-shrink=0 viewBox="0 0 24 24" fill="none" stroke="ui.muted-foreground" stroke-width="ui.stroke-standard" aria-hidden=true
          path d="m6 9 6 6 6-6"
      if expanded
        for epoch in [openEpoch] key=epoch
          scroll id=(id + "-list") testId=(id + "-list") role="listbox" aria-label=label
            popover="auto" toggle=toggled(epoch) tabindex=-1 key=keys
            position-area="bottom" position-try-fallbacks="flip-block, flip-inline, flip-block flip-inline"
            width=width max-width="calc(100vw - ui.space-32)" max-height="min(24rem, 60vh)" box-sizing="border-box" margin="ui.space-4" padding="ui.space-4"
            border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.radius-menu" background-color="ui.surface" box-shadow="ui.shadow-menu"
            animation=(reduced ? "none" : "uiPopupIn ui.duration-fast ui.easing both")
            -exact-exit-animation=(reduced ? "none" : "uiPopupOut ui.duration-fast ui.easing both")
            column gap="0px"
              for option, i in options key=option
                button id=(id + "-option-" + i.toString()) testId=(id + "-option-" + i.toString())
                  appearance="none" role="option" aria-selected=(value == option) tabindex=(active == i ? 0 : -1)
                  press=pick(i) class=Pressable
                  background-color=(active == i ? "ui.muted" : "ui.surface")
                  color="ui.foreground" border="0" border-radius="ui.radius-item" min-height="ui.space-32" padding="ui.space-6 ui.space-8" font-weight=uiWeights.regular
                  :focus-visible
                    box-shadow="ui.shadow-focus"
                  row gap="ui.space-8" align-items="center"
                    text option flex=1 min-width="0px" font-size="ui.text-sm" text-align="left" font-family=uiFontFamily(uiFont) data-rightui-font="body"
                    if value == option
                      svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="ui.foreground" stroke-width="ui.stroke-standard" aria-hidden=true
                        path d="m5 12 4 4L19 6"

```
