gooee 0.50.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 -143
- 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 +11 -39
- 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/focus.ts +4 -47
- package/src/internal/icons.module.css +39 -27
- package/src/internal/layers.ts +7 -61
- 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/floating.ts +58 -0
- package/src/internal/motion/focus.ts +53 -0
- package/src/internal/motion/formField.ts +19 -0
- package/src/internal/motion/holdToConfirm.ts +147 -0
- package/src/internal/motion/keys.ts +42 -0
- package/src/internal/motion/layers.ts +78 -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/scrubLook.ts +15 -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/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/themeReveal.ts +3 -61
- package/src/internal/useFloating.ts +9 -49
- package/src/internal/useScrub.ts +1 -13
- 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,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,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
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { rubber, speedTracker } from '../feel'
|
|
2
|
+
import { release } from './fling'
|
|
3
|
+
import { returnTarget } from './focus'
|
|
4
|
+
|
|
5
|
+
// A sheet that rests at set heights (low, half, full), the same in every framework: drag, flick, keys on
|
|
6
|
+
// the grabber, and where focus goes. The framework keeps the state (the height, the target the drag
|
|
7
|
+
// aims at, whether it is dragging, the spoken text) and hands it over through the host.
|
|
8
|
+
|
|
9
|
+
/** A height a sheet can rest at: low with only its title showing, half the screen, or nearly all of it. */
|
|
10
|
+
export type SheetDetent = 'peek' | 'half' | 'full'
|
|
11
|
+
|
|
12
|
+
// Shares of the screen, from the heights Apple Maps on the web and Vaul rest at.
|
|
13
|
+
const SHARE: Record<SheetDetent, number> = { peek: 0.19, half: 0.5, full: 0.96 }
|
|
14
|
+
const ORDER: SheetDetent[] = ['peek', 'half', 'full']
|
|
15
|
+
export const DETENT_NAME: Record<SheetDetent, string> = { peek: 'Low', half: 'Half', full: 'Full' }
|
|
16
|
+
// px per second: faster than this, a let-go is a flick and moves one height its way.
|
|
17
|
+
const FLICK = 400
|
|
18
|
+
// How far ahead, in seconds, a slow let-go is carried before the nearest height is picked.
|
|
19
|
+
const PROJECT = 0.2
|
|
20
|
+
// A press is not a drag until it moves this far, so taps on the content still work.
|
|
21
|
+
const SLOP = 6
|
|
22
|
+
|
|
23
|
+
/** The detents asked for, low to high. */
|
|
24
|
+
export const detentOrder = (detents: SheetDetent[]) => ORDER.filter((d) => detents.includes(d))
|
|
25
|
+
/** Each detent's height in px for a screen `vh` tall. */
|
|
26
|
+
export const detentHeights = (order: SheetDetent[], vh: number) => order.map((d) => Math.round(vh * SHARE[d]))
|
|
27
|
+
|
|
28
|
+
const nearest = (v: number, hs: number[]) => hs.reduce((best, h, i) => (Math.abs(h - v) < Math.abs(hs[best] - v) ? i : best), 0)
|
|
29
|
+
|
|
30
|
+
// A flick moves one height from where the drag began, never two, even when the hand has already
|
|
31
|
+
// passed the next one. Real sheets measured all do this.
|
|
32
|
+
function pick(pos: number, v: number, start: number, hs: number[]) {
|
|
33
|
+
if (Math.abs(v) <= FLICK) return nearest(pos + v * PROJECT, hs)
|
|
34
|
+
return Math.max(0, Math.min(hs.length - 1, start + Math.sign(v)))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type DetentHost = {
|
|
38
|
+
panel: () => HTMLElement | null
|
|
39
|
+
overlay: () => HTMLElement | null
|
|
40
|
+
grabber: () => HTMLElement | null
|
|
41
|
+
body: () => HTMLElement | null
|
|
42
|
+
/** The detents, low to high (detentOrder). */
|
|
43
|
+
order: () => SheetDetent[]
|
|
44
|
+
/** Their heights now, in px (detentHeights). */
|
|
45
|
+
heights: () => number[]
|
|
46
|
+
/** The height it rests at now. */
|
|
47
|
+
index: () => number
|
|
48
|
+
open: () => boolean
|
|
49
|
+
/** Asked to rest at height `i`. */
|
|
50
|
+
change: (i: number) => void
|
|
51
|
+
/** Asked to close. */
|
|
52
|
+
close: () => void
|
|
53
|
+
/** The height a drag or a settle aims at, for the grabber's look. */
|
|
54
|
+
target: (i: number) => void
|
|
55
|
+
dragging: (on: boolean) => void
|
|
56
|
+
/** Text for the polite live region. */
|
|
57
|
+
say: (text: string) => void
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type PointerLike = { pointerType: string; button: number; target: EventTarget | null; clientY: number; pointerId: number }
|
|
61
|
+
type KeyLike = { key: string; preventDefault(): void }
|
|
62
|
+
|
|
63
|
+
export function createDetentSheet(host: DetentHost) {
|
|
64
|
+
let at = 0
|
|
65
|
+
let aimed = -1
|
|
66
|
+
let stopGlide = () => {}
|
|
67
|
+
let unlisten = () => {}
|
|
68
|
+
let opener: HTMLElement | null = null
|
|
69
|
+
let drag: { y0: number; at0: number; start: number; pointer: number; live: boolean; speed: ReturnType<typeof speedTracker> } | null = null
|
|
70
|
+
const top = () => host.heights().length - 1
|
|
71
|
+
|
|
72
|
+
const paint = (v: number) => {
|
|
73
|
+
at = v
|
|
74
|
+
const hs = host.heights()
|
|
75
|
+
const panel = host.panel()
|
|
76
|
+
if (!panel) return
|
|
77
|
+
panel.style.transform = `translateY(${hs[hs.length - 1] - v}px)`
|
|
78
|
+
// The dim follows the height between the two highest, so it never jumps.
|
|
79
|
+
const lo = hs.length > 1 ? hs[hs.length - 2] : 0
|
|
80
|
+
const dim = Math.max(0, Math.min(1, (v - lo) / (hs[hs.length - 1] - lo || 1)))
|
|
81
|
+
host.overlay()?.style.setProperty('opacity', String(dim))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const settle = (to: number, v: number, done?: () => void) => {
|
|
85
|
+
stopGlide()
|
|
86
|
+
stopGlide = release(at, to, v, paint, done)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const shownHidden = () => {
|
|
90
|
+
const active = document.activeElement as HTMLElement | null
|
|
91
|
+
return !!active && !!host.body()?.contains(active) && active.getBoundingClientRect().top > window.innerHeight - 8
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const go = (i: number, v = 0, spoken = true) => {
|
|
95
|
+
const hs = host.heights()
|
|
96
|
+
if (i < 0) {
|
|
97
|
+
// A glide carries it off at the hand's speed, so the CSS close stays out of the way.
|
|
98
|
+
host.panel()?.setAttribute('data-fluid', '')
|
|
99
|
+
settle(0, v)
|
|
100
|
+
host.close()
|
|
101
|
+
requestAnimationFrame(() => {
|
|
102
|
+
if (host.open()) {
|
|
103
|
+
host.panel()?.removeAttribute('data-fluid')
|
|
104
|
+
settle(host.heights()[host.index()], 0)
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
aimed = i
|
|
110
|
+
host.target(i)
|
|
111
|
+
if (i !== host.index()) {
|
|
112
|
+
host.change(i)
|
|
113
|
+
if (spoken) host.say(`${DETENT_NAME[host.order()[i]]} height`)
|
|
114
|
+
}
|
|
115
|
+
// Shrunk below a focused control: focus goes to the grabber, so it never sits on something hidden.
|
|
116
|
+
settle(hs[i], v, () => shownHidden() && host.grabber()?.focus({ preventScroll: true }))
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// The hand often leaves the sheet before the drag counts as one, so the moves are followed on the window.
|
|
120
|
+
const block = (e: Event) => e.preventDefault()
|
|
121
|
+
|
|
122
|
+
const endDrag = () => {
|
|
123
|
+
drag = null
|
|
124
|
+
unlisten()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const onMove = (e: PointerEvent) => {
|
|
128
|
+
const d = drag
|
|
129
|
+
const hs = host.heights()
|
|
130
|
+
if (!d || d.pointer !== e.pointerId) return
|
|
131
|
+
const dy = d.y0 - e.clientY
|
|
132
|
+
if (!d.live) {
|
|
133
|
+
if (Math.abs(dy) < SLOP) return
|
|
134
|
+
if (host.body()?.contains(e.target as Node) && host.index() === top() && dy > 0) return endDrag()
|
|
135
|
+
// A tap must not stop a settle half way, so the glide is caught only once it is a drag.
|
|
136
|
+
stopGlide()
|
|
137
|
+
d.live = true
|
|
138
|
+
d.at0 = at
|
|
139
|
+
d.start = nearest(at, hs)
|
|
140
|
+
d.speed.add(at)
|
|
141
|
+
d.y0 = e.clientY + (dy > 0 ? SLOP : -SLOP)
|
|
142
|
+
// Only now, so a tap on a button inside still clicks it.
|
|
143
|
+
const panel = host.panel()
|
|
144
|
+
if (e.target instanceof Element && panel?.contains(e.target)) panel.setPointerCapture?.(e.pointerId)
|
|
145
|
+
window.addEventListener('selectstart', block)
|
|
146
|
+
host.dragging(true)
|
|
147
|
+
}
|
|
148
|
+
// A drag moves the sheet, never selects the page's text: a selection left over from one drag
|
|
149
|
+
// would turn the next press on it into the browser's own drag, which cancels the pointer. Safari
|
|
150
|
+
// keeps growing the selection it began on the press, so it is cleared on every move.
|
|
151
|
+
const selection = window.getSelection()
|
|
152
|
+
if (selection && !selection.isCollapsed) selection.removeAllRanges()
|
|
153
|
+
let raw = d.at0 + (d.y0 - e.clientY)
|
|
154
|
+
const full = hs[hs.length - 1]
|
|
155
|
+
if (raw > full) raw = full + rubber(raw - full, window.innerHeight - full)
|
|
156
|
+
if (raw < 0) raw = 0
|
|
157
|
+
d.speed.add(raw)
|
|
158
|
+
paint(raw)
|
|
159
|
+
host.target(pick(raw, d.speed.speed(), d.start, hs))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const onUp = (e: PointerEvent) => {
|
|
163
|
+
const d = drag
|
|
164
|
+
if (!d || d.pointer !== e.pointerId) return
|
|
165
|
+
endDrag()
|
|
166
|
+
if (!d.live) return
|
|
167
|
+
host.dragging(false)
|
|
168
|
+
const hs = host.heights()
|
|
169
|
+
const cancelled = e.type === 'pointercancel'
|
|
170
|
+
const v = cancelled ? 0 : d.speed.speed()
|
|
171
|
+
if (!cancelled && d.start === 0 && (v < -FLICK || at < hs[0] * 0.5)) return go(-1, v)
|
|
172
|
+
go(pick(at, v, d.start, hs), v)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
/** Call as it opens, before focus moves: it remembers the opener and starts fresh. */
|
|
177
|
+
opening() {
|
|
178
|
+
opener = returnTarget()
|
|
179
|
+
aimed = -1
|
|
180
|
+
},
|
|
181
|
+
/** Call once it is in the page, and when the height or the screen changes. */
|
|
182
|
+
sync(initialFocus?: HTMLElement | null) {
|
|
183
|
+
const i = host.index()
|
|
184
|
+
const hs = host.heights()
|
|
185
|
+
if (aimed === -1) {
|
|
186
|
+
aimed = i
|
|
187
|
+
host.target(i)
|
|
188
|
+
paint(hs[i])
|
|
189
|
+
if (i !== top()) (initialFocus ?? host.grabber())?.focus({ preventScroll: true })
|
|
190
|
+
return
|
|
191
|
+
}
|
|
192
|
+
if (drag?.live) return
|
|
193
|
+
if (aimed !== i) {
|
|
194
|
+
aimed = i
|
|
195
|
+
host.target(i)
|
|
196
|
+
settle(hs[i], 0)
|
|
197
|
+
} else paint(hs[i])
|
|
198
|
+
},
|
|
199
|
+
/** Call once it has closed: focus goes back to its opener. */
|
|
200
|
+
closed() {
|
|
201
|
+
const panel = host.panel()
|
|
202
|
+
const active = document.activeElement
|
|
203
|
+
if (panel && opener?.isConnected && (!active || active === document.body || panel.contains(active))) opener.focus({ preventScroll: true })
|
|
204
|
+
},
|
|
205
|
+
/** Esc: down one height, and closed from the lowest. */
|
|
206
|
+
stepDown: () => (host.index() > 0 ? go(host.index() - 1) : host.close()),
|
|
207
|
+
/** The panel's pointerdown. */
|
|
208
|
+
down(e: PointerLike) {
|
|
209
|
+
if ((e.pointerType === 'mouse' && e.button !== 0) || drag) return
|
|
210
|
+
const el = e.target as Element
|
|
211
|
+
if (el.closest('input, textarea, select, [contenteditable="true"]')) return
|
|
212
|
+
const hs = host.heights()
|
|
213
|
+
const body = host.body()
|
|
214
|
+
// At the top the content scrolls; a drag on it moves the sheet only from the top of the content.
|
|
215
|
+
if (body?.contains(el) && host.index() === top() && (body.scrollTop ?? 0) > 0) return
|
|
216
|
+
drag = { y0: e.clientY, at0: at, start: nearest(at, hs), pointer: e.pointerId, live: false, speed: speedTracker() }
|
|
217
|
+
window.addEventListener('pointermove', onMove)
|
|
218
|
+
window.addEventListener('pointerup', onUp)
|
|
219
|
+
window.addEventListener('pointercancel', onUp)
|
|
220
|
+
window.addEventListener('dragstart', block)
|
|
221
|
+
// Safari starts a text selection on the press itself, so a press outside the content (the grabber,
|
|
222
|
+
// the edges) blocks it at once. Text in the content can still be selected by dragging inside it.
|
|
223
|
+
if (!body?.contains(el)) window.addEventListener('selectstart', block)
|
|
224
|
+
unlisten = () => {
|
|
225
|
+
window.removeEventListener('pointermove', onMove)
|
|
226
|
+
window.removeEventListener('pointerup', onUp)
|
|
227
|
+
window.removeEventListener('pointercancel', onUp)
|
|
228
|
+
window.removeEventListener('dragstart', block)
|
|
229
|
+
window.removeEventListener('selectstart', block)
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
/** The grabber's keydown: it is a slider for the height. */
|
|
233
|
+
grabKey(e: KeyLike) {
|
|
234
|
+
const i = host.index()
|
|
235
|
+
const t = top()
|
|
236
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
237
|
+
e.preventDefault()
|
|
238
|
+
return go(i === t ? 0 : i + 1, 0, false)
|
|
239
|
+
}
|
|
240
|
+
const keys: Record<string, number> = { ArrowUp: i + 1, ArrowRight: i + 1, ArrowDown: i - 1, ArrowLeft: i - 1, Home: 0, End: t, PageUp: t, PageDown: 0 }
|
|
241
|
+
if (!(e.key in keys)) return
|
|
242
|
+
e.preventDefault()
|
|
243
|
+
go(Math.max(0, Math.min(t, keys[e.key])), 0, false)
|
|
244
|
+
},
|
|
245
|
+
/** The grabber's click: up one height, and from the top back to the lowest. */
|
|
246
|
+
grabClick: () => go(host.index() === top() ? 0 : host.index() + 1),
|
|
247
|
+
/** The panel's focusin: tabbing onto a control hidden below the screen's edge grows the sheet to the top, so it shows. */
|
|
248
|
+
focusIn(target: EventTarget | null) {
|
|
249
|
+
if (!host.body()?.contains(target as Node) || host.index() === top()) return
|
|
250
|
+
if ((target as Element).getBoundingClientRect().bottom > window.innerHeight - 4) go(top())
|
|
251
|
+
},
|
|
252
|
+
/** A touch at the top of the content, moving down, belongs to the sheet: the browser would scroll it. Returns a stop. */
|
|
253
|
+
holdTouch(body: HTMLElement) {
|
|
254
|
+
let y = 0
|
|
255
|
+
const start = (e: TouchEvent) => (y = e.touches[0].clientY)
|
|
256
|
+
const move = (e: TouchEvent) => {
|
|
257
|
+
if (drag?.live || (host.index() === top() && body.scrollTop <= 0 && e.touches[0].clientY > y)) e.preventDefault()
|
|
258
|
+
}
|
|
259
|
+
body.addEventListener('touchstart', start, { passive: true })
|
|
260
|
+
body.addEventListener('touchmove', move, { passive: false })
|
|
261
|
+
return () => {
|
|
262
|
+
body.removeEventListener('touchstart', start)
|
|
263
|
+
body.removeEventListener('touchmove', move)
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
destroy() {
|
|
267
|
+
stopGlide()
|
|
268
|
+
unlisten()
|
|
269
|
+
},
|
|
270
|
+
}
|
|
271
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { bezier, nearestMark } from '../feel'
|
|
2
|
+
import { morphCurve, motionMs, prefersReducedMotion } from '../motion'
|
|
3
|
+
|
|
4
|
+
export type SliderRead = { value: number; min: number; max: number; step: number; marks?: number[] }
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A native range input that glides to a click and lands on a mark let go near it. `read` gives the
|
|
8
|
+
* value and props now; `setValue` sets a new value; `onShown` gets where the thumb is drawn while it
|
|
9
|
+
* glides (null once it is at the value). Wire `down`, `move`, `up`, `end`, `input` and `key` to the input.
|
|
10
|
+
*/
|
|
11
|
+
export function createSlider(read: () => SliderRead, setValue: (v: number) => void, onShown: (v: number | null) => void) {
|
|
12
|
+
// `grab` is how far from the thumb's centre it was picked up, so the thumb does not jump to the pointer.
|
|
13
|
+
let press: { x: number; lastX: number; moved: boolean; grab: number } | null = null
|
|
14
|
+
let shown: number | null = null
|
|
15
|
+
let glide = 0
|
|
16
|
+
const show = (v: number | null) => {
|
|
17
|
+
shown = v
|
|
18
|
+
onShown(v)
|
|
19
|
+
}
|
|
20
|
+
const at = () => shown ?? read().value
|
|
21
|
+
|
|
22
|
+
const toStep = (v: number) => {
|
|
23
|
+
const { min, max, step } = read()
|
|
24
|
+
const decimals = (String(step).split('.')[1] ?? '').length
|
|
25
|
+
return Math.min(max, Math.max(min, Number((Math.round((v - min) / step) * step + min).toFixed(decimals))))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The thumb travels from where it is drawn to `to` on the morph curve. A click and a landing both use it.
|
|
29
|
+
function glideTo(from: number, to: number) {
|
|
30
|
+
cancelAnimationFrame(glide)
|
|
31
|
+
const ms = motionMs('morph-out')
|
|
32
|
+
if (!ms || from === to || prefersReducedMotion()) return show(null)
|
|
33
|
+
const ease = bezier(morphCurve())
|
|
34
|
+
const t0 = performance.now()
|
|
35
|
+
const tick = (now: number) => {
|
|
36
|
+
const p = Math.min(1, (now - t0) / ms)
|
|
37
|
+
show(p < 1 ? from + (to - from) * ease(p) : null)
|
|
38
|
+
if (p < 1) glide = requestAnimationFrame(tick)
|
|
39
|
+
}
|
|
40
|
+
glide = requestAnimationFrame(tick)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// The value under the pointer, before any step, from the input's own geometry: the thumb's centre
|
|
44
|
+
// runs from half a thumb in at each end.
|
|
45
|
+
function track(input: HTMLInputElement) {
|
|
46
|
+
const r = input.getBoundingClientRect()
|
|
47
|
+
const thumb = parseFloat(getComputedStyle(input).getPropertyValue('--thumb')) || 0
|
|
48
|
+
return { left: r.left + thumb / 2, width: Math.max(1, r.width - thumb), thumb }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function fromPointer(input: HTMLInputElement, x: number) {
|
|
52
|
+
const { min, max } = read()
|
|
53
|
+
const t = track(input)
|
|
54
|
+
return min + Math.min(1, Math.max(0, (x - t.left) / t.width)) * (max - min)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const end = () => {
|
|
58
|
+
press = null
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
/** The input's "input" event, with the number the browser moved to. */
|
|
63
|
+
input(input: HTMLInputElement, native: number) {
|
|
64
|
+
const p = press
|
|
65
|
+
const next = p && read().marks?.length ? toStep(fromPointer(input, p.lastX - p.grab)) : native
|
|
66
|
+
// A press that has not moved is a click: the value jumps, the thumb glides there. Once it moves, the hand leads.
|
|
67
|
+
if (p && !p.moved) glideTo(at(), next)
|
|
68
|
+
else if (shown !== null) {
|
|
69
|
+
cancelAnimationFrame(glide)
|
|
70
|
+
show(null)
|
|
71
|
+
}
|
|
72
|
+
setValue(next)
|
|
73
|
+
},
|
|
74
|
+
down(input: HTMLInputElement, x: number) {
|
|
75
|
+
const { value, min, max, marks } = read()
|
|
76
|
+
const t = track(input)
|
|
77
|
+
const centre = t.left + ((value - min) / (max - min || 1)) * t.width
|
|
78
|
+
const grab = marks?.length && Math.abs(x - centre) <= t.thumb / 2 ? x - centre : 0
|
|
79
|
+
press = { x, lastX: x, moved: false, grab }
|
|
80
|
+
},
|
|
81
|
+
move(x: number) {
|
|
82
|
+
if (!press) return
|
|
83
|
+
press.lastX = x
|
|
84
|
+
if (Math.abs(x - press.x) > 3) press.moved = true
|
|
85
|
+
},
|
|
86
|
+
up() {
|
|
87
|
+
end()
|
|
88
|
+
// Let go near a mark: it lands on the mark. Within half of 6% of the track counts as near.
|
|
89
|
+
const { value, min, max, marks } = read()
|
|
90
|
+
const mark = marks?.length ? nearestMark(value, marks) : null
|
|
91
|
+
if (mark !== null && mark !== value && Math.abs(mark - value) < ((max - min) * 0.06) / 2) {
|
|
92
|
+
glideTo(at(), mark)
|
|
93
|
+
setValue(mark)
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
end,
|
|
97
|
+
/** A key down, after the caller's own handler. Returns true when it used the key. */
|
|
98
|
+
key(key: string) {
|
|
99
|
+
end()
|
|
100
|
+
const { value, min, max, marks } = read()
|
|
101
|
+
if (!marks?.length || (key !== 'PageUp' && key !== 'PageDown')) return false
|
|
102
|
+
const sorted = [...marks].sort((a, b) => a - b)
|
|
103
|
+
setValue(key === 'PageUp' ? (sorted.find((m) => m > value) ?? max) : ([...sorted].reverse().find((m) => m < value) ?? min))
|
|
104
|
+
return true
|
|
105
|
+
},
|
|
106
|
+
destroy: () => cancelAnimationFrame(glide),
|
|
107
|
+
}
|
|
108
|
+
}
|