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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/components/Dial/Dial.module.css +303 -0
- package/src/components/Dial/Dial.tsx +291 -0
- package/src/components/Dial/index.ts +1 -0
- package/src/components/HoldToConfirm/HoldToConfirm.module.css +166 -0
- package/src/components/HoldToConfirm/HoldToConfirm.tsx +332 -0
- package/src/components/HoldToConfirm/index.ts +1 -0
- package/src/components/MorphButton/MorphButton.module.css +151 -0
- package/src/components/MorphButton/MorphButton.tsx +174 -0
- package/src/components/MorphButton/index.ts +1 -0
- package/src/components/PickerWheel/PickerWheel.module.css +123 -0
- package/src/components/PickerWheel/PickerWheel.tsx +333 -0
- package/src/components/PickerWheel/index.ts +1 -0
- package/src/components/RulerScrubber/RulerScrubber.module.css +146 -0
- package/src/components/RulerScrubber/RulerScrubber.tsx +206 -0
- package/src/components/RulerScrubber/index.ts +1 -0
- package/src/components/Sheet/Sheet.module.css +65 -0
- package/src/components/Sheet/Sheet.tsx +376 -4
- package/src/internal/Roll.module.css +31 -0
- package/src/internal/Roll.tsx +19 -0
- package/src/internal/feel.ts +419 -0
- package/src/internal/focus.ts +1 -1
- package/src/internal/layers.ts +16 -7
- package/src/internal/magnet.ts +1 -17
- package/src/internal/spring.ts +4 -52
- package/src/internal/useScrub.ts +166 -0
- package/src/tokens.css +30 -2
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState, type CSSProperties, type ReactNode, type RefObject } from 'react'
|
|
4
|
+
import { cx } from '../../internal/cx'
|
|
5
|
+
import { focusables } from '../../internal/focus'
|
|
6
|
+
import { XIcon } from '../../internal/icons'
|
|
7
|
+
import { useDismiss } from '../../internal/layers'
|
|
8
|
+
import { useControllable } from '../../internal/useControllable'
|
|
9
|
+
import styles from './MorphButton.module.css'
|
|
10
|
+
|
|
11
|
+
export type MorphButtonProps = {
|
|
12
|
+
/** The button's text. It stays on the same spot and becomes the panel's title and name. */
|
|
13
|
+
label: ReactNode
|
|
14
|
+
/** Icon before the label. */
|
|
15
|
+
icon?: ReactNode
|
|
16
|
+
/** What the panel holds, such as a short form or a list of choices. */
|
|
17
|
+
children: ReactNode
|
|
18
|
+
/** Whether the panel is open, when you control it. */
|
|
19
|
+
open?: boolean
|
|
20
|
+
/** Starting open state when you do not control it. */
|
|
21
|
+
defaultOpen?: boolean
|
|
22
|
+
/** Called with the new open state. */
|
|
23
|
+
onOpenChange?: (open: boolean) => void
|
|
24
|
+
/** How wide the panel grows. It never runs past the edge of the screen. */
|
|
25
|
+
width?: 'sm' | 'md'
|
|
26
|
+
/** Screen reader name for the close button. */
|
|
27
|
+
closeLabel?: string
|
|
28
|
+
/** Element to focus when it opens. Defaults to the first control in the panel. */
|
|
29
|
+
initialFocus?: RefObject<HTMLElement | null>
|
|
30
|
+
/** Extra class for the outer box, which keeps the button's place in the page. */
|
|
31
|
+
className?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type Corner = 'tl' | 'tr' | 'bl' | 'br'
|
|
35
|
+
|
|
36
|
+
// The space kept between the panel and the edge of the screen.
|
|
37
|
+
const EDGE = 16
|
|
38
|
+
|
|
39
|
+
export function MorphButton({
|
|
40
|
+
label,
|
|
41
|
+
icon,
|
|
42
|
+
children,
|
|
43
|
+
open: openProp,
|
|
44
|
+
defaultOpen = false,
|
|
45
|
+
onOpenChange,
|
|
46
|
+
width = 'sm',
|
|
47
|
+
closeLabel = 'Close',
|
|
48
|
+
initialFocus,
|
|
49
|
+
className,
|
|
50
|
+
}: MorphButtonProps) {
|
|
51
|
+
const id = useId()
|
|
52
|
+
const [open, setOpen] = useControllable(openProp, defaultOpen, onOpenChange)
|
|
53
|
+
const [corner, setCorner] = useState<Corner>('tl')
|
|
54
|
+
const [room, setRoom] = useState<number>()
|
|
55
|
+
const [box, setBox] = useState({ tw: 0, th: 0, pw: 0, ph: 0 })
|
|
56
|
+
const anchorRef = useRef<HTMLDivElement>(null)
|
|
57
|
+
const shellRef = useRef<HTMLDivElement>(null)
|
|
58
|
+
const triggerRef = useRef<HTMLButtonElement>(null)
|
|
59
|
+
const panelRef = useRef<HTMLDivElement>(null)
|
|
60
|
+
|
|
61
|
+
useLayoutEffect(() => {
|
|
62
|
+
const t = triggerRef.current
|
|
63
|
+
const s = shellRef.current
|
|
64
|
+
if (!t || !s) return
|
|
65
|
+
const measure = () => setBox({ tw: t.offsetWidth, th: t.offsetHeight, pw: s.offsetWidth, ph: s.offsetHeight })
|
|
66
|
+
measure()
|
|
67
|
+
const ro = new ResizeObserver(measure)
|
|
68
|
+
ro.observe(t)
|
|
69
|
+
ro.observe(s)
|
|
70
|
+
return () => ro.disconnect()
|
|
71
|
+
}, [])
|
|
72
|
+
|
|
73
|
+
// It grows toward the side with room, decided before the move starts and never during it.
|
|
74
|
+
const place = () => {
|
|
75
|
+
const a = anchorRef.current?.getBoundingClientRect()
|
|
76
|
+
const s = shellRef.current
|
|
77
|
+
if (!a || !s) return
|
|
78
|
+
const right = window.innerWidth - EDGE - a.left
|
|
79
|
+
const left = a.right - EDGE
|
|
80
|
+
const below = window.innerHeight - EDGE - a.top
|
|
81
|
+
const above = a.bottom - EDGE
|
|
82
|
+
const flipX = right < box.pw && left > right
|
|
83
|
+
const up = below < box.ph && above > below
|
|
84
|
+
setCorner(`${up ? 'b' : 't'}${flipX ? 'r' : 'l'}` as Corner)
|
|
85
|
+
setRoom(Math.max(box.tw, flipX ? left : right))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const show = (next: boolean) => {
|
|
89
|
+
if (next) place()
|
|
90
|
+
setOpen(next)
|
|
91
|
+
}
|
|
92
|
+
const backToButton = () => triggerRef.current?.focus({ preventScroll: true })
|
|
93
|
+
|
|
94
|
+
// A controlled open from outside picks its side too.
|
|
95
|
+
useLayoutEffect(() => {
|
|
96
|
+
if (open) place()
|
|
97
|
+
}, [open])
|
|
98
|
+
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
if (!open || !panelRef.current) return
|
|
101
|
+
const first = initialFocus?.current ?? focusables(panelRef.current).find((el) => !el.hasAttribute('data-skip-initial-focus')) ?? panelRef.current
|
|
102
|
+
first.focus({ preventScroll: true })
|
|
103
|
+
}, [open])
|
|
104
|
+
|
|
105
|
+
// Esc, a press outside, or focus moving on closes it. A layer it opened, such as a Select's list, is
|
|
106
|
+
// above it, so picking there does not count. Focus goes back to the button unless the press landed on
|
|
107
|
+
// something that takes focus itself.
|
|
108
|
+
useDismiss(
|
|
109
|
+
open,
|
|
110
|
+
[shellRef],
|
|
111
|
+
(reason, e) => {
|
|
112
|
+
setOpen(false)
|
|
113
|
+
const target = e.target instanceof Element ? e.target : null
|
|
114
|
+
if (reason === 'escape') backToButton()
|
|
115
|
+
else if (e.type === 'pointerdown' && !target?.closest('a[href], button, input, select, textarea, label, [tabindex]:not([tabindex="-1"])')) window.setTimeout(backToButton)
|
|
116
|
+
},
|
|
117
|
+
{ focusOut: true },
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
// Shut, the surface is cut back to the button's own box in the chosen corner.
|
|
121
|
+
const dx = Math.max(0, box.pw - box.tw)
|
|
122
|
+
const dy = Math.max(0, box.ph - box.th)
|
|
123
|
+
const shut = { tl: [0, dx, dy, 0], tr: [0, 0, dy, dx], bl: [dy, dx, 0, 0], br: [dy, 0, 0, dx] }[corner]
|
|
124
|
+
const [t, r, b, l] = open ? [0, 0, 0, 0] : shut
|
|
125
|
+
const vars = {
|
|
126
|
+
'--t': `${t}px`,
|
|
127
|
+
'--r': `${r}px`,
|
|
128
|
+
'--b': `${b}px`,
|
|
129
|
+
'--l': `${l}px`,
|
|
130
|
+
'--pill': `${box.th / 2}px`,
|
|
131
|
+
maxWidth: room,
|
|
132
|
+
} as CSSProperties
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
<div ref={anchorRef} className={cx(styles.anchor, className)} style={{ width: box.tw || undefined, height: box.th || undefined }}>
|
|
136
|
+
<div ref={shellRef} className={styles.shell} data-open={open || undefined} data-corner={corner} data-width={width} style={vars}>
|
|
137
|
+
<div className={styles.surface} aria-hidden="true" />
|
|
138
|
+
<div className={styles.content}>
|
|
139
|
+
<button
|
|
140
|
+
ref={triggerRef}
|
|
141
|
+
type="button"
|
|
142
|
+
className={styles.trigger}
|
|
143
|
+
aria-expanded={open}
|
|
144
|
+
aria-haspopup="dialog"
|
|
145
|
+
aria-controls={`${id}-panel`}
|
|
146
|
+
onClick={() => show(!open)}
|
|
147
|
+
>
|
|
148
|
+
{icon && (
|
|
149
|
+
<span className={styles.icon} aria-hidden="true">
|
|
150
|
+
{icon}
|
|
151
|
+
</span>
|
|
152
|
+
)}
|
|
153
|
+
<span id={`${id}-label`}>{label}</span>
|
|
154
|
+
</button>
|
|
155
|
+
<div ref={panelRef} id={`${id}-panel`} role="dialog" aria-labelledby={`${id}-label`} tabIndex={-1} className={styles.panel} inert={!open} aria-hidden={!open || undefined}>
|
|
156
|
+
<button
|
|
157
|
+
type="button"
|
|
158
|
+
className={styles.close}
|
|
159
|
+
aria-label={closeLabel}
|
|
160
|
+
data-skip-initial-focus
|
|
161
|
+
onClick={() => {
|
|
162
|
+
setOpen(false)
|
|
163
|
+
backToButton()
|
|
164
|
+
}}
|
|
165
|
+
>
|
|
166
|
+
<XIcon />
|
|
167
|
+
</button>
|
|
168
|
+
{children}
|
|
169
|
+
</div>
|
|
170
|
+
</div>
|
|
171
|
+
</div>
|
|
172
|
+
</div>
|
|
173
|
+
)
|
|
174
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './MorphButton'
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
.root {
|
|
2
|
+
display: grid;
|
|
3
|
+
gap: var(--hl-space-5);
|
|
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
|
+
font-variant-numeric: tabular-nums;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.detail {
|
|
29
|
+
font-size: var(--hl-text-sm);
|
|
30
|
+
color: var(--hl-color-fg-muted);
|
|
31
|
+
font-variant-numeric: tabular-nums;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.wheel {
|
|
35
|
+
display: flex;
|
|
36
|
+
justify-content: center;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.column {
|
|
40
|
+
/* The drum's radius, so each row of --hl-hit-min turns by --hl-wheel-tilt. */
|
|
41
|
+
--wheel-r: calc(var(--hl-hit-min) / 2 / tan(var(--hl-wheel-tilt) / 2));
|
|
42
|
+
position: relative;
|
|
43
|
+
flex: none;
|
|
44
|
+
width: var(--hl-wheel-w);
|
|
45
|
+
height: calc(var(--hl-wheel-rows) * var(--hl-hit-min));
|
|
46
|
+
perspective: calc(var(--wheel-r) * 7);
|
|
47
|
+
touch-action: none;
|
|
48
|
+
cursor: grab;
|
|
49
|
+
outline: none;
|
|
50
|
+
user-select: none;
|
|
51
|
+
-webkit-user-select: none;
|
|
52
|
+
-webkit-tap-highlight-color: transparent;
|
|
53
|
+
mask-image: linear-gradient(180deg, transparent, var(--hl-color-fg) 30%, var(--hl-color-fg) 70%, transparent);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.column[data-wide] {
|
|
57
|
+
width: calc(var(--hl-wheel-w) * 2);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.column[data-moving] {
|
|
61
|
+
cursor: grabbing;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* One band across the columns. Each column's part swells as a row is drawn into it. */
|
|
65
|
+
.band {
|
|
66
|
+
position: absolute;
|
|
67
|
+
left: 0;
|
|
68
|
+
right: 0;
|
|
69
|
+
top: 50%;
|
|
70
|
+
height: calc(var(--hl-hit-min) - var(--hl-space-3) + var(--hl-space-3) * var(--grip, 1));
|
|
71
|
+
transform: translateY(-50%);
|
|
72
|
+
background: var(--hl-color-surface-sunken);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.column:first-child .band {
|
|
76
|
+
border-radius: var(--hl-radius-md) 0 0 var(--hl-radius-md);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.column:last-child .band {
|
|
80
|
+
border-radius: 0 var(--hl-radius-md) var(--hl-radius-md) 0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.column:only-child .band {
|
|
84
|
+
border-radius: var(--hl-radius-md);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.column:focus-visible .band {
|
|
88
|
+
border-radius: var(--hl-radius-md);
|
|
89
|
+
box-shadow: var(--hl-focus-ring);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.drum {
|
|
93
|
+
position: absolute;
|
|
94
|
+
inset: 0;
|
|
95
|
+
transform-style: preserve-3d;
|
|
96
|
+
transform: translateZ(calc(var(--wheel-r) * -1));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.row {
|
|
100
|
+
position: absolute;
|
|
101
|
+
left: 0;
|
|
102
|
+
right: 0;
|
|
103
|
+
top: 50%;
|
|
104
|
+
height: var(--hl-hit-min);
|
|
105
|
+
margin-top: calc(var(--hl-hit-min) / -2);
|
|
106
|
+
display: grid;
|
|
107
|
+
place-items: center;
|
|
108
|
+
font-size: var(--hl-text-xl);
|
|
109
|
+
font-variant-numeric: tabular-nums;
|
|
110
|
+
white-space: nowrap;
|
|
111
|
+
backface-visibility: hidden;
|
|
112
|
+
transform: rotateX(calc(var(--d) * -1 * var(--hl-wheel-tilt))) translateZ(var(--wheel-r));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
.row[data-strong]::after {
|
|
116
|
+
content: '';
|
|
117
|
+
position: absolute;
|
|
118
|
+
right: var(--hl-space-2);
|
|
119
|
+
width: var(--hl-space-2);
|
|
120
|
+
height: var(--hl-space-2);
|
|
121
|
+
border-radius: var(--hl-radius-full);
|
|
122
|
+
background: currentColor;
|
|
123
|
+
}
|
|
@@ -0,0 +1,333 @@
|
|
|
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 { clamp, magnetTo, pull, wheelSteps, wrap } from '../../internal/feel'
|
|
6
|
+
import { useControllable } from '../../internal/useControllable'
|
|
7
|
+
import { ink, tokenNumber, useOnChange, useScrub } from '../../internal/useScrub'
|
|
8
|
+
import styles from './PickerWheel.module.css'
|
|
9
|
+
|
|
10
|
+
export type PickerWheelColumn = {
|
|
11
|
+
/** Name screen readers hear for this column, such as "Hour". */
|
|
12
|
+
label: string
|
|
13
|
+
/** The rows, top to bottom. */
|
|
14
|
+
options: string[]
|
|
15
|
+
/** The last row runs on into the first, as hours do. */
|
|
16
|
+
loop?: boolean
|
|
17
|
+
/** Rows, by index, that pull harder, such as :00 and :30, or today. */
|
|
18
|
+
strong?: number[]
|
|
19
|
+
/** Twice as wide, for words such as month names. */
|
|
20
|
+
wide?: boolean
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type PickerWheelProps = Omit<ComponentPropsWithRef<'div'>, 'defaultValue' | 'onChange' | 'children'> & {
|
|
24
|
+
/** Visible name of the whole wheel, read by screen readers as the group's name. */
|
|
25
|
+
label: string
|
|
26
|
+
/** The columns, left to right. */
|
|
27
|
+
columns: PickerWheelColumn[]
|
|
28
|
+
/** The chosen row of each column, by index, when you control it. */
|
|
29
|
+
value?: number[]
|
|
30
|
+
/** Starting rows when you do not control them. Undefined starts on the first row of each. */
|
|
31
|
+
defaultValue?: number[]
|
|
32
|
+
/** Called once a column comes to rest on a new row, with every column's row. */
|
|
33
|
+
onValueChange?: (value: number[]) => void
|
|
34
|
+
/** Called with the rows in the band while the wheel still turns. */
|
|
35
|
+
onScrub?: (value: number[]) => void
|
|
36
|
+
/** Turns the rows into a large readout above the wheel. No readout when left out. */
|
|
37
|
+
formatValue?: (value: number[]) => string
|
|
38
|
+
/** A line under the label and readout, such as what the choice means. */
|
|
39
|
+
detail?: ReactNode
|
|
40
|
+
/** A short buzz on each row while it turns, on phones that can (Android). */
|
|
41
|
+
haptics?: boolean
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Rows drawn either side of the band. At the tilt token's 18 degrees a row this far round is nearly edge on.
|
|
45
|
+
const SPAN = 4
|
|
46
|
+
|
|
47
|
+
type ColumnProps = {
|
|
48
|
+
column: PickerWheelColumn
|
|
49
|
+
index: number
|
|
50
|
+
onChange: (index: number) => void
|
|
51
|
+
onShown: (index: number) => void
|
|
52
|
+
haptics: boolean
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** One column: its rows are the marks. Free between rows, drawn in near one, harder near a strong row. */
|
|
56
|
+
function Column({ column, index, onChange, onShown, haptics }: ColumnProps) {
|
|
57
|
+
const { options, loop = false } = column
|
|
58
|
+
const n = options.length
|
|
59
|
+
const strong = new Set(column.strong ?? [])
|
|
60
|
+
const typed = useRef({ text: '', at: 0 })
|
|
61
|
+
const row = useRef(0)
|
|
62
|
+
const wheel = useRef(wheelSteps())
|
|
63
|
+
// In a loop the position runs on past the last row; the row shown is the position folded into the list.
|
|
64
|
+
const nearest = (t: number, i: number) => (loop ? Math.round(t) + ((((i - wrap(Math.round(t), n)) % n) + n + Math.floor(n / 2)) % n) - Math.floor(n / 2) : i)
|
|
65
|
+
const fit = (v: number) => (loop ? v : clamp(v, 0, n - 1))
|
|
66
|
+
const rested = useRef(index)
|
|
67
|
+
const strongFrom = (t: number, dir: 1 | -1) => {
|
|
68
|
+
const i = wrap(Math.round(t), n)
|
|
69
|
+
for (let d = 1; d < n; d++) if (strong.has(wrap(i + dir * d, n)) && (loop || (i + dir * d >= 0 && i + dir * d < n))) return fit(Math.round(t) + dir * d)
|
|
70
|
+
return fit(Math.round(t) + dir * 5)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const sc = useScrub({
|
|
74
|
+
value: nearest(rested.current, clamp(index, 0, n - 1)),
|
|
75
|
+
min: loop ? undefined : 0,
|
|
76
|
+
max: loop ? undefined : n - 1,
|
|
77
|
+
edge: 0.6,
|
|
78
|
+
attract: (r) => {
|
|
79
|
+
const k = Math.round(r)
|
|
80
|
+
const { reach = 0.35, strong: far = 1.5 } = sc.reach()
|
|
81
|
+
// Rows are the marks, so a strong row reaches at most half a row: the rows beside it stay easy to stop on.
|
|
82
|
+
return magnetTo(r, [k], strong.has(wrap(k, n)) ? Math.min(far, 0.5) : reach)
|
|
83
|
+
},
|
|
84
|
+
land: (p) => fit(Math.round(p)),
|
|
85
|
+
maxSpeed: 40,
|
|
86
|
+
rest: 0.01,
|
|
87
|
+
move: (e, last) => -(e.clientY - last.y) / (row.current || 1),
|
|
88
|
+
// The APG spinbutton keys: Up is the next, higher value and Down the one before.
|
|
89
|
+
key: (k, t) => {
|
|
90
|
+
if (k === 'ArrowUp') return fit(Math.round(t) + 1)
|
|
91
|
+
if (k === 'ArrowDown') return fit(Math.round(t) - 1)
|
|
92
|
+
if (k === 'PageUp') return strongFrom(t, 1)
|
|
93
|
+
if (k === 'PageDown') return strongFrom(t, -1)
|
|
94
|
+
if (k === 'Home') return nearest(t, 0)
|
|
95
|
+
if (k === 'End') return nearest(t, n - 1)
|
|
96
|
+
if (k.length === 1 && /\S/.test(k)) {
|
|
97
|
+
const now = performance.now()
|
|
98
|
+
typed.current = { text: (now - typed.current.at < 800 ? typed.current.text : '') + k.toLowerCase(), at: now }
|
|
99
|
+
const q = typed.current.text
|
|
100
|
+
// A padded number answers to its plain digits, so "5" finds "05" before "50".
|
|
101
|
+
const said = (o: string) => (/^\d+$/.test(o) ? String(Number(o)) : o.toLowerCase())
|
|
102
|
+
const exact = options.findIndex((o) => o.toLowerCase() === q || said(o) === q)
|
|
103
|
+
const hit = exact >= 0 ? exact : options.findIndex((o) => o.toLowerCase().startsWith(q))
|
|
104
|
+
return hit >= 0 ? nearest(t, hit) : null
|
|
105
|
+
}
|
|
106
|
+
return null
|
|
107
|
+
},
|
|
108
|
+
tap: (e, at) => {
|
|
109
|
+
const r = e.currentTarget.getBoundingClientRect()
|
|
110
|
+
const tilt = (tokenNumber(e.currentTarget, '--hl-wheel-tilt') * Math.PI) / 180
|
|
111
|
+
const radius = row.current / 2 / Math.tan(tilt / 2)
|
|
112
|
+
const dy = e.clientY - (r.top + r.height / 2)
|
|
113
|
+
if (!tilt || !radius) return null
|
|
114
|
+
return fit(Math.round(at + Math.asin(clamp(dy / radius, -1, 1)) / tilt))
|
|
115
|
+
},
|
|
116
|
+
// Only a focused column takes the wheel, and at an end it lets the page scroll on.
|
|
117
|
+
wheel: (e, t, focused) => {
|
|
118
|
+
if (!focused || !e.deltaY) return null
|
|
119
|
+
const dir = Math.sign(e.deltaY)
|
|
120
|
+
if (!loop && ((dir > 0 && t >= n - 1) || (dir < 0 && t <= 0))) return null
|
|
121
|
+
return fit(t + wheel.current(e.deltaY * (e.deltaMode === 1 ? 40 : 1)))
|
|
122
|
+
},
|
|
123
|
+
onSettle: (to) => {
|
|
124
|
+
rested.current = to
|
|
125
|
+
onChange(wrap(to, n))
|
|
126
|
+
},
|
|
127
|
+
haptics,
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
useLayoutEffect(() => {
|
|
131
|
+
row.current = tokenNumber(sc.ref.current, '--hl-hit-min')
|
|
132
|
+
}, [sc.ref])
|
|
133
|
+
|
|
134
|
+
// The list got shorter (31 days to 30): stay on the last row instead of jumping.
|
|
135
|
+
const lastN = useRef(n)
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
if (lastN.current === n) return
|
|
138
|
+
const i = Math.min(wrap(Math.round(sc.target()), lastN.current), n - 1)
|
|
139
|
+
lastN.current = n
|
|
140
|
+
rested.current = i
|
|
141
|
+
sc.place(i)
|
|
142
|
+
}, [n, sc])
|
|
143
|
+
|
|
144
|
+
const settled = wrap(Math.round(rested.current), n)
|
|
145
|
+
const shown = wrap(Math.round(sc.at), n)
|
|
146
|
+
const told = useRef(onShown)
|
|
147
|
+
told.current = onShown
|
|
148
|
+
useEffect(() => told.current(shown), [shown])
|
|
149
|
+
|
|
150
|
+
const grip = pull(sc.at - Math.round(sc.at), 0.5)
|
|
151
|
+
const rows: { k: number; d: number; i: number }[] = []
|
|
152
|
+
for (let k = Math.floor(sc.at) - SPAN; k <= Math.ceil(sc.at) + SPAN; k++) {
|
|
153
|
+
if (!loop && (k < 0 || k > n - 1)) continue
|
|
154
|
+
rows.push({ k, d: k - sc.at, i: wrap(k, n) })
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<div
|
|
159
|
+
ref={sc.ref}
|
|
160
|
+
className={styles.column}
|
|
161
|
+
role="spinbutton"
|
|
162
|
+
tabIndex={0}
|
|
163
|
+
aria-label={column.label}
|
|
164
|
+
aria-valuenow={settled}
|
|
165
|
+
aria-valuetext={options[settled]}
|
|
166
|
+
aria-valuemin={0}
|
|
167
|
+
aria-valuemax={n - 1}
|
|
168
|
+
data-wide={column.wide || undefined}
|
|
169
|
+
data-moving={sc.moving || undefined}
|
|
170
|
+
{...sc.handlers}
|
|
171
|
+
>
|
|
172
|
+
<span className={styles.band} style={{ ['--grip' as string]: grip.toFixed(3) }} aria-hidden />
|
|
173
|
+
<div className={styles.drum} aria-hidden>
|
|
174
|
+
{rows.map(({ k, d, i }) => (
|
|
175
|
+
<span key={k} className={styles.row} data-strong={strong.has(i) || undefined} style={{ ['--d' as string]: d.toFixed(3), color: ink(pull(d, 1)) }}>
|
|
176
|
+
{options[i]}
|
|
177
|
+
</span>
|
|
178
|
+
))}
|
|
179
|
+
</div>
|
|
180
|
+
</div>
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function PickerWheel({ label, columns, value: valueProp, defaultValue, onValueChange, onScrub, formatValue, detail, haptics = false, className, ...rest }: PickerWheelProps) {
|
|
185
|
+
const labelId = useId()
|
|
186
|
+
const detailId = useId()
|
|
187
|
+
const [raw, setValue] = useControllable(valueProp, defaultValue ?? columns.map(() => 0), onValueChange)
|
|
188
|
+
const value = columns.map((c, i) => clamp(raw[i] ?? 0, 0, c.options.length - 1))
|
|
189
|
+
const latest = useRef(value)
|
|
190
|
+
latest.current = value
|
|
191
|
+
const [live, setLive] = useState(value)
|
|
192
|
+
useOnChange(live, onScrub, live.join(','))
|
|
193
|
+
|
|
194
|
+
return (
|
|
195
|
+
<div className={cx(styles.root, className)} {...rest}>
|
|
196
|
+
<div className={styles.head}>
|
|
197
|
+
<span id={labelId} className={styles.label}>
|
|
198
|
+
{label}
|
|
199
|
+
</span>
|
|
200
|
+
{formatValue && <span className={styles.readout}>{formatValue(live)}</span>}
|
|
201
|
+
{detail && (
|
|
202
|
+
<span id={detailId} className={styles.detail}>
|
|
203
|
+
{detail}
|
|
204
|
+
</span>
|
|
205
|
+
)}
|
|
206
|
+
</div>
|
|
207
|
+
<div className={styles.wheel} role="group" aria-labelledby={labelId} aria-describedby={detail ? detailId : undefined}>
|
|
208
|
+
{columns.map((c, i) => (
|
|
209
|
+
<Column
|
|
210
|
+
key={i}
|
|
211
|
+
column={c}
|
|
212
|
+
index={value[i]}
|
|
213
|
+
haptics={haptics}
|
|
214
|
+
onChange={(row) => {
|
|
215
|
+
if (latest.current[i] === row) return
|
|
216
|
+
const next = latest.current.map((v, j) => (j === i ? row : v))
|
|
217
|
+
latest.current = next
|
|
218
|
+
setValue(next)
|
|
219
|
+
}}
|
|
220
|
+
onShown={(row) => setLive((l) => (l[i] === row ? l : l.map((v, j) => (j === i ? row : v))))}
|
|
221
|
+
/>
|
|
222
|
+
))}
|
|
223
|
+
</div>
|
|
224
|
+
</div>
|
|
225
|
+
)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const pad = (v: number) => String(v).padStart(2, '0')
|
|
229
|
+
|
|
230
|
+
export type TimeWheelProps = Omit<PickerWheelProps, 'columns' | 'value' | 'defaultValue' | 'onValueChange' | 'onScrub' | 'formatValue'> & {
|
|
231
|
+
/** The time as "HH:mm" in 24 hours, when you control it. */
|
|
232
|
+
value?: string
|
|
233
|
+
/** Starting time when you do not control it. */
|
|
234
|
+
defaultValue?: string
|
|
235
|
+
/** Called once the wheel comes to rest on a new time, as "HH:mm". */
|
|
236
|
+
onValueChange?: (value: string) => void
|
|
237
|
+
/** Called with the time in the band while the wheel still turns. */
|
|
238
|
+
onScrub?: (value: string) => void
|
|
239
|
+
/** 12 shows AM and PM; 24 shows hours 00 to 23. */
|
|
240
|
+
hourCycle?: 12 | 24
|
|
241
|
+
/** Minutes between two rows. */
|
|
242
|
+
minuteStep?: number
|
|
243
|
+
/** Minutes that pull harder. */
|
|
244
|
+
strong?: number[]
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** PickerWheel set up for a time: hour, minute and, on a 12 hour clock, AM or PM. */
|
|
248
|
+
export function TimeWheel({ value: valueProp, defaultValue = '09:00', onValueChange, onScrub, hourCycle = 12, minuteStep = 1, strong = [0, 30], ...rest }: TimeWheelProps) {
|
|
249
|
+
const [value, setValue] = useControllable(valueProp, defaultValue, onValueChange)
|
|
250
|
+
const twelve = hourCycle === 12
|
|
251
|
+
const minutes = Array.from({ length: Math.ceil(60 / minuteStep) }, (_, i) => i * minuteStep)
|
|
252
|
+
const columns: PickerWheelColumn[] = [
|
|
253
|
+
{ label: 'Hour', options: twelve ? Array.from({ length: 12 }, (_, i) => String(i + 1)) : Array.from({ length: 24 }, (_, i) => pad(i)), loop: true },
|
|
254
|
+
{ label: 'Minute', options: minutes.map(pad), loop: true, strong: strong.map((m) => minutes.indexOf(m)).filter((i) => i >= 0) },
|
|
255
|
+
...(twelve ? [{ label: 'AM or PM', options: ['AM', 'PM'] }] : []),
|
|
256
|
+
]
|
|
257
|
+
const toRows = (hhmm: string) => {
|
|
258
|
+
const [h = 0, m = 0] = hhmm.split(':').map(Number)
|
|
259
|
+
const mi = clamp(Math.round(m / minuteStep), 0, minutes.length - 1)
|
|
260
|
+
return twelve ? [(h % 12 || 12) - 1, mi, h >= 12 ? 1 : 0] : [h, mi]
|
|
261
|
+
}
|
|
262
|
+
const toTime = (rows: number[]) => {
|
|
263
|
+
const h = twelve ? ((rows[0] + 1) % 12) + (rows[2] ? 12 : 0) : rows[0]
|
|
264
|
+
return `${pad(h)}:${pad(minutes[rows[1]])}`
|
|
265
|
+
}
|
|
266
|
+
const show = (rows: number[]) => (twelve ? `${rows[0] + 1}:${pad(minutes[rows[1]])} ${rows[2] ? 'PM' : 'AM'}` : `${pad(rows[0])}:${pad(minutes[rows[1]])}`)
|
|
267
|
+
return <PickerWheel {...rest} columns={columns} value={toRows(value)} onValueChange={(rows) => setValue(toTime(rows))} onScrub={onScrub && ((rows) => onScrub(toTime(rows)))} formatValue={show} />
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
|
|
271
|
+
const daysIn = (month: number, year: number) => new Date(year, month + 1, 0).getDate()
|
|
272
|
+
const iso = (y: number, m: number, d: number) => `${y}-${pad(m + 1)}-${pad(d)}`
|
|
273
|
+
const parse = (s: string) => {
|
|
274
|
+
const [y, m, d] = s.split('-').map(Number)
|
|
275
|
+
return { y, m: m - 1, d }
|
|
276
|
+
}
|
|
277
|
+
const today = () => {
|
|
278
|
+
const t = new Date()
|
|
279
|
+
return iso(t.getFullYear(), t.getMonth(), t.getDate())
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export type DateWheelProps = Omit<PickerWheelProps, 'columns' | 'value' | 'defaultValue' | 'onValueChange' | 'onScrub' | 'formatValue'> & {
|
|
283
|
+
/** The date as "YYYY-MM-DD", when you control it. */
|
|
284
|
+
value?: string
|
|
285
|
+
/** Starting date when you do not control it. Undefined starts today. */
|
|
286
|
+
defaultValue?: string
|
|
287
|
+
/** Called once the wheel comes to rest on a new date, as "YYYY-MM-DD". */
|
|
288
|
+
onValueChange?: (value: string) => void
|
|
289
|
+
/** Called with the date in the band while the wheel still turns. */
|
|
290
|
+
onScrub?: (value: string) => void
|
|
291
|
+
/** The first year on the wheel. Undefined is 100 years ago. */
|
|
292
|
+
fromYear?: number
|
|
293
|
+
/** The last year on the wheel. Undefined is 10 years from now. */
|
|
294
|
+
toYear?: number
|
|
295
|
+
/** Dates, as "YYYY-MM-DD", that pull harder. Undefined is today. */
|
|
296
|
+
strong?: string[]
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** PickerWheel set up for a date: day, month and year, with each month's real length. */
|
|
300
|
+
export function DateWheel({ value: valueProp, defaultValue, onValueChange, onScrub, fromYear, toYear, strong, ...rest }: DateWheelProps) {
|
|
301
|
+
const now = today()
|
|
302
|
+
const [value, setValue] = useControllable(valueProp, defaultValue ?? now, onValueChange)
|
|
303
|
+
const first = fromYear ?? parse(now).y - 100
|
|
304
|
+
const last = toYear ?? parse(now).y + 10
|
|
305
|
+
const years = Array.from({ length: Math.max(1, last - first + 1) }, (_, i) => first + i)
|
|
306
|
+
const at = parse(value)
|
|
307
|
+
const y = clamp(at.y, first, last)
|
|
308
|
+
const count = daysIn(at.m, y)
|
|
309
|
+
const marks = (strong ?? [now]).map(parse)
|
|
310
|
+
const columns: PickerWheelColumn[] = [
|
|
311
|
+
{ label: 'Day', options: Array.from({ length: count }, (_, i) => String(i + 1)), loop: true, strong: marks.filter((s) => s.y === y && s.m === at.m).map((s) => s.d - 1) },
|
|
312
|
+
{ label: 'Month', options: MONTHS, loop: true, wide: true, strong: marks.filter((s) => s.y === y).map((s) => s.m) },
|
|
313
|
+
{ label: 'Year', options: years.map(String), strong: marks.map((s) => years.indexOf(s.y)).filter((i) => i >= 0) },
|
|
314
|
+
]
|
|
315
|
+
const toDate = (rows: number[]) => {
|
|
316
|
+
const yy = years[rows[2]]
|
|
317
|
+
return iso(yy, rows[1], Math.min(rows[0] + 1, daysIn(rows[1], yy)))
|
|
318
|
+
}
|
|
319
|
+
const show = (rows: number[]) => {
|
|
320
|
+
const p = parse(toDate(rows))
|
|
321
|
+
return new Date(p.y, p.m, p.d).toLocaleDateString('en-GB', { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' })
|
|
322
|
+
}
|
|
323
|
+
return (
|
|
324
|
+
<PickerWheel
|
|
325
|
+
{...rest}
|
|
326
|
+
columns={columns}
|
|
327
|
+
value={[Math.min(at.d, count) - 1, at.m, years.indexOf(y)]}
|
|
328
|
+
onValueChange={(rows) => setValue(toDate(rows))}
|
|
329
|
+
onScrub={onScrub && ((rows) => onScrub(toDate(rows)))}
|
|
330
|
+
formatValue={show}
|
|
331
|
+
/>
|
|
332
|
+
)
|
|
333
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './PickerWheel'
|