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,166 @@
1
+ import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent, type PointerEvent } from 'react'
2
+ import { createScrub, type Reach, type ScrubOptions } from './feel'
3
+ import { readMotionMs } from './motion'
4
+
5
+ // The React side of feel.ts for Dial, RulerScrubber and PickerWheel: pointer, keys and wheel in, the
6
+ // drawn position out. The motion itself lives in feel.ts.
7
+
8
+ type Last = { x: number; y: number }
9
+
10
+ export type UseScrubOptions = Pick<ScrubOptions, 'min' | 'max' | 'edge' | 'attract' | 'land' | 'coast' | 'maxSpeed' | 'rest'> & {
11
+ /** The value from props: a change from outside glides there. */
12
+ value: number
13
+ onSettle: (value: number) => void
14
+ /** A new resting value came under the hand while it moves. */
15
+ onMark?: (value: number) => void
16
+ /** How far a pointer move turns the value, in its units. */
17
+ move: (e: PointerEvent<HTMLElement>, last: Last, el: HTMLElement) => number
18
+ /** The value a key goes to, or null to leave the key alone. */
19
+ key: (key: string, target: number) => number | null
20
+ /** The value a plain tap goes to, or null to land where it is. */
21
+ tap?: (e: PointerEvent<HTMLElement>, at: number) => number | null
22
+ /** The value a wheel turn goes to, or null to let the page scroll. */
23
+ wheel?: (e: WheelEvent, target: number, focused: boolean) => number | null
24
+ /** A short buzz on each new value under the hand, where the phone can (Android). */
25
+ haptics?: boolean
26
+ }
27
+
28
+ type Press = { id: number; sx: number; sy: number; x: number; y: number; moved: boolean }
29
+
30
+ // A press is not a drag until it moves this far, so a tap stays a tap.
31
+ const SLOP = 4
32
+
33
+ export function useScrub(o: UseScrubOptions) {
34
+ const opt = useRef(o)
35
+ opt.current = o
36
+ const [at, setAt] = useState(o.value)
37
+ const [moving, setMoving] = useState(false)
38
+ const press = useRef<Press | null>(null)
39
+ const ref = useRef<HTMLDivElement>(null)
40
+ const scrub = useRef<ReturnType<typeof createScrub> | null>(null)
41
+ if (!scrub.current) {
42
+ scrub.current = createScrub({
43
+ value: o.value,
44
+ land: (p) => opt.current.land(p),
45
+ period: () => readMotionMs('fling'),
46
+ onFrame: setAt,
47
+ onMoving: setMoving,
48
+ onMark: (v) => {
49
+ opt.current.onMark?.(v)
50
+ if (opt.current.haptics) navigator.vibrate?.(5)
51
+ },
52
+ // Only a new value is news: a tap, Home at the start, or a drag back to where it was says nothing.
53
+ onSettle: (v) => v !== opt.current.value && opt.current.onSettle(v),
54
+ })
55
+ }
56
+ const s = scrub.current
57
+ const reach = useRef<Reach>({})
58
+ useLayoutEffect(() => {
59
+ const el = ref.current
60
+ reach.current = { reach: tokenNumber(el, '--hl-magnet-reach') || undefined, strong: tokenNumber(el, '--hl-magnet-strong') || undefined }
61
+ }, [])
62
+ s.update({ min: o.min, max: o.max, edge: o.edge, attract: o.attract, coast: o.coast, maxSpeed: o.maxSpeed, rest: o.rest })
63
+
64
+ // A value set from outside (a controlled prop, a reset) glides there. So does one the parent kept
65
+ // or changed when it came to rest.
66
+ useEffect(() => {
67
+ if (press.current || s.moving || o.value === s.target) return
68
+ s.go(o.value)
69
+ }, [o.value, s, moving])
70
+
71
+ const wheels = !!o.wheel
72
+
73
+ useEffect(() => {
74
+ const el = ref.current
75
+ if (!el || !wheels) return
76
+ const onWheel = (e: WheelEvent) => {
77
+ const t = opt.current.wheel?.(e, s.target, document.activeElement === el) ?? null
78
+ if (t === null) return
79
+ e.preventDefault()
80
+ if (t !== s.target) s.go(t)
81
+ }
82
+ el.addEventListener('wheel', onWheel, { passive: false })
83
+ return () => el.removeEventListener('wheel', onWheel)
84
+ }, [s, wheels])
85
+ useEffect(() => () => s.stop(), [s])
86
+
87
+ const onPointerDown = (e: PointerEvent<HTMLElement>) => {
88
+ if (e.pointerType === 'mouse' && e.button !== 0) return
89
+ try {
90
+ e.currentTarget.setPointerCapture(e.pointerId)
91
+ } catch {
92
+ // A pointer the browser no longer knows (a synthetic one) cannot be captured, and needs no capture.
93
+ }
94
+ s.grab(e.timeStamp)
95
+ press.current = { id: e.pointerId, sx: e.clientX, sy: e.clientY, x: e.clientX, y: e.clientY, moved: false }
96
+ }
97
+ const onPointerMove = (e: PointerEvent<HTMLElement>) => {
98
+ const p = press.current
99
+ if (!p || p.id !== e.pointerId) return
100
+ if (!p.moved) {
101
+ if (Math.hypot(e.clientX - p.sx, e.clientY - p.sy) < SLOP) return
102
+ p.moved = true
103
+ }
104
+ const delta = opt.current.move(e, p, e.currentTarget)
105
+ p.x = e.clientX
106
+ p.y = e.clientY
107
+ s.drag(delta, e.timeStamp)
108
+ }
109
+ const onPointerUp = (e: PointerEvent<HTMLElement>) => {
110
+ const p = press.current
111
+ if (!p || p.id !== e.pointerId) return
112
+ press.current = null
113
+ if (e.type === 'pointercancel') return s.cancel()
114
+ if (p.moved) return s.release(e.timeStamp)
115
+ const t = opt.current.tap?.(e, s.at)
116
+ if (t === null || t === undefined) s.release(e.timeStamp)
117
+ else s.go(t)
118
+ }
119
+ const onKeyDown = (e: KeyboardEvent<HTMLElement>) => {
120
+ if (e.altKey || e.ctrlKey || e.metaKey) return
121
+ const t = opt.current.key(e.key, s.target)
122
+ if (t === null) return
123
+ e.preventDefault()
124
+ s.go(t)
125
+ }
126
+
127
+ return {
128
+ ref,
129
+ /** Where it is drawn now. */
130
+ at,
131
+ moving,
132
+ go: (to: number) => s.go(to),
133
+ place: (to: number) => s.place(to),
134
+ target: () => s.target,
135
+ /** How far marks reach, from --hl-magnet-reach and --hl-magnet-strong. */
136
+ reach: () => reach.current,
137
+ handlers: { onPointerDown, onPointerMove, onPointerUp, onPointerCancel: onPointerUp, onKeyDown },
138
+ }
139
+ }
140
+
141
+ /** A colour between two tokens by the pull, so ink fills in as a mark is drawn in. */
142
+ export const ink = (p: number, from = 'var(--hl-color-fg-muted)', to = 'var(--hl-color-fg)') => `color-mix(in srgb, ${to} ${Math.round(p * 100)}%, ${from})`
143
+
144
+ /** A number token read from `el`, such as a px size or a share. */
145
+ export const tokenNumber = (el: Element | null, name: string) => (el ? parseFloat(getComputedStyle(el).getPropertyValue(name)) || 0 : 0)
146
+
147
+ /** A wheel turn's sideways part in px: a sideways swipe, or Shift and the wheel. Null when it is mostly up and down, so the page scrolls. */
148
+ export function sideways(e: WheelEvent) {
149
+ const line = e.deltaMode === 1 ? 40 : 1
150
+ const dx = (e.shiftKey && !e.deltaX ? e.deltaY : e.deltaX) * line
151
+ const dy = e.shiftKey ? 0 : e.deltaY * line
152
+ return Math.abs(dx) > Math.abs(dy) ? dx : null
153
+ }
154
+
155
+ /** Calls `fn` each time `value` changes (by `key`), but not for the value it starts with. */
156
+ export function useOnChange<T>(value: T, fn: ((value: T) => void) | undefined, key = String(value)) {
157
+ const latest = useRef(fn)
158
+ latest.current = fn
159
+ const seen = useRef(key)
160
+ useEffect(() => {
161
+ if (seen.current === key) return
162
+ seen.current = key
163
+ latest.current?.(value)
164
+ // eslint-disable-next-line react-hooks/exhaustive-deps
165
+ }, [key])
166
+ }
package/src/tokens.css CHANGED
@@ -142,6 +142,8 @@
142
142
  --hl-mini-button: 24px;
143
143
  --hl-handle-w: 40px;
144
144
  --hl-handle-w-active: 56px;
145
+ /* A sheet's grabber at its lowest height. Its width shows the height: short, then --hl-handle-w, then -active */
146
+ --hl-handle-w-short: 24px;
145
147
  --hl-handle-h: 5px;
146
148
  --hl-empty-icon: 64px;
147
149
  --hl-empty-w: 420px;
@@ -180,6 +182,9 @@
180
182
  --hl-motion-fling: calc(280ms * var(--hl-motion-scale));
181
183
  --hl-motion-stagger: calc(30ms * var(--hl-motion-scale));
182
184
  --hl-motion-delay-tooltip: 400ms;
185
+ /* Hold to confirm: how long a press must last. A timer, not motion, so reduced motion keeps it */
186
+ --hl-hold-time: 1500ms;
187
+ --hl-hold-stroke: 2px;
183
188
  --hl-motion-shimmer: 1400ms;
184
189
  --hl-motion-spin: 800ms;
185
190
  --hl-motion-indeterminate: 1500ms;
@@ -356,8 +361,15 @@
356
361
  --hl-schedule-hour: 56px;
357
362
  --hl-schedule-gutter: 56px;
358
363
  --hl-schedule-min-w: 520px;
359
- /* How far a later, overlapping event steps in, so the one under it keeps its title in view */
364
+ /* Unused since 0.34.0, when overlapping events moved into lanes. Kept so overrides of it do not break. */
360
365
  --hl-schedule-indent: 16px;
366
+ /* One stripe and its gap on a hatched event, and how far an event lifts on hover */
367
+ --hl-schedule-hatch: var(--hl-dot);
368
+ --hl-schedule-lift: var(--hl-space-1);
369
+ /* The narrowest a lane gets on a day with overlaps. When the busy days cannot get this, fewer days show. */
370
+ --hl-schedule-lane-min: 112px;
371
+ /* The narrowest a day with no overlaps gets, so a quiet week keeps all seven days from --hl-schedule-min-w up */
372
+ --hl-schedule-day-min: 64px;
361
373
 
362
374
  /* Smarter inputs */
363
375
  --hl-pin-w: 44px;
@@ -407,9 +419,12 @@
407
419
  --hl-fab-gap: 16px;
408
420
  --hl-sidebar-w: 248px;
409
421
  --hl-sidebar-w-collapsed: 64px;
410
- --hl-tabbar-h: 64px;
422
+ --hl-tabbar-h: 56px;
411
423
  --hl-tabbar-pill-w: 56px;
412
424
  --hl-tabbar-pill-h: 30px;
425
+ /* With labels on the chosen tab only: its share of the row against the others' 1, and how long its label may run */
426
+ --hl-tabbar-active-grow: 2.4;
427
+ --hl-tabbar-label-max: 12ch;
413
428
  --hl-scrollbar-w: 6px;
414
429
  --hl-scrollbar-w-active: 10px;
415
430
  --hl-scrollbar-min: 24px;
@@ -570,6 +585,19 @@
570
585
  /* Touch lists: one swipe action's width, and how far a pull must go before it refreshes */
571
586
  --hl-swipe-action-w: 80px;
572
587
  --hl-pull-distance: 72px;
588
+
589
+ /* Magnetic marks on Dial, RulerScrubber and PickerWheel: how far a mark reaches, and a strong one,
590
+ each as a share of the gap between two marks */
591
+ --hl-magnet-reach: 0.35;
592
+ --hl-magnet-strong: 1.5;
593
+ /* Dial: the knob, the ring round it, and the speed arc's radius and the length of one step on its rim */
594
+ --hl-dial-size: 208px;
595
+ --hl-dial-ring: 244px;
596
+ --hl-dial-arc-radius: 620px;
597
+ --hl-dial-arc-gap: 20px;
598
+ /* RulerScrubber: the space between two ticks, and its height (the speed arc's too) */
599
+ --hl-ruler-tick-gap: 12px;
600
+ --hl-ruler-h: 120px;
573
601
  }
574
602
 
575
603
  :root,