gooee 0.50.0 → 0.51.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -4
- package/package.json +5 -3
- 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,205 @@
|
|
|
1
|
+
import { easing, readMotionMs } from '../motion'
|
|
2
|
+
|
|
3
|
+
// SplitFlap's board with no framework: each cell runs forward through its drum to the new letter, and
|
|
4
|
+
// the board's edge grows or folds when the text gets longer or shorter.
|
|
5
|
+
|
|
6
|
+
/** The class names the board draws with (SplitFlap.module.css). */
|
|
7
|
+
export type FlapLook = { cell: string; top: string; bottom: string; glyph: string; flap: string; front: string; back: string }
|
|
8
|
+
|
|
9
|
+
/** The drum of a real board: a blank, letters, digits and a few marks. */
|
|
10
|
+
export const SPLIT_FLAP_CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:.,-+%/'
|
|
11
|
+
|
|
12
|
+
// A fold of about 90ms, fast steps of 40ms, and the last three slow down. A cell passes 8 letters at
|
|
13
|
+
// most, and the cells start one after another within 350ms, so a whole change ends inside a second.
|
|
14
|
+
const FOLD = 90
|
|
15
|
+
const FAST = 40
|
|
16
|
+
const TAIL = [70, 100, 140]
|
|
17
|
+
const MAX_STEPS = 8
|
|
18
|
+
const STAGGER = 35
|
|
19
|
+
const CASCADE_CAP = 350
|
|
20
|
+
// The fast motion token at its base, so Retime scales the board with everything else.
|
|
21
|
+
const FAST_TOKEN = 140
|
|
22
|
+
|
|
23
|
+
type Cell = { root: HTMLSpanElement; top: HTMLSpanElement; bottom: HTMLSpanElement; shown: string }
|
|
24
|
+
|
|
25
|
+
/** The letters a cell passes on its way from `from` to `to`, at most MAX_STEPS, ending on `to`. */
|
|
26
|
+
function route(from: string, to: string, drum: string) {
|
|
27
|
+
const a = drum.indexOf(from)
|
|
28
|
+
const b = drum.indexOf(to)
|
|
29
|
+
if (a < 0 || b < 0) return from === to ? [] : [to]
|
|
30
|
+
const n = Math.min((b - a + drum.length) % drum.length, MAX_STEPS)
|
|
31
|
+
return Array.from({ length: n }, (_, i) => drum[(b - n + 1 + i + drum.length) % drum.length])
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The wait before each step: fast in the middle, slow for the last three. */
|
|
35
|
+
const gaps = (n: number) => Array.from({ length: n }, (_, i) => (n - 1 - i < TAIL.length ? TAIL[TAIL.length - n + i] : FAST))
|
|
36
|
+
|
|
37
|
+
const NBSP = ' '
|
|
38
|
+
const shownChar = (ch: string) => (ch === ' ' ? NBSP : ch)
|
|
39
|
+
const escape = (ch: string) => ({ '&': '&', '<': '<', '>': '>' })[ch] ?? ch
|
|
40
|
+
|
|
41
|
+
const halfHtml = (look: FlapLook, cls: string, ch: string) => `<span class="${cls}"><span class="${look.glyph}">${escape(shownChar(ch))}</span></span>`
|
|
42
|
+
const cellHtml = (look: FlapLook, ch: string) => `<span class="${look.cell}">${halfHtml(look, look.top, ch)}${halfHtml(look, look.bottom, ch)}</span>`
|
|
43
|
+
|
|
44
|
+
/** The first cells as markup, so a server page shows the board before any script runs. */
|
|
45
|
+
export const flapCellsHtml = (look: FlapLook, target: string) => [...target].map((ch) => cellHtml(look, ch)).join('')
|
|
46
|
+
|
|
47
|
+
const fromHtml = (html: string) => {
|
|
48
|
+
const t = document.createElement('template')
|
|
49
|
+
t.innerHTML = html
|
|
50
|
+
return t.content.firstElementChild as HTMLSpanElement
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const makeCell = (look: FlapLook, ch: string): Cell => {
|
|
54
|
+
const root = fromHtml(cellHtml(look, ch))
|
|
55
|
+
return { root, top: root.children[0] as HTMLSpanElement, bottom: root.children[1] as HTMLSpanElement, shown: ch }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const setHalf = (h: Element, ch: string) => {
|
|
59
|
+
;(h.firstElementChild as HTMLElement).textContent = shownChar(ch)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Runs a board whose cells are already in `b` and show `first`. `show` flips them to new text and calls
|
|
64
|
+
* `onDone` once every cell has landed; a call in the middle of a run goes straight on to the newest text.
|
|
65
|
+
*/
|
|
66
|
+
export function createSplitFlap(b: HTMLElement, look: FlapLook, first: string) {
|
|
67
|
+
let cells: Cell[] = [...b.children].map((root, i) => ({
|
|
68
|
+
root: root as HTMLSpanElement,
|
|
69
|
+
top: root.children[0] as HTMLSpanElement,
|
|
70
|
+
bottom: root.children[1] as HTMLSpanElement,
|
|
71
|
+
shown: [...first][i] ?? ' ',
|
|
72
|
+
}))
|
|
73
|
+
let timers: number[] = []
|
|
74
|
+
const anims = new Set<Animation>()
|
|
75
|
+
let run = 0
|
|
76
|
+
let shrinkEnd: (() => void) | null = null
|
|
77
|
+
|
|
78
|
+
const stopAll = () => {
|
|
79
|
+
timers.forEach(clearTimeout)
|
|
80
|
+
timers = []
|
|
81
|
+
anims.forEach((a) => a.finish())
|
|
82
|
+
anims.clear()
|
|
83
|
+
shrinkEnd?.()
|
|
84
|
+
// Caught mid-run, each cell carries on from the letter it was showing.
|
|
85
|
+
for (const c of cells) setHalf(c.bottom, c.shown)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const show = (target: string, chars: string, onDone: () => void) => {
|
|
89
|
+
const id = ++run
|
|
90
|
+
const live = () => id === run
|
|
91
|
+
const running = timers.length > 0 || anims.size > 0
|
|
92
|
+
stopAll()
|
|
93
|
+
const done = () => {
|
|
94
|
+
if (!live()) return
|
|
95
|
+
timers = []
|
|
96
|
+
onDone()
|
|
97
|
+
}
|
|
98
|
+
const list = cells
|
|
99
|
+
if (list.map((c) => c.shown).join('') === target) {
|
|
100
|
+
if (running) done()
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const scale = readMotionMs('fast') / FAST_TOKEN
|
|
105
|
+
// Reduced motion: the new text at once, with no flips and no morph.
|
|
106
|
+
if (!scale) {
|
|
107
|
+
cells = [...target].map((ch) => makeCell(look, ch))
|
|
108
|
+
b.replaceChildren(...cells.map((c) => c.root))
|
|
109
|
+
done()
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const n0 = list.length
|
|
114
|
+
const n1 = target.length
|
|
115
|
+
const radius = getComputedStyle(b).borderRadius || '0px'
|
|
116
|
+
const grow = { duration: readMotionMs('morph'), easing: easing('morph') }
|
|
117
|
+
const shrink = { duration: readMotionMs('morph-out'), easing: easing('morph') }
|
|
118
|
+
let left = 0
|
|
119
|
+
const landed = () => {
|
|
120
|
+
if (--left === 0) done()
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Longer: the new cells are laid out at once, and the board's edge grows over them.
|
|
124
|
+
if (n1 > n0) {
|
|
125
|
+
const before = b.offsetWidth
|
|
126
|
+
for (let i = n0; i < n1; i++) {
|
|
127
|
+
const c = makeCell(look, ' ')
|
|
128
|
+
list.push(c)
|
|
129
|
+
b.append(c.root)
|
|
130
|
+
}
|
|
131
|
+
const wider = b.offsetWidth - before
|
|
132
|
+
const a = b.animate({ clipPath: [`inset(0 ${wider}px 0 0 round ${radius})`, `inset(0 0 0 0 round ${radius})`] }, grow)
|
|
133
|
+
anims.add(a)
|
|
134
|
+
a.finished.then(
|
|
135
|
+
() => anims.delete(a),
|
|
136
|
+
() => {},
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Shorter: the board's edge folds over the extra cells while the rest flip, then they go.
|
|
141
|
+
if (n1 < n0) {
|
|
142
|
+
const keep = list.slice(0, n1)
|
|
143
|
+
const box = b.getBoundingClientRect()
|
|
144
|
+
const edge = keep.length ? keep[keep.length - 1].root.getBoundingClientRect().right : box.left
|
|
145
|
+
const a = b.animate({ clipPath: [`inset(0 0 0 0 round ${radius})`, `inset(0 ${box.right - edge}px 0 0 round ${radius})`] }, { ...shrink, fill: 'forwards' })
|
|
146
|
+
left++
|
|
147
|
+
const end = () => {
|
|
148
|
+
shrinkEnd = null
|
|
149
|
+
list.slice(n1).forEach((c) => c.root.remove())
|
|
150
|
+
cells = keep
|
|
151
|
+
a.cancel()
|
|
152
|
+
if (live()) landed()
|
|
153
|
+
}
|
|
154
|
+
shrinkEnd = end
|
|
155
|
+
a.finished.then(
|
|
156
|
+
() => shrinkEnd === end && end(),
|
|
157
|
+
() => {},
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const goal = [...target]
|
|
162
|
+
const changed = goal.map((ch, i) => (list[i].shown !== ch ? i : -1)).filter((i) => i >= 0)
|
|
163
|
+
const stagger = Math.min(STAGGER, CASCADE_CAP / Math.max(1, changed.length))
|
|
164
|
+
left += changed.length
|
|
165
|
+
if (!left) return done()
|
|
166
|
+
|
|
167
|
+
const flip = (cell: Cell, next: string, last: boolean, fold: number) => {
|
|
168
|
+
const prev = cell.shown
|
|
169
|
+
cell.shown = next
|
|
170
|
+
setHalf(cell.top, next)
|
|
171
|
+
// The falling flap: the old letter's top half in front, the new letter's bottom half behind.
|
|
172
|
+
const flap = fromHtml(`<span class="${look.flap}">${halfHtml(look, look.front, prev)}${halfHtml(look, look.back, next)}</span>`)
|
|
173
|
+
cell.root.append(flap)
|
|
174
|
+
const a = flap.animate([{ transform: 'rotateX(0deg)' }, { transform: 'rotateX(-180deg)' }], { duration: fold, easing: easing('morph') })
|
|
175
|
+
anims.add(a)
|
|
176
|
+
const end = () => {
|
|
177
|
+
anims.delete(a)
|
|
178
|
+
setHalf(cell.bottom, next)
|
|
179
|
+
flap.remove()
|
|
180
|
+
if (last && live()) landed()
|
|
181
|
+
}
|
|
182
|
+
a.finished.then(end, end)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
changed.forEach((i, order) => {
|
|
186
|
+
const cell = list[i]
|
|
187
|
+
const path = route(cell.shown, goal[i], chars)
|
|
188
|
+
const wait = gaps(path.length)
|
|
189
|
+
let t = order * stagger
|
|
190
|
+
path.forEach((next, k) => {
|
|
191
|
+
timers.push(window.setTimeout(() => flip(cell, next, k === path.length - 1, Math.max(FOLD, wait[k]) * scale), t * scale))
|
|
192
|
+
t += wait[k]
|
|
193
|
+
})
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Gone: the run is over, so a cancelled flip ends without calling onDone.
|
|
198
|
+
const destroy = () => {
|
|
199
|
+
run++
|
|
200
|
+
timers.forEach(clearTimeout)
|
|
201
|
+
anims.forEach((a) => a.cancel())
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return { show, destroy }
|
|
205
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { clamp, pull, speedTracker } from '../feel'
|
|
2
|
+
import { easing, readMotionMs } from '../motion'
|
|
3
|
+
import { release } from './fling'
|
|
4
|
+
|
|
5
|
+
// A stack of cards: the top one is thrown to either side to decide. The part keeps the decisions as its
|
|
6
|
+
// own state and draws the cards; this core drags, throws and brings back the top card, and fills the
|
|
7
|
+
// ruler and the two buttons as it goes.
|
|
8
|
+
|
|
9
|
+
export type DeckSide = -1 | 1
|
|
10
|
+
export type DeckDir = 'start' | 'end'
|
|
11
|
+
|
|
12
|
+
/** What a drag needs from a pointer event: native and React events both have these. */
|
|
13
|
+
type Press = { clientX: number; clientY: number; pointerId: number; pointerType: string; button: number; timeStamp: number; currentTarget: EventTarget | null }
|
|
14
|
+
|
|
15
|
+
// A press is a drag only once it moves this far sideways, so a tap on a card's own button stays a tap.
|
|
16
|
+
const SLOP = 8
|
|
17
|
+
// The throw rule: past --gooee-deck-throw of the card's width, or a flick this fast that has moved at least FLICK_MIN.
|
|
18
|
+
const FLICK = 600
|
|
19
|
+
const FLICK_MIN = 40
|
|
20
|
+
const TILT = 0.06
|
|
21
|
+
const TILT_MAX = 14
|
|
22
|
+
// The speed a card leaves at, or comes back at on undo, when no hand threw it.
|
|
23
|
+
const THROW_SPEED = 1800
|
|
24
|
+
|
|
25
|
+
export type SwipeDeckOptions = {
|
|
26
|
+
root: () => HTMLElement | null
|
|
27
|
+
deck: () => HTMLElement | null
|
|
28
|
+
/** The top card, the one the hand moves. */
|
|
29
|
+
top: () => HTMLElement | null
|
|
30
|
+
knob: () => HTMLElement | null
|
|
31
|
+
button: (side: DeckSide) => HTMLElement | null
|
|
32
|
+
setArmed: (side: DeckSide | 0) => void
|
|
33
|
+
/** Whether any card is left to decide. */
|
|
34
|
+
hasCards: () => boolean
|
|
35
|
+
/**
|
|
36
|
+
* Decides the top card. The next card must be in the page when it returns (or when its promise
|
|
37
|
+
* settles), before the thrown card's transform resets: React uses flushSync, Vue nextTick.
|
|
38
|
+
*/
|
|
39
|
+
commit: (side: DeckSide) => void | Promise<void>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createSwipeDeck(o: SwipeDeckOptions) {
|
|
43
|
+
let at = 0
|
|
44
|
+
let stop = () => {}
|
|
45
|
+
let busy = false
|
|
46
|
+
let queued: DeckSide | null = null
|
|
47
|
+
let moved = false
|
|
48
|
+
let undoing: DeckSide | null = null
|
|
49
|
+
let drag: { x: number; y: number; base: number; id: number; sideways: boolean; speed: ReturnType<typeof speedTracker> } | null = null
|
|
50
|
+
let share = 0.35
|
|
51
|
+
|
|
52
|
+
const rtl = () => {
|
|
53
|
+
const root = o.root()
|
|
54
|
+
return !!root && getComputedStyle(root).direction === 'rtl'
|
|
55
|
+
}
|
|
56
|
+
// Right is the end side, or the start side in a right-to-left page.
|
|
57
|
+
const named = (side: DeckSide): DeckDir => ((side > 0) !== rtl() ? 'end' : 'start')
|
|
58
|
+
const physical = (dir: DeckDir): DeckSide => ((dir === 'end') !== rtl() ? 1 : -1)
|
|
59
|
+
const widthOf = () => o.top()?.offsetWidth || o.deck()?.offsetWidth || 300
|
|
60
|
+
const line = () => widthOf() * share
|
|
61
|
+
const reduced = () => readMotionMs('morph-out') === 0
|
|
62
|
+
|
|
63
|
+
const paint = (x: number) => {
|
|
64
|
+
at = x
|
|
65
|
+
const el = o.top()
|
|
66
|
+
if (el) {
|
|
67
|
+
el.style.transform = x ? `translate3d(${x.toFixed(1)}px, 0, 0) rotate(${clamp(x * TILT, -TILT_MAX, TILT_MAX).toFixed(2)}deg)` : ''
|
|
68
|
+
// Past a card's width away it fades as it goes, so it leaves before any edge cuts it.
|
|
69
|
+
const w = widthOf()
|
|
70
|
+
const fade = clamp(1 - (Math.abs(x) - w) / (w * 0.8), 0, 1)
|
|
71
|
+
el.style.opacity = fade < 1 ? fade.toFixed(3) : ''
|
|
72
|
+
}
|
|
73
|
+
const T = line()
|
|
74
|
+
const near = pull(T - Math.min(Math.abs(x), T), T)
|
|
75
|
+
o.deck()?.style.setProperty('--rise', near.toFixed(3))
|
|
76
|
+
const side: DeckSide | 0 = x > 0 ? 1 : x < 0 ? -1 : 0
|
|
77
|
+
for (const dir of ['start', 'end'] as DeckDir[]) {
|
|
78
|
+
const btn = o.button(dir === 'start' ? -1 : 1)
|
|
79
|
+
if (btn) btn.style.setProperty('--fill', side === physical(dir) ? near.toFixed(3) : '0')
|
|
80
|
+
}
|
|
81
|
+
o.setArmed(Math.abs(x) >= T - 0.5 ? side : 0)
|
|
82
|
+
const knob = o.knob()
|
|
83
|
+
if (knob) knob.style.transform = `translateX(${clamp(x, -widthOf() / 2, widthOf() / 2).toFixed(1)}px)`
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const offStage = (side: DeckSide) => side * ((o.root()?.offsetWidth ?? 400) / 2 + widthOf() * 1.2)
|
|
87
|
+
|
|
88
|
+
const finish = (side: DeckSide) => {
|
|
89
|
+
stop = () => {}
|
|
90
|
+
if (!o.hasCards()) {
|
|
91
|
+
busy = false
|
|
92
|
+
return paint(0)
|
|
93
|
+
}
|
|
94
|
+
const applied = o.commit(side)
|
|
95
|
+
if (applied) applied.then(() => paint(0))
|
|
96
|
+
else paint(0)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const fly = (side: DeckSide, velocity: number) => {
|
|
100
|
+
const el = o.top()
|
|
101
|
+
if (!el || !o.hasCards()) return
|
|
102
|
+
if (busy) {
|
|
103
|
+
queued = side
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
busy = true
|
|
107
|
+
stop()
|
|
108
|
+
if (reduced()) {
|
|
109
|
+
const anim = el.animate({ opacity: [1, 0] }, { duration: readMotionMs('base'), easing: easing('out'), fill: 'forwards' })
|
|
110
|
+
let ended = false
|
|
111
|
+
const end = () => {
|
|
112
|
+
if (ended) return
|
|
113
|
+
ended = true
|
|
114
|
+
anim.cancel()
|
|
115
|
+
finish(side)
|
|
116
|
+
}
|
|
117
|
+
anim.finished.then(end, end)
|
|
118
|
+
// Stopped (the deck went away mid-fade): the fade ends and nothing is decided.
|
|
119
|
+
stop = () => {
|
|
120
|
+
ended = true
|
|
121
|
+
anim.cancel()
|
|
122
|
+
}
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
const speed = side * Math.max(Math.abs(velocity), THROW_SPEED)
|
|
126
|
+
stop = release(at, offStage(side), speed, paint, () => finish(side))
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const back = (velocity = 0) => {
|
|
130
|
+
stop()
|
|
131
|
+
stop = release(at, 0, velocity, paint)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// A key pressed mid-move plays once, when the move has ended.
|
|
135
|
+
const playQueue = () => {
|
|
136
|
+
busy = false
|
|
137
|
+
const q = queued
|
|
138
|
+
queued = null
|
|
139
|
+
if (q !== null) fly(q, 0)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const letGo = (e: Press, cancelled: boolean) => {
|
|
143
|
+
const d = drag
|
|
144
|
+
if (!d || d.id !== e.pointerId) return
|
|
145
|
+
drag = null
|
|
146
|
+
if (!d.sideways) return
|
|
147
|
+
o.deck()?.removeAttribute('data-dragging')
|
|
148
|
+
if (cancelled) return back()
|
|
149
|
+
const x = at
|
|
150
|
+
const v = d.speed.speed(e.timeStamp)
|
|
151
|
+
if (Math.abs(x) >= line()) return fly(x > 0 ? 1 : -1, v)
|
|
152
|
+
if (Math.abs(v) > FLICK && Math.abs(x) >= FLICK_MIN && Math.sign(v) === Math.sign(x)) return fly(v > 0 ? 1 : -1, v)
|
|
153
|
+
back(v)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
named,
|
|
158
|
+
physical,
|
|
159
|
+
fly,
|
|
160
|
+
/** Reads the throw line from --gooee-deck-throw. Call once the deck is in the page. */
|
|
161
|
+
mount() {
|
|
162
|
+
const root = o.root()
|
|
163
|
+
share = (root ? parseFloat(getComputedStyle(root).getPropertyValue('--gooee-deck-throw')) || 0 : 0) || share
|
|
164
|
+
},
|
|
165
|
+
destroy: () => stop(),
|
|
166
|
+
/** Asks to bring the last card back from `side`. False while a card is moving: then nothing changes. */
|
|
167
|
+
undo(side: DeckSide) {
|
|
168
|
+
if (busy) return false
|
|
169
|
+
busy = true
|
|
170
|
+
queued = null
|
|
171
|
+
undoing = side
|
|
172
|
+
return true
|
|
173
|
+
},
|
|
174
|
+
/** Call after the part has drawn a new count of decisions. Undo plays the throw backwards: the card comes in from where it left. */
|
|
175
|
+
changed() {
|
|
176
|
+
const side = undoing
|
|
177
|
+
const el = o.top()
|
|
178
|
+
if (side === null) return playQueue()
|
|
179
|
+
undoing = null
|
|
180
|
+
if (!el) return playQueue()
|
|
181
|
+
if (reduced()) {
|
|
182
|
+
const anim = el.animate({ opacity: [0, 1] }, { duration: readMotionMs('base'), easing: easing('out') })
|
|
183
|
+
let ended = false
|
|
184
|
+
anim.finished.then(
|
|
185
|
+
() => !ended && playQueue(),
|
|
186
|
+
() => !ended && playQueue(),
|
|
187
|
+
)
|
|
188
|
+
stop = () => {
|
|
189
|
+
ended = true
|
|
190
|
+
anim.cancel()
|
|
191
|
+
}
|
|
192
|
+
return
|
|
193
|
+
}
|
|
194
|
+
paint(offStage(side))
|
|
195
|
+
stop = release(offStage(side), 0, -side * THROW_SPEED, paint, playQueue)
|
|
196
|
+
},
|
|
197
|
+
down(e: Press) {
|
|
198
|
+
if ((e.pointerType === 'mouse' && e.button !== 0) || busy) return
|
|
199
|
+
moved = false
|
|
200
|
+
drag = { x: e.clientX, y: e.clientY, base: at, id: e.pointerId, sideways: false, speed: speedTracker() }
|
|
201
|
+
},
|
|
202
|
+
move(e: Press) {
|
|
203
|
+
const d = drag
|
|
204
|
+
if (!d || d.id !== e.pointerId) return
|
|
205
|
+
const dx = e.clientX - d.x
|
|
206
|
+
const dy = e.clientY - d.y
|
|
207
|
+
if (!d.sideways) {
|
|
208
|
+
// Up or down first means the page scrolls, and the card lets go.
|
|
209
|
+
if (Math.abs(dy) > SLOP && Math.abs(dy) > Math.abs(dx)) return void (drag = null)
|
|
210
|
+
if (Math.abs(dx) < SLOP) return
|
|
211
|
+
d.sideways = true
|
|
212
|
+
moved = true
|
|
213
|
+
stop()
|
|
214
|
+
d.base = at - dx
|
|
215
|
+
try {
|
|
216
|
+
;(e.currentTarget as Element).setPointerCapture(e.pointerId)
|
|
217
|
+
} catch {
|
|
218
|
+
// A pointer the browser no longer knows cannot be captured, and needs no capture.
|
|
219
|
+
}
|
|
220
|
+
o.deck()?.setAttribute('data-dragging', '')
|
|
221
|
+
}
|
|
222
|
+
const raw = d.base + dx
|
|
223
|
+
d.speed.add(raw, e.timeStamp)
|
|
224
|
+
paint(raw)
|
|
225
|
+
},
|
|
226
|
+
up: (e: Press) => letGo(e, false),
|
|
227
|
+
cancel: (e: Press) => letGo(e, true),
|
|
228
|
+
// A drag is not a tap on the card's own buttons.
|
|
229
|
+
clickCapture(e: { preventDefault: () => void; stopPropagation: () => void }) {
|
|
230
|
+
if (!moved) return
|
|
231
|
+
moved = false
|
|
232
|
+
e.preventDefault()
|
|
233
|
+
e.stopPropagation()
|
|
234
|
+
},
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { frames, morphEase, motionFor } from './liquidMatter'
|
|
2
|
+
|
|
3
|
+
// The front edge moves over this share of the time; the back edge starts the rest of the way in.
|
|
4
|
+
const LEAD = 0.75
|
|
5
|
+
// The longest the thumb gets, as a share of its round size.
|
|
6
|
+
const STRETCH = 1.4
|
|
7
|
+
|
|
8
|
+
type El = HTMLElement | (() => HTMLElement | null)
|
|
9
|
+
const get = (t: El) => (typeof t === 'function' ? t() : t)
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A switch's thumb that stretches: its front edge leaves first and the back edge catches up, so it stays
|
|
13
|
+
* one shape. The on fill's edge (--fill on the track, the thumb's parent) rides on the thumb's edge, so
|
|
14
|
+
* the colour never sits apart from it. Call `set` with the new state each time it may have changed.
|
|
15
|
+
*/
|
|
16
|
+
export function createSwitchThumb(target: El, checked: boolean) {
|
|
17
|
+
let was = checked
|
|
18
|
+
let run: { a: number; b: number; stop: () => void } | null = null
|
|
19
|
+
const end = () => {
|
|
20
|
+
const thumb = get(target)
|
|
21
|
+
run?.stop()
|
|
22
|
+
run = null
|
|
23
|
+
if (!thumb) return
|
|
24
|
+
thumb.style.removeProperty('transform')
|
|
25
|
+
thumb.style.removeProperty('width')
|
|
26
|
+
const track = thumb.parentElement
|
|
27
|
+
if (track) {
|
|
28
|
+
delete track.dataset.moving
|
|
29
|
+
track.style.removeProperty('--fill')
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const set = (checked: boolean) => {
|
|
33
|
+
const thumb = get(target)
|
|
34
|
+
const track = thumb?.parentElement
|
|
35
|
+
if (was === checked || !thumb || !track) return
|
|
36
|
+
was = checked
|
|
37
|
+
const ms = motionFor('slow')
|
|
38
|
+
if (!ms) {
|
|
39
|
+
end()
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
const s = getComputedStyle(track)
|
|
43
|
+
const line = parseFloat(s.borderLeftWidth) || 0
|
|
44
|
+
const pad = parseFloat(s.getPropertyValue('--pad')) || 0
|
|
45
|
+
const size = thumb.offsetHeight
|
|
46
|
+
const rest = thumb.offsetLeft
|
|
47
|
+
const offX = rest
|
|
48
|
+
const onX = track.clientWidth - rest - size
|
|
49
|
+
const to = checked ? onX : offX
|
|
50
|
+
const from = run ? { a: run.a, b: run.b } : { a: checked ? offX : onX, b: (checked ? offX : onX) + size }
|
|
51
|
+
run?.stop()
|
|
52
|
+
const ahead = to + size / 2 >= (from.a + from.b) / 2
|
|
53
|
+
const e = morphEase()
|
|
54
|
+
const outer = track.offsetWidth
|
|
55
|
+
const live = { ...from, stop: () => {} }
|
|
56
|
+
run = live
|
|
57
|
+
track.dataset.moving = ''
|
|
58
|
+
live.stop = frames(
|
|
59
|
+
ms,
|
|
60
|
+
(u) => {
|
|
61
|
+
const lead = e(Math.min(1, u / LEAD))
|
|
62
|
+
const trail = e(Math.max(0, (u - 1 + LEAD) / LEAD))
|
|
63
|
+
live.a = from.a + (to - from.a) * (ahead ? trail : lead)
|
|
64
|
+
live.b = from.b + (to + size - from.b) * (ahead ? lead : trail)
|
|
65
|
+
// A short pill at most, so the fill behind it stays in view.
|
|
66
|
+
const long = live.b - live.a - size * STRETCH
|
|
67
|
+
if (long > 0) {
|
|
68
|
+
if (ahead) live.a += long
|
|
69
|
+
else live.b -= long
|
|
70
|
+
}
|
|
71
|
+
thumb.style.transform = `translateX(${live.a - rest}px)`
|
|
72
|
+
thumb.style.width = `${live.b - live.a}px`
|
|
73
|
+
// How far on the thumb is, 0 to 1: the fill opens out to the track's edge as it goes.
|
|
74
|
+
const p = Math.min(1, Math.max(0, ((live.a + live.b) / 2 - offX - size / 2) / (onX - offX)))
|
|
75
|
+
const side = (pad + line) * (1 - p)
|
|
76
|
+
const right = outer - live.b - line + line * (1 - p) - pad * p
|
|
77
|
+
track.style.setProperty('--fill', `inset(${side}px ${Math.max(0, right)}px ${side}px ${side}px round var(--gooee-radius-full))`)
|
|
78
|
+
},
|
|
79
|
+
end,
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
return { set, destroy: end }
|
|
83
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { easing, motionMs } from '../motion'
|
|
2
|
+
|
|
3
|
+
// Table's sort, groups, scroll edges and row glide, with no framework.
|
|
4
|
+
|
|
5
|
+
export type TableSort = { key: string; direction: 'ascending' | 'descending' }
|
|
6
|
+
|
|
7
|
+
type Sortable<T> = { key: string; sortValue?: (row: T) => string | number }
|
|
8
|
+
|
|
9
|
+
export const readCell = (row: unknown, key: string) => (row as Record<string, unknown>)[key]
|
|
10
|
+
|
|
11
|
+
/** The rows in the sort's order, or as given when unsorted. */
|
|
12
|
+
export function sortRows<T>(rows: T[], columns: Sortable<T>[], sort: TableSort | null): T[] {
|
|
13
|
+
if (!sort) return rows
|
|
14
|
+
const col = columns.find((c) => c.key === sort.key)
|
|
15
|
+
if (!col) return rows
|
|
16
|
+
const value = (r: T) => (col.sortValue ? col.sortValue(r) : (readCell(r, col.key) as string | number))
|
|
17
|
+
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' })
|
|
18
|
+
return [...rows].sort((a, b) => {
|
|
19
|
+
const va = value(a)
|
|
20
|
+
const vb = value(b)
|
|
21
|
+
const c = typeof va === 'number' && typeof vb === 'number' ? va - vb : collator.compare(String(va ?? ''), String(vb ?? ''))
|
|
22
|
+
return sort.direction === 'ascending' ? c : -c
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Groups take the order they first appear in, and keep it: a sort, or a row moving to another group,
|
|
28
|
+
* never shuffles the groups themselves. A group seen for the first time joins `order` at the end.
|
|
29
|
+
*/
|
|
30
|
+
export function groupRows<T>(rows: T[], sorted: T[], groupBy: (row: T) => string, order: string[]) {
|
|
31
|
+
for (const key of new Set(rows.map(groupBy))) if (!order.includes(key)) order.push(key)
|
|
32
|
+
return order.map((key) => ({ key, rows: sorted.filter((r) => groupBy(r) === key) })).filter((g) => g.rows.length)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The next sort after a press on a header: ascending, descending, then off. */
|
|
36
|
+
export const nextSort = (sort: TableSort | null, key: string): TableSort | null =>
|
|
37
|
+
sort?.key !== key ? { key, direction: 'ascending' } : sort.direction === 'ascending' ? { key, direction: 'descending' } : null
|
|
38
|
+
|
|
39
|
+
/** Which edges have more to scroll to, so a fade shows the table goes on. */
|
|
40
|
+
export function scrollEdges(el: HTMLElement) {
|
|
41
|
+
// The sticky first column's width, so a cell that takes focus scrolls clear of it (WCAG 2.4.11).
|
|
42
|
+
const first = el.querySelector<HTMLElement>('thead th')
|
|
43
|
+
el.style.setProperty('--first-w', first && getComputedStyle(first).left === '0px' ? `${first.offsetWidth}px` : '0px')
|
|
44
|
+
return { start: el.scrollLeft > 1, end: el.scrollLeft + el.clientWidth < el.scrollWidth - 1 }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Browsers do not scroll a sticky header cell into view on focus, so its button could stay under the
|
|
49
|
+
* sticky first column. Nudges the sideways scroll until the focused cell is clear on both sides.
|
|
50
|
+
*/
|
|
51
|
+
export function revealCell(el: HTMLElement, target: EventTarget | null) {
|
|
52
|
+
const cell = (target as Element | null)?.closest?.('th, td')
|
|
53
|
+
if (!cell || cell === cell.parentElement?.firstElementChild) return
|
|
54
|
+
const box = el.getBoundingClientRect()
|
|
55
|
+
const r = cell.getBoundingClientRect()
|
|
56
|
+
const under = r.left - box.left - (parseFloat(el.style.getPropertyValue('--first-w')) || 0)
|
|
57
|
+
if (under < 0) el.scrollLeft += under
|
|
58
|
+
else if (r.right > box.right) el.scrollLeft += r.right - box.right
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* FLIP: rows glide from their last place to the new one. When the groups themselves changed (`regrouped`),
|
|
63
|
+
* they snap there with the spring, so the table visibly splits apart and closes up again. Returns the new places.
|
|
64
|
+
*/
|
|
65
|
+
export function flipRows(table: HTMLElement, last: Map<string, number>, regrouped: boolean) {
|
|
66
|
+
const duration = motionMs('slow')
|
|
67
|
+
const ease = easing(regrouped ? 'spring' : 'out')
|
|
68
|
+
const now = new Map<string, number>()
|
|
69
|
+
table.querySelectorAll<HTMLElement>('tbody [data-row-id]').forEach((tr) => {
|
|
70
|
+
const rid = tr.dataset.rowId!
|
|
71
|
+
const top = tr.offsetTop
|
|
72
|
+
now.set(rid, top)
|
|
73
|
+
const before = last.get(rid)
|
|
74
|
+
if (before !== undefined && before !== top && duration > 0) tr.animate([{ transform: `translateY(${before - top}px)` }, { transform: 'none' }], { duration, easing: ease })
|
|
75
|
+
})
|
|
76
|
+
return now
|
|
77
|
+
}
|