gooee 0.48.0 → 0.50.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/package.json +1 -1
- package/src/components/HoldToConfirm/HoldToConfirm.tsx +2 -1
- package/src/components/LivePill/LivePill.tsx +1 -1
- package/src/internal/fling.ts +29 -106
- package/src/internal/focus.ts +2 -34
- package/src/internal/liquid.ts +26 -213
- package/src/internal/liquidMatter.ts +9 -181
- package/src/internal/liquidMore.ts +6 -669
- package/src/internal/motion/fling.ts +147 -0
- package/src/internal/motion/focus.ts +35 -0
- package/src/internal/motion/index.ts +20 -0
- package/src/internal/motion/liquid.ts +241 -0
- package/src/internal/motion/liquidMatter.ts +198 -0
- package/src/internal/motion/liquidMore.ts +676 -0
- package/src/internal/motion/scrollFrame.ts +152 -0
- package/src/internal/motion/scrubber.ts +154 -0
- package/src/internal/motion/softPress.ts +334 -0
- package/src/internal/scrollFrame.ts +4 -145
- package/src/internal/softPress.ts +27 -289
- package/src/internal/useScrub.ts +14 -107
|
@@ -1,132 +1,9 @@
|
|
|
1
1
|
import { useLayoutEffect, useRef } from 'react'
|
|
2
|
+
import { onScrollFrame, type ScrollReader } from './motion/scrollFrame'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
// after a moment of stillness; a scroll, a resize or a change in the page's size wakes it again.
|
|
4
|
+
export { bandProgress, easeToward, readScrollTokens, scrollTokens, type ScrollFrame, type ScrollReader, type ScrollTokens } from './motion/scrollFrame'
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
export type ScrollFrame = { y: number; v: number; dt: number }
|
|
8
|
-
|
|
9
|
-
/** Reads layout and returns the writes, so every part reads before any part writes. */
|
|
10
|
-
export type ScrollReader = (frame: ScrollFrame) => void | (() => void)
|
|
11
|
-
|
|
12
|
-
export type ScrollTokens = { ease: number; start: number; end: number; turn: number; turnNarrow: number }
|
|
13
|
-
|
|
14
|
-
const readers = new Set<{ current: ScrollReader }>()
|
|
15
|
-
let frame: ScrollFrame = { y: 0, v: 0, dt: 0 }
|
|
16
|
-
let last = { y: 0, t: -1 }
|
|
17
|
-
let raf = 0
|
|
18
|
-
let still = 0
|
|
19
|
-
let observer: ResizeObserver | null = null
|
|
20
|
-
let reduce: MediaQueryList | null = null
|
|
21
|
-
let tokens: ScrollTokens | null = null
|
|
22
|
-
|
|
23
|
-
export function readScrollTokens(): ScrollTokens {
|
|
24
|
-
const s = getComputedStyle(document.documentElement)
|
|
25
|
-
const n = (name: string) => parseFloat(s.getPropertyValue(name))
|
|
26
|
-
return { ease: n('--gooee-scroll-ease'), start: n('--gooee-draft-start'), end: n('--gooee-draft-end'), turn: n('--gooee-story-turn'), turnNarrow: n('--gooee-story-turn-narrow') }
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** The tokens, read once each time the loop starts or the window changes size. */
|
|
30
|
-
export const scrollTokens = () => (tokens ??= readScrollTokens())
|
|
31
|
-
|
|
32
|
-
/** Progress from 0 to 1 as an element's top moves from `start` to `end` (shares of the screen height). */
|
|
33
|
-
export function bandProgress(top: number, start: number, end: number) {
|
|
34
|
-
const p = (start - top / window.innerHeight) / (start - end)
|
|
35
|
-
return Math.max(0, Math.min(1, p))
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Set by a part that is still easing, so the loop keeps going after the page stops.
|
|
39
|
-
let settling = false
|
|
40
|
-
|
|
41
|
-
/** Moves `from` toward `to` by the scroll ease, the same weight at any frame rate. On mount it lands at once. */
|
|
42
|
-
export function easeToward(from: number, to: number, dt: number) {
|
|
43
|
-
if (dt <= 0) return to
|
|
44
|
-
const k = 1 - Math.pow(1 - scrollTokens().ease, Math.min(4, dt / (1000 / 60)))
|
|
45
|
-
const next = from + (to - from) * k
|
|
46
|
-
if (Math.abs(to - next) < 0.0005) return to
|
|
47
|
-
settling = true
|
|
48
|
-
return next
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// One part that throws must not stop the loop for every other part, so each runs on its own and the
|
|
52
|
-
// error is reported after the frame.
|
|
53
|
-
function guard<T>(fn: () => T): T | undefined {
|
|
54
|
-
try {
|
|
55
|
-
return fn()
|
|
56
|
-
} catch (error) {
|
|
57
|
-
queueMicrotask(() => {
|
|
58
|
-
throw error
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function runAll(list: Iterable<{ current: ScrollReader }>, f: ScrollFrame) {
|
|
64
|
-
const writes: Array<() => void> = []
|
|
65
|
-
for (const r of list) {
|
|
66
|
-
const w = guard(() => r.current(f))
|
|
67
|
-
if (w) writes.push(w)
|
|
68
|
-
}
|
|
69
|
-
writes.forEach((w) => guard(w))
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function run(f: ScrollFrame) {
|
|
73
|
-
runAll(readers, f)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function tick(t: number) {
|
|
77
|
-
const y = window.scrollY
|
|
78
|
-
// The first frame after a wake only takes its bearings: its time can be earlier than the wake's.
|
|
79
|
-
let v = frame.v
|
|
80
|
-
if (last.t >= 0) {
|
|
81
|
-
v += (((y - last.y) / Math.max(1, t - last.t)) * 1000 - v) * 0.2
|
|
82
|
-
if (Math.abs(v) < 1) v = 0
|
|
83
|
-
}
|
|
84
|
-
const moved = y !== last.y
|
|
85
|
-
const dt = last.t >= 0 ? t - last.t : 0
|
|
86
|
-
last = { y, t }
|
|
87
|
-
frame = { y, v, dt }
|
|
88
|
-
settling = false
|
|
89
|
-
run(frame)
|
|
90
|
-
still = !moved && v === 0 && !settling ? still + 1 : 0
|
|
91
|
-
raf = still > 30 ? 0 : requestAnimationFrame(tick)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function wake() {
|
|
95
|
-
still = 0
|
|
96
|
-
if (raf) return
|
|
97
|
-
tokens = null
|
|
98
|
-
last = { y: window.scrollY, t: -1 }
|
|
99
|
-
raf = requestAnimationFrame(tick)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function onResize() {
|
|
103
|
-
tokens = null
|
|
104
|
-
wake()
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function start() {
|
|
108
|
-
window.addEventListener('scroll', wake, { passive: true })
|
|
109
|
-
window.addEventListener('resize', onResize)
|
|
110
|
-
// Fonts, pictures or an opened panel can move things without a scroll.
|
|
111
|
-
observer = new ResizeObserver(wake)
|
|
112
|
-
observer.observe(document.body)
|
|
113
|
-
reduce = window.matchMedia('(prefers-reduced-motion: reduce)')
|
|
114
|
-
reduce.addEventListener('change', wake)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function stop() {
|
|
118
|
-
window.removeEventListener('scroll', wake)
|
|
119
|
-
window.removeEventListener('resize', onResize)
|
|
120
|
-
observer?.disconnect()
|
|
121
|
-
observer = null
|
|
122
|
-
reduce?.removeEventListener('change', wake)
|
|
123
|
-
reduce = null
|
|
124
|
-
cancelAnimationFrame(raf)
|
|
125
|
-
raf = 0
|
|
126
|
-
frame = { y: 0, v: 0, dt: 0 }
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/** Calls `read` once on mount and then every frame while the page moves. */
|
|
6
|
+
/** React's hold on onScrollFrame (motion/scrollFrame.ts): calls the latest `read` on mount and every frame while the page moves. */
|
|
130
7
|
export function useScrollFrame(read: ScrollReader, active = true) {
|
|
131
8
|
const ref = useRef(read)
|
|
132
9
|
useLayoutEffect(() => {
|
|
@@ -134,24 +11,6 @@ export function useScrollFrame(read: ScrollReader, active = true) {
|
|
|
134
11
|
})
|
|
135
12
|
useLayoutEffect(() => {
|
|
136
13
|
if (!active) return
|
|
137
|
-
|
|
138
|
-
readers.add(ref)
|
|
139
|
-
mounting.add(ref)
|
|
140
|
-
if (mounting.size === 1) queueMicrotask(runMounts)
|
|
141
|
-
wake()
|
|
142
|
-
return () => {
|
|
143
|
-
readers.delete(ref)
|
|
144
|
-
mounting.delete(ref)
|
|
145
|
-
if (!readers.size) stop()
|
|
146
|
-
}
|
|
14
|
+
return onScrollFrame((f) => ref.current(f))
|
|
147
15
|
}, [active])
|
|
148
16
|
}
|
|
149
|
-
|
|
150
|
-
// Parts that mount together read once, together, before the first paint, so the page lays out once.
|
|
151
|
-
const mounting = new Set<{ current: ScrollReader }>()
|
|
152
|
-
|
|
153
|
-
function runMounts() {
|
|
154
|
-
const list = [...mounting]
|
|
155
|
-
mounting.clear()
|
|
156
|
-
runAll(list, { y: window.scrollY, v: frame.v, dt: 0 })
|
|
157
|
-
}
|
|
@@ -1,299 +1,37 @@
|
|
|
1
1
|
import { useEffect, useRef, type KeyboardEvent, type PointerEvent } from 'react'
|
|
2
|
-
import {
|
|
3
|
-
import { morphEase, motionFor } from './liquidMatter'
|
|
4
|
-
import { motionMs, prefersReducedMotion } from './motion'
|
|
2
|
+
import { createSoftPress, type SoftLook } from './motion/softPress'
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
// with a soft edge. The edge dents where the finger is (the middle, for a key) and follows it, a mouse
|
|
8
|
-
// near the edge pulls it out a little, and a let-go sends one small wave round the edge. A wet spot
|
|
9
|
-
// spreads under the press and fades after. The label never moves. At rest the SVG is gone and the
|
|
10
|
-
// button's CSS shape is back, exact.
|
|
11
|
-
|
|
12
|
-
const NS = 'http://www.w3.org/2000/svg'
|
|
13
|
-
let ids = 0
|
|
14
|
-
|
|
15
|
-
export type SoftLook = { layer: string; fill: string; ring: string; wet: string }
|
|
16
|
-
|
|
17
|
-
type Ring = Array<[number, number, number, number]>
|
|
18
|
-
|
|
19
|
-
type Live = {
|
|
20
|
-
el: HTMLElement
|
|
21
|
-
svg: SVGSVGElement
|
|
22
|
-
fill: SVGPathElement
|
|
23
|
-
ring: SVGPathElement
|
|
24
|
-
clip: SVGPathElement
|
|
25
|
-
wet: SVGCircleElement
|
|
26
|
-
outer: Ring
|
|
27
|
-
inner: Ring
|
|
28
|
-
w: number
|
|
29
|
-
h: number
|
|
30
|
-
top: number
|
|
31
|
-
bottom: number
|
|
32
|
-
meet: number
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const fine = () => window.matchMedia('(hover: hover) and (pointer: fine)').matches
|
|
4
|
+
export type { SoftLook } from './motion/softPress'
|
|
36
5
|
|
|
6
|
+
/** React's hold on createSoftPress (motion/softPress.ts): returns the button's event handlers. */
|
|
37
7
|
export function useSoftPress(look: SoftLook, inert: boolean, keys: string[]) {
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
raf: 0,
|
|
54
|
-
off: () => {},
|
|
55
|
-
}).current
|
|
56
|
-
|
|
57
|
-
// The shape for the button's size now. Runs again when the size changes while the layer is up, such
|
|
58
|
-
// as a label that changes on click.
|
|
59
|
-
const fit = (L: Pick<Live, 'el' | 'svg'>) => {
|
|
60
|
-
const { el, svg } = L
|
|
61
|
-
const w = el.offsetWidth
|
|
62
|
-
const h = el.offsetHeight
|
|
63
|
-
const css = getComputedStyle(el)
|
|
64
|
-
const line = parseFloat(css.borderTopWidth) || 0
|
|
65
|
-
const r = Math.min(h / 2, parseFloat(css.borderTopLeftRadius) || 0)
|
|
66
|
-
const outer = boxRing({ cx: w / 2, cy: h / 2, hw: w / 2, hh: h / 2, r }, 1)
|
|
67
|
-
const inner = boxRing({ cx: w / 2, cy: h / 2, hw: w / 2 - line, hh: h / 2 - line, r: Math.max(0, r - line) }, 1)
|
|
68
|
-
svg.setAttribute('width', String(w))
|
|
69
|
-
svg.setAttribute('height', String(h))
|
|
70
|
-
let len = 0
|
|
71
|
-
outer.forEach((p, i) => (len += Math.hypot(outer[(i + 1) % outer.length][0] - p[0], outer[(i + 1) % outer.length][1] - p[1])))
|
|
72
|
-
return { outer, inner, w, h, top: nearest(outer, w / 2, 0), bottom: nearest(outer, w / 2, h), meet: len / 2 }
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const build = (el: HTMLElement): Live => {
|
|
76
|
-
const svg = document.createElementNS(NS, 'svg')
|
|
77
|
-
svg.setAttribute('class', look.layer)
|
|
78
|
-
svg.setAttribute('aria-hidden', 'true')
|
|
79
|
-
svg.setAttribute('data-gooee-liquid', '')
|
|
80
|
-
const id = `gooee-soft-${++ids}`
|
|
81
|
-
const defs = svg.appendChild(document.createElementNS(NS, 'defs'))
|
|
82
|
-
const grad = defs.appendChild(document.createElementNS(NS, 'radialGradient'))
|
|
83
|
-
grad.id = `${id}-wet`
|
|
84
|
-
for (const [at, o] of [['0', '0.9'], ['0.5', '0.6'], ['1', '0']]) {
|
|
85
|
-
const stop = grad.appendChild(document.createElementNS(NS, 'stop'))
|
|
86
|
-
stop.setAttribute('offset', at)
|
|
87
|
-
stop.setAttribute('stop-opacity', o)
|
|
88
|
-
}
|
|
89
|
-
const clipEl = defs.appendChild(document.createElementNS(NS, 'clipPath'))
|
|
90
|
-
clipEl.id = `${id}-clip`
|
|
91
|
-
const clip = clipEl.appendChild(document.createElementNS(NS, 'path'))
|
|
92
|
-
const fill = svg.appendChild(document.createElementNS(NS, 'path'))
|
|
93
|
-
fill.setAttribute('class', look.fill)
|
|
94
|
-
const ring = svg.appendChild(document.createElementNS(NS, 'path'))
|
|
95
|
-
ring.setAttribute('class', look.ring)
|
|
96
|
-
ring.setAttribute('fill-rule', 'evenodd')
|
|
97
|
-
const wet = svg.appendChild(document.createElementNS(NS, 'circle'))
|
|
98
|
-
wet.setAttribute('class', look.wet)
|
|
99
|
-
wet.setAttribute('fill', `url(#${id}-wet)`)
|
|
100
|
-
wet.setAttribute('clip-path', `url(#${id}-clip)`)
|
|
101
|
-
wet.setAttribute('r', '0')
|
|
102
|
-
el.prepend(svg)
|
|
103
|
-
el.dataset.soft = ''
|
|
104
|
-
return { el, svg, fill, ring, clip, wet, ...fit({ el, svg }) }
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const teardown = () => {
|
|
108
|
-
cancelAnimationFrame(s.raf)
|
|
109
|
-
s.raf = 0
|
|
110
|
-
s.off()
|
|
111
|
-
s.off = () => {}
|
|
112
|
-
if (s.live) {
|
|
113
|
-
const el = s.live.el
|
|
114
|
-
s.live.svg.remove()
|
|
115
|
-
// The button's own paint comes back at once, with no fade, so the hand-over does not show.
|
|
116
|
-
const own = el.style.transition
|
|
117
|
-
el.style.transition = 'none'
|
|
118
|
-
delete el.dataset.soft
|
|
119
|
-
delete el.dataset.held
|
|
120
|
-
void getComputedStyle(el).backgroundColor
|
|
121
|
-
el.style.transition = own
|
|
122
|
-
}
|
|
123
|
-
s.live = null
|
|
124
|
-
s.dent = 0
|
|
125
|
-
s.press = null
|
|
126
|
-
s.held = false
|
|
127
|
-
s.pull = 0
|
|
128
|
-
s.pullAim = 0
|
|
129
|
-
s.wave = null
|
|
130
|
-
s.wet = null
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const frame = (now: number) => {
|
|
134
|
-
s.raf = 0
|
|
135
|
-
const L = s.live
|
|
136
|
-
if (!L) return
|
|
137
|
-
if (L.el.offsetWidth !== L.w || L.el.offsetHeight !== L.h) Object.assign(L, fit(L))
|
|
138
|
-
const dt = Math.max(0, now - s.last)
|
|
139
|
-
s.last = now
|
|
140
|
-
const e = morphEase()
|
|
141
|
-
const still = prefersReducedMotion()
|
|
142
|
-
const fast = motionMs('fast') || 1
|
|
143
|
-
const follow = still ? 1 : 1 - Math.exp(-dt / (fast / 3.5))
|
|
144
|
-
s.at = [s.at[0] + (s.aim[0] - s.at[0]) * follow, s.at[1] + (s.aim[1] - s.at[1]) * follow]
|
|
145
|
-
const pullTo = s.held || s.press || s.wave ? 0 : s.pullAim
|
|
146
|
-
s.pull = still ? 0 : s.pull + (pullTo - s.pull) * (1 - Math.exp(-dt / (fast / 1.5)))
|
|
147
|
-
if (pullTo === 0 && s.pull < 0.01) s.pull = 0
|
|
148
|
-
const ease = still ? 1 : 1 - Math.exp(-dt / (fast / 2.3))
|
|
149
|
-
s.to = [s.to[0] + (s.toAim[0] - s.to[0]) * ease, s.to[1] + (s.toAim[1] - s.to[1]) * ease]
|
|
150
|
-
const depth = parseFloat(getComputedStyle(L.el).getPropertyValue('--gooee-press-depth')) || 0
|
|
151
|
-
const p = s.press
|
|
152
|
-
if (p) {
|
|
153
|
-
const k = p.ms ? clamp01((now - p.t0) / p.ms) : 1
|
|
154
|
-
if (p.into) s.dent = lerp(p.from, depth, e(k))
|
|
155
|
-
else s.dent = p.wave ? p.from * (1 - e(seg(k, 0, 0.45))) : 0
|
|
156
|
-
if (!p.into && k >= 1) {
|
|
157
|
-
s.press = null
|
|
158
|
-
s.dent = 0
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
let wave = 0
|
|
162
|
-
let run = 0
|
|
163
|
-
if (s.wave) {
|
|
164
|
-
const k = s.wave.ms ? clamp01((now - s.wave.t0) / s.wave.ms) : 1
|
|
165
|
-
if (k >= 1) s.wave = null
|
|
166
|
-
else {
|
|
167
|
-
// It slows near the far side and fades out on the morph curve in its second half.
|
|
168
|
-
wave = Math.sin((Math.PI / 2) * clamp01(k / 0.12)) * (1 - e(seg(k, 0.55, 1)))
|
|
169
|
-
run = (1 - (1 - k) ** 2) * (L.meet / s.wave.from.length)
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
let wetOn = false
|
|
173
|
-
if (s.wet) {
|
|
174
|
-
const r = lerp(L.h * 0.21, L.h, s.wet.ms ? e(clamp01((now - s.wet.t0) / s.wet.ms)) : 1)
|
|
175
|
-
const f = s.wet.fade
|
|
176
|
-
const o = f ? (f.ms ? 1 - e(clamp01((now - f.t0) / f.ms)) : 0) : 1
|
|
177
|
-
if (o <= 0.002) s.wet = null
|
|
178
|
-
else {
|
|
179
|
-
wetOn = true
|
|
180
|
-
L.wet.setAttribute('cx', s.at[0].toFixed(2))
|
|
181
|
-
L.wet.setAttribute('cy', s.at[1].toFixed(2))
|
|
182
|
-
L.wet.setAttribute('r', r.toFixed(2))
|
|
183
|
-
L.wet.setAttribute('opacity', o.toFixed(3))
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
L.wet.style.display = wetOn ? '' : 'none'
|
|
187
|
-
const shape = { dent: s.dent, at: s.at, pull: s.pull, to: s.to, wave, run, from: s.wave?.from ?? [] }
|
|
188
|
-
if (!s.dent && !s.pull && !wave && !wetOn && !s.held && !s.press && !s.pullAim) {
|
|
189
|
-
teardown()
|
|
190
|
-
return
|
|
191
|
-
}
|
|
192
|
-
const outer = polyPath(softRing(L.outer, L.h, shape))
|
|
193
|
-
L.fill.setAttribute('d', outer)
|
|
194
|
-
L.clip.setAttribute('d', outer)
|
|
195
|
-
L.ring.setAttribute('d', outer + polyPath(softRing(L.inner, L.h, shape)))
|
|
196
|
-
s.raf = requestAnimationFrame(frame)
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const wake = (el: HTMLElement) => {
|
|
200
|
-
if (!s.live) {
|
|
201
|
-
s.live = build(el)
|
|
202
|
-
s.at = [...s.aim]
|
|
203
|
-
s.to = [...s.toAim]
|
|
204
|
-
s.last = performance.now()
|
|
205
|
-
}
|
|
206
|
-
if (!s.raf) s.raf = requestAnimationFrame(frame)
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const local = (el: HTMLElement, x: number, y: number): Pt => {
|
|
210
|
-
const r = el.getBoundingClientRect()
|
|
211
|
-
return [x - r.left, y - r.top]
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const press = (el: HTMLElement, at: Pt, byKey: boolean) => {
|
|
215
|
-
s.aim = at
|
|
216
|
-
if (!s.held) s.at = [...at]
|
|
217
|
-
s.held = true
|
|
218
|
-
s.byKey = byKey
|
|
219
|
-
s.wave = null
|
|
220
|
-
el.dataset.held = ''
|
|
221
|
-
wake(el)
|
|
222
|
-
const L = s.live!
|
|
223
|
-
s.at = [...at]
|
|
224
|
-
s.press = { into: true, from: s.dent, t0: performance.now(), ms: motionFor('fast'), wave: false }
|
|
225
|
-
s.wet = { t0: performance.now(), ms: motionFor('slower'), fade: null }
|
|
226
|
-
// The wet spot darkens a light fill and lightens a dark one.
|
|
227
|
-
const rgb = getComputedStyle(L.fill).fill.match(/[\d.]+/g)?.map(Number) ?? [255, 255, 255, 0]
|
|
228
|
-
const light = (rgb[3] ?? 1) < 0.1 || 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2] > 140
|
|
229
|
-
L.svg.dataset.wet = light ? 'dark' : 'light'
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const release = () => {
|
|
233
|
-
if (!s.held || !s.live) return
|
|
234
|
-
s.held = false
|
|
235
|
-
delete s.live.el.dataset.held
|
|
236
|
-
s.off()
|
|
237
|
-
s.off = () => {}
|
|
238
|
-
const ms = motionFor('slow')
|
|
239
|
-
s.press = { into: false, from: s.dent, t0: performance.now(), ms, wave: ms > 0 }
|
|
240
|
-
if (ms) s.wave = { t0: performance.now(), ms, from: s.byKey ? [s.live.top, s.live.bottom] : [nearest(s.live.outer, s.at[0], s.at[1])] }
|
|
241
|
-
if (s.wet) s.wet.fade = { t0: performance.now(), ms: motionFor('slow') }
|
|
242
|
-
wake(s.live.el)
|
|
243
|
-
}
|
|
8
|
+
const props = useRef({ look, inert, keys })
|
|
9
|
+
props.current = { look, inert, keys }
|
|
10
|
+
const soft = useRef<ReturnType<typeof createSoftPress>>(null)
|
|
11
|
+
soft.current ??= createSoftPress({
|
|
12
|
+
get look() {
|
|
13
|
+
return props.current.look
|
|
14
|
+
},
|
|
15
|
+
get keys() {
|
|
16
|
+
return props.current.keys
|
|
17
|
+
},
|
|
18
|
+
get inert() {
|
|
19
|
+
return props.current.inert
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
const p = soft.current
|
|
244
23
|
|
|
245
|
-
useEffect(() =>
|
|
24
|
+
useEffect(() => p.destroy, [p])
|
|
246
25
|
useEffect(() => {
|
|
247
|
-
if (inert)
|
|
248
|
-
}, [inert])
|
|
26
|
+
if (inert) p.stop()
|
|
27
|
+
}, [inert, p])
|
|
249
28
|
|
|
250
29
|
return {
|
|
251
|
-
onPointerDown: (e: PointerEvent<HTMLElement>) =>
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
const up = (m: globalThis.PointerEvent) => {
|
|
259
|
-
if (m.pointerId === e.pointerId) release()
|
|
260
|
-
}
|
|
261
|
-
window.addEventListener('pointermove', move)
|
|
262
|
-
window.addEventListener('pointerup', up)
|
|
263
|
-
window.addEventListener('pointercancel', up)
|
|
264
|
-
s.off = () => {
|
|
265
|
-
window.removeEventListener('pointermove', move)
|
|
266
|
-
window.removeEventListener('pointerup', up)
|
|
267
|
-
window.removeEventListener('pointercancel', up)
|
|
268
|
-
}
|
|
269
|
-
},
|
|
270
|
-
onPointerMove: (e: PointerEvent<HTMLElement>) => {
|
|
271
|
-
if (inert || e.pointerType !== 'mouse' || !fine() || prefersReducedMotion() || s.held) return
|
|
272
|
-
const el = e.currentTarget
|
|
273
|
-
const [x, y] = local(el, e.clientX, e.clientY)
|
|
274
|
-
const w = el.offsetWidth
|
|
275
|
-
const h = el.offsetHeight
|
|
276
|
-
// Strongest at the edge, less toward the middle.
|
|
277
|
-
const edge = Math.min(x, w - x, y, h - y)
|
|
278
|
-
s.pullAim = Math.exp(-(edge * edge) / (2 * (h * 0.58) ** 2))
|
|
279
|
-
const ring = s.live?.outer ?? boxRing({ cx: w / 2, cy: h / 2, hw: w / 2, hh: h / 2, r: h / 2 }, 1)
|
|
280
|
-
const p = ring[nearest(ring, x, y)]
|
|
281
|
-
s.toAim = [p[0], p[1]]
|
|
282
|
-
wake(el)
|
|
283
|
-
},
|
|
284
|
-
onPointerLeave: () => {
|
|
285
|
-
s.pullAim = 0
|
|
286
|
-
},
|
|
287
|
-
onKeyDown: (e: KeyboardEvent<HTMLElement>) => {
|
|
288
|
-
if (inert || e.repeat || !keys.includes(e.key)) return
|
|
289
|
-
const el = e.currentTarget
|
|
290
|
-
press(el, [el.offsetWidth / 2, el.offsetHeight / 2], true)
|
|
291
|
-
},
|
|
292
|
-
onKeyUp: (e: KeyboardEvent<HTMLElement>) => {
|
|
293
|
-
if (keys.includes(e.key) && s.byKey) release()
|
|
294
|
-
},
|
|
295
|
-
onBlur: () => {
|
|
296
|
-
if (s.byKey) release()
|
|
297
|
-
},
|
|
30
|
+
onPointerDown: (e: PointerEvent<HTMLElement>) => p.down(e),
|
|
31
|
+
onPointerMove: (e: PointerEvent<HTMLElement>) => p.hover(e),
|
|
32
|
+
onPointerLeave: p.leave,
|
|
33
|
+
onKeyDown: (e: KeyboardEvent<HTMLElement>) => p.keyDown(e),
|
|
34
|
+
onKeyUp: (e: KeyboardEvent<HTMLElement>) => p.keyUp(e),
|
|
35
|
+
onBlur: p.blur,
|
|
298
36
|
}
|
|
299
37
|
}
|
package/src/internal/useScrub.ts
CHANGED
|
@@ -1,134 +1,41 @@
|
|
|
1
1
|
import { useEffect, useRef, useState, type KeyboardEvent, type PointerEvent } from 'react'
|
|
2
|
-
import {
|
|
3
|
-
import { morphCurve, readMotionMs } from './motion'
|
|
2
|
+
import { createScrubber, type ScrubberOptions } from './motion/scrubber'
|
|
4
3
|
|
|
5
|
-
//
|
|
6
|
-
// drawn position out. The motion itself lives in feel.ts.
|
|
4
|
+
// React's hold on createScrubber (motion/scrubber.ts) for Dial, RulerScrubber and PickerWheel.
|
|
7
5
|
|
|
8
|
-
type
|
|
9
|
-
|
|
10
|
-
export type UseScrubOptions = Pick<ScrubOptions, 'min' | 'max' | 'edge' | 'land' | 'coast' | 'maxSpeed'> & {
|
|
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
|
|
6
|
+
export type UseScrubOptions = Omit<ScrubberOptions<PointerEvent<HTMLElement>>, 'onFrame' | 'onMoving'>
|
|
32
7
|
|
|
33
8
|
export function useScrub(o: UseScrubOptions) {
|
|
34
|
-
const opt = useRef(o)
|
|
35
|
-
opt.current = o
|
|
36
9
|
const [at, setAt] = useState(o.value)
|
|
37
10
|
const [moving, setMoving] = useState(false)
|
|
38
|
-
const press = useRef<Press | null>(null)
|
|
39
11
|
const ref = useRef<HTMLDivElement>(null)
|
|
40
|
-
const scrub = useRef<ReturnType<typeof
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
value: o.value,
|
|
44
|
-
land: (p) => opt.current.land(p),
|
|
45
|
-
duration: () => readMotionMs('morph-out'),
|
|
46
|
-
curve: morphCurve,
|
|
47
|
-
onFrame: setAt,
|
|
48
|
-
onMoving: setMoving,
|
|
49
|
-
onMark: (v) => {
|
|
50
|
-
opt.current.onMark?.(v)
|
|
51
|
-
if (opt.current.haptics) navigator.vibrate?.(5)
|
|
52
|
-
},
|
|
53
|
-
// Only a new value is news: a tap, Home at the start, or a drag back to where it was says nothing.
|
|
54
|
-
onSettle: (v) => v !== opt.current.value && opt.current.onSettle(v),
|
|
55
|
-
})
|
|
56
|
-
}
|
|
12
|
+
const scrub = useRef<ReturnType<typeof createScrubber<PointerEvent<HTMLElement>>>>(null)
|
|
13
|
+
const options = { ...o, onFrame: setAt, onMoving: setMoving }
|
|
14
|
+
scrub.current ??= createScrubber(options)
|
|
57
15
|
const s = scrub.current
|
|
58
|
-
s.update(
|
|
16
|
+
s.update(options)
|
|
59
17
|
|
|
60
18
|
// A value set from outside (a controlled prop, a reset) glides there. So does one the parent kept
|
|
61
19
|
// or changed when it came to rest.
|
|
62
|
-
useEffect(() =>
|
|
63
|
-
if (press.current || s.moving || o.value === s.target) return
|
|
64
|
-
s.go(o.value)
|
|
65
|
-
}, [o.value, s, moving])
|
|
20
|
+
useEffect(() => s.sync(o.value), [o.value, s, moving])
|
|
66
21
|
|
|
67
22
|
const wheels = !!o.wheel
|
|
68
|
-
|
|
69
23
|
useEffect(() => {
|
|
70
24
|
const el = ref.current
|
|
71
25
|
if (!el || !wheels) return
|
|
72
|
-
|
|
73
|
-
const t = opt.current.wheel?.(e, s.target, document.activeElement === el) ?? null
|
|
74
|
-
if (t === null) return
|
|
75
|
-
e.preventDefault()
|
|
76
|
-
if (t !== s.target) s.go(t)
|
|
77
|
-
}
|
|
78
|
-
el.addEventListener('wheel', onWheel, { passive: false })
|
|
79
|
-
return () => el.removeEventListener('wheel', onWheel)
|
|
26
|
+
return s.wheelOn(el)
|
|
80
27
|
}, [s, wheels])
|
|
81
|
-
useEffect(() => () => s.
|
|
82
|
-
|
|
83
|
-
const onPointerDown = (e: PointerEvent<HTMLElement>) => {
|
|
84
|
-
if (e.pointerType === 'mouse' && e.button !== 0) return
|
|
85
|
-
try {
|
|
86
|
-
e.currentTarget.setPointerCapture(e.pointerId)
|
|
87
|
-
} catch {
|
|
88
|
-
// A pointer the browser no longer knows (a synthetic one) cannot be captured, and needs no capture.
|
|
89
|
-
}
|
|
90
|
-
s.grab(e.timeStamp)
|
|
91
|
-
press.current = { id: e.pointerId, sx: e.clientX, sy: e.clientY, x: e.clientX, y: e.clientY, moved: false }
|
|
92
|
-
}
|
|
93
|
-
const onPointerMove = (e: PointerEvent<HTMLElement>) => {
|
|
94
|
-
const p = press.current
|
|
95
|
-
if (!p || p.id !== e.pointerId) return
|
|
96
|
-
if (!p.moved) {
|
|
97
|
-
if (Math.hypot(e.clientX - p.sx, e.clientY - p.sy) < SLOP) return
|
|
98
|
-
p.moved = true
|
|
99
|
-
}
|
|
100
|
-
const delta = opt.current.move(e, p, e.currentTarget)
|
|
101
|
-
p.x = e.clientX
|
|
102
|
-
p.y = e.clientY
|
|
103
|
-
s.drag(delta, e.timeStamp)
|
|
104
|
-
}
|
|
105
|
-
const onPointerUp = (e: PointerEvent<HTMLElement>) => {
|
|
106
|
-
const p = press.current
|
|
107
|
-
if (!p || p.id !== e.pointerId) return
|
|
108
|
-
press.current = null
|
|
109
|
-
if (e.type === 'pointercancel') return s.cancel()
|
|
110
|
-
if (p.moved) return s.release(e.timeStamp)
|
|
111
|
-
const t = opt.current.tap?.(e, s.at)
|
|
112
|
-
if (t === null || t === undefined) s.release(e.timeStamp)
|
|
113
|
-
else s.go(t)
|
|
114
|
-
}
|
|
115
|
-
const onKeyDown = (e: KeyboardEvent<HTMLElement>) => {
|
|
116
|
-
if (e.altKey || e.ctrlKey || e.metaKey) return
|
|
117
|
-
const t = opt.current.key(e.key, s.target)
|
|
118
|
-
if (t === null) return
|
|
119
|
-
e.preventDefault()
|
|
120
|
-
s.go(t)
|
|
121
|
-
}
|
|
28
|
+
useEffect(() => () => s.destroy(), [s])
|
|
122
29
|
|
|
123
30
|
return {
|
|
124
31
|
ref,
|
|
125
32
|
/** Where it is drawn now. */
|
|
126
33
|
at,
|
|
127
34
|
moving,
|
|
128
|
-
go:
|
|
129
|
-
place:
|
|
130
|
-
target:
|
|
131
|
-
handlers: { onPointerDown, onPointerMove, onPointerUp, onPointerCancel:
|
|
35
|
+
go: s.go,
|
|
36
|
+
place: s.place,
|
|
37
|
+
target: s.target,
|
|
38
|
+
handlers: { onPointerDown: s.down, onPointerMove: s.move, onPointerUp: s.up, onPointerCancel: s.up, onKeyDown: (e: KeyboardEvent<HTMLElement>) => s.keyDown(e) },
|
|
132
39
|
}
|
|
133
40
|
}
|
|
134
41
|
|