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.
Files changed (172) hide show
  1. package/README.md +17 -4
  2. package/package.json +5 -3
  3. package/scripts/add.js +28 -6
  4. package/src/components/Accordion/Accordion.vue +62 -0
  5. package/src/components/Accordion/AccordionItem.vue +45 -0
  6. package/src/components/Accordion/accordionContext.ts +6 -0
  7. package/src/components/Alert/Alert.tsx +7 -27
  8. package/src/components/Alert/Alert.vue +59 -0
  9. package/src/components/AlertDialog/AlertDialog.vue +126 -0
  10. package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
  11. package/src/components/AspectRatio/AspectRatio.vue +17 -0
  12. package/src/components/Avatar/Avatar.vue +56 -0
  13. package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
  14. package/src/components/Badge/Badge.vue +32 -0
  15. package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
  16. package/src/components/Button/Button.vue +130 -0
  17. package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
  18. package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
  19. package/src/components/Card/Card.vue +34 -0
  20. package/src/components/Center/Center.vue +32 -0
  21. package/src/components/Checkbox/Checkbox.tsx +8 -25
  22. package/src/components/Checkbox/Checkbox.vue +108 -0
  23. package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
  24. package/src/components/Cluster/Cluster.vue +34 -0
  25. package/src/components/CodeBlock/CodeBlock.vue +45 -0
  26. package/src/components/Collapsible/Collapsible.vue +59 -0
  27. package/src/components/Container/Container.vue +22 -0
  28. package/src/components/CopyButton/CopyButton.tsx +3 -49
  29. package/src/components/CopyButton/CopyButton.vue +116 -0
  30. package/src/components/Dial/Dial.tsx +12 -120
  31. package/src/components/Dial/Dial.vue +228 -0
  32. package/src/components/Dialog/Dialog.vue +139 -0
  33. package/src/components/Divider/Divider.vue +27 -0
  34. package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
  35. package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
  36. package/src/components/EmptyState/EmptyState.vue +33 -0
  37. package/src/components/Fab/Fab.tsx +4 -14
  38. package/src/components/Fab/Fab.vue +115 -0
  39. package/src/components/Fieldset/Fieldset.vue +42 -0
  40. package/src/components/FormField/FormField.tsx +2 -17
  41. package/src/components/FormField/FormField.vue +64 -0
  42. package/src/components/Grid/Grid.vue +43 -0
  43. package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -143
  44. package/src/components/HoldToConfirm/HoldToConfirm.vue +281 -0
  45. package/src/components/IconButton/IconButton.vue +25 -0
  46. package/src/components/Input/Input.vue +112 -0
  47. package/src/components/InputGroup/InputGroup.vue +102 -0
  48. package/src/components/Kbd/Kbd.tsx +2 -12
  49. package/src/components/Kbd/Kbd.vue +21 -0
  50. package/src/components/Link/Link.vue +38 -0
  51. package/src/components/LivePill/LivePill.tsx +11 -39
  52. package/src/components/LivePill/LivePill.vue +152 -0
  53. package/src/components/Meter/Meter.vue +88 -0
  54. package/src/components/MorphButton/MorphButton.tsx +11 -67
  55. package/src/components/MorphButton/MorphButton.vue +120 -0
  56. package/src/components/NumberInput/NumberInput.tsx +11 -48
  57. package/src/components/NumberInput/NumberInput.vue +184 -0
  58. package/src/components/Pagination/Pagination.tsx +2 -15
  59. package/src/components/Pagination/Pagination.vue +87 -0
  60. package/src/components/Pagination/range.ts +15 -0
  61. package/src/components/PasswordInput/PasswordInput.vue +53 -0
  62. package/src/components/PickerWheel/DateWheel.vue +48 -0
  63. package/src/components/PickerWheel/PickerWheel.tsx +15 -129
  64. package/src/components/PickerWheel/PickerWheel.vue +77 -0
  65. package/src/components/PickerWheel/TimeWheel.vue +50 -0
  66. package/src/components/PickerWheel/WheelColumn.vue +70 -0
  67. package/src/components/Popover/Popover.vue +108 -0
  68. package/src/components/ProgressBar/ProgressBar.vue +48 -0
  69. package/src/components/Prose/Prose.vue +18 -0
  70. package/src/components/Radio/Radio.vue +49 -0
  71. package/src/components/Radio/RadioGroup.vue +69 -0
  72. package/src/components/Retime/Retime.tsx +7 -40
  73. package/src/components/Retime/Retime.vue +64 -0
  74. package/src/components/RulerScrubber/RulerScrubber.tsx +9 -38
  75. package/src/components/RulerScrubber/RulerScrubber.vue +166 -0
  76. package/src/components/ScrollArea/ScrollArea.tsx +17 -129
  77. package/src/components/ScrollArea/ScrollArea.vue +58 -0
  78. package/src/components/SearchInput/SearchInput.tsx +2 -15
  79. package/src/components/SearchInput/SearchInput.vue +70 -0
  80. package/src/components/SegmentedControl/SegmentedControl.tsx +7 -51
  81. package/src/components/SegmentedControl/SegmentedControl.vue +111 -0
  82. package/src/components/Select/Select.tsx +16 -47
  83. package/src/components/Select/Select.vue +236 -0
  84. package/src/components/Sheet/Sheet.tsx +52 -231
  85. package/src/components/Sheet/Sheet.vue +249 -0
  86. package/src/components/Skeleton/Skeleton.vue +38 -0
  87. package/src/components/Slider/Slider.tsx +25 -84
  88. package/src/components/Slider/Slider.vue +113 -0
  89. package/src/components/Spinner/Spinner.vue +30 -0
  90. package/src/components/Split/Split.vue +34 -0
  91. package/src/components/SplitFlap/SplitFlap.tsx +11 -175
  92. package/src/components/SplitFlap/SplitFlap.vue +52 -0
  93. package/src/components/Stack/Stack.vue +22 -0
  94. package/src/components/SwipeDeck/SwipeDeck.tsx +30 -194
  95. package/src/components/SwipeDeck/SwipeDeck.vue +168 -0
  96. package/src/components/Switch/Switch.tsx +6 -75
  97. package/src/components/Switch/Switch.vue +66 -0
  98. package/src/components/Table/Table.module.css +12 -3
  99. package/src/components/Table/Table.tsx +8 -59
  100. package/src/components/Table/Table.vue +274 -0
  101. package/src/components/TableOfContents/TableOfContents.vue +90 -0
  102. package/src/components/Tabs/Tab.vue +37 -0
  103. package/src/components/Tabs/TabList.vue +47 -0
  104. package/src/components/Tabs/TabPanel.vue +26 -0
  105. package/src/components/Tabs/Tabs.module.css +2 -2
  106. package/src/components/Tabs/Tabs.tsx +11 -84
  107. package/src/components/Tabs/Tabs.vue +33 -0
  108. package/src/components/Tabs/tabsContext.ts +15 -0
  109. package/src/components/Tag/Tag.tsx +6 -26
  110. package/src/components/Tag/Tag.vue +63 -0
  111. package/src/components/Textarea/Textarea.tsx +2 -22
  112. package/src/components/Textarea/Textarea.vue +120 -0
  113. package/src/components/ThemeToggle/ThemeToggle.tsx +10 -84
  114. package/src/components/ThemeToggle/ThemeToggle.vue +90 -0
  115. package/src/components/Toast/Toast.tsx +34 -264
  116. package/src/components/Toast/Toast.vue +138 -0
  117. package/src/components/Toast/ToastItem.vue +108 -0
  118. package/src/components/Toggle/Toggle.vue +52 -0
  119. package/src/components/Tooltip/Tooltip.tsx +17 -48
  120. package/src/components/Tooltip/Tooltip.vue +95 -0
  121. package/src/components/VisuallyHidden/VisuallyHidden.vue +17 -0
  122. package/src/internal/FieldMessage.vue +41 -0
  123. package/src/internal/Roll.vue +20 -0
  124. package/src/internal/focus.ts +4 -47
  125. package/src/internal/icons.module.css +39 -27
  126. package/src/internal/layers.ts +7 -61
  127. package/src/internal/motion/alert.ts +17 -0
  128. package/src/internal/motion/buttonGroup.ts +21 -0
  129. package/src/internal/motion/checkbox.ts +39 -0
  130. package/src/internal/motion/collapseOut.ts +26 -0
  131. package/src/internal/motion/copyButton.ts +48 -0
  132. package/src/internal/motion/dial.ts +137 -0
  133. package/src/internal/motion/fab.ts +14 -0
  134. package/src/internal/motion/floating.ts +58 -0
  135. package/src/internal/motion/focus.ts +53 -0
  136. package/src/internal/motion/formField.ts +19 -0
  137. package/src/internal/motion/holdToConfirm.ts +147 -0
  138. package/src/internal/motion/keys.ts +42 -0
  139. package/src/internal/motion/layers.ts +78 -0
  140. package/src/internal/motion/listbox.ts +69 -0
  141. package/src/internal/motion/livePill.ts +71 -0
  142. package/src/internal/motion/menuKeys.ts +58 -0
  143. package/src/internal/motion/morphButton.ts +71 -0
  144. package/src/internal/motion/numberInput.ts +57 -0
  145. package/src/internal/motion/pickerWheel.ts +166 -0
  146. package/src/internal/motion/retime.ts +45 -0
  147. package/src/internal/motion/ruler.ts +57 -0
  148. package/src/internal/motion/scrollArea.ts +133 -0
  149. package/src/internal/motion/scrubLook.ts +15 -0
  150. package/src/internal/motion/segmented.ts +63 -0
  151. package/src/internal/motion/sheetDetents.ts +271 -0
  152. package/src/internal/motion/slider.ts +108 -0
  153. package/src/internal/motion/splitFlap.ts +205 -0
  154. package/src/internal/motion/swipeDeck.ts +236 -0
  155. package/src/internal/motion/switchThumb.ts +83 -0
  156. package/src/internal/motion/table.ts +77 -0
  157. package/src/internal/motion/tabs.ts +105 -0
  158. package/src/internal/motion/textarea.ts +22 -0
  159. package/src/internal/motion/themeIcon.ts +91 -0
  160. package/src/internal/motion/themeReveal.ts +71 -0
  161. package/src/internal/motion/toast.ts +291 -0
  162. package/src/internal/motion/tooltip.ts +58 -0
  163. package/src/internal/themeReveal.ts +3 -61
  164. package/src/internal/useFloating.ts +9 -49
  165. package/src/internal/useScrub.ts +1 -13
  166. package/src/internal/vue/trigger.ts +18 -0
  167. package/src/internal/vue/useFloating.ts +38 -0
  168. package/src/internal/vue/usePresence.ts +21 -0
  169. package/src/internal/vue/useScrub.ts +40 -0
  170. package/src/internal/vueIcons.ts +134 -0
  171. package/src/tokens.css +5 -0
  172. package/src/internal/relativeTime.ts +0 -26
@@ -0,0 +1,69 @@
1
+ export type ListItem = { label: string; disabled?: boolean }
2
+
3
+ /**
4
+ * What a key does in a select-only combobox: `open` opens the list with `active` as the active option,
5
+ * `active` alone moves it, `choose` picks the active option, `tab` keeps it and closes.
6
+ */
7
+ export type ListKey = { prevent?: boolean; open?: boolean; active?: number; choose?: boolean; tab?: boolean }
8
+
9
+ type Key = { key: string; ctrlKey: boolean; metaKey: boolean; altKey: boolean }
10
+
11
+ /** The keys of a select-only combobox: arrows, Home, End, Enter, Space, Tab and type-ahead. Disabled options are skipped. */
12
+ export function createListbox(items: () => ListItem[]) {
13
+ const typed = { text: '', at: 0 }
14
+ const enabled = (i: number) => {
15
+ const o = items()
16
+ return i >= 0 && i < o.length && !o[i].disabled
17
+ }
18
+ const step = (from: number, dir: 1 | -1) => {
19
+ const n = items().length
20
+ let i = from
21
+ for (let k = 0; k < n; k++) {
22
+ i = (i + dir + n) % n
23
+ if (enabled(i)) return i
24
+ }
25
+ return from
26
+ }
27
+ const first = () => step(-1, 1)
28
+ const last = () => step(items().length, -1)
29
+
30
+ // Letters typed within 600ms of each other search together; one letter again moves to the next match.
31
+ const typeahead = (ch: string, active: number) => {
32
+ const o = items()
33
+ const now = Date.now()
34
+ typed.text = now - typed.at > 600 ? ch.toLowerCase() : typed.text + ch.toLowerCase()
35
+ typed.at = now
36
+ const start = Math.max(active, 0)
37
+ for (let n = 0; n < o.length; n++) {
38
+ const i = (start + n + (typed.text.length === 1 ? 1 : 0)) % o.length
39
+ if (enabled(i) && o[i].label.toLowerCase().startsWith(typed.text)) return i
40
+ }
41
+ return -1
42
+ }
43
+
44
+ const key = (e: Key, open: boolean, active: number, selected: number): ListKey | null => {
45
+ const k = e.key
46
+ if (!open) {
47
+ if (k === 'ArrowDown' || k === 'Enter' || k === ' ') return { prevent: true, open: true, active: enabled(selected) ? selected : first() }
48
+ if (k === 'ArrowUp') return { prevent: true, open: true, active: enabled(selected) ? selected : last() }
49
+ if (k.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
50
+ const found = typeahead(k, active)
51
+ return { open: true, active: found >= 0 ? found : enabled(selected) ? selected : first() }
52
+ }
53
+ return null
54
+ }
55
+ if (k === 'ArrowDown') return { prevent: true, active: step(active, 1) }
56
+ if (k === 'ArrowUp') return { prevent: true, active: step(active, -1) }
57
+ if (k === 'Home') return { prevent: true, active: first() }
58
+ if (k === 'End') return { prevent: true, active: last() }
59
+ if (k === 'Enter' || k === ' ') return { prevent: true, choose: true }
60
+ if (k === 'Tab') return { tab: true }
61
+ if (k.length === 1) {
62
+ const found = typeahead(k, active)
63
+ return found >= 0 ? { active: found } : null
64
+ }
65
+ return null
66
+ }
67
+
68
+ return { enabled, first, last, key }
69
+ }
@@ -0,0 +1,71 @@
1
+ import { motionMs } from '../motion'
2
+
3
+ /** The pill's shut shape: the card cut back to the button's box, the ring's offset to its slot, the row's height. */
4
+ export type PillShape = { shut: string; ringAt?: [number, number]; rowH: number }
5
+
6
+ /**
7
+ * Measures the shut shape now and on every resize, while the pill is shut. Open, the pill's button shrinks
8
+ * to the ring, so the shut shape is read only while it is shut. Returns a stop function.
9
+ */
10
+ export function measurePill(
11
+ shell: HTMLElement,
12
+ button: HTMLElement,
13
+ parts: { slot: () => HTMLElement | null; lead: () => HTMLElement | null; isOpen: () => boolean },
14
+ onShape: (shape: PillShape) => void,
15
+ ) {
16
+ const measure = () => {
17
+ if (parts.isOpen()) return
18
+ // Real fractions, not whole pixels: a half-pixel miss shows as an outline thin on one side and thick on the other.
19
+ const s = shell.getBoundingClientRect()
20
+ const r = button.getBoundingClientRect()
21
+ // A page that scales its content (a zoom, an entrance) scales these boxes too; k turns them back into CSS pixels.
22
+ const k = s.width / shell.offsetWidth || 1
23
+ const shut = `inset(${(r.top - s.top) / k}px ${(s.right - r.right) / k}px ${(s.bottom - r.bottom) / k}px ${(r.left - s.left) / k}px round ${r.height / k / 2}px)`
24
+ // Shut, the card's ring sits on the pill's ring slot: the distance between the two, read from layout.
25
+ const sl = parts.slot()?.getBoundingClientRect()
26
+ const ld = parts.lead()
27
+ const ringAt: [number, number] | undefined = sl && ld ? [(sl.left - s.left) / k - ld.offsetLeft, (sl.top - s.top) / k - ld.offsetTop] : undefined
28
+ onShape({ shut, ringAt, rowH: (button.parentElement as HTMLElement).offsetHeight })
29
+ }
30
+ measure()
31
+ const ro = new ResizeObserver(measure)
32
+ ro.observe(shell)
33
+ ro.observe(button)
34
+ return () => ro.disconnect()
35
+ }
36
+
37
+ /** The CSS variables that carry the shut shape. */
38
+ export const pillVars = (shut?: string, rowH?: number, ringAt?: [number, number]) => ({
39
+ '--pill-shut': shut,
40
+ '--pill-row': rowH === undefined ? undefined : `${rowH}px`,
41
+ '--ring-x': ringAt && `${ringAt[0]}px`,
42
+ '--ring-y': ringAt && `${ringAt[1]}px`,
43
+ })
44
+
45
+ // What the pill can hand focus to when it closes from a tap outside, so a tap on another control keeps it.
46
+ const TAKES_FOCUS = 'a[href], button, input, select, textarea, label, [tabindex]:not([tabindex="-1"])'
47
+
48
+ /** Whether focus is inside the open card (not on the pill's own button). */
49
+ export const focusInCard = (shell: HTMLElement | null, button: HTMLElement | null) => !!shell?.contains(document.activeElement) && document.activeElement !== button
50
+
51
+ /**
52
+ * While open: a press outside folds it back. If focus was in the card and the press was not on another
53
+ * control, focus goes back to the pill. Returns a stop function.
54
+ */
55
+ export function pillOutside(shell: () => HTMLElement | null, button: () => HTMLElement | null, close: () => void) {
56
+ const outside = (e: PointerEvent) => {
57
+ const target = e.target instanceof Element ? e.target : null
58
+ if (!target || shell()?.contains(target)) return
59
+ const inside = focusInCard(shell(), button())
60
+ close()
61
+ if (inside && !target.closest(TAKES_FOCUS)) window.setTimeout(() => button()?.focus({ preventScroll: true }))
62
+ }
63
+ document.addEventListener('pointerdown', outside)
64
+ return () => document.removeEventListener('pointerdown', outside)
65
+ }
66
+
67
+ /** A card an alert opened shrinks back after alertMs, never sooner than its own open motion. Returns a stop function. */
68
+ export function pillAlertTimer(alertMs: number, onDone: () => void) {
69
+ const t = window.setTimeout(onDone, Math.max(alertMs, motionMs('liquid')))
70
+ return () => window.clearTimeout(t)
71
+ }
@@ -0,0 +1,58 @@
1
+ // The keys of a menu button and its menu (WAI-ARIA menu button pattern), the same in every framework.
2
+
3
+ /** What the keys need from a key event: native and React events both have these. */
4
+ export type KeyLike = { key: string; preventDefault(): void }
5
+
6
+ /** The items focus can land on: disabled ones are skipped. */
7
+ export const menuItems = (root: HTMLElement | null) => Array.from(root?.querySelectorAll<HTMLElement>('[role="menuitem"]:not([aria-disabled="true"])') ?? [])
8
+
9
+ /** Down or Up on the menu button opens the menu at its first or last item. */
10
+ export function menuButtonKey(e: KeyLike): 'first' | 'last' | null {
11
+ if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') return null
12
+ e.preventDefault()
13
+ return e.key === 'ArrowDown' ? 'first' : 'last'
14
+ }
15
+
16
+ /** Type-ahead: letters typed within 600ms of each other build a word, and focus moves to the next item after `i` that starts with it. */
17
+ export function createTypeahead() {
18
+ const typeahead = { text: '', at: 0 }
19
+ return (key: string, list: HTMLElement[], i: number) => {
20
+ const now = Date.now()
21
+ typeahead.text = now - typeahead.at > 600 ? key.toLowerCase() : typeahead.text + key.toLowerCase()
22
+ typeahead.at = now
23
+ for (let n = 1; n <= list.length; n++) {
24
+ const el = list[(i + n) % list.length]
25
+ if (el.textContent?.trim().toLowerCase().startsWith(typeahead.text)) {
26
+ el.focus()
27
+ break
28
+ }
29
+ }
30
+ }
31
+ }
32
+
33
+ /** Arrows, Home, End and type-ahead move focus between the items of `root`; Tab calls `onTab`. Returns the key handler. */
34
+ export function createMenuKeys(root: () => HTMLElement | null, onTab: () => void) {
35
+ const typeahead = createTypeahead()
36
+ return (e: KeyLike) => {
37
+ const list = menuItems(root())
38
+ const i = list.indexOf(document.activeElement as HTMLElement)
39
+ const go = (n: number) => {
40
+ e.preventDefault()
41
+ list[(n + list.length) % list.length]?.focus()
42
+ }
43
+ if (e.key === 'ArrowDown') go(i + 1)
44
+ else if (e.key === 'ArrowUp') go(i - 1)
45
+ else if (e.key === 'Home') go(0)
46
+ else if (e.key === 'End') go(list.length - 1)
47
+ // The menu is portalled, so focus goes back to the trigger and Tab carries on from there.
48
+ else if (e.key === 'Tab') onTab()
49
+ else if (e.key.length === 1 && /\S/.test(e.key)) typeahead(e.key, list, i)
50
+ }
51
+ }
52
+
53
+ /** Enter and Space on an item click it. */
54
+ export function itemKey(e: KeyLike & { currentTarget: EventTarget | null }) {
55
+ if (e.key !== 'Enter' && e.key !== ' ') return
56
+ e.preventDefault()
57
+ ;(e.currentTarget as HTMLElement).click()
58
+ }
@@ -0,0 +1,71 @@
1
+ // The morph button's measuring and placing: the panel grows out of the button toward the side with room,
2
+ // and never past the box it sits in.
3
+
4
+ export type MorphCorner = 'tl' | 'tr' | 'bl' | 'br'
5
+ export type MorphBox = { tw: number; th: number; pw: number; ph: number }
6
+
7
+ // The space kept between the panel and the edge of the screen.
8
+ const EDGE = 16
9
+
10
+ // The box it opens in: the nearest ancestor that clips (a card, a scroller, a demo frame), else the
11
+ // window. The panel never grows past its sides, so it fits whatever it sits in, not only the screen.
12
+ function sides(anchor: HTMLElement | null) {
13
+ let clip: Element | null = null
14
+ for (let e = anchor?.parentElement; e && e !== document.body; e = e.parentElement)
15
+ if (getComputedStyle(e).overflowX !== 'visible') {
16
+ clip = e
17
+ break
18
+ }
19
+ const c = clip?.getBoundingClientRect()
20
+ return { clip, left: Math.max(0, c?.left ?? 0), right: Math.min(window.innerWidth, c?.right ?? window.innerWidth), top: c?.top ?? 0, bottom: c?.bottom ?? window.innerHeight }
21
+ }
22
+
23
+ /**
24
+ * Measures the button and the panel now and on every resize (`measure`), and while shut keeps the panel's
25
+ * box no wider than the room it has (`fit`), so it never overflows its box. Returns the function that stops.
26
+ */
27
+ export function observeMorphButton(anchor: HTMLElement, trigger: HTMLElement, shell: HTMLElement, measure: (box: MorphBox) => void, fit: (room: number) => void) {
28
+ const read = () => {
29
+ measure({ tw: trigger.offsetWidth, th: trigger.offsetHeight, pw: shell.offsetWidth, ph: shell.offsetHeight })
30
+ if (shell.dataset.open !== undefined) return
31
+ const a = anchor.getBoundingClientRect(),
32
+ { left, right } = sides(anchor)
33
+ fit(Math.max(trigger.offsetWidth, shell.dataset.corner?.endsWith('r') ? a.right - left - EDGE : right - EDGE - a.left))
34
+ }
35
+ read()
36
+ const ro = new ResizeObserver(read)
37
+ ro.observe(trigger)
38
+ ro.observe(shell)
39
+ const clip = sides(anchor).clip
40
+ if (clip) ro.observe(clip)
41
+ return () => ro.disconnect()
42
+ }
43
+
44
+ /** It grows toward the side with room, decided before the move starts and never during it. */
45
+ export function placeMorphButton(anchor: HTMLElement | null, box: MorphBox): { corner: MorphCorner; room: number } | null {
46
+ const a = anchor?.getBoundingClientRect()
47
+ if (!a) return null
48
+ const { left: L, right: R, top: T, bottom: B } = sides(anchor)
49
+ const right = R - EDGE - a.left
50
+ const left = a.right - L - EDGE
51
+ // Up or down: first the way that fits both its box and the screen, then the way that fits its box
52
+ // (a card that clips it would cut it; the page can still scroll), then the way with more room.
53
+ const below = { box: B - a.top, screen: window.innerHeight - EDGE - a.top }
54
+ const above = { box: a.bottom - T, screen: a.bottom - EDGE }
55
+ const fits = (r: { box: number; screen: number }, both: boolean) => r.box >= box.ph && (!both || r.screen >= box.ph)
56
+ const up = fits(below, true) ? false : fits(above, true) ? true : fits(below, false) ? false : fits(above, false) ? true : above.box > below.box
57
+ const flipX = right < box.pw && left > right
58
+ return { corner: `${up ? 'b' : 't'}${flipX ? 'r' : 'l'}` as MorphCorner, room: Math.max(box.tw, flipX ? left : right) }
59
+ }
60
+
61
+ /** Shut, the surface is cut back to the button's own box in the chosen corner. Open, it is whole. */
62
+ export function morphInsets(corner: MorphCorner, box: MorphBox, open: boolean) {
63
+ const dx = Math.max(0, box.pw - box.tw)
64
+ const dy = Math.max(0, box.ph - box.th)
65
+ const shut = { tl: [0, dx, dy, 0], tr: [0, 0, dy, dx], bl: [dy, dx, 0, 0], br: [dy, 0, 0, dx] }[corner]
66
+ const [t, r, b, l] = open ? [0, 0, 0, 0] : shut
67
+ return { '--t': `${t}px`, '--r': `${r}px`, '--b': `${b}px`, '--l': `${l}px`, '--pill': `${box.th / 2}px` }
68
+ }
69
+
70
+ /** A press outside that lands on something that takes focus itself keeps it; anything else gives focus back to the button. */
71
+ export const takesFocus = (target: EventTarget | null) => target instanceof Element && !!target.closest('a[href], button, input, select, textarea, label, [tabindex]:not([tabindex="-1"])')
@@ -0,0 +1,57 @@
1
+ // NumberInput: reading typed numbers in any locale, and the repeat while a step button is held.
2
+
3
+ export type NumberInputLabels = {
4
+ /** Name of the minus button. A function gets the field label; a string is used as it is. */
5
+ decrease: string | ((label: string) => string)
6
+ /** Name of the plus button. A function gets the field label; a string is used as it is. */
7
+ increase: string | ((label: string) => string)
8
+ }
9
+
10
+ // Input timings, so reduced motion does not change them
11
+ export function tokenMs(name: string, fallback: number): number {
12
+ if (typeof window === 'undefined') return fallback
13
+ const raw = getComputedStyle(document.documentElement).getPropertyValue(name).trim()
14
+ if (!raw) return fallback
15
+ // The browser may report 500ms as .5s
16
+ return raw.endsWith('ms') ? parseFloat(raw) : parseFloat(raw) * 1000
17
+ }
18
+
19
+ export function parseNumber(text: string, locale?: string): number | null {
20
+ const parts = new Intl.NumberFormat(locale).formatToParts(-12345.6)
21
+ const group = parts.find((p) => p.type === 'group')?.value ?? ','
22
+ const decimal = parts.find((p) => p.type === 'decimal')?.value ?? '.'
23
+ const minus = parts.find((p) => p.type === 'minusSign')?.value ?? '-'
24
+ // sv, nb and fi write minus as U+2212, which the cleanup below would drop
25
+ const clean = text
26
+ .split(minus).join('-')
27
+ .replace(/−/g, '-')
28
+ .split(group).join('')
29
+ .split(decimal).join('.')
30
+ .replace(/[^\d.-]/g, '')
31
+ if (clean === '' || clean === '-' || clean === '.') return null
32
+ const n = Number(clean)
33
+ return Number.isFinite(n) ? n : null
34
+ }
35
+
36
+ /** Hold to repeat: one step at once, then steps that speed up (big after 12) until the end is reached or the pointer lifts. */
37
+ export function createHold() {
38
+ let timer: number | undefined
39
+ let count = 0
40
+ const stop = () => {
41
+ window.clearTimeout(timer)
42
+ count = 0
43
+ }
44
+ const start = (bump: (big: boolean) => void, atEnd: () => boolean) => {
45
+ bump(false)
46
+ // A disabled button gets no pointerup, so listen on the window
47
+ window.addEventListener('pointerup', stop, { once: true })
48
+ const tick = () => {
49
+ if (atEnd()) return stop()
50
+ count++
51
+ bump(count > 12)
52
+ timer = window.setTimeout(tick, tokenMs('--gooee-motion-repeat', 70))
53
+ }
54
+ timer = window.setTimeout(tick, tokenMs('--gooee-motion-repeat-delay', 400))
55
+ }
56
+ return { start, stop }
57
+ }
@@ -0,0 +1,166 @@
1
+ import { clamp, wrap, type wheelSteps } from '../feel'
2
+ import type { PointerLike, ScrubberOptions } from './scrubber'
3
+ import { tokenNumber } from './scrubLook'
4
+
5
+ // The PickerWheel's columns, keys, tap and wheel, and the time and date set-ups, for React and Vue.
6
+ // The motion is createScrubber's.
7
+
8
+ export type WheelColumn = {
9
+ /** Name screen readers hear for this column, such as "Hour". */
10
+ label: string
11
+ /** The rows, top to bottom. */
12
+ options: string[]
13
+ /** The last row runs on into the first, as hours do. */
14
+ loop?: boolean
15
+ /** Rows, by index, drawn stronger, such as :00 and :30, or today. Page Up and Page Down jump between them. */
16
+ strong?: number[]
17
+ /** Twice as wide, for words such as month names. */
18
+ wide?: boolean
19
+ }
20
+
21
+ // Rows drawn either side of the band. At the tilt token's 18 degrees a row this far round is nearly edge on.
22
+ export const SPAN = 4
23
+
24
+ /** What a column keeps between events: the row height, the row it last rested on and what was typed. */
25
+ export type ColumnState = { row: number; rested: number; typed: { text: string; at: number } }
26
+ export const columnState = (index: number): ColumnState => ({ row: 0, rested: index, typed: { text: '', at: 0 } })
27
+
28
+ /** In a loop the position runs on past the last row; this is the position of row `i` nearest `t`. */
29
+ export const nearestRow = (t: number, i: number, n: number, loop: boolean) => (loop ? Math.round(t) + ((((i - wrap(Math.round(t), n)) % n) + n + Math.floor(n / 2)) % n) - Math.floor(n / 2) : i)
30
+
31
+ /** The scrubber options for one column: rows are the marks, a drag follows the hand, a let-go lands on a row. */
32
+ export function columnScrub(column: WheelColumn, index: number, state: ColumnState, wheel: ReturnType<typeof wheelSteps>): Pick<ScrubberOptions<PointerLike>, 'value' | 'min' | 'max' | 'edge' | 'land' | 'maxSpeed' | 'move' | 'key' | 'tap' | 'wheel'> {
33
+ const { options, loop = false } = column
34
+ const n = options.length
35
+ const strong = new Set(column.strong ?? [])
36
+ const nearest = (t: number, i: number) => nearestRow(t, i, n, loop)
37
+ const fit = (v: number) => (loop ? v : clamp(v, 0, n - 1))
38
+ const strongFrom = (t: number, dir: 1 | -1) => {
39
+ const i = wrap(Math.round(t), n)
40
+ for (let d = 1; d < n; d++) if (strong.has(wrap(i + dir * d, n)) && (loop || (i + dir * d >= 0 && i + dir * d < n))) return fit(Math.round(t) + dir * d)
41
+ return fit(Math.round(t) + dir * 5)
42
+ }
43
+ return {
44
+ value: nearest(state.rested, clamp(index, 0, n - 1)),
45
+ min: loop ? undefined : 0,
46
+ max: loop ? undefined : n - 1,
47
+ edge: 0.6,
48
+ land: (p) => fit(Math.round(p)),
49
+ maxSpeed: 40,
50
+ move: (e, last) => -(e.clientY - last.y) / (state.row || 1),
51
+ // The APG spinbutton keys: Up is the next, higher value and Down the one before.
52
+ key: (k, t) => {
53
+ if (k === 'ArrowUp') return fit(Math.round(t) + 1)
54
+ if (k === 'ArrowDown') return fit(Math.round(t) - 1)
55
+ if (k === 'PageUp') return strongFrom(t, 1)
56
+ if (k === 'PageDown') return strongFrom(t, -1)
57
+ if (k === 'Home') return nearest(t, 0)
58
+ if (k === 'End') return nearest(t, n - 1)
59
+ if (k.length === 1 && /\S/.test(k)) {
60
+ const now = performance.now()
61
+ state.typed = { text: (now - state.typed.at < 800 ? state.typed.text : '') + k.toLowerCase(), at: now }
62
+ const q = state.typed.text
63
+ // A padded number answers to its plain digits, so typing 5 finds the padded five before fifty.
64
+ const said = (o: string) => (/^\d+$/.test(o) ? String(Number(o)) : o.toLowerCase())
65
+ const exact = options.findIndex((o) => o.toLowerCase() === q || said(o) === q)
66
+ const hit = exact >= 0 ? exact : options.findIndex((o) => o.toLowerCase().startsWith(q))
67
+ return hit >= 0 ? nearest(t, hit) : null
68
+ }
69
+ return null
70
+ },
71
+ tap: (e, at) => {
72
+ const el = e.currentTarget as HTMLElement
73
+ const r = el.getBoundingClientRect()
74
+ const tilt = (tokenNumber(el, '--gooee-wheel-tilt') * Math.PI) / 180
75
+ const radius = state.row / 2 / Math.tan(tilt / 2)
76
+ const dy = e.clientY - (r.top + r.height / 2)
77
+ if (!tilt || !radius) return null
78
+ return fit(Math.round(at + Math.asin(clamp(dy / radius, -1, 1)) / tilt))
79
+ },
80
+ // Only a focused column takes the wheel, and at an end it lets the page scroll on.
81
+ wheel: (e, t, focused) => {
82
+ if (!focused || !e.deltaY) return null
83
+ const dir = Math.sign(e.deltaY)
84
+ if (!loop && ((dir > 0 && t >= n - 1) || (dir < 0 && t <= 0))) return null
85
+ return fit(t + wheel(e.deltaY * (e.deltaMode === 1 ? 40 : 1)))
86
+ },
87
+ }
88
+ }
89
+
90
+ /** The rows drawn round the drum at position `at`: each one's key, distance from the band and row. */
91
+ export function drumRows(at: number, n: number, loop: boolean) {
92
+ const rows: { k: number; d: number; i: number }[] = []
93
+ for (let k = Math.floor(at) - SPAN; k <= Math.ceil(at) + SPAN; k++) {
94
+ if (!loop && (k < 0 || k > n - 1)) continue
95
+ rows.push({ k, d: k - at, i: wrap(k, n) })
96
+ }
97
+ return rows
98
+ }
99
+
100
+ const pad = (v: number) => String(v).padStart(2, '0')
101
+
102
+ /** A time wheel: hour, minute and, on a 12 hour clock, AM or PM, and the turns between rows and "HH:mm". */
103
+ export function timeWheel(hourCycle: 12 | 24, minuteStep: number, strong: number[]) {
104
+ const twelve = hourCycle === 12
105
+ const minutes = Array.from({ length: Math.ceil(60 / minuteStep) }, (_, i) => i * minuteStep)
106
+ const columns: WheelColumn[] = [
107
+ { label: 'Hour', options: twelve ? Array.from({ length: 12 }, (_, i) => String(i + 1)) : Array.from({ length: 24 }, (_, i) => pad(i)), loop: true },
108
+ { label: 'Minute', options: minutes.map(pad), loop: true, strong: strong.map((m) => minutes.indexOf(m)).filter((i) => i >= 0) },
109
+ ...(twelve ? [{ label: 'AM or PM', options: ['AM', 'PM'] }] : []),
110
+ ]
111
+ return {
112
+ columns,
113
+ toRows(hhmm: string) {
114
+ const [h = 0, m = 0] = hhmm.split(':').map(Number)
115
+ const mi = clamp(Math.round(m / minuteStep), 0, minutes.length - 1)
116
+ return twelve ? [(h % 12 || 12) - 1, mi, h >= 12 ? 1 : 0] : [h, mi]
117
+ },
118
+ toTime(rows: number[]) {
119
+ const h = twelve ? ((rows[0] + 1) % 12) + (rows[2] ? 12 : 0) : rows[0]
120
+ return `${pad(h)}:${pad(minutes[rows[1]])}`
121
+ },
122
+ show: (rows: number[]) => (twelve ? `${rows[0] + 1}:${pad(minutes[rows[1]])} ${rows[2] ? 'PM' : 'AM'}` : `${pad(rows[0])}:${pad(minutes[rows[1]])}`),
123
+ }
124
+ }
125
+
126
+ const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
127
+ const daysIn = (month: number, year: number) => new Date(year, month + 1, 0).getDate()
128
+ const iso = (y: number, m: number, d: number) => `${y}-${pad(m + 1)}-${pad(d)}`
129
+ const parse = (s: string) => {
130
+ const [y, m, d] = s.split('-').map(Number)
131
+ return { y, m: m - 1, d }
132
+ }
133
+ /** Today as "YYYY-MM-DD". */
134
+ export const today = () => {
135
+ const t = new Date()
136
+ return iso(t.getFullYear(), t.getMonth(), t.getDate())
137
+ }
138
+
139
+ /** A date wheel for `value`: day, month and year with each month's real length, and the turns between rows and "YYYY-MM-DD". */
140
+ export function dateWheel(value: string, now: string, fromYear?: number, toYear?: number, strong?: string[]) {
141
+ const first = fromYear ?? parse(now).y - 100
142
+ const last = toYear ?? parse(now).y + 10
143
+ const years = Array.from({ length: Math.max(1, last - first + 1) }, (_, i) => first + i)
144
+ const at = parse(value)
145
+ const y = clamp(at.y, first, last)
146
+ const count = daysIn(at.m, y)
147
+ const marks = (strong ?? [now]).map(parse)
148
+ const columns: WheelColumn[] = [
149
+ { label: 'Day', options: Array.from({ length: count }, (_, i) => String(i + 1)), loop: true, strong: marks.filter((s) => s.y === y && s.m === at.m).map((s) => s.d - 1) },
150
+ { label: 'Month', options: MONTHS, loop: true, wide: true, strong: marks.filter((s) => s.y === y).map((s) => s.m) },
151
+ { label: 'Year', options: years.map(String), strong: marks.map((s) => years.indexOf(s.y)).filter((i) => i >= 0) },
152
+ ]
153
+ const toDate = (rows: number[]) => {
154
+ const yy = years[rows[2]]
155
+ return iso(yy, rows[1], Math.min(rows[0] + 1, daysIn(rows[1], yy)))
156
+ }
157
+ return {
158
+ columns,
159
+ rows: [Math.min(at.d, count) - 1, at.m, years.indexOf(y)],
160
+ toDate,
161
+ show(rows: number[]) {
162
+ const p = parse(toDate(rows))
163
+ return new Date(p.y, p.m, p.d).toLocaleDateString('en-GB', { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' })
164
+ },
165
+ }
166
+ }
@@ -0,0 +1,45 @@
1
+ // Retime's speed, shared by the React and Vue parts: one CSS scale for every duration token, saved on the
2
+ // device and told to every other Retime control on the page.
3
+
4
+ export type RetimeSpeed = 'off' | 'slower' | 'normal' | 'faster'
5
+
6
+ export type RetimeLabels = Record<RetimeSpeed, string>
7
+
8
+ export const defaultRetimeLabels: RetimeLabels = { off: 'Off', slower: 'Slower', normal: 'Normal', faster: 'Faster' }
9
+
10
+ export const retimeSpeeds = ['off', 'slower', 'normal', 'faster'] as const
11
+
12
+ // How long motion takes compared with normal. Off is 0, so nothing moves at all.
13
+ const SCALE: Record<RetimeSpeed, number> = { off: 0, slower: 1.5, normal: 1, faster: 0.6 }
14
+
15
+ const STORAGE_KEY = 'gooee-motion-speed'
16
+ export const RETIME_EVENT = 'gooee-retime'
17
+
18
+ /** Retimes every animation in the library at once. Reduced motion in the system still wins. */
19
+ export function applyRetime(speed: RetimeSpeed) {
20
+ if (typeof document === 'undefined') return
21
+ document.documentElement.style.setProperty('--gooee-motion-scale', String(SCALE[speed]))
22
+ // Other Retime controls on the page (a header and a menu, say) follow the same choice.
23
+ window.dispatchEvent(new CustomEvent<RetimeSpeed>(RETIME_EVENT, { detail: speed }))
24
+ }
25
+
26
+ export function savedSpeed(): RetimeSpeed | null {
27
+ try {
28
+ const v = localStorage.getItem(STORAGE_KEY)
29
+ return v && v in SCALE ? (v as RetimeSpeed) : null
30
+ } catch {
31
+ return null
32
+ }
33
+ }
34
+
35
+ /** Applies the speed unless the page already has it, and saves it when asked. */
36
+ export function commitSpeed(speed: RetimeSpeed, remember: boolean) {
37
+ if (document.documentElement.style.getPropertyValue('--gooee-motion-scale') === String(SCALE[speed])) return
38
+ applyRetime(speed)
39
+ if (!remember) return
40
+ try {
41
+ localStorage.setItem(STORAGE_KEY, speed)
42
+ } catch {
43
+ // Not saved; the speed still holds for this visit.
44
+ }
45
+ }
@@ -0,0 +1,57 @@
1
+ import { clamp, lastStep, roundTo, stepKey, steps, type wheelSteps } from '../feel'
2
+ import type { PointerLike, ScrubberOptions } from './scrubber'
3
+ import { sideways } from './scrubLook'
4
+
5
+ // The RulerScrubber's scale, drag, tap and wheel, for the React and Vue ruler. The motion is createScrubber's.
6
+
7
+ export type RulerScale = { min: number; max: number; step: number; marks?: number[] }
8
+ /** The px between two ticks (a token), how many ticks fit either side of the needle, the ruler's width and a tag's. */
9
+ export type RulerFit = { gap: number; span: number; width: number; tag: number }
10
+
11
+ /** The steps of a ruler: the last one, every one, the numbered ones, and how a value lands on one. */
12
+ export function rulerScale({ min, max, step, marks }: RulerScale) {
13
+ const top = lastStep(min, max, step)
14
+ const all = steps(min, max, step)
15
+ return {
16
+ top,
17
+ all,
18
+ labelled: new Set((marks ?? all.filter((_, i) => i % 10 === 0)).map((v) => roundTo(v, step, min))),
19
+ land: (p: number) => clamp(roundTo(p, step, min), min, top),
20
+ }
21
+ }
22
+
23
+ /** The scrubber options for a ruler: how far a drag moves it, where keys, a tap and the wheel take it. */
24
+ export function rulerScrub(p: RulerScale, fit: RulerFit, wheel: ReturnType<typeof wheelSteps>): Pick<ScrubberOptions<PointerLike & { shiftKey: boolean }>, 'min' | 'max' | 'edge' | 'land' | 'maxSpeed' | 'move' | 'key' | 'tap' | 'wheel'> {
25
+ const { min, max, step } = p
26
+ const { top, labelled, land } = rulerScale(p)
27
+ return {
28
+ min,
29
+ max: top,
30
+ edge: step,
31
+ land,
32
+ // The hand at 4000px a second at most, so a hard throw still stops in reach.
33
+ maxSpeed: fit.gap ? (4000 / fit.gap) * step : undefined,
34
+ // Dragging left brings higher values in, like pulling a tape.
35
+ move: (e, last) => (-(e.clientX - last.x) / (fit.gap || 1)) * step * (e.shiftKey ? 0.25 : 1),
36
+ key: (k, t) => stepKey(k, t, { min, max, step, marks: [...labelled] }),
37
+ // A tap without a drag goes to the tick under it, so the ruler works with one plain press.
38
+ tap: (e, at) => {
39
+ if (!fit.gap) return null
40
+ const r = (e.currentTarget as HTMLElement).getBoundingClientRect()
41
+ return land(at + ((e.clientX - (r.left + r.width / 2)) / fit.gap) * step)
42
+ },
43
+ wheel: (e, t) => {
44
+ const dx = sideways(e)
45
+ if (dx === null || !fit.gap) return null
46
+ const n = wheel(dx, fit.gap, e.timeStamp)
47
+ const to = clamp(roundTo(t + n * step, step, min), min, top)
48
+ return to === t && (t === min || t === top) ? null : to
49
+ },
50
+ }
51
+ }
52
+
53
+ /** Measures the ruler: the tick gap token, the ticks that fit, its width and the tag's. */
54
+ export function measureRuler(el: HTMLElement, tag: HTMLElement | null): RulerFit {
55
+ const gap = parseFloat(getComputedStyle(el).getPropertyValue('--gooee-ruler-tick-gap')) || 0
56
+ return { gap, span: gap ? Math.ceil(el.clientWidth / 2 / gap) + 1 : 24, width: el.clientWidth, tag: tag?.offsetWidth ?? 0 }
57
+ }