Slider
نمای کلیimport { Slider, SliderValue } from '@nili/ui';این همان صفحهی نمای کلیِ خود کتابخانه است که زنده رندر میشود. زبان یا تم را از هدر عوض کنید، همراهش میآید.
تکدستگیره و بازهای
شکلِ مقدار همان نوع است: یک عدد یک دستگیره میسازد و یک [number, number] یک بازه. اسلایدر بازهای بهطور تصادفی یک عدد تنها نمیگیرد.
import { Slider } from "@nili/ui"
export function Example() {
const [value, setValue] = useState(40)
const [range, setRange] = useState([20, 70])
return (
<>
<Slider
label="Volume"
value={value}
onValueChange={setValue}
showValue
/>
<Slider
label="Price"
value={range}
onValueChange={setRange}
showValue
/>
</>
)
}برچسب، زیربرچسب و مقدار
label علاوه بر اینکه بالای ریل دیده میشود، دستگیرهها را هم نامگذاری میکند. showValue عدد فعلی را کنارش میگذارد و sublabel یک خط دوم کمرنگ اضافه میکند.
import { Slider } from "@nili/ui"
export function Example() {
return (
<>
<Slider label={t('brightness')} defaultValue={60} />
<Slider label="Brightness" sublabel="Screen only" defaultValue={60} showValue />
{/* no visible label — name the thumbs instead */}
<Slider defaultValue={60} thumbLabels={["Brightness"]} />
</>
)
}تولتیپ
tooltip هنگام کشیدن، مقدار را در حبابی بالای دستگیره نشان میدهد — وقتی جایی برای نمایش دائمی نیست به کار میآید.
import { Slider } from "@nili/ui"
export function Example() {
return <Slider label={t('opacity')} defaultValue={75} tooltip />
}گامها و کرانها
min، max و step همانطور رفتار میکنند که انتظار دارید. minStepsBetweenThumbs نمیگذارد دو سر یک بازه از هم رد شوند.
import { Slider } from "@nili/ui"
export function Example() {
return (
<>
<Slider label="Rating" min={1} max={5} step={1} defaultValue={3} showValue />
<Slider label="Temperature" min={-20} max={50} step={5} defaultValue={20} showValue />
<Slider
label="Budget"
min={0}
max={1000}
step={50}
defaultValue={[200, 600]}
minStepsBetweenThumbs={2}
showValue
/>
</>
)
}قالببندی
formatValue هر عددی را که کاربر میبیند یا میشنود عوض میکند، نه فقط عدد دیدهشدنی را. ارقام بهطور پیشفرض از لوکال محیط پیروی میکنند، پس رابط فارسی ارقام فارسی را مفت میگیرد.
import { Slider } from "@nili/ui"
export function Example() {
return (
<>
<Slider
label="Storage"
max={500}
defaultValue={120}
showValue
formatValue={(value) => `${value} GB`}
/>
<Slider
label="Discount"
defaultValue={25}
showValue
formatValue={(value) => `${value}%`}
/>
</>
)
}ثبت نهایی و غیرفعال
onValueChange در هر گام حین کشیدن صدا زده میشود؛ onValueCommit فقط یک بار، وقتی اشارهگر یا کلید رها شود. برای هر کار پرهزینه دومی را بردارید — یک درخواست بهازای هر پیکسل کشیدن، راه معمولِ ازپاانداختن سرور با اسلایدر است.
import { Slider } from "@nili/ui"
export function Example() {
return (
<>
<Slider
label="Results per page"
defaultValue={20}
max={100}
step={10}
showValue
onValueCommit={(value) => refetch(value)}
/>
<Slider label="Disabled" defaultValue={40} disabled />
</>
)
}Slider
extends Omit< ComponentPropsWithoutRef<'div'>, 'onChange' | 'defaultValue' | 'color' >
بقیهی پراپها
| پراپ | نوع | پیشفرض | توضیح |
|---|---|---|---|
| children | ReactNode | — | |
| aria-label | string | — | |
| value | SliderValue | — | Controlled value. A `number` renders one thumb, a `[number, number]` renders a range — the shape of the value *is* Figma's "Type", so a range slider cannot be handed a single number by accident. |
| defaultValue | SliderValue | 0 | |
| onValueChange | (value: SliderValue) => void | — | Fires on every step while dragging. |
| onValueCommit | (value: SliderValue) => void | — | Fires once, when the pointer or key is released. Use this for anything expensive — a request per pixel of drag is the usual way a slider takes a server down. |
| min | number | 0 | |
| max | number | 100 | |
| step | number | 1 | |
| minStepsBetweenThumbs | number | 0 | Smallest allowed gap between the two thumbs of a range. |
| disabled | boolean | false | |
| label | ReactNode | — | Visible label above the track. Also names the thumbs. Figma's "Label". |
| sublabel | ReactNode | — | Muted second line under the label. Figma's "Sublabel". |
| showValue | boolean | false | Shows the current value beside the label. Figma's "Edit Amount". |
| tooltip | boolean | false | Value bubble above the thumb while dragging. Figma's "Tooltip". |
| formatValue | (value: number) => string | — | Formats every number the user sees or hears. Defaults to localized digits. |
| locale | string | — | BCP-47 tag driving the digits (`'fa'` → Persian). Defaults to the ambient locale from `NiliProvider`. |
| dir | ltr | rtl | — | Forces the drag direction, overriding the ambient locale. Needed for a track whose axis is not the reading axis — a video timeline runs from the video's start to its end, which is not a sentence, so it is pinned `ltr` even in a Persian UI. Without this the CSS renders one way and the pointer maths mirrors the other, and the playhead jumps backwards. |
| thumbLabels | [string, string?] | — | Accessible names for the thumbs, when there is no visible `label`. |
| name | string | — | `name` for the hidden inputs, when the form is submitted natively. |
SliderHeader
داخلیSliderRange
داخلیSliderRoot
داخلیمحورهای واریانت — از تعریف cva کامپوننت
| پراپ | نوع | پیشفرض | توضیح |
|---|---|---|---|
| disabled | 'true' | 'false' | false |
SliderThumb
داخلیمحورهای واریانت — از تعریف cva کامپوننت
| پراپ | نوع | پیشفرض | توضیح |
|---|---|---|---|
| disabled | 'true' | 'false' | false |
SliderTooltip
داخلیSliderTrack
داخلیمحورهای واریانت — از تعریف cva کامپوننت
| پراپ | نوع | پیشفرض | توضیح |
|---|---|---|---|
| disabled | 'true' | 'false' | false |