# Slider
URL: /lynx/components/slider
Source: https://github.com/daangn/seed-design/blob/dev/docs/content/lynx/components/slider.mdx
A slider component that selects one or two values from a specified range.
Lynx Engine 최소 버전: 3.6
사용 가능 버전: @seed-design/lynx-react@0.8.0, @seed-design/lynx-css@0.12.0
## Preview
```tsx
import "./styles";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
"값"} />
);
}
```
## Installation
- npm: npx @seed-design/cli@latest add ui:slider --framework lynx
- pnpm: pnpm dlx @seed-design/cli@latest add ui:slider --framework lynx
- yarn: yarn dlx @seed-design/cli@latest add ui:slider --framework lynx
- bun: bun x @seed-design/cli@latest add ui:slider --framework lynx
## Props
### `Slider`
The `Slider` in Registry is a convenience wrapper that combines the Slider primitive with Field guidance elements. When using one thumb, put one value in `defaultValues` or `values`, and when selecting a range, put two values.
The Registry wrapper provides two types of props:
- **Slider state and behavior**: Directly passes `values`, `defaultValues`, `min`, `max`, `step`, `allowedValues`, `minStepsBetweenThumbs`, `dir`, `disabled`, `readOnly`, `invalid`, `onValuesChange`, `onValuesCommit`, `getAccessibilityLabel`, `getAccessibilityValueText`, `getValueIndicatorLabel`, and `valueIndicatorTrigger` from the `low-level Root`.
- **Display and Field guidance**: Provides `label`, `labelWeight`, `indicator`, `description`, `errorMessage`, `showRequiredIndicator`, `markers`, `ticks`, `tickWeight`, `hideRange`, `hideValueIndicator`, and `fieldRef`. Each item in `markers` is a number or a `{ value, label?, align? }` object.
When `allowedValues` is specified, only allowed values can be selected and it takes precedence over `step` and `minStepsBetweenThumbs`. When `values` and `onValuesChange` are used together, it becomes a controlled state, and `onValuesCommit` is called when the value is finalized.
## Usage
Instead of using the Registry composition, you can directly use the `@seed-design/lynx-react` primitives. Combine `SliderControl` and `SliderTrack` inside `SliderRoot`, and place `SliderRange` and necessary `SliderTick` inside the track. Create each thumb with `SliderThumb`, and the Value Indicator is composed of `SliderValueIndicatorRoot`, `SliderValueIndicatorArrow`, and `SliderValueIndicatorArrowTip` and `SliderValueIndicatorLabel` within it. If you need markers, place `SliderMarker` inside `SliderMarkers`.
```tsx
import { useState } from "@lynx-js/react";
import {
ActionButton,
SliderControl,
SliderMarker,
SliderMarkers,
SliderRange,
SliderRoot,
SliderThumb,
SliderTick,
SliderTrack,
SliderValueIndicatorArrow,
SliderValueIndicatorArrowTip,
SliderValueIndicatorLabel,
SliderValueIndicatorRoot,
} from "@seed-design/lynx-react";
export function PriceSlider() {
const [values, setValues] = useState([40]);
const setPreset = () => {
setValues([60]);
};
return (
"Price"}
getAccessibilityValueText={(value) => `${value} KRW`}
>
0 KRW
100 KRW
Set to 60 KRW
);
}
```
The `index` of `SliderThumb` indicates the position of the thumb in the `values` array. When directly combining package primitives, value changes and submission and validation are still handled in app state and request flow.
## Examples
### Basic
Set the selection range with `min` and `max`, and select a value with one thumb.
```tsx
import "./styles";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
"값"} />
"값"} />
);
}
```
### Steps
Set the selection interval with `step` to select only values in fixed increments.
```tsx
import "./styles";
import { useState } from "@lynx-js/react";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
const [value, setValue] = useState([50]);
function handleValuesChange(nextValues: number[]) {
"background only";
setValue(nextValues);
}
return (
"값"}
/>
{JSON.stringify(value)}
);
}
```
### Allowed Values
Specify selectable values with `allowedValues`. When this prop is set, `allowedValues` takes precedence over `step` and `minStepsBetweenThumbs`.
```tsx
import "./styles";
import { useState } from "@lynx-js/react";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
const ALLOWED_VALUES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
const [values, setValues] = useState([ALLOWED_VALUES[0], ALLOWED_VALUES[2]]);
function handleValuesChange(nextValues: number[]) {
"background only";
setValues(nextValues);
}
return (
({ label: value, value }))}
getAccessibilityLabel={() => "값"}
/>
{JSON.stringify(values)}
);
}
```
### With Ticks
You can display ticks on the track that indicate the selection unit.
#### Thin
Use small intervals and display thin ticks to make it appear as continuous manipulation.
```tsx
import "./styles";
import { Slider } from "@/components/ui/slider";
import { useSeedClassName } from "@seed-design/lynx-react";
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
"값"}
/>
);
}
```
#### Thick
Display thick ticks when the value intervals are large or selection is only allowed at tick positions.
```tsx
import "./styles";
import { Slider } from "@/components/ui/slider";
import { useSeedClassName } from "@seed-design/lynx-react";
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
"값"}
/>
);
}
```
### With Markers
Display markers below the slider that explain key values. Marker visual text is not automatically included in native accessibility values, so pass necessary meaning through `getAccessibilityValueText`.
You can pass a number or a `{ value, label?, align? }` object to `markers`. If `align` is omitted in the object, the minimum value is placed at the start, the maximum value at the end, and others in the center.
```tsx
import "./styles";
import { Slider } from "@/components/ui/slider";
import { useSeedClassName } from "@seed-design/lynx-react";
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
`${value}°C`}
getValueIndicatorLabel={({ value }) => `${value}°C`}
getAccessibilityLabel={() => "온도"}
/>
"값"}
/>
);
}
```
### Controlled
Control the slider value with app state using `values` and `onValuesChange`.
```tsx
import "./styles";
import { useState } from "@lynx-js/react";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
const DEFAULT_VALUE = [50];
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
const [value, setValue] = useState(DEFAULT_VALUE);
function handleValuesChange(nextValues: number[]) {
"background only";
setValue(nextValues);
}
function handleSetMin() {
"background only";
setValue([0]);
}
function handleReset() {
"background only";
setValue(DEFAULT_VALUE);
}
function handleSetMax() {
"background only";
setValue([100]);
}
return (
"값"}
/>
{JSON.stringify(value)}
Set Min
Reset
Set Max
);
}
```
### Listening to Value Changes
- `onValuesChange`: Called whenever the value changes during dragging. Use this to display the current value on screen or update app state in real time.
- `onValuesCommit`: Called when value changes are complete. In Lynx native, it is called once on touch release of a drag with value changes. You do not need to add debounce to this callback.
```tsx
import "./styles";
import { useState } from "@lynx-js/react";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
const [value, setValue] = useState([20]);
const [committedValue, setCommittedValue] = useState([20]);
function handleValuesChange(nextValues: number[]) {
"background only";
setValue(nextValues);
}
function handleValuesCommit(nextValues: number[]) {
"background only";
setCommittedValue(nextValues);
}
return (
"값"}
/>
Current value: {JSON.stringify(value)}
Committed value: {JSON.stringify(committedValue)}
);
}
```
### Disabled
Use `disabled` to prevent thumb manipulation and display disabled state styling.
```tsx
import "./styles";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
export default function SliderDisabled() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
"값"}
/>
(thumbIndex === 0 ? "최소값" : "최대값")}
/>
);
}
```
### Hide Range
Use `hideRange` to hide the active range color and display only the track.
```tsx
import "./styles";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
export default function SliderHideRange() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
"값"}
/>
(thumbIndex === 0 ? "최소값" : "최대값")}
/>
);
}
```
### Customizing Value Indicator
#### Value Indicator Label
Change the text of the Value Indicator displayed when manipulating the thumb with `getValueIndicatorLabel`.
The Value Indicator is supplementary UI displayed during native touch manipulation and is not the value read by accessibility navigation. If you need to provide meaningful units or descriptions, also use `getAccessibilityValueText`.
```tsx
import "./styles";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
const formatter = new Intl.NumberFormat("ko-KR", { style: "decimal" });
export default function SliderCustomValueIndicatorLabel() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
(
{`thumb ${thumbIndex}\n${formatter.format(value)}`}
)}
getAccessibilityValueText={formatter.format}
getAccessibilityLabel={() => "값"}
/>
);
}
```
#### Value Indicator Trigger
Set the conditions for displaying the Value Indicator with `valueIndicatorTrigger`. In Lynx, you can use `"auto"` and `"active"`.
- `"active"`: Display on the thumb being manipulated with the current touch.
- `"auto"` (default): Behaves identically to `"active"` in native touch environments.
Web's hover-based display conditions or keyboard focus-based behavior are not supported in Lynx native.
```tsx
import "./styles";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
export default function SliderValueIndicatorTrigger() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
"값"}
/>
"값"}
/>
auto와 active 모두 터치로 활성화된 동안에만 값 표시
);
}
```
#### Hide Value Indicator
Hide the Value Indicator above the thumb with `hideValueIndicator`.
```tsx
import "./styles";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
export default function SliderHideValueIndicator() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
"값"}
/>
);
}
```
### Range Slider
Select a range between two thumbs by passing two values to `defaultValues` or `values`.
```tsx
import "./styles";
import { Slider } from "@/components/ui/slider";
import { useState } from "@lynx-js/react";
import { useSeedClassName } from "@seed-design/lynx-react";
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
const [priceRange, setPriceRange] = useState([20, 80]);
return (
(thumbIndex === 0 ? "최소값" : "최대값")}
/>
);
}
```
#### Minimum Steps Between Thumbs
Specify the minimum distance to maintain between two thumbs with `minStepsBetweenThumbs`.
```tsx
import "./styles";
import { Slider } from "@/components/ui/slider";
import { useState } from "@lynx-js/react";
import { useSeedClassName } from "@seed-design/lynx-react";
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
const [values, setValues] = useState([20, 80]);
return (
(thumbIndex === 0 ? "최소값" : "최대값")}
/>
{JSON.stringify(values)}
);
}
```
### Accessibility
#### `getAccessibilityValueText`
Native accessibility navigation provides each thumb with a value in the format `minimum , maximum , current ` by default. If the number alone is not meaningful, use `getAccessibilityValueText` to provide a description including units or meaning of the value.
```tsx
import "./styles";
import { useState } from "@lynx-js/react";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
const days = ["일", "월", "화", "수", "목", "금", "토"];
function getHumanReadableDayOfWeek(value: number) {
if (days[value] === undefined) throw new Error("Invalid day value");
return `${days[value]}요일`;
}
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
const [values, setValues] = useState([1, 3]);
return (
({ label, value }))}
ticks={days.slice(1, -1).map((_, index) => index + 1)}
tickWeight="thick"
values={values}
onValuesChange={setValues}
getAccessibilityLabel={(thumbIndex) => (thumbIndex === 0 ? "시작" : "종료")}
getAccessibilityValueText={getHumanReadableDayOfWeek}
getValueIndicatorLabel={({ value }) => getHumanReadableDayOfWeek(value)}
/>
values: {JSON.stringify(values)}
accessibility-value-text: {JSON.stringify(values.map(getHumanReadableDayOfWeek))}
);
}
```
#### `getAccessibilityLabel`
Describe the purpose of each thumb with `getAccessibilityLabel`. Particularly in range sliders, provide a label that distinguishes whether each thumb represents the minimum or maximum value.
```tsx
import "./styles";
import { useState } from "@lynx-js/react";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
const getAccessibilityLabel = (thumbIndex: number) => (thumbIndex === 0 ? "최소값" : "최대값");
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
const [values, setValues] = useState([10, 30]);
return (
values: {JSON.stringify(values)}
accessibility-label:{" "}
{JSON.stringify(values.map((_, index) => getAccessibilityLabel(index)))}
);
}
```
### Field Integration
Use `label`, `labelWeight`, `indicator`, `description`, `errorMessage`, and `showRequiredIndicator` to display Field guidance around the slider. When `invalid` is `true` and there is an `errorMessage`, error guidance is displayed.
```tsx
import "./styles";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
const markers = [
{ value: 0, label: "매우 동의하지 않음" },
{ value: 14, label: "매우 동의함" },
];
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
`${value} ${markers.find((marker) => marker.value === value)?.label ?? ""}`.trim()
}
/>
);
}
```
### RTL Support
Create a slider with values arranged from right to left with `dir="rtl"`.
```tsx
import "./styles";
import { useSeedClassName } from "@seed-design/lynx-react";
import { Slider } from "@/components/ui/slider";
export default function Example() {
const seedClassName = useSeedClassName({ colorMode: "system" });
return (
`${value}°C`}
getValueIndicatorLabel={({ value }) => `${value}°C`}
getAccessibilityLabel={() => "온도"}
/>
"값"}
/>
);
}
```
## Differences from Web Version
Lynx `Slider` has the following differences from React Slider:
- **Rendering elements**: Uses native `` and `` instead of HTML form input and DOM elements.
- **Input events**: Uses native touch input instead of browser pointer/keyboard events, with value changes passed via `onValuesChange` and finalization via `onValuesCommit`.
- **Value Indicator**: `valueIndicatorTrigger="auto"` is equivalent to touch-active in native and is not displayed on hover. It provides the same result as `"active"`.
- **Accessibility**: Provides native accessibility name and value through `getAccessibilityLabel` and `getAccessibilityValueText` instead of DOM ARIA.
- **Field guidance**: Registry's `label`, `description`, and `errorMessage` are displayed with native layout. Label and description connection via DOM id is not used.
- **Range selection**: Represents range with `values` array and two native thumbs.
## Lynx Unsupported Features
Since Lynx does not have HTML form and browser keyboard models, the following React-only features are not provided as working examples:
| Feature | Replacement or Limitation in Lynx |
| -------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `form` submission, `React Hook Form` | Manage app state with `values` and `onValuesChange`, and handle submission in the app's request flow. |
| `HiddenInput` | There is no HTML hidden input. Construct the value to submit directly from app state. |
| `name` | There is no native form field name. Use app state keys or request payload. |
| Browser validation and form field connection | Display screen errors with `invalid` and `errorMessage`, and perform validation in the app. |
| `getAriaLabelledby` and DOM ARIA | There is no DOM id connection. Provide native accessibility through `getAccessibilityLabel` and `getAccessibilityValueText`. |
| Keyboard, focus, focus-visible | There is no keyboard thumb focus model. Use native touch and accessibility navigation. |
| Hover | There is no hover interaction in native. `valueIndicatorTrigger="auto"` behaves as touch-active like `"active"`. |
Therefore, Form (Uncontrolled) and React Hook Form are excluded from working examples in Lynx documentation. Even when submission and validation are needed, connect slider values to app state and use the app's submission and validation logic along with native accessibility guidance.