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

# Button

Button 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 Button, IconButton, ButtonContent, DisclosureButton from "@rightui/ui"
```

## Usage

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

## Example

```contract
use uiFontFamily, Button, ButtonContent, IconButton, Spinner from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiFont: string
  state chosen = ""
  action choose(value: string)
    chosen = value
  view
    column gap="ui.space-20" align-items="center"
      row gap="ui.space-12" flex-wrap="wrap" justify-content="center"
        for variant in ["primary", "secondary", "outline", "ghost", "link", "destructive"] key=variant
          Button(id="button-" + variant, label=(variant == "primary" ? "Button" : variant.slice(0, 1).toUpperCase() + variant.slice(1)), variant=variant, disabled=false, press=choose(variant))
      Button(id="button-disabled", label="Disabled", variant="outline", disabled=true, press=choose("disabled"))
      row gap="ui.space-12" flex-wrap="wrap" justify-content="center"
        for size in ["sm", "default", "lg"] key=size
          ButtonContent(id="button-size-" + size, label=size, variant="outline", size=size, disabled=false, press=choose(size))
            text (size == "sm" ? "Small" : size == "lg" ? "Large" : "Default") font-family=uiFontFamily(uiFont) data-rightui-font="body"
      row gap="ui.space-12" flex-wrap="wrap" justify-content="center"
        IconButton(id="button-icon", label="Next", variant="outline", disabled=false, press=choose("icon"))
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" aria-hidden=true
            path d="m9 18 6-6-6-6"
        ButtonContent(id="button-with-icon", label="Send email", variant="outline", size="default", disabled=false, press=choose("email"))
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" aria-hidden=true
            rect x=2 y=4 width=20 height=16 rx=2
            path d="m2 6 10 7 10-7"
          text "Send email" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        ButtonContent(id="button-loading", label="Please wait", variant="secondary", size="default", disabled=true, press=choose("loading"))
          Spinner(id="button-loading-spinner", label="Please wait", reduced=reduced)
      if chosen != ""
        text (chosen + " button pressed") role="status" font-size="ui.text-sm" color="ui.muted-foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"

```

## API Reference

| Property | Type |
| --- | --- |
| Button.id | string |
| Button.label | string |
| Button.variant | string |
| Button.disabled | bool |
| Button.press | action |
| IconButton.id | string |
| IconButton.label | string |
| IconButton.variant | string |
| IconButton.disabled | bool |
| IconButton.press | action |
| ButtonContent.id | string |
| ButtonContent.label | string |
| ButtonContent.variant | string |
| ButtonContent.size | string |
| ButtonContent.disabled | bool |
| ButtonContent.press | action |
| DisclosureButton.id | string |
| DisclosureButton.label | string |
| DisclosureButton.variant | string |
| DisclosureButton.controls | string |
| DisclosureButton.expanded | bool |
| DisclosureButton.icon | bool |
| DisclosureButton.press | action |

## Source

File: packages/ui/button.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use Control from "./styles.contract"
fn buttonFill(variant: string, hovered: bool): string = ((variant == "primary" or variant == "default") ? (hovered ? "ui.primary-hover" : "ui.primary") : variant == "destructive" ? (hovered ? "ui.destructive-hover" : "ui.destructive") : variant == "secondary" ? (hovered ? "ui.secondary-hover" : "ui.muted") : variant == "outline" ? (hovered ? "ui.outline-hover" : "ui.outline") : variant == "link" ? "transparent" : hovered ? "ui.ghost-hover" : "transparent")
fn buttonInk(variant: string): string = ((variant == "primary" or variant == "default") ? "ui.primary-foreground" : variant == "destructive" ? "ui.on-danger" : variant == "danger" ? "ui.danger" : "ui.foreground")

component ButtonFrame
  props
    id: string
    label: string
    variant: string
    disabled: bool
    press: action
    size: string
    radius: string
    compact: bool
    pressed: option<bool>
    describedBy: string
  params
    gap: length = "ui.control-gap" control="token" group="Spacing"
    outlineWidth: length = "ui.border-width" control="token" group="Border"
    shadow: box-shadow = "ui.shadow-control" control="token" group="Elevation"
  slot
  derive icon = size.startsWith("icon")
  derive height = size.endsWith("xs") ? "ui.control-xs" : size.endsWith("sm") ? (compact ? "ui.ai-control-sm" : "ui.control-sm") : size.endsWith("lg") ? (compact ? "ui.ai-control-lg" : "ui.control-lg") : (compact ? "ui.ai-control-default" : "ui.control")
  state hovered = false
  state tipFocused = false
  action hoverChanged(inside: bool)
    hovered = inside
    if describedBy != ""
      if inside
        showPopover(describedBy, source=id)
      else if not tipFocused
        hidePopover(describedBy)
  action focusTip
    tipFocused = true
    if describedBy != ""
      showPopover(describedBy, source=id)
  action blurTip
    tipFocused = false
    if describedBy != ""
      hidePopover(describedBy)
  action tipKeys(k: string)
    if k == "Escape" and describedBy != ""
      hidePopover(describedBy)
  inject
    uiWeights: FontWeights
  view
    button id=id testId=id class=Control font-weight=uiWeights.medium press=press disabled=disabled hover=hoverChanged focus=focusTip blur=blurTip key=tipKeys aria-label=label aria-pressed=(match pressed { case some(value) => value ? "true" : "false", case none => "undefined" }) aria-describedby=describedBy
      display="flex" flex-shrink=0 align-items="center" justify-content="center" padding=(icon ? "0px" : size == "xs" ? "0px ui.space-8" : size == "sm" ? "ui.space-4 ui.space-12" : size == "lg" ? "ui.space-8 ui.space-24" : "ui.space-6 ui.space-16") gap="param.gap"
      width=(icon ? height : "auto") height=height min-height=height
      border-radius=(radius != "" ? radius : size.endsWith("xs") or size.endsWith("sm") ? "ui.radius-md" : "ui.radius-control")
      text-decoration=(variant == "link" and hovered ? "underline" : "none")
      background-color=buttonFill(variant, hovered)
      color=buttonInk(variant)
      border-width=(variant == "outline" ? "param.outlineWidth" : "0px") border-style="solid" border-color="ui.input"
      box-shadow=(variant == "outline" ? "param.shadow" : "none")
      transition="background-color ui.duration-fast ui.easing, color ui.duration-fast ui.easing, box-shadow ui.duration-fast ui.easing"
      children

component Button
  props
    id: string
    label: string
    variant: string
    disabled: bool
    press: action
  inject
    uiFont: string
  view
    ButtonFrame(id=id, label=label, variant=variant, disabled=disabled, press=press, size="default", radius="", compact=false, pressed=none, describedBy="")
      text label min-width=0 font-family=uiFontFamily(uiFont) data-rightui-font="body"

component IconButton
  props
    id: string
    label: string
    variant: string
    disabled: bool
    press: action
  slot
  view
    ButtonFrame(id=id, label=label, variant=variant, disabled=disabled, press=press, size="icon-sm", radius="", compact=false, pressed=none, describedBy="")
      children

component ButtonContent
  props
    id: string
    label: string
    variant: string
    size: string
    disabled: bool
    press: action
  slot
  view
    ButtonFrame(id=id, label=label, variant=variant, disabled=disabled, press=press, size=size, radius="", compact=false, pressed=none, describedBy="")
      children

component DisclosureButton
  props
    id: string
    label: string
    variant: string
    controls: string
    expanded: bool
    icon: bool
    press: action
  slot
  view
    DisclosureButtonFrame(id=id, label=label, variant=variant, controls=controls, expanded=expanded, icon=icon, popoverTarget="", press=press)
      children

// A controlled popover still declares its invoker so pointer-down light dismiss
// does not close it before the same pointer-up can toggle it.
component PopoverTrigger
  props
    id: string
    label: string
    variant: string
    controls: string
    expanded: bool
    icon: bool
    press: action
  slot
  view
    DisclosureButtonFrame(id=id, label=label, variant=variant, controls=controls, expanded=expanded, icon=icon, popoverTarget=(expanded ? controls : ""), press=press)
      children

component DisclosureButtonFrame
  props
    id: string
    label: string
    variant: string
    controls: string
    expanded: bool
    icon: bool
    popoverTarget: string
    press: action
  slot
  state hovered = false
  action hoverChanged(inside: bool)
    hovered = inside
  inject
    uiWeights: FontWeights
  view
    button id=id testId=id class=Control font-weight=uiWeights.medium press=press hover=hoverChanged aria-label=label aria-expanded=expanded aria-controls=controls popovertarget=popoverTarget popovertargetaction="show"
      display="flex" align-items="center" justify-content=(icon ? "center" : "flex-start") text-align="left" gap="ui.control-gap"
      width=(icon ? "ui.control-sm" : "100%") min-height=(icon ? "ui.control-sm" : "ui.control") height=(icon ? "ui.control-sm" : "auto") flex-shrink=0 padding=(icon ? "0px" : "ui.space-8 ui.space-12")
      background-color=buttonFill(variant, hovered) color=buttonInk(variant) border-width=(variant == "outline" ? "ui.border-width" : "0px") border-style="solid" border-color="ui.input"
      transition="background-color ui.duration-fast ui.easing, color ui.duration-fast ui.easing"
      children

component ButtonControl
  props
    id: string
    label: string
    variant: string
    size: string
    disabled: bool
    pressed: option<bool>
    describedBy: string
    press: action
  slot
  view
    ButtonFrame(id=id, label=label, variant=variant, disabled=disabled, press=press, size=size, radius="ui.ai-radius-control", compact=true, pressed=pressed, describedBy=describedBy)
      children

component ButtonSegment
  props
    id: string
    label: string
    disabled: bool
    press: action
  slot
  view
    ButtonFrame(id=id, label=label, variant="ghost", disabled=disabled, press=press, size="sm", radius="0px", compact=false, pressed=none, describedBy="")
      children

component ButtonAI
  props
    id: string
    label: string
    variant: string
    size: string
    disabled: bool
    press: action
  slot
  view
    ButtonFrame(id=id, label=label, variant=variant, disabled=disabled, press=press, size=size, radius="ui.ai-radius-control", compact=true, pressed=none, describedBy="")
      children

```
