Slider
Form and input
A transparent field with a neutral border and a focus ring, label above, hint and inline error text below, in the body typeface. File drop targets follow the same field style. Settings pages use a label column and a value column with a save bar that appears only after a change.
실제로 돌아가는 부품입니다. 눌러 보고 입력해 보세요.
내 앱에 넣기
아직 npm 패키지가 없어서 Taster 리포를 받아 그 안의 명령을 씁니다. 내 앱 폴더가 들어 있는 폴더에서 한 번만 받습니다.
git clone https://github.com/makim-414/taster.git && npm ci --prefix taster/packages/engine내 앱 폴더에서 이 부품을 넣습니다. 부품 파일과 토큰 파일이
src/components/taster에 들어갑니다.node ../taster/packages/engine/cli.mjs add blocks/slider --from https://taster-six.vercel.app --dir src/components/taster부품은 Tailwind 클래스로 그리고, Taster 프리셋이 그 클래스를 토큰에 연결합니다. Tailwind CSS 를 설치하고,
tokens.css를 앱 루트에서 한 번 불러온 뒤, 앱 루트에 이 설정을 둡니다. 프리셋이 없으면 빌드는 되어도 부품이 스타일 없이 나옵니다.// tailwind.config.cjs module.exports = { presets: [require('./src/components/taster/tailwind-preset.cjs')], content: ['./index.html', './src/**/*.{ts,tsx}'], };Tailwind v3 는 이 파일을 그대로 읽습니다. v4 는 Tailwind 를 불러오는
src/의 스타일시트 맨 위에 이 두 줄을 넣습니다.@import "tailwindcss"; @config "../tailwind.config.cjs";tokens.css는:root에--bg,--text,--border,--accent를 정의합니다. 내 앱의 스타일시트가 같은 이름을 뒤에서 다시 정의하면 그쪽 색이 이깁니다. 새 Vite 앱의 스타터 CSS 처럼 그런 변수가 있으면 지우거나 이름을 바꾸세요.
언제 쓰나요
- Text input fieldA transparent field with a neutral border and a focus ring, label above, hint and inline error text below, in the body typeface. File drop targets follow the same field style. Settings pages use a label column and a value column with a save bar that appears only after a change.
- Usage and cost estimatorA small calculator that starts from a preset and shows what a plan or order will cost for the entered usage.
대신 없애 주는 AI 티
- Tinted notice band (AC-06)
- Monospace font for interface labels (AC-09)
- Flat gray card with a hairline border, everywhere (AC-14)
- Touch target under 44px (RSP-06)
정보
코드 보기
'use client'
import * as React from 'react'
import { cn } from './_cn'
// Slider. A native range input (so arrow keys, Page Up and Down, Home and End and touch all work) laid over a
// token-drawn track: neutral rail, accent fill, ink thumb. The value is printed beside the label with tabular
// figures and read out through aria-valuetext. Optional marks label a few stops under the track.
export interface SliderProps
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'type' | 'value' | 'onChange' | 'min' | 'max' | 'step'> {
label: React.ReactNode
value: number
onValueChange: (value: number) => void
min?: number
max?: number
step?: number
/** Turns the number into display text, for example (v) => `${v} GB`. */
format?: (value: number) => string
marks?: number[]
hint?: React.ReactNode
}
export const Slider = React.forwardRef<HTMLInputElement, SliderProps>(
({ label, value, onValueChange, min = 0, max = 100, step = 1, format = String, marks, hint, id, disabled, className, ...props }, ref) => {
const reactId = React.useId()
const inputId = id ?? `slider-${reactId}`
const hintId = hint ? `${inputId}-hint` : undefined
const span = max - min || 1
const pct = (v: number) => ((Math.min(max, Math.max(min, v)) - min) / span) * 100
const p = pct(value)
return (
<div className={cn('flex w-full flex-col gap-1.5', disabled && 'opacity-50', className)}>
<div className="flex items-baseline justify-between gap-3">
<label htmlFor={inputId} className="text-sm font-medium text-text">
{label}
</label>
<output htmlFor={inputId} className="text-sm tabular-nums text-text-muted">
{format(value)}
</output>
</div>
<div className="group relative h-11 desk:h-8">
<input
ref={ref}
id={inputId}
type="range"
min={min}
max={max}
step={step}
value={value}
disabled={disabled}
aria-valuetext={format(value)}
aria-describedby={hintId}
onChange={(e) => onValueChange(Number(e.target.value))}
className={cn(
'absolute inset-0 z-10 m-0 h-full w-full cursor-pointer appearance-none bg-transparent outline-none',
'disabled:cursor-not-allowed',
// The native parts stay in place for hit testing but draw nothing; the token track below is what shows.
'[&::-webkit-slider-runnable-track]:bg-transparent [&::-moz-range-track]:bg-transparent',
'[&::-webkit-slider-thumb]:size-4 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:opacity-0',
'[&::-moz-range-thumb]:size-4 [&::-moz-range-thumb]:border-0 [&::-moz-range-thumb]:opacity-0',
)}
{...props}
/>
<div aria-hidden="true" className="pointer-events-none absolute inset-x-2 top-1/2 -translate-y-1/2">
<div className="relative h-1.5 w-full rounded-pill bg-bg-sunken">
<div className="absolute inset-y-0 left-0 rounded-pill bg-accent" style={{ width: `${p}%` }} />
{marks?.map((m) => (
<span key={m} className="absolute top-1/2 h-2.5 w-px -translate-y-1/2 bg-text-subtle" style={{ left: `${pct(m)}%` }} />
))}
<span
className={cn(
'absolute top-1/2 size-4 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-bg bg-text',
'transition-transform duration-press ease-out group-active:scale-press',
'ring-offset-2 ring-offset-bg group-has-[:focus-visible]:ring-2 group-has-[:focus-visible]:ring-ring',
)}
style={{ left: `${p}%` }}
/>
</div>
</div>
</div>
{marks && marks.length > 0 && (
<div aria-hidden="true" className="relative mx-2 h-4 text-label tabular-nums text-text-subtle">
{marks.map((m, i) => (
<span
key={m}
className={cn('absolute top-0 whitespace-nowrap', i === 0 ? '' : i === marks.length - 1 ? '-translate-x-full' : '-translate-x-1/2')}
style={{ left: `${pct(m)}%` }}
>
{format(m)}
</span>
))}
</div>
)}
{hint && (
<p id={hintId} className="text-label text-text-muted">
{hint}
</p>
)}
</div>
)
},
)
Slider.displayName = 'Slider'