gooee 0.48.0 → 0.51.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 +4 -2
- package/scripts/add.js +28 -6
- package/src/components/Accordion/Accordion.vue +62 -0
- package/src/components/Accordion/AccordionItem.vue +45 -0
- package/src/components/Accordion/accordionContext.ts +6 -0
- package/src/components/Alert/Alert.tsx +7 -27
- package/src/components/Alert/Alert.vue +59 -0
- package/src/components/AlertDialog/AlertDialog.vue +126 -0
- package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
- package/src/components/AspectRatio/AspectRatio.vue +17 -0
- package/src/components/Avatar/Avatar.vue +56 -0
- package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
- package/src/components/Badge/Badge.vue +32 -0
- package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
- package/src/components/Button/Button.vue +130 -0
- package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
- package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
- package/src/components/Card/Card.vue +34 -0
- package/src/components/Center/Center.vue +32 -0
- package/src/components/Checkbox/Checkbox.tsx +8 -25
- package/src/components/Checkbox/Checkbox.vue +108 -0
- package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
- package/src/components/Cluster/Cluster.vue +34 -0
- package/src/components/CodeBlock/CodeBlock.vue +45 -0
- package/src/components/Collapsible/Collapsible.vue +59 -0
- package/src/components/Container/Container.vue +22 -0
- package/src/components/CopyButton/CopyButton.tsx +3 -49
- package/src/components/CopyButton/CopyButton.vue +116 -0
- package/src/components/Dial/Dial.tsx +12 -120
- package/src/components/Dial/Dial.vue +228 -0
- package/src/components/Dialog/Dialog.vue +139 -0
- package/src/components/Divider/Divider.vue +27 -0
- package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
- package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
- package/src/components/EmptyState/EmptyState.vue +33 -0
- package/src/components/Fab/Fab.tsx +4 -14
- package/src/components/Fab/Fab.vue +115 -0
- package/src/components/Fieldset/Fieldset.vue +42 -0
- package/src/components/FormField/FormField.tsx +2 -17
- package/src/components/FormField/FormField.vue +64 -0
- package/src/components/Grid/Grid.vue +43 -0
- package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -142
- package/src/components/HoldToConfirm/HoldToConfirm.vue +281 -0
- package/src/components/IconButton/IconButton.vue +25 -0
- package/src/components/Input/Input.vue +112 -0
- package/src/components/InputGroup/InputGroup.vue +102 -0
- package/src/components/Kbd/Kbd.tsx +2 -12
- package/src/components/Kbd/Kbd.vue +21 -0
- package/src/components/Link/Link.vue +38 -0
- package/src/components/LivePill/LivePill.tsx +12 -40
- package/src/components/LivePill/LivePill.vue +152 -0
- package/src/components/Meter/Meter.vue +88 -0
- package/src/components/MorphButton/MorphButton.tsx +11 -67
- package/src/components/MorphButton/MorphButton.vue +120 -0
- package/src/components/NumberInput/NumberInput.tsx +11 -48
- package/src/components/NumberInput/NumberInput.vue +184 -0
- package/src/components/Pagination/Pagination.tsx +2 -15
- package/src/components/Pagination/Pagination.vue +87 -0
- package/src/components/Pagination/range.ts +15 -0
- package/src/components/PasswordInput/PasswordInput.vue +53 -0
- package/src/components/PickerWheel/DateWheel.vue +48 -0
- package/src/components/PickerWheel/PickerWheel.tsx +15 -129
- package/src/components/PickerWheel/PickerWheel.vue +77 -0
- package/src/components/PickerWheel/TimeWheel.vue +50 -0
- package/src/components/PickerWheel/WheelColumn.vue +70 -0
- package/src/components/Popover/Popover.vue +108 -0
- package/src/components/ProgressBar/ProgressBar.vue +48 -0
- package/src/components/Prose/Prose.vue +18 -0
- package/src/components/Radio/Radio.vue +49 -0
- package/src/components/Radio/RadioGroup.vue +69 -0
- package/src/components/Retime/Retime.tsx +7 -40
- package/src/components/Retime/Retime.vue +64 -0
- package/src/components/RulerScrubber/RulerScrubber.tsx +9 -38
- package/src/components/RulerScrubber/RulerScrubber.vue +166 -0
- package/src/components/ScrollArea/ScrollArea.tsx +17 -129
- package/src/components/ScrollArea/ScrollArea.vue +58 -0
- package/src/components/SearchInput/SearchInput.tsx +2 -15
- package/src/components/SearchInput/SearchInput.vue +70 -0
- package/src/components/SegmentedControl/SegmentedControl.tsx +7 -51
- package/src/components/SegmentedControl/SegmentedControl.vue +111 -0
- package/src/components/Select/Select.tsx +16 -47
- package/src/components/Select/Select.vue +236 -0
- package/src/components/Sheet/Sheet.tsx +52 -231
- package/src/components/Sheet/Sheet.vue +249 -0
- package/src/components/Skeleton/Skeleton.vue +38 -0
- package/src/components/Slider/Slider.tsx +25 -84
- package/src/components/Slider/Slider.vue +113 -0
- package/src/components/Spinner/Spinner.vue +30 -0
- package/src/components/Split/Split.vue +34 -0
- package/src/components/SplitFlap/SplitFlap.tsx +11 -175
- package/src/components/SplitFlap/SplitFlap.vue +52 -0
- package/src/components/Stack/Stack.vue +22 -0
- package/src/components/SwipeDeck/SwipeDeck.tsx +30 -194
- package/src/components/SwipeDeck/SwipeDeck.vue +168 -0
- package/src/components/Switch/Switch.tsx +6 -75
- package/src/components/Switch/Switch.vue +66 -0
- package/src/components/Table/Table.module.css +12 -3
- package/src/components/Table/Table.tsx +8 -59
- package/src/components/Table/Table.vue +274 -0
- package/src/components/TableOfContents/TableOfContents.vue +90 -0
- package/src/components/Tabs/Tab.vue +37 -0
- package/src/components/Tabs/TabList.vue +47 -0
- package/src/components/Tabs/TabPanel.vue +26 -0
- package/src/components/Tabs/Tabs.module.css +2 -2
- package/src/components/Tabs/Tabs.tsx +11 -84
- package/src/components/Tabs/Tabs.vue +33 -0
- package/src/components/Tabs/tabsContext.ts +15 -0
- package/src/components/Tag/Tag.tsx +6 -26
- package/src/components/Tag/Tag.vue +63 -0
- package/src/components/Textarea/Textarea.tsx +2 -22
- package/src/components/Textarea/Textarea.vue +120 -0
- package/src/components/ThemeToggle/ThemeToggle.tsx +10 -84
- package/src/components/ThemeToggle/ThemeToggle.vue +90 -0
- package/src/components/Toast/Toast.tsx +34 -264
- package/src/components/Toast/Toast.vue +138 -0
- package/src/components/Toast/ToastItem.vue +108 -0
- package/src/components/Toggle/Toggle.vue +52 -0
- package/src/components/Tooltip/Tooltip.tsx +17 -48
- package/src/components/Tooltip/Tooltip.vue +95 -0
- package/src/components/VisuallyHidden/VisuallyHidden.vue +17 -0
- package/src/internal/FieldMessage.vue +41 -0
- package/src/internal/Roll.vue +20 -0
- package/src/internal/fling.ts +29 -106
- package/src/internal/focus.ts +4 -79
- package/src/internal/icons.module.css +39 -27
- package/src/internal/layers.ts +7 -61
- 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/alert.ts +17 -0
- package/src/internal/motion/buttonGroup.ts +21 -0
- package/src/internal/motion/checkbox.ts +39 -0
- package/src/internal/motion/collapseOut.ts +26 -0
- package/src/internal/motion/copyButton.ts +48 -0
- package/src/internal/motion/dial.ts +137 -0
- package/src/internal/motion/fab.ts +14 -0
- package/src/internal/motion/fling.ts +147 -0
- package/src/internal/motion/floating.ts +58 -0
- package/src/internal/motion/focus.ts +88 -0
- package/src/internal/motion/formField.ts +19 -0
- package/src/internal/motion/holdToConfirm.ts +147 -0
- package/src/internal/motion/index.ts +20 -0
- package/src/internal/motion/keys.ts +42 -0
- package/src/internal/motion/layers.ts +78 -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/listbox.ts +69 -0
- package/src/internal/motion/livePill.ts +71 -0
- package/src/internal/motion/menuKeys.ts +58 -0
- package/src/internal/motion/morphButton.ts +71 -0
- package/src/internal/motion/numberInput.ts +57 -0
- package/src/internal/motion/pickerWheel.ts +166 -0
- package/src/internal/motion/retime.ts +45 -0
- package/src/internal/motion/ruler.ts +57 -0
- package/src/internal/motion/scrollArea.ts +133 -0
- package/src/internal/motion/scrollFrame.ts +152 -0
- package/src/internal/motion/scrubLook.ts +15 -0
- package/src/internal/motion/scrubber.ts +154 -0
- package/src/internal/motion/segmented.ts +63 -0
- package/src/internal/motion/sheetDetents.ts +271 -0
- package/src/internal/motion/slider.ts +108 -0
- package/src/internal/motion/softPress.ts +334 -0
- package/src/internal/motion/splitFlap.ts +205 -0
- package/src/internal/motion/swipeDeck.ts +236 -0
- package/src/internal/motion/switchThumb.ts +83 -0
- package/src/internal/motion/table.ts +77 -0
- package/src/internal/motion/tabs.ts +105 -0
- package/src/internal/motion/textarea.ts +22 -0
- package/src/internal/motion/themeIcon.ts +91 -0
- package/src/internal/motion/themeReveal.ts +71 -0
- package/src/internal/motion/toast.ts +291 -0
- package/src/internal/motion/tooltip.ts +58 -0
- package/src/internal/scrollFrame.ts +4 -145
- package/src/internal/softPress.ts +27 -289
- package/src/internal/themeReveal.ts +3 -61
- package/src/internal/useFloating.ts +9 -49
- package/src/internal/useScrub.ts +15 -120
- package/src/internal/vue/trigger.ts +18 -0
- package/src/internal/vue/useFloating.ts +38 -0
- package/src/internal/vue/usePresence.ts +21 -0
- package/src/internal/vue/useScrub.ts +40 -0
- package/src/internal/vueIcons.ts +134 -0
- package/src/tokens.css +5 -0
- package/src/internal/relativeTime.ts +0 -26
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { prefersReducedMotion } from '../motion'
|
|
2
|
+
|
|
3
|
+
// ScrollArea's drawn scrollbar with no framework: the thumbs follow the native scroll, fade when idle and
|
|
4
|
+
// can be dragged. Every write goes straight to the DOM, so scrolling never re-renders.
|
|
5
|
+
|
|
6
|
+
export type ScrollAxis = 'y' | 'x'
|
|
7
|
+
const IDLE_MS = 900
|
|
8
|
+
|
|
9
|
+
// A pointer event from the DOM or from React: React's own, so stopPropagation also stops React's handlers.
|
|
10
|
+
type Press = Pick<PointerEvent, 'pointerType' | 'button' | 'clientX' | 'clientY' | 'pointerId' | 'preventDefault' | 'stopPropagation'>
|
|
11
|
+
|
|
12
|
+
type Parts = {
|
|
13
|
+
root: () => HTMLElement | null
|
|
14
|
+
viewport: () => HTMLElement | null
|
|
15
|
+
thumb: (axis: ScrollAxis) => HTMLElement | null
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function createScrollArea({ root, viewport, thumb }: Parts) {
|
|
19
|
+
let idle: number | undefined
|
|
20
|
+
let drag: { axis: ScrollAxis; start: number; scroll: number; ratio: number; pointer: number } | null = null
|
|
21
|
+
|
|
22
|
+
const update = () => {
|
|
23
|
+
const vp = viewport()
|
|
24
|
+
const el = root()
|
|
25
|
+
if (!vp || !el) return
|
|
26
|
+
const place = (axis: ScrollAxis) => {
|
|
27
|
+
const t = thumb(axis)
|
|
28
|
+
if (!t) return
|
|
29
|
+
const track = t.parentElement as HTMLElement
|
|
30
|
+
const size = axis === 'y' ? vp.clientHeight : vp.clientWidth
|
|
31
|
+
const full = axis === 'y' ? vp.scrollHeight : vp.scrollWidth
|
|
32
|
+
const pos = axis === 'y' ? vp.scrollTop : vp.scrollLeft
|
|
33
|
+
const overflow = full > size + 1
|
|
34
|
+
track.toggleAttribute('data-hidden', !overflow)
|
|
35
|
+
if (!overflow) return
|
|
36
|
+
const pad = parseFloat(getComputedStyle(track).paddingTop) || 0
|
|
37
|
+
const trackLen = (axis === 'y' ? track.clientHeight : track.clientWidth) - pad * 2
|
|
38
|
+
const minThumb = parseFloat(getComputedStyle(el).getPropertyValue('--gooee-scrollbar-min')) || 24
|
|
39
|
+
const len = Math.max(minThumb, (size / full) * trackLen)
|
|
40
|
+
const offset = (pos / (full - size)) * (trackLen - len)
|
|
41
|
+
if (axis === 'y') {
|
|
42
|
+
t.style.height = `${len}px`
|
|
43
|
+
t.style.transform = `translateY(${offset}px)`
|
|
44
|
+
} else {
|
|
45
|
+
t.style.width = `${len}px`
|
|
46
|
+
t.style.transform = `translateX(${offset}px)`
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
place('y')
|
|
50
|
+
place('x')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const wake = () => {
|
|
54
|
+
const el = root()
|
|
55
|
+
if (!el) return
|
|
56
|
+
el.setAttribute('data-active', '')
|
|
57
|
+
window.clearTimeout(idle)
|
|
58
|
+
idle = window.setTimeout(() => {
|
|
59
|
+
if (!drag) el.removeAttribute('data-active')
|
|
60
|
+
}, IDLE_MS)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Places the thumbs now and again whenever the viewport or its content changes size. Returns a cleanup. */
|
|
64
|
+
const observe = () => {
|
|
65
|
+
update()
|
|
66
|
+
const vp = viewport()
|
|
67
|
+
if (!vp) return () => {}
|
|
68
|
+
const ro = new ResizeObserver(update)
|
|
69
|
+
ro.observe(vp)
|
|
70
|
+
if (vp.firstElementChild) ro.observe(vp.firstElementChild)
|
|
71
|
+
return () => ro.disconnect()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const onScroll = () => {
|
|
75
|
+
update()
|
|
76
|
+
wake()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const thumbDown = (axis: ScrollAxis, e: Press) => {
|
|
80
|
+
const vp = viewport()
|
|
81
|
+
const t = thumb(axis)
|
|
82
|
+
if (!vp || !t || (e.pointerType === 'mouse' && e.button !== 0)) return
|
|
83
|
+
e.preventDefault()
|
|
84
|
+
e.stopPropagation()
|
|
85
|
+
const track = t.parentElement as HTMLElement
|
|
86
|
+
const size = axis === 'y' ? vp.clientHeight : vp.clientWidth
|
|
87
|
+
const full = axis === 'y' ? vp.scrollHeight : vp.scrollWidth
|
|
88
|
+
const pad = parseFloat(getComputedStyle(track).paddingTop) || 0
|
|
89
|
+
const trackLen = (axis === 'y' ? track.clientHeight : track.clientWidth) - pad * 2
|
|
90
|
+
const thumbLen = axis === 'y' ? t.offsetHeight : t.offsetWidth
|
|
91
|
+
// The thumb travels the track minus its own length, which is all the scroll there is.
|
|
92
|
+
drag = {
|
|
93
|
+
axis,
|
|
94
|
+
start: axis === 'y' ? e.clientY : e.clientX,
|
|
95
|
+
scroll: axis === 'y' ? vp.scrollTop : vp.scrollLeft,
|
|
96
|
+
ratio: (full - size) / Math.max(1, trackLen - thumbLen),
|
|
97
|
+
pointer: e.pointerId,
|
|
98
|
+
}
|
|
99
|
+
t.setPointerCapture(e.pointerId)
|
|
100
|
+
root()?.setAttribute('data-dragging', '')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const thumbMove = (e: Pick<PointerEvent, 'clientX' | 'clientY' | 'pointerId'>) => {
|
|
104
|
+
const vp = viewport()
|
|
105
|
+
if (!drag || !vp || drag.pointer !== e.pointerId) return
|
|
106
|
+
const delta = (drag.axis === 'y' ? e.clientY : e.clientX) - drag.start
|
|
107
|
+
if (drag.axis === 'y') vp.scrollTop = drag.scroll + delta * drag.ratio
|
|
108
|
+
else vp.scrollLeft = drag.scroll + delta * drag.ratio
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const thumbUp = () => {
|
|
112
|
+
if (!drag) return
|
|
113
|
+
thumb(drag.axis)?.releasePointerCapture(drag.pointer)
|
|
114
|
+
drag = null
|
|
115
|
+
root()?.removeAttribute('data-dragging')
|
|
116
|
+
wake()
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// A press on the track pages towards it.
|
|
120
|
+
const trackDown = (axis: ScrollAxis, e: Pick<PointerEvent, 'clientX' | 'clientY'>) => {
|
|
121
|
+
const vp = viewport()
|
|
122
|
+
const t = thumb(axis)
|
|
123
|
+
if (!vp || !t) return
|
|
124
|
+
const r = t.getBoundingClientRect()
|
|
125
|
+
const before = axis === 'y' ? e.clientY < r.top : e.clientX < r.left
|
|
126
|
+
const page = (axis === 'y' ? vp.clientHeight : vp.clientWidth) * 0.9
|
|
127
|
+
vp.scrollBy({ [axis === 'y' ? 'top' : 'left']: before ? -page : page, behavior: prefersReducedMotion() ? 'auto' : 'smooth' })
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const destroy = () => window.clearTimeout(idle)
|
|
131
|
+
|
|
132
|
+
return { update, wake, observe, onScroll, thumbDown, thumbMove, thumbUp, trackDown, destroy }
|
|
133
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// One frame loop for every scroll part on the page. It runs only while the page moves, and stops
|
|
2
|
+
// after a moment of stillness; a scroll, a resize or a change in the page's size wakes it again.
|
|
3
|
+
|
|
4
|
+
/** Where the page is (px), how fast it moves (px/s, signed), and the time since the last frame (ms, 0 on mount). */
|
|
5
|
+
export type ScrollFrame = { y: number; v: number; dt: number }
|
|
6
|
+
|
|
7
|
+
/** Reads layout and returns the writes, so every part reads before any part writes. */
|
|
8
|
+
export type ScrollReader = (frame: ScrollFrame) => void | (() => void)
|
|
9
|
+
|
|
10
|
+
export type ScrollTokens = { ease: number; start: number; end: number; turn: number; turnNarrow: number }
|
|
11
|
+
|
|
12
|
+
const readers = new Set<{ current: ScrollReader }>()
|
|
13
|
+
let frame: ScrollFrame = { y: 0, v: 0, dt: 0 }
|
|
14
|
+
let last = { y: 0, t: -1 }
|
|
15
|
+
let raf = 0
|
|
16
|
+
let still = 0
|
|
17
|
+
let observer: ResizeObserver | null = null
|
|
18
|
+
let reduce: MediaQueryList | null = null
|
|
19
|
+
let tokens: ScrollTokens | null = null
|
|
20
|
+
|
|
21
|
+
export function readScrollTokens(): ScrollTokens {
|
|
22
|
+
const s = getComputedStyle(document.documentElement)
|
|
23
|
+
const n = (name: string) => parseFloat(s.getPropertyValue(name))
|
|
24
|
+
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') }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The tokens, read once each time the loop starts or the window changes size. */
|
|
28
|
+
export const scrollTokens = () => (tokens ??= readScrollTokens())
|
|
29
|
+
|
|
30
|
+
/** Progress from 0 to 1 as an element's top moves from `start` to `end` (shares of the screen height). */
|
|
31
|
+
export function bandProgress(top: number, start: number, end: number) {
|
|
32
|
+
const p = (start - top / window.innerHeight) / (start - end)
|
|
33
|
+
return Math.max(0, Math.min(1, p))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Set by a part that is still easing, so the loop keeps going after the page stops.
|
|
37
|
+
let settling = false
|
|
38
|
+
|
|
39
|
+
/** Moves `from` toward `to` by the scroll ease, the same weight at any frame rate. On mount it lands at once. */
|
|
40
|
+
export function easeToward(from: number, to: number, dt: number) {
|
|
41
|
+
if (dt <= 0) return to
|
|
42
|
+
const k = 1 - Math.pow(1 - scrollTokens().ease, Math.min(4, dt / (1000 / 60)))
|
|
43
|
+
const next = from + (to - from) * k
|
|
44
|
+
if (Math.abs(to - next) < 0.0005) return to
|
|
45
|
+
settling = true
|
|
46
|
+
return next
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// One part that throws must not stop the loop for every other part, so each runs on its own and the
|
|
50
|
+
// error is reported after the frame.
|
|
51
|
+
function guard<T>(fn: () => T): T | undefined {
|
|
52
|
+
try {
|
|
53
|
+
return fn()
|
|
54
|
+
} catch (error) {
|
|
55
|
+
queueMicrotask(() => {
|
|
56
|
+
throw error
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function runAll(list: Iterable<{ current: ScrollReader }>, f: ScrollFrame) {
|
|
62
|
+
const writes: Array<() => void> = []
|
|
63
|
+
for (const r of list) {
|
|
64
|
+
const w = guard(() => r.current(f))
|
|
65
|
+
if (w) writes.push(w)
|
|
66
|
+
}
|
|
67
|
+
writes.forEach((w) => guard(w))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function run(f: ScrollFrame) {
|
|
71
|
+
runAll(readers, f)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function tick(t: number) {
|
|
75
|
+
const y = window.scrollY
|
|
76
|
+
// The first frame after a wake only takes its bearings: its time can be earlier than the wake's.
|
|
77
|
+
let v = frame.v
|
|
78
|
+
if (last.t >= 0) {
|
|
79
|
+
v += (((y - last.y) / Math.max(1, t - last.t)) * 1000 - v) * 0.2
|
|
80
|
+
if (Math.abs(v) < 1) v = 0
|
|
81
|
+
}
|
|
82
|
+
const moved = y !== last.y
|
|
83
|
+
const dt = last.t >= 0 ? t - last.t : 0
|
|
84
|
+
last = { y, t }
|
|
85
|
+
frame = { y, v, dt }
|
|
86
|
+
settling = false
|
|
87
|
+
run(frame)
|
|
88
|
+
still = !moved && v === 0 && !settling ? still + 1 : 0
|
|
89
|
+
raf = still > 30 ? 0 : requestAnimationFrame(tick)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function wake() {
|
|
93
|
+
still = 0
|
|
94
|
+
if (raf) return
|
|
95
|
+
tokens = null
|
|
96
|
+
last = { y: window.scrollY, t: -1 }
|
|
97
|
+
raf = requestAnimationFrame(tick)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function onResize() {
|
|
101
|
+
tokens = null
|
|
102
|
+
wake()
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function start() {
|
|
106
|
+
window.addEventListener('scroll', wake, { passive: true })
|
|
107
|
+
window.addEventListener('resize', onResize)
|
|
108
|
+
// Fonts, pictures or an opened panel can move things without a scroll.
|
|
109
|
+
observer = new ResizeObserver(wake)
|
|
110
|
+
observer.observe(document.body)
|
|
111
|
+
reduce = window.matchMedia('(prefers-reduced-motion: reduce)')
|
|
112
|
+
reduce.addEventListener('change', wake)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function stop() {
|
|
116
|
+
window.removeEventListener('scroll', wake)
|
|
117
|
+
window.removeEventListener('resize', onResize)
|
|
118
|
+
observer?.disconnect()
|
|
119
|
+
observer = null
|
|
120
|
+
reduce?.removeEventListener('change', wake)
|
|
121
|
+
reduce = null
|
|
122
|
+
cancelAnimationFrame(raf)
|
|
123
|
+
raf = 0
|
|
124
|
+
frame = { y: 0, v: 0, dt: 0 }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Calls `read` once (with the other parts that start in the same task, before the first paint) and then
|
|
129
|
+
* every frame while the page moves. Returns a function that stops it.
|
|
130
|
+
*/
|
|
131
|
+
export function onScrollFrame(read: ScrollReader) {
|
|
132
|
+
const ref = { current: read }
|
|
133
|
+
if (!readers.size) start()
|
|
134
|
+
readers.add(ref)
|
|
135
|
+
mounting.add(ref)
|
|
136
|
+
if (mounting.size === 1) queueMicrotask(runMounts)
|
|
137
|
+
wake()
|
|
138
|
+
return () => {
|
|
139
|
+
readers.delete(ref)
|
|
140
|
+
mounting.delete(ref)
|
|
141
|
+
if (!readers.size) stop()
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Parts that mount together read once, together, before the first paint, so the page lays out once.
|
|
146
|
+
const mounting = new Set<{ current: ScrollReader }>()
|
|
147
|
+
|
|
148
|
+
function runMounts() {
|
|
149
|
+
const list = [...mounting]
|
|
150
|
+
mounting.clear()
|
|
151
|
+
runAll(list, { y: window.scrollY, v: frame.v, dt: 0 })
|
|
152
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Small helpers the scrubbed parts (Dial, RulerScrubber, PickerWheel) share in React and Vue.
|
|
2
|
+
|
|
3
|
+
/** A colour between two tokens by the pull, so ink fills in as a mark is drawn in. */
|
|
4
|
+
export const ink = (p: number, from = 'var(--gooee-color-fg-muted)', to = 'var(--gooee-color-fg)') => `color-mix(in srgb, ${to} ${Math.round(p * 100)}%, ${from})`
|
|
5
|
+
|
|
6
|
+
/** A number token read from `el`, such as a px size or a share. */
|
|
7
|
+
export const tokenNumber = (el: Element | null, name: string) => (el ? parseFloat(getComputedStyle(el).getPropertyValue(name)) || 0 : 0)
|
|
8
|
+
|
|
9
|
+
/** 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. */
|
|
10
|
+
export function sideways(e: WheelEvent) {
|
|
11
|
+
const line = e.deltaMode === 1 ? 40 : 1
|
|
12
|
+
const dx = (e.shiftKey && !e.deltaX ? e.deltaY : e.deltaX) * line
|
|
13
|
+
const dy = e.shiftKey ? 0 : e.deltaY * line
|
|
14
|
+
return Math.abs(dx) > Math.abs(dy) ? dx : null
|
|
15
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { createScrub, type ScrubOptions } from '../feel'
|
|
2
|
+
import { morphCurve, readMotionMs } from '../motion'
|
|
3
|
+
|
|
4
|
+
// Pointer, keys and wheel in, the drawn position out, for Dial, RulerScrubber and PickerWheel. The
|
|
5
|
+
// motion itself lives in feel.ts (createScrub).
|
|
6
|
+
|
|
7
|
+
type Last = { x: number; y: number }
|
|
8
|
+
|
|
9
|
+
/** What the scrubber reads from a pointer event: native and React events both have these. */
|
|
10
|
+
export type PointerLike = Pick<PointerEvent, 'button' | 'clientX' | 'clientY' | 'pointerId' | 'pointerType' | 'timeStamp' | 'type'> & { currentTarget: EventTarget | null }
|
|
11
|
+
type KeyLike = Pick<KeyboardEvent, 'key' | 'altKey' | 'ctrlKey' | 'metaKey' | 'preventDefault'>
|
|
12
|
+
|
|
13
|
+
export type ScrubberOptions<P extends PointerLike = PointerEvent> = Pick<ScrubOptions, 'min' | 'max' | 'edge' | 'land' | 'coast' | 'maxSpeed'> & {
|
|
14
|
+
/** The value it starts at, and later the value from outside: `sync` glides there. */
|
|
15
|
+
value: number
|
|
16
|
+
onSettle: (value: number) => void
|
|
17
|
+
/** A new resting value came under the hand while it moves. */
|
|
18
|
+
onMark?: (value: number) => void
|
|
19
|
+
/** How far a pointer move turns the value, in its units. */
|
|
20
|
+
move: (e: P, last: Last, el: HTMLElement) => number
|
|
21
|
+
/** The value a key goes to, or null to leave the key alone. */
|
|
22
|
+
key: (key: string, target: number) => number | null
|
|
23
|
+
/** The value a plain tap goes to, or null to land where it is. */
|
|
24
|
+
tap?: (e: P, at: number) => number | null
|
|
25
|
+
/** The value a wheel turn goes to, or null to let the page scroll. */
|
|
26
|
+
wheel?: (e: WheelEvent, target: number, focused: boolean) => number | null
|
|
27
|
+
/** A short buzz on each new value under the hand, where the phone can (Android). */
|
|
28
|
+
haptics?: boolean
|
|
29
|
+
/** Where it is drawn now, each frame. */
|
|
30
|
+
onFrame?: (at: number) => void
|
|
31
|
+
/** True while it moves, false once it rests. */
|
|
32
|
+
onMoving?: (moving: boolean) => void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type Press = { id: number; sx: number; sy: number; x: number; y: number; moved: boolean }
|
|
36
|
+
|
|
37
|
+
// A press is not a drag until it moves this far, so a tap stays a tap.
|
|
38
|
+
const SLOP = 4
|
|
39
|
+
|
|
40
|
+
/** A scrubbed value: feed it the control's events, or call `listen` on the control. `update` takes new options. */
|
|
41
|
+
export function createScrubber<P extends PointerLike = PointerEvent>(options: ScrubberOptions<P>) {
|
|
42
|
+
let o = options
|
|
43
|
+
let press: Press | null = null
|
|
44
|
+
const s = createScrub({
|
|
45
|
+
value: o.value,
|
|
46
|
+
land: (p) => o.land(p),
|
|
47
|
+
duration: () => readMotionMs('morph-out'),
|
|
48
|
+
curve: morphCurve,
|
|
49
|
+
onFrame: (v) => o.onFrame?.(v),
|
|
50
|
+
onMoving: (m) => o.onMoving?.(m),
|
|
51
|
+
onMark: (v) => {
|
|
52
|
+
o.onMark?.(v)
|
|
53
|
+
if (o.haptics) navigator.vibrate?.(5)
|
|
54
|
+
},
|
|
55
|
+
// Only a new value is news: a tap, Home at the start, or a drag back to where it was says nothing.
|
|
56
|
+
onSettle: (v) => v !== o.value && o.onSettle(v),
|
|
57
|
+
})
|
|
58
|
+
const update = (next: ScrubberOptions<P>) => {
|
|
59
|
+
o = next
|
|
60
|
+
s.update({ min: o.min, max: o.max, edge: o.edge, coast: o.coast, maxSpeed: o.maxSpeed })
|
|
61
|
+
}
|
|
62
|
+
update(o)
|
|
63
|
+
|
|
64
|
+
const down = (e: P) => {
|
|
65
|
+
if (e.pointerType === 'mouse' && e.button !== 0) return
|
|
66
|
+
try {
|
|
67
|
+
;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
|
|
68
|
+
} catch {
|
|
69
|
+
// A pointer the browser no longer knows (a synthetic one) cannot be captured, and needs no capture.
|
|
70
|
+
}
|
|
71
|
+
s.grab(e.timeStamp)
|
|
72
|
+
press = { id: e.pointerId, sx: e.clientX, sy: e.clientY, x: e.clientX, y: e.clientY, moved: false }
|
|
73
|
+
}
|
|
74
|
+
const move = (e: P) => {
|
|
75
|
+
const p = press
|
|
76
|
+
if (!p || p.id !== e.pointerId) return
|
|
77
|
+
if (!p.moved) {
|
|
78
|
+
if (Math.hypot(e.clientX - p.sx, e.clientY - p.sy) < SLOP) return
|
|
79
|
+
p.moved = true
|
|
80
|
+
}
|
|
81
|
+
const delta = o.move(e, p, e.currentTarget as HTMLElement)
|
|
82
|
+
p.x = e.clientX
|
|
83
|
+
p.y = e.clientY
|
|
84
|
+
s.drag(delta, e.timeStamp)
|
|
85
|
+
}
|
|
86
|
+
const up = (e: P) => {
|
|
87
|
+
const p = press
|
|
88
|
+
if (!p || p.id !== e.pointerId) return
|
|
89
|
+
press = null
|
|
90
|
+
if (e.type === 'pointercancel') return s.cancel()
|
|
91
|
+
if (p.moved) return s.release(e.timeStamp)
|
|
92
|
+
const t = o.tap?.(e, s.at)
|
|
93
|
+
if (t === null || t === undefined) s.release(e.timeStamp)
|
|
94
|
+
else s.go(t)
|
|
95
|
+
}
|
|
96
|
+
const keyDown = (e: KeyLike) => {
|
|
97
|
+
if (e.altKey || e.ctrlKey || e.metaKey) return
|
|
98
|
+
const t = o.key(e.key, s.target)
|
|
99
|
+
if (t === null) return
|
|
100
|
+
e.preventDefault()
|
|
101
|
+
s.go(t)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Lets the wheel turn it, when `wheel` is set. Returns a function that stops. */
|
|
105
|
+
const wheelOn = (el: HTMLElement) => {
|
|
106
|
+
const onWheel = (e: WheelEvent) => {
|
|
107
|
+
const t = o.wheel?.(e, s.target, document.activeElement === el) ?? null
|
|
108
|
+
if (t === null) return
|
|
109
|
+
e.preventDefault()
|
|
110
|
+
if (t !== s.target) s.go(t)
|
|
111
|
+
}
|
|
112
|
+
el.addEventListener('wheel', onWheel, { passive: false })
|
|
113
|
+
return () => el.removeEventListener('wheel', onWheel)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
down,
|
|
118
|
+
move,
|
|
119
|
+
up,
|
|
120
|
+
keyDown,
|
|
121
|
+
wheelOn,
|
|
122
|
+
/** A value set from outside (a controlled prop, a reset) glides there, unless the hand holds it. */
|
|
123
|
+
sync(value: number) {
|
|
124
|
+
if (press || s.moving || value === s.target) return
|
|
125
|
+
s.go(value)
|
|
126
|
+
},
|
|
127
|
+
update,
|
|
128
|
+
get at() {
|
|
129
|
+
return s.at
|
|
130
|
+
},
|
|
131
|
+
get moving() {
|
|
132
|
+
return s.moving
|
|
133
|
+
},
|
|
134
|
+
target: () => s.target,
|
|
135
|
+
go: (to: number) => s.go(to),
|
|
136
|
+
place: (to: number) => s.place(to),
|
|
137
|
+
destroy: () => s.stop(),
|
|
138
|
+
/** Listens on the control (pointer, keys, and the wheel when `wheel` is set). Returns a function that stops. */
|
|
139
|
+
listen(el: HTMLElement) {
|
|
140
|
+
const ac = new AbortController()
|
|
141
|
+
const opts = { signal: ac.signal }
|
|
142
|
+
el.addEventListener('pointerdown', (e) => down(e as unknown as P), opts)
|
|
143
|
+
el.addEventListener('pointermove', (e) => move(e as unknown as P), opts)
|
|
144
|
+
el.addEventListener('pointerup', (e) => up(e as unknown as P), opts)
|
|
145
|
+
el.addEventListener('pointercancel', (e) => up(e as unknown as P), opts)
|
|
146
|
+
el.addEventListener('keydown', keyDown, opts)
|
|
147
|
+
const stopWheel = o.wheel ? wheelOn(el) : () => {}
|
|
148
|
+
return () => {
|
|
149
|
+
ac.abort()
|
|
150
|
+
stopWheel()
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// The parts of a segmented row that are the same in every framework: the ink copy of the labels under
|
|
2
|
+
// the flowing pill, the tight mode, and the arrow keys.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Labels under the liquid take the chosen colour, from a copy of the row clipped to the drawn outline.
|
|
6
|
+
* Returns the `ink` callback for createFlow: an outline draws the copy, null removes it.
|
|
7
|
+
*/
|
|
8
|
+
export function segmentInk(root: () => HTMLElement | null, items: () => Array<HTMLElement | null>, cls: string) {
|
|
9
|
+
let layer: HTMLDivElement | null = null
|
|
10
|
+
return (outline: string | null) => {
|
|
11
|
+
const host = root()
|
|
12
|
+
if (!outline || !host) {
|
|
13
|
+
layer?.remove()
|
|
14
|
+
layer = null
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
if (!layer) {
|
|
18
|
+
layer = document.createElement('div')
|
|
19
|
+
layer.className = cls
|
|
20
|
+
layer.setAttribute('aria-hidden', 'true')
|
|
21
|
+
layer.inert = true
|
|
22
|
+
for (const el of items()) {
|
|
23
|
+
if (!el) continue
|
|
24
|
+
const copy = el.cloneNode(true) as HTMLElement
|
|
25
|
+
copy.removeAttribute('id')
|
|
26
|
+
copy.style.left = `${el.offsetLeft}px`
|
|
27
|
+
copy.style.top = `${el.offsetTop}px`
|
|
28
|
+
copy.style.width = `${el.offsetWidth}px`
|
|
29
|
+
copy.style.height = `${el.offsetHeight}px`
|
|
30
|
+
layer.append(copy)
|
|
31
|
+
}
|
|
32
|
+
host.append(layer)
|
|
33
|
+
}
|
|
34
|
+
layer.style.clipPath = `path('${outline}')`
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Tight mode only when the segments overflow at their normal size, so a wide screen keeps the normal look. Returns the chosen segment's place. */
|
|
39
|
+
export function measureSegments(root: HTMLElement | null, chosen: HTMLElement | null | undefined) {
|
|
40
|
+
if (root) {
|
|
41
|
+
root.removeAttribute('data-tight')
|
|
42
|
+
if (root.scrollWidth > root.clientWidth) root.setAttribute('data-tight', '')
|
|
43
|
+
}
|
|
44
|
+
return chosen ? { x: chosen.offsetLeft, w: chosen.offsetWidth } : null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The segment a key moves to, skipping disabled ones, or -1 for a key it leaves alone. */
|
|
48
|
+
export function segmentKey(key: string, from: number, disabled: boolean[]) {
|
|
49
|
+
const n = disabled.length
|
|
50
|
+
const step = (start: number, dir: 1 | -1) => {
|
|
51
|
+
let i = start
|
|
52
|
+
for (let k = 0; k < n; k++) {
|
|
53
|
+
i = (i + dir + n) % n
|
|
54
|
+
if (!disabled[i]) return i
|
|
55
|
+
}
|
|
56
|
+
return start
|
|
57
|
+
}
|
|
58
|
+
if (key === 'ArrowRight' || key === 'ArrowDown') return step(from, 1)
|
|
59
|
+
if (key === 'ArrowLeft' || key === 'ArrowUp') return step(from, -1)
|
|
60
|
+
if (key === 'Home') return step(-1, 1)
|
|
61
|
+
if (key === 'End') return step(n, -1)
|
|
62
|
+
return -1
|
|
63
|
+
}
|