---
title: "Slider"
description: "Adjust a controlled numeric range using two handles."
---

# Slider

Adjust a controlled numeric range using two handles.

## 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 Slider, RangeSlider from "@rightui/ui"
```

## Usage

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

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  state low = 20
  state high = 80
  action setRange(lower: number, upper: number)
    low = lower
    high = upper
  state volume = 50
  action updateVolume(v: number)
    volume = v
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      RangeSlider(id="advanced-range", label="Price range", lower=low, upper=high, minimum=0, maximum=100, step=5, minimumSteps=1, vertical=false, disabled=false, change=setRange)
      text "Single thumb" font-size="ui.text-sm" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      Slider(id="variation-slider-single", label="Volume", value=volume, minimum=0, maximum=100, step=1, disabled=false, change=updateVolume)
      text "Vertical range" font-size="ui.text-sm" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      RangeSlider(id="variation-slider-true-false", label="Vertical range", lower=low, upper=high, minimum=0, maximum=100, step=5, minimumSteps=1, vertical=true, disabled=false, change=setRange)
      text "Disabled range" font-size="ui.text-sm" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      RangeSlider(id="variation-slider-false-true", label="Disabled range", lower=low, upper=high, minimum=0, maximum=100, step=5, minimumSteps=1, vertical=false, disabled=true, change=setRange)

```

## API Reference

| Property | Type |
| --- | --- |
| RangeSlider.id | string |
| RangeSlider.label | string |
| RangeSlider.lower | number |
| RangeSlider.upper | number |
| RangeSlider.minimum | number |
| RangeSlider.maximum | number |
| RangeSlider.step | number |
| RangeSlider.minimumSteps | number |
| RangeSlider.vertical | bool |
| RangeSlider.disabled | bool |
| RangeSlider.change | action |
| Slider.id | string |
| Slider.label | string |
| Slider.value | number |
| Slider.minimum | number |
| Slider.maximum | number |
| Slider.step | number |
| Slider.disabled | bool |
| Slider.change | action |

## Source

File: packages/ui/slider.contract

```contract
use uiFontFamily from "./typography-context.contract"
component Slider
  props
    id: string
    label: string
    value: number
    minimum: number
    maximum: number
    step: number
    disabled: bool
    change: action
  inject
    uiFont: string
  state start = 0
  state trackWidth = 1
  derive ratio = max(0, min(1, (value - minimum) / max(1, maximum - minimum)))
  action update(n: number)
    if not disabled
      change(max(minimum, min(maximum, minimum + round((n - minimum) / step) * step)))
  action begin(e: PointerEvent)
    let box = frame(id + "-track")
    let width = max(1, box.width)
    let position = max(0, min(width, e.clientX - box.x))
    trackWidth = width
    start = position
    update(minimum + position / width * (maximum - minimum))
    focus(id)
  action move(dx: number, dy: number)
    let position = start + dx
    start = position
    update(minimum + position / trackWidth * (maximum - minimum))
  action keys(k: string)
    if k == "ArrowRight" or k == "ArrowUp"
      preventDefault()
      update(value + step)
    if k == "ArrowLeft" or k == "ArrowDown"
      preventDefault()
      update(value - step)
    if k == "Home"
      preventDefault()
      update(minimum)
    if k == "End"
      preventDefault()
      update(maximum)
    if k == "PageUp"
      preventDefault()
      update(value + step * 10)
    if k == "PageDown"
      preventDefault()
      update(value - step * 10)
  view
    column gap="ui.space-8" min-width="0px"
      row justify-content="space-between" align-items="center" flex-wrap="wrap" gap="ui.space-8"
        text label flex="1 1 8rem" min-width="0px" hyphens="auto" font-size="ui.text-sm" color="ui.foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        text value.toFixed(0) flex-shrink=0 font-size="ui.text-caption" color="ui.muted-foreground" font-variant-numeric="tabular-nums" testId=(id + "-value") font-family=uiFontFamily(uiFont) data-rightui-font="body"
      view id=id testId=id role="slider" aria-label=label aria-valuemin=minimum aria-valuemax=maximum aria-valuenow=value
        aria-disabled=disabled tabindex=(disabled ? -1 : 0) key=keys pointerdown=begin pan=move
        height="ui.touch-target" width="100%" position="relative" touch-action="none" cursor="pointer" opacity=(disabled ? "ui.opacity-disabled" : 1)
        :focus-visible
          box-shadow="ui.shadow-focus"
        view id=(id + "-track") position="absolute" left="ui.space-10" right="ui.space-10" top="50%" translate="0px -50%" height="ui.space-6" border-radius="ui.radius-full" background-color="ui.muted" pointer-events="none"
          view height="100%" width=((ratio * 100).toString() + "%") border-radius="ui.radius-full" background-color="ui.primary"
          view position="absolute" left=((ratio * 100).toString() + "%") top="50%" width="ui.space-20" height="ui.space-20" translate="-50% -50%"
            border-radius="ui.radius-full" background-color="ui.surface" border-width="ui.border-strong-width" border-style="solid" border-color="ui.primary" box-sizing="border-box"

```
