仕組み
The thumb is a 64 × 40 pill scaled to 0.56 under a white cap. While it is held it springs to full size, the cap fades and the lens shows. The lens holds an exact copy of the track: the copy is shifted so that the point under the lens's centre lands on it, then scaled 1.45 × around that same point, so copy and track meet exactly at the centre and the lens magnifies everywhere, in every browser. In Chromium-based browsers (Chrome, Edge, Opera, Brave, Arc) the lens also goes through an SVG feDisplacementMap whose map is drawn for the pill shape on a canvas: deep inside nothing moves, and within the 10px rim band each point samples the picture up to 9px further in along the inward normal (the vertical part at a third of that, so the thin track is not dragged into an I shape at the round ends), so the fill bends and stretches where it meets the glass. Safari, Firefox and every other engine get the frosted lens instead: the same magnified copy with a 0.6px blur and a little more saturation. All of them share the lit rim (inset highlights, a specular streak and a faint ring of the secondary and tertiary colours). The position is a registered number: presses glide there, a drag writes it directly, and a release springs onto the nearest step.
向いている場面
- Controls that people hold and watch while they change something: brightness, volume, a colour temperature, a zoom level.
- Media players, smart-home panels and camera or photo tools that want a tactile, current material.
- Tracks that carry colour, such as a gradient to pick from, where the magnified picture under the lens helps aim.
向かない場面
- Picking a range with two ends: use the range slider.
- Dense settings lists with many sliders, where every thumb turning to glass would be too much.
使い方
import { useState } from 'react'
import { GlassLensSlider } from './GlassLensSlider.jsx'
export function LampControls() {
const [level, setLevel] = useState(72)
const [warmth, setWarmth] = useState(70)
return (
<>
<GlassLensSlider label="Brightness" value={level} onChange={setLevel} formatValue={(v) => `${v}%`} />
<GlassLensSlider
label="Warmth"
value={warmth}
onChange={setWarmth}
track="linear-gradient(90deg, #bcd4f6, #fde8c4, #f59e3b)"
/>
</>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
value / defaultValue / onChange | number / number / (value) => void | - / min / - | The value, controlled or uncontrolled. onChange fires on every step while dragging. |
onChangeEnd | (value) => void | — | Fires once when a drag or press ends, and after each key press. |
min / max / step | number / number / number | 0 / 100 / 1 | The range and the step. While dragging the thumb follows the pointer exactly; on release it springs onto the nearest step. |
label / formatValue | string / (value) => string | 'Value' / String | Accessible name, and the value as read by screen readers (aria-valuetext). |
track | string | — | A CSS background for the whole track, such as a gradient of colours to choose from. When set, the fill is not drawn. |
fill | boolean | true | Draws the accent fill from the start of the track to the thumb. |
marks | number | 0 | Draws this many evenly spaced dots on the track; the lens magnifies them too. |
refraction | 'auto' | 'soft' | 'auto' | auto bends the picture at the lens's rim with an SVG displacement filter in Chromium-based browsers and uses the frosted lens elsewhere; soft uses the frosted lens everywhere. |
disabled | boolean | false | Fades the slider and ignores input. |
...rest | HTMLAttributes | — | Other attributes go to the root <div>. |
アクセシビリティ
- The track is role="slider" with aria-valuemin, aria-valuemax, aria-valuenow and aria-valuetext from formatValue, named by label; the lens and its copy of the track are hidden from assistive tech.
- Left and Down take a step away, Right and Up add one, PageUp and PageDown move a tenth of the range, Home and End go to the ends. Keys show the glass for a moment too. Focus shows a ring round the slider.
- Touch: touch-action keeps vertical page scrolling; a finger takes the thumb once it moves sideways, and a tap without moving moves the thumb there.
- The 44px tall control is the whole hit area, not only the thumb.
- Reduced motion moves the thumb and the glass straight to their new state.
モーション
Glass in: the thumb and the magnification spring over 300ms on cubic-bezier(0.34, 1.45, 0.6, 1) while the cap fades over --ds-duration-fast; out is the same in reverse. Position: 420ms on cubic-bezier(0.3, 1.35, 0.5, 1), a small overshoot onto the step; while dragging it follows within 60ms.
プロジェクトに追加
Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。
GlassLensSlider.jsxglass-lens-slider.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。







