Thanh trượt
Một trường nhập liệu có thể kéo thả để chọn giá trị hoặc phạm vi — chẳng hạn như mức âm lượng, bộ lọc giá, cài đặt — với hiệu ứng viền và bóng theo phong cách neobrutalist.
import { Slider } from "@/components/ui/slider"
export function SliderDemo() {Slider cho phép người dùng chọn một giá trị — hoặc một khoảng giữa hai núm kéo — bằng cách kéo dọc theo thanh trượt. Được xây dựng trên thành phần Base UI Slider và tạo kiểu theo công thức neobrutalist: viền dày, núm kéo vuông với bóng đổ đậm và dải giá trị được tô đặc.
Nên dùng khi:
- Âm lượng, độ sáng, vị trí phát — trình phát media và các điều khiển kiểu thiết bị, nơi kéo tiện hơn gõ.
- Bộ lọc giá và khoảng giá trị — hai núm kéo tạo thành bộ lọc min–max cho tìm kiếm và danh sách thương mại điện tử.
- Cài đặt số có giới hạn hợp lý — độ mờ, bo góc, cỡ chữ; bất cứ nơi nào một con số có giới hạn nên được hiển thị thay vì phải gõ.
Cài đặt#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/slider.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/slider.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/slider.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/base/slider.json
Cài đặt các dependency sau:
pnpm add @base-ui/reactnpm install @base-ui/reactyarn add @base-ui/reactbun add @base-ui/react
Sao chép và dán đoạn mã sau vào dự án của bạn.
"use client"
import * as React from "react"
import { Slider as SliderPrimitive } from "radix-ui"
import { cn } from "@/lib/utils"
function Slider({
className,
defaultValue,
value,
min = 0,
max = 100,
// Pulled out of the root spread so they land on the Thumb (the element
// with role="slider" that screen readers announce), not just the Root.
"aria-label": ariaLabel,
"aria-labelledby": ariaLabelledby,
...props
}: React.ComponentProps<typeof SliderPrimitive.Root>) {
const _values = React.useMemo(
() =>
Array.isArray(value)
? value
: Array.isArray(defaultValue)
? defaultValue
: [min, max],
[value, defaultValue, min, max]
)
return (
<SliderPrimitive.Root
data-slot="slider"
defaultValue={defaultValue}
value={value}
min={min}
max={max}
className={cn(
"relative flex w-full touch-none items-center select-none data-disabled:opacity-50 data-vertical:h-full data-vertical:min-h-40 data-vertical:w-auto data-vertical:flex-col",
className
)}
{...props}
>
<SliderPrimitive.Track
data-slot="slider-track"
className="relative grow overflow-hidden border-2 bg-background data-horizontal:h-3 data-horizontal:w-full data-vertical:h-full data-vertical:w-3"
>
<SliderPrimitive.Range
data-slot="slider-range"
className="absolute bg-primary select-none data-horizontal:h-full data-vertical:w-full"
/>
</SliderPrimitive.Track>
{Array.from({ length: _values.length }, (_, index) => (
<SliderPrimitive.Thumb
data-slot="slider-thumb"
key={index}
aria-label={ariaLabel}
aria-labelledby={ariaLabelledby}
className="relative block size-4.5 shrink-0 border-2 bg-background shadow-sm transition-[color,box-shadow] select-none after:absolute after:-inset-2 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:pointer-events-none disabled:opacity-50"
/>
))}
</SliderPrimitive.Root>
)
}
export { Slider }
Cập nhật các đường dẫn import cho phù hợp với cấu hình dự án của bạn.
Cách dùng#
import { Slider } from "@/components/ui/slider"<Slider defaultValue={[33]} max={100} step={1} />Ví dụ#
Khoảng#
Dùng một mảng có hai giá trị cho slider khoảng.
import { Slider } from "@/components/ui/slider"
export function SliderRange() {Nhiều Núm#
Dùng một mảng có nhiều giá trị cho nhiều núm.
import { Slider } from "@/components/ui/slider"
export function SliderMultiple() {Vertical#
Dùng orientation="vertical" cho slider dọc.
import { Slider } from "@/components/ui/slider"
export function SliderVertical() {Được kiểm soát#
"use client"
import * as React from "react"Vô hiệu hóa#
Dùng prop disabled để vô hiệu hóa slider.
import { Slider } from "@/components/ui/slider"
export function SliderDisabled() {RTL#
Để bật hỗ trợ RTL trong Neobrutalism, xem hướng dẫn cấu hình RTL.
"use client"
import * as React from "react"Khả năng truy cập#
Slider tuân theo mẫu Slider của WAI-ARIA: mỗi núm kéo là một phần tử role="slider" focus được với aria-valuemin, aria-valuemax và aria-valuenow luôn được đồng bộ, kèm một input ẩn để gửi giá trị cùng form. Slider không có nhãn hiển thị riêng, nên hãy truyền aria-label hoặc aria-labelledby — wrapper này chuyển tiếp cả hai xuống núm kéo, phần tử mà trình đọc màn hình thực sự đọc lên.
Tương tác bàn phím:
| Phím | Hành động |
|---|---|
Tab / Shift + Tab | Di chuyển focus giữa các núm kéo và ra ngoài |
ArrowRight / ArrowUp | Tăng giá trị một bước |
ArrowLeft / ArrowDown | Giảm giá trị một bước |
PageUp / PageDown | Tăng / giảm theo largeStep (mặc định 10) |
Shift + Arrow | Tăng / giảm theo largeStep |
Home / End | Nhảy đến giá trị tối thiểu / tối đa |
Trong chế độ RTL, các phím mũi tên ngang đi theo hướng đọc.
Tham chiếu API#
Xem tài liệu Base UI Slider.