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

# Command

Command 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 Command, CommandEntry from "@rightui/ui"
```

## Usage

Import Command, CommandEntry 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, Command, CommandEntry from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  action commandAction(value: string)
    lastAction = value
    counter = counter + 1
  state lastAction = "No action selected"
  state counter = 0
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      Command(id="desktop-command", label="Search commands", entries=[CommandEntry(id="new", label="New project", keywords="create file", shortcut="", disabled=false), CommandEntry(id="settings", label="Settings", keywords="preferences settings", shortcut="", disabled=false), CommandEntry(id="export", label="Export", keywords="video", shortcut="Unavailable", disabled=true)], choose=commandAction)
      text lastAction role="status" font-size="ui.text-sm" color="ui.muted-foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      text "Keyboard shortcuts" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      Command(id="command-shortcuts", label="Project commands", entries=[CommandEntry(id="shortcut-new", label="New project", keywords="create", shortcut="⌘N", disabled=false)], choose=commandAction)

```

## API Reference

| Property | Type |
| --- | --- |
| Command.id | string |
| Command.label | string |
| Command.entries | CommandEntry[] |
| Command.choose | action |

## Source

File: packages/ui/command.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use Control, Field from "./styles.contract"

shape CommandEntry
  id: string
  label: string
  keywords: string
  shortcut: string
  disabled: bool

component Command
  props
    id: string
    label: string
    entries: CommandEntry[]
    choose: action
  inject
    uiWeights: FontWeights
    uiFont: string
  state query = ""
  derive filtered = entries.filter(item => (item.label + " " + item.keywords).toLowerCase().includes(query.trim().toLowerCase()))
  derive enabled = filtered.filter(item => not item.disabled)
  action edit(v: string)
    query = v
  action pick(item: CommandEntry)
    if not item.disabled
      choose(item.id)
  action searchKeys(k: string, e: KeyboardEvent)
    if not e.isComposing
      if k == "ArrowDown" or k == "Enter"
        preventDefault()
        match first(enabled)
          case some(item)
            if k == "Enter"
              pick(item)
            else
              focus(id + "-item-" + item.id)
          case none
            focus(id + "-search")
      else if k == "Escape"
        query = ""
  action itemKeys(itemId: string, k: string)
    let index = enabled.map(item => item.id).indexOf(itemId)
    if k == "ArrowUp" or k == "ArrowDown" or k == "Home" or k == "End"
      preventDefault()
      let next = (k == "Home" ? 0 : k == "End" ? enabled.length - 1 : min(enabled.length - 1, index + (k == "ArrowUp" ? -1 : 1)))
      match enabled.at(next)
        case some(item)
          focus(id + "-item-" + item.id)
        case none
          focus(id + "-search")
    else if k == "Escape"
      preventDefault()
      focus(id + "-search")
    else if k == "Enter" or k == " "
      preventDefault()
      match enabled.at(index)
        case some(item)
          pick(item)
        case none
          focus(id + "-search")
  view
    column id=id testId=id role="region" aria-label=label min-width="0px" border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.radius-panel" background-color="ui.surface" padding="ui.space-6" gap="ui.space-6"
      input id=(id + "-search") testId=(id + "-search") class=Field font-weight=uiWeights.regular value=query input=edit key=searchKeys font-family=uiFontFamily(uiFont) data-rightui-font="body"
        aria-label=label aria-controls=(id + "-results") placeholder="Rechercher une commande…"
      scroll max-height="18rem" min-height="0px"
        column id=(id + "-results") gap="ui.space-2"
          for item in filtered key=item.id
            button id=(id + "-item-" + item.id) testId=(id + "-item-" + item.id) class=Control font-weight=uiWeights.medium
              disabled=item.disabled press=pick(item) key=itemKeys(item.id)
              display="flex" align-items="center" flex-wrap="wrap" gap="ui.space-12" padding="ui.space-10 ui.space-12" border-radius="ui.radius-sm" border="0" background-color="transparent" color="ui.foreground"
              :hover
                background-color="ui.muted"
              :focus-visible
                background-color="ui.muted"
              text item.label flex="1 1 8rem" min-width="0px" text-align="left" font-family=uiFontFamily(uiFont) data-rightui-font="body"
              if item.shortcut != ""
                text item.shortcut font-size="ui.text-xs" color="ui.muted-foreground" aria-hidden=true font-family=uiFontFamily(uiFont) data-rightui-font="body"
          if filtered.length == 0
            text "Aucune commande." testId=(id + "-empty") role="status" padding="ui.space-16" font-size="ui.text-caption" color="ui.muted-foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"

```
