hlmnhkl-ui 0.32.2 → 0.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,146 @@
1
+ .root {
2
+ display: grid;
3
+ gap: var(--hl-space-6);
4
+ width: 100%;
5
+ color: var(--hl-color-fg);
6
+ }
7
+
8
+ .head {
9
+ display: grid;
10
+ justify-items: center;
11
+ gap: var(--hl-space-1);
12
+ text-align: center;
13
+ }
14
+
15
+ .label {
16
+ font-size: var(--hl-text-sm);
17
+ color: var(--hl-color-fg-muted);
18
+ }
19
+
20
+ .readout {
21
+ font-size: var(--hl-text-4xl);
22
+ font-weight: var(--hl-weight-semibold);
23
+ letter-spacing: var(--hl-tracking-tight);
24
+ line-height: 1;
25
+ }
26
+
27
+ .detail {
28
+ font-size: var(--hl-text-sm);
29
+ color: var(--hl-color-fg-muted);
30
+ font-variant-numeric: tabular-nums;
31
+ }
32
+
33
+ .track {
34
+ --base: calc(var(--hl-ruler-h) * 0.63);
35
+ position: relative;
36
+ height: var(--hl-ruler-h);
37
+ border-radius: var(--hl-radius-md);
38
+ cursor: grab;
39
+ outline: none;
40
+ user-select: none;
41
+ -webkit-user-select: none;
42
+ -webkit-tap-highlight-color: transparent;
43
+ /* A sideways drag moves the ruler; up and down still scrolls the page. */
44
+ touch-action: pan-y;
45
+ }
46
+
47
+ .track[data-moving] {
48
+ cursor: grabbing;
49
+ }
50
+
51
+ .track:focus-visible {
52
+ box-shadow: var(--hl-focus-ring);
53
+ }
54
+
55
+ .clip {
56
+ position: absolute;
57
+ inset: 0;
58
+ overflow: hidden;
59
+ border-radius: inherit;
60
+ mask-image: linear-gradient(90deg, transparent, var(--hl-color-fg) 18%, var(--hl-color-fg) 82%, transparent);
61
+ }
62
+
63
+ .needle {
64
+ position: absolute;
65
+ z-index: 1;
66
+ left: 50%;
67
+ top: var(--hl-space-2);
68
+ width: var(--hl-indicator);
69
+ height: var(--hl-space-5);
70
+ margin-left: calc(var(--hl-indicator) / -2);
71
+ border-radius: var(--hl-radius-full);
72
+ background: var(--hl-color-fg);
73
+ }
74
+
75
+ .needle::before {
76
+ content: '';
77
+ position: absolute;
78
+ left: 50%;
79
+ top: 0;
80
+ width: var(--hl-control-dot);
81
+ height: var(--hl-control-dot);
82
+ border-radius: var(--hl-radius-full);
83
+ background: var(--hl-color-fg);
84
+ transform: translate(-50%, -50%);
85
+ }
86
+
87
+ .mark {
88
+ position: absolute;
89
+ left: 50%;
90
+ top: var(--base);
91
+ width: 0;
92
+ height: 0;
93
+ }
94
+
95
+ .tick {
96
+ position: absolute;
97
+ left: calc(var(--hl-indicator) / -2);
98
+ bottom: 0;
99
+ width: var(--hl-indicator);
100
+ height: var(--hl-space-4);
101
+ border-radius: var(--hl-radius-full);
102
+ transform-origin: bottom center;
103
+ }
104
+
105
+ .tick[data-tier='mid'] {
106
+ height: var(--hl-space-6);
107
+ }
108
+
109
+ .tick[data-tier='major'] {
110
+ height: var(--hl-space-8);
111
+ }
112
+
113
+ .num {
114
+ position: absolute;
115
+ left: 0;
116
+ top: var(--hl-space-3);
117
+ font-size: var(--hl-text-xs);
118
+ font-variant-numeric: tabular-nums;
119
+ white-space: nowrap;
120
+ transform: translateX(-50%);
121
+ }
122
+
123
+ .tags {
124
+ position: absolute;
125
+ inset: 0;
126
+ overflow: hidden;
127
+ pointer-events: none;
128
+ }
129
+
130
+ .tag {
131
+ position: absolute;
132
+ left: 50%;
133
+ top: calc(var(--base) + var(--hl-space-7));
134
+ padding: 0 var(--hl-space-3);
135
+ border: var(--hl-line) solid var(--hl-color-border-strong);
136
+ border-radius: var(--hl-radius-full);
137
+ background: var(--hl-color-surface);
138
+ font-size: var(--hl-text-xs);
139
+ line-height: var(--hl-leading-snug);
140
+ color: var(--hl-color-fg-muted);
141
+ white-space: nowrap;
142
+ }
143
+
144
+ .tag[data-measure] {
145
+ visibility: hidden;
146
+ }
@@ -0,0 +1,206 @@
1
+ 'use client'
2
+
3
+ import { useEffect, useId, useLayoutEffect, useRef, useState, type ComponentPropsWithRef, type ReactNode } from 'react'
4
+ import { cx } from '../../internal/cx'
5
+ import { attract, clamp, follow, lastStep, pull, roundTo, stepKey, steps, wheelSteps, type Mark } from '../../internal/feel'
6
+ import { readMotionMs } from '../../internal/motion'
7
+ import { Roll } from '../../internal/Roll'
8
+ import { useControllable } from '../../internal/useControllable'
9
+ import { ink, sideways, tokenNumber, useOnChange, useScrub } from '../../internal/useScrub'
10
+ import styles from './RulerScrubber.module.css'
11
+
12
+ export type RulerScrubberProps = Omit<ComponentPropsWithRef<'div'>, 'defaultValue' | 'onChange' | 'children'> & {
13
+ /** Visible name of the ruler, also read by screen readers. */
14
+ label: string
15
+ /** The value, when you control it. */
16
+ value?: number
17
+ /** Starting value when you do not control it. Undefined starts at min. */
18
+ defaultValue?: number
19
+ /** Called once the ruler comes to rest on a new value. */
20
+ onValueChange?: (value: number) => void
21
+ /** Called with the value under the needle while the ruler still moves. */
22
+ onScrub?: (value: number) => void
23
+ /** Lowest value. */
24
+ min?: number
25
+ /** Highest value. */
26
+ max?: number
27
+ /** Size of one step: one tick on the ruler, and a mark it is drawn onto. */
28
+ step?: number
29
+ /** Values with a long, numbered tick. Page Up and Page Down jump between them. Defaults to every 10 steps. */
30
+ marks?: number[]
31
+ /** Values that pull harder from further away, such as a limit or the usual choice. */
32
+ strong?: number[]
33
+ /** A tag under each strong mark, such as "Allowance". */
34
+ strongLabel?: string
35
+ /** Turns the value into the readout and what screen readers hear. */
36
+ formatValue?: (value: number) => string
37
+ /** Turns a mark into the number under its tick. Defaults to formatValue. */
38
+ formatMark?: (value: number) => string
39
+ /** A line under the readout, such as what the value means. */
40
+ detail?: ReactNode
41
+ /** A short buzz on each tick while it moves, on phones that can (Android). */
42
+ haptics?: boolean
43
+ }
44
+
45
+ export function RulerScrubber({
46
+ label,
47
+ value: valueProp,
48
+ defaultValue,
49
+ onValueChange,
50
+ onScrub,
51
+ min = 0,
52
+ max = 100,
53
+ step = 1,
54
+ marks,
55
+ strong = [],
56
+ strongLabel,
57
+ formatValue = String,
58
+ formatMark,
59
+ detail,
60
+ haptics = false,
61
+ className,
62
+ ...rest
63
+ }: RulerScrubberProps) {
64
+ const labelId = useId()
65
+ const detailId = useId()
66
+ const top = lastStep(min, max, step)
67
+ const [raw, setValue] = useControllable(valueProp, defaultValue ?? min, onValueChange)
68
+ const value = clamp(raw, min, top)
69
+ const all = steps(min, max, step)
70
+ const labelled = new Set((marks ?? all.filter((_, i) => i % 10 === 0)).map((v) => roundTo(v, step, min)))
71
+ const strongSet = new Set(strong.map((v) => roundTo(v, step, min)))
72
+ const pulls: Mark[] = all.map((at) => ({ at, strong: strongSet.has(at) }))
73
+ const land = (p: number) => clamp(roundTo(p, step, min), min, top)
74
+ const markText = formatMark ?? formatValue
75
+ // The px between two ticks (a token) and how many ticks fit either side of the needle.
76
+ const [fit, setFit] = useState({ gap: 0, span: 24, width: 0, tag: 0 })
77
+ const tagRef = useRef<HTMLSpanElement>(null)
78
+ const wheel = useRef(wheelSteps())
79
+
80
+ const sc = useScrub({
81
+ value,
82
+ min,
83
+ max: top,
84
+ edge: step,
85
+ attract: (r) => attract(r, pulls, step, sc.reach()),
86
+ land,
87
+ // The hand at 4000px a second at most, so a hard throw still stops in reach.
88
+ maxSpeed: fit.gap ? (4000 / fit.gap) * step : undefined,
89
+ rest: step / 50,
90
+ // Dragging left brings higher values in, like pulling a tape.
91
+ move: (e, last) => (-(e.clientX - last.x) / (fit.gap || 1)) * step * (e.shiftKey ? 0.25 : 1),
92
+ key: (k, t) => stepKey(k, t, { min, max, step, marks: [...labelled] }),
93
+ wheel: (e, t) => {
94
+ const dx = sideways(e)
95
+ if (dx === null || !fit.gap) return null
96
+ const n = wheel.current(dx, fit.gap, e.timeStamp)
97
+ const to = clamp(roundTo(t + n * step, step, min), min, top)
98
+ return to === t && (t === min || t === top) ? null : to
99
+ },
100
+ onSettle: setValue,
101
+ haptics,
102
+ })
103
+
104
+ useLayoutEffect(() => {
105
+ const el = sc.ref.current
106
+ if (!el) return
107
+ const measure = () => {
108
+ const gap = tokenNumber(el, '--hl-ruler-tick-gap')
109
+ setFit({ gap, span: gap ? Math.ceil(el.clientWidth / 2 / gap) + 1 : 24, width: el.clientWidth, tag: tagRef.current?.offsetWidth ?? 0 })
110
+ }
111
+ measure()
112
+ const ro = new ResizeObserver(measure)
113
+ ro.observe(el)
114
+ return () => ro.disconnect()
115
+ }, [sc.ref, strongLabel])
116
+
117
+ // The ticks lean and grow only while the ruler moves. The lean eases in as it starts and settles flat as it stops.
118
+ const [lean, setLean] = useState(0)
119
+ const leaning = useRef<ReturnType<typeof follow> | null>(null)
120
+ if (!leaning.current) leaning.current = follow({ time: () => readMotionMs('fling'), onFrame: setLean })
121
+ useEffect(() => {
122
+ leaning.current?.set(sc.moving && readMotionMs('fling') > 0 ? 1 : 0)
123
+ }, [sc.moving])
124
+ useEffect(() => () => leaning.current?.stop(), [])
125
+
126
+ const shown = land(sc.at)
127
+ useOnChange(shown, onScrub)
128
+
129
+ return (
130
+ <div className={cx(styles.root, className)} {...rest}>
131
+ <div className={styles.head}>
132
+ <span id={labelId} className={styles.label}>
133
+ {label}
134
+ </span>
135
+ <Roll text={formatValue(shown)} className={styles.readout} />
136
+ {detail && (
137
+ <span id={detailId} className={styles.detail}>
138
+ {detail}
139
+ </span>
140
+ )}
141
+ </div>
142
+ <div
143
+ ref={sc.ref}
144
+ className={styles.track}
145
+ role="slider"
146
+ tabIndex={0}
147
+ aria-labelledby={labelId}
148
+ aria-describedby={detail ? detailId : undefined}
149
+ aria-valuemin={min}
150
+ aria-valuemax={top}
151
+ aria-valuenow={value}
152
+ aria-valuetext={formatValue(value)}
153
+ data-moving={sc.moving || undefined}
154
+ data-leaning={lean > 0 || undefined}
155
+ {...sc.handlers}
156
+ >
157
+ <span className={styles.needle} aria-hidden />
158
+ <div className={styles.clip} aria-hidden>
159
+ {all.map((v, i) => {
160
+ const d = (v - sc.at) / step
161
+ if (Math.abs(d) > fit.span) return null
162
+ const on = pull(d, 1)
163
+ const near = pull(d, 5)
164
+ const tilt = -Math.sign(d) * 14 * Math.sin(Math.PI * Math.min(1, Math.abs(d) / 6)) * (1 - on)
165
+ const tier = labelled.has(v) ? 'major' : i % 2 === 0 ? 'mid' : 'minor'
166
+ return (
167
+ <span key={v} className={styles.mark} style={{ transform: `translateX(calc(${d.toFixed(3)} * var(--hl-ruler-tick-gap)))` }}>
168
+ <i
169
+ className={styles.tick}
170
+ data-tier={tier}
171
+ style={{
172
+ background: ink(on, tier === 'major' ? 'var(--hl-color-fg-muted)' : 'var(--hl-color-border-strong)', 'var(--hl-color-accent)'),
173
+ transform: lean > 0 ? `rotate(${(tilt * lean).toFixed(2)}deg) scaleY(${(1 + near * 0.9 * lean).toFixed(3)})` : undefined,
174
+ }}
175
+ />
176
+ {tier === 'major' && (
177
+ <span className={styles.num} style={{ color: ink(pull(d, 2)) }}>
178
+ {markText(v)}
179
+ </span>
180
+ )}
181
+ </span>
182
+ )
183
+ })}
184
+ </div>
185
+ {strongLabel && (
186
+ <div className={styles.tags} aria-hidden>
187
+ {/* Measured once, so a tag near an end can stay whole inside the ruler. */}
188
+ <span ref={tagRef} className={styles.tag} data-measure>
189
+ {strongLabel}
190
+ </span>
191
+ {[...strongSet].map((v) => {
192
+ const x = ((v - sc.at) / step) * fit.gap
193
+ const room = (fit.width - fit.tag) / 2
194
+ if (!fit.gap || Math.abs(x) > fit.width / 2) return null
195
+ return (
196
+ <span key={v} className={styles.tag} style={{ transform: `translateX(calc(${clamp(x, -room, room).toFixed(1)}px - 50%))` }}>
197
+ {strongLabel}
198
+ </span>
199
+ )
200
+ })}
201
+ </div>
202
+ )}
203
+ </div>
204
+ </div>
205
+ )
206
+ }
@@ -0,0 +1 @@
1
+ export * from './RulerScrubber'
@@ -161,3 +161,68 @@
161
161
  @keyframes fadeOut { to { opacity: 0; } }
162
162
  @keyframes sheetIn { from { transform: translateY(100%); } }
163
163
  @keyframes sheetOut { to { transform: translateY(100%); } }
164
+
165
+ /* With detents. The sheet is as tall as its highest height and slides to show more or less of itself.
166
+ The dim follows the height, and only the highest height takes the page behind. */
167
+ .overlay[data-detents] {
168
+ opacity: 0;
169
+ pointer-events: none;
170
+ }
171
+
172
+ .overlay[data-detents][data-top] { pointer-events: auto; }
173
+
174
+ .sheet[data-detents] {
175
+ max-height: none;
176
+ transition: none;
177
+ touch-action: none;
178
+ }
179
+
180
+ .sheet[data-detents][data-dragging] { user-select: none; }
181
+ .sheet[data-detents]:not([data-top]) .body { overflow: hidden; }
182
+ .sheet[data-detents][data-top] .body { touch-action: pan-y; }
183
+
184
+ /* The grabber is the control for the height. Its width shows it: short when low, long when full. */
185
+ .grabber {
186
+ display: grid;
187
+ flex: none;
188
+ place-items: center;
189
+ align-self: center;
190
+ width: calc(var(--hl-hit-min) * 2);
191
+ height: var(--hl-hit-min);
192
+ border-radius: var(--hl-radius-full);
193
+ cursor: grab;
194
+ outline: none;
195
+ }
196
+
197
+ .sheet[data-dragging] .grabber { cursor: grabbing; }
198
+ .grabber:focus-visible { box-shadow: var(--hl-focus-ring); }
199
+
200
+ .pill {
201
+ display: block;
202
+ width: var(--hl-handle-w);
203
+ height: var(--hl-handle-h);
204
+ border-radius: var(--hl-radius-full);
205
+ background: var(--hl-color-border-strong);
206
+ transition:
207
+ width var(--hl-motion-base) var(--hl-ease-liquid),
208
+ background-color var(--hl-motion-fast) var(--hl-ease-out);
209
+ }
210
+
211
+ .grabber[data-detent="peek"] .pill { width: var(--hl-handle-w-short); }
212
+ .grabber[data-detent="full"] .pill { width: var(--hl-handle-w-active); }
213
+ .grabber:focus-visible .pill { background: var(--hl-color-accent); }
214
+
215
+ @media (hover: hover) {
216
+ .grabber:hover .pill { background: var(--hl-color-fg-muted); }
217
+ }
218
+
219
+ .sheet[data-detents] .header { padding-top: 0; }
220
+
221
+ .srOnly {
222
+ position: absolute;
223
+ width: 1px;
224
+ height: 1px;
225
+ overflow: hidden;
226
+ clip-path: inset(50%);
227
+ white-space: nowrap;
228
+ }