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,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
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { bezier } from '../feel'
|
|
2
|
+
import { morphCurve, motionMs, prefersReducedMotion } from '../motion'
|
|
3
|
+
|
|
4
|
+
// The parts of Tabs that are the same in every framework: where the chosen tab is, the tab stop, the
|
|
5
|
+
// arrow keys and the rise that stretches over to a new choice.
|
|
6
|
+
|
|
7
|
+
export type TabBar = { x: number; w: number; room: number; value: string }
|
|
8
|
+
|
|
9
|
+
/** The chosen tab's place in the row, or null when no tab is chosen. */
|
|
10
|
+
export function measureTab(list: HTMLElement, value: string): TabBar | null {
|
|
11
|
+
const el = list.querySelector<HTMLElement>('[role="tab"][aria-selected="true"]')
|
|
12
|
+
return el ? { x: el.offsetLeft, w: el.offsetWidth, room: list.offsetWidth, value } : null
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A Tab only knows itself, so the list picks the tab stop when the value matches no enabled tab. */
|
|
16
|
+
export function setTabStop(list: HTMLElement | null) {
|
|
17
|
+
const tabs = Array.from(list?.querySelectorAll<HTMLButtonElement>('[role="tab"]') ?? [])
|
|
18
|
+
const enabled = tabs.filter((t) => !t.disabled)
|
|
19
|
+
const stop = enabled.find((t) => t.getAttribute('aria-selected') === 'true') ?? enabled[0]
|
|
20
|
+
for (const t of tabs) t.tabIndex = t === stop ? 0 : -1
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Automatic activation, per the WAI-ARIA tabs pattern. */
|
|
24
|
+
export function tabKey(list: HTMLElement | null, e: { key: string; preventDefault(): void }) {
|
|
25
|
+
const tabs = Array.from(list?.querySelectorAll<HTMLButtonElement>('[role="tab"]:not(:disabled)') ?? [])
|
|
26
|
+
const i = tabs.indexOf(document.activeElement as HTMLButtonElement)
|
|
27
|
+
if (i < 0) return
|
|
28
|
+
let next = -1
|
|
29
|
+
if (e.key === 'ArrowRight') next = (i + 1) % tabs.length
|
|
30
|
+
else if (e.key === 'ArrowLeft') next = (i - 1 + tabs.length) % tabs.length
|
|
31
|
+
else if (e.key === 'Home') next = 0
|
|
32
|
+
else if (e.key === 'End') next = tabs.length - 1
|
|
33
|
+
if (next < 0) return
|
|
34
|
+
e.preventDefault()
|
|
35
|
+
tabs[next].focus()
|
|
36
|
+
tabs[next].click()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Brings a picked tab into view in a row that scrolls sideways. */
|
|
40
|
+
export function revealTab(id: string) {
|
|
41
|
+
document.getElementById(id)?.scrollIntoView({ block: 'nearest', inline: 'nearest', behavior: prefersReducedMotion() ? 'auto' : 'smooth' })
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The rise moves by script, so each frame knows how far each edge is from the row's ends. Near an end the
|
|
46
|
+
* panel's corner and the rise's inside corner shrink together; at the end the rise and the panel share
|
|
47
|
+
* one straight edge. Call `place` with each new measure.
|
|
48
|
+
*/
|
|
49
|
+
export function createTabsRise() {
|
|
50
|
+
let drawn: { l: number; r: number; value: string } | null = null
|
|
51
|
+
let raf = 0
|
|
52
|
+
// Where a running move is heading. A re-measure of the same tab (the size watcher reports once more
|
|
53
|
+
// after every change) only updates this; it never restarts or cuts the move short.
|
|
54
|
+
let aim: { l: number; r: number; value: string } | null = null
|
|
55
|
+
|
|
56
|
+
const place = (rise: HTMLElement | null, bar: TabBar | null) => {
|
|
57
|
+
const root = rise?.closest<HTMLElement>('[data-gooee-tabs]')
|
|
58
|
+
if (!bar || !rise || !root) return
|
|
59
|
+
const R = parseFloat(getComputedStyle(root).getPropertyValue('--tabs-r')) || 0
|
|
60
|
+
// Within two corners of an end, the panel's corner and the inside corner each take half the room, so
|
|
61
|
+
// they meet as one even S; at the end both are gone.
|
|
62
|
+
const curve = (gap: number) => `${Math.max(0, Math.min(R, gap / 2))}px`
|
|
63
|
+
const draw = (l: number, r: number) => {
|
|
64
|
+
rise.style.left = `${l}px`
|
|
65
|
+
rise.style.right = `${r}px`
|
|
66
|
+
rise.style.setProperty('--tabs-in-start', curve(l))
|
|
67
|
+
rise.style.setProperty('--tabs-in-end', curve(r))
|
|
68
|
+
root.style.setProperty('--tabs-corner-start', curve(l))
|
|
69
|
+
root.style.setProperty('--tabs-corner-end', curve(r))
|
|
70
|
+
drawn = { l, r, value: aim?.value ?? bar.value }
|
|
71
|
+
}
|
|
72
|
+
const to = { l: bar.x, r: bar.room - bar.x - bar.w }
|
|
73
|
+
if (aim?.value === bar.value) {
|
|
74
|
+
aim = { ...to, value: bar.value }
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
const from = drawn
|
|
78
|
+
cancelAnimationFrame(raf)
|
|
79
|
+
aim = null
|
|
80
|
+
const length = motionMs('morph-out')
|
|
81
|
+
// Only a new choice moves; a resize or the first paint just puts it in place.
|
|
82
|
+
if (!from || from.value === bar.value || !length || prefersReducedMotion()) return draw(to.l, to.r)
|
|
83
|
+
// The edge on the way leads; the other follows a beat later, so the rise stretches, then settles.
|
|
84
|
+
const lag = motionMs('instant')
|
|
85
|
+
const ease = bezier(morphCurve())
|
|
86
|
+
const toEnd = to.l > from.l
|
|
87
|
+
const t0 = performance.now()
|
|
88
|
+
aim = { ...to, value: bar.value }
|
|
89
|
+
const step = (now: number) => {
|
|
90
|
+
const at = aim
|
|
91
|
+
if (!at) return
|
|
92
|
+
const t = now - t0
|
|
93
|
+
const lead = ease(Math.min(1, t / length))
|
|
94
|
+
const trail = ease(Math.min(1, Math.max(0, (t - lag) / length)))
|
|
95
|
+
const ul = toEnd ? trail : lead
|
|
96
|
+
const ur = toEnd ? lead : trail
|
|
97
|
+
draw(from.l + (at.l - from.l) * ul, from.r + (at.r - from.r) * ur)
|
|
98
|
+
if (t < length + lag) raf = requestAnimationFrame(step)
|
|
99
|
+
else aim = null
|
|
100
|
+
}
|
|
101
|
+
raf = requestAnimationFrame(step)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return { place, destroy: () => cancelAnimationFrame(raf) }
|
|
105
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Textarea: the field grows smoothly with its text between minRows and maxRows, and the live count follows.
|
|
2
|
+
export function growTextarea(el: HTMLTextAreaElement | null, count: HTMLElement | null, o: { autoGrow: boolean; minRows: number; maxRows: number; maxLength?: number }) {
|
|
3
|
+
if (!el) return
|
|
4
|
+
if (count) count.textContent = `${el.value.length} / ${o.maxLength}`
|
|
5
|
+
if (!o.autoGrow) return
|
|
6
|
+
const cs = getComputedStyle(el)
|
|
7
|
+
const line = parseFloat(cs.lineHeight) || 24
|
|
8
|
+
const pad = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom)
|
|
9
|
+
const border = parseFloat(cs.borderTopWidth) + parseFloat(cs.borderBottomWidth)
|
|
10
|
+
const prev = el.style.height
|
|
11
|
+
el.style.transition = 'none'
|
|
12
|
+
el.style.height = 'auto'
|
|
13
|
+
const min = line * o.minRows + pad + border
|
|
14
|
+
const max = line * o.maxRows + pad + border
|
|
15
|
+
const next = Math.min(Math.max(el.scrollHeight + border, min), max)
|
|
16
|
+
// Restore the old height and force layout, so the transition runs
|
|
17
|
+
el.style.height = prev || `${next}px`
|
|
18
|
+
void el.offsetHeight
|
|
19
|
+
el.style.transition = ''
|
|
20
|
+
el.style.height = `${next}px`
|
|
21
|
+
el.style.overflowY = el.scrollHeight + border > max ? 'auto' : 'hidden'
|
|
22
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { bezier } from '../feel'
|
|
2
|
+
import { morphCurve, motionMs, prefersReducedMotion } from '../motion'
|
|
3
|
+
|
|
4
|
+
// Sun to moon the way the real moon works: a round disc with a shadow passing over it. The disc stays
|
|
5
|
+
// round and grows from the sun's size to the moon's; a second circle, the bite, slides in from the top
|
|
6
|
+
// right and carves the crescent. The outline is drawn exactly each frame as "disc minus bite", so every
|
|
7
|
+
// frame is a true shape. The rays draw back into the disc and fade first, before it grows into them.
|
|
8
|
+
const C = 12
|
|
9
|
+
const lerp = (a: number, b: number, t: number) => a + (b - a) * t
|
|
10
|
+
const part = (t: number, a: number, b: number) => Math.min(1, Math.max(0, (t - a) / (b - a)))
|
|
11
|
+
const f = (n: number) => Math.round(n * 100) / 100
|
|
12
|
+
const BITE = 7
|
|
13
|
+
const TOWARD = -Math.PI / 4
|
|
14
|
+
export function bodyAt(t: number) {
|
|
15
|
+
const r = lerp(4, 8, part(t, 0.2, 1))
|
|
16
|
+
// The bite starts clear of the disc and ends 5.6 from its centre.
|
|
17
|
+
const d = lerp(r + BITE + 1, 5.6, part(t, 0.15, 1))
|
|
18
|
+
if (d >= r + BITE) return `M${C - r} ${C}A${f(r)} ${f(r)} 0 1 1 ${C + r} ${C}A${f(r)} ${f(r)} 0 1 1 ${C - r} ${C}Z`
|
|
19
|
+
const half = Math.acos((r * r + d * d - BITE * BITE) / (2 * r * d))
|
|
20
|
+
const at = (o: [number, number], rad: number, ang: number): [number, number] => [o[0] + rad * Math.cos(ang), o[1] + rad * Math.sin(ang)]
|
|
21
|
+
const centre: [number, number] = [C, C]
|
|
22
|
+
const bite = at(centre, d, TOWARD)
|
|
23
|
+
const A = at(centre, r, TOWARD + half)
|
|
24
|
+
const B = at(centre, r, TOWARD - half)
|
|
25
|
+
// The disc's edge from A the long way round to B, then the bite's edge back to A through the disc.
|
|
26
|
+
const outer = 2 * Math.PI - 2 * half
|
|
27
|
+
// Of the two ways round the bite from B to A, the one whose middle lies inside the disc.
|
|
28
|
+
const angA = Math.atan2(A[1] - bite[1], A[0] - bite[0])
|
|
29
|
+
const angB = Math.atan2(B[1] - bite[1], B[0] - bite[0])
|
|
30
|
+
const up = (((angA - angB) % (2 * Math.PI)) + 2 * Math.PI) % (2 * Math.PI)
|
|
31
|
+
const mid = at(bite, BITE, angB + up / 2)
|
|
32
|
+
const sweep = Math.hypot(mid[0] - C, mid[1] - C) < r ? 1 : 0
|
|
33
|
+
const inner = sweep ? up : 2 * Math.PI - up
|
|
34
|
+
return `M${f(A[0])} ${f(A[1])}A${f(r)} ${f(r)} 0 ${outer > Math.PI ? 1 : 0} 1 ${f(B[0])} ${f(B[1])}A${BITE} ${BITE} 0 ${inner > Math.PI ? 1 : 0} ${sweep} ${f(A[0])} ${f(A[1])}Z`
|
|
35
|
+
}
|
|
36
|
+
export const RAYS = Array.from({ length: 8 }, (_, i) => {
|
|
37
|
+
const a = (i * Math.PI) / 4
|
|
38
|
+
return `M${f(C + 7 * Math.cos(a))} ${f(C + 7 * Math.sin(a))}L${f(C + 9.5 * Math.cos(a))} ${f(C + 9.5 * Math.sin(a))}`
|
|
39
|
+
}).join('')
|
|
40
|
+
// The rays draw in toward the disc, turn a little and fade, all within the first 40%.
|
|
41
|
+
export const raysAt = (t: number) => {
|
|
42
|
+
const k = part(t, 0, 0.4)
|
|
43
|
+
return { transform: `rotate(${f(-30 * k)}deg) scale(${f(1 - 0.45 * k)})`, opacity: String(f(1 - k)) }
|
|
44
|
+
}
|
|
45
|
+
function draw(body: SVGPathElement, t: number) {
|
|
46
|
+
body.setAttribute('d', bodyAt(t))
|
|
47
|
+
const rays = body.nextElementSibling as SVGPathElement | null
|
|
48
|
+
if (rays) Object.assign(rays.style, raysAt(t))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The theme toggle's icon: 1 is the moon, 0 the sun. The body morphs on the morph curve, in the reveal's
|
|
53
|
+
* time, so the icon and the theme's circle end together; the rays are drawn on the same frames.
|
|
54
|
+
*/
|
|
55
|
+
export function createThemeIcon(body: () => SVGPathElement | null, start: number) {
|
|
56
|
+
let shown = start
|
|
57
|
+
let aim = start
|
|
58
|
+
let raf = 0
|
|
59
|
+
const morphTo = (to: number) => {
|
|
60
|
+
const el = body()
|
|
61
|
+
aim = to
|
|
62
|
+
cancelAnimationFrame(raf)
|
|
63
|
+
if (!el) return
|
|
64
|
+
const from = shown
|
|
65
|
+
const length = motionMs('reveal')
|
|
66
|
+
if (from === to || !length || prefersReducedMotion()) {
|
|
67
|
+
shown = to
|
|
68
|
+
draw(el, to)
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
const ease = bezier(morphCurve())
|
|
72
|
+
const t0 = performance.now()
|
|
73
|
+
const step = (now: number) => {
|
|
74
|
+
const u = Math.min(1, (now - t0) / length)
|
|
75
|
+
shown = from + (to - from) * ease(u)
|
|
76
|
+
draw(el, shown)
|
|
77
|
+
if (u < 1) raf = requestAnimationFrame(step)
|
|
78
|
+
}
|
|
79
|
+
raf = requestAnimationFrame(step)
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
get shown() {
|
|
83
|
+
return shown
|
|
84
|
+
},
|
|
85
|
+
get aim() {
|
|
86
|
+
return aim
|
|
87
|
+
},
|
|
88
|
+
morphTo,
|
|
89
|
+
destroy: () => cancelAnimationFrame(raf),
|
|
90
|
+
}
|
|
91
|
+
}
|