gooee 0.50.0 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/package.json +4 -2
  2. package/scripts/add.js +28 -6
  3. package/src/components/Accordion/Accordion.vue +62 -0
  4. package/src/components/Accordion/AccordionItem.vue +45 -0
  5. package/src/components/Accordion/accordionContext.ts +6 -0
  6. package/src/components/Alert/Alert.tsx +7 -27
  7. package/src/components/Alert/Alert.vue +59 -0
  8. package/src/components/AlertDialog/AlertDialog.vue +126 -0
  9. package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
  10. package/src/components/AspectRatio/AspectRatio.vue +17 -0
  11. package/src/components/Avatar/Avatar.vue +56 -0
  12. package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
  13. package/src/components/Badge/Badge.vue +32 -0
  14. package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
  15. package/src/components/Button/Button.vue +130 -0
  16. package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
  17. package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
  18. package/src/components/Card/Card.vue +34 -0
  19. package/src/components/Center/Center.vue +32 -0
  20. package/src/components/Checkbox/Checkbox.tsx +8 -25
  21. package/src/components/Checkbox/Checkbox.vue +108 -0
  22. package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
  23. package/src/components/Cluster/Cluster.vue +34 -0
  24. package/src/components/CodeBlock/CodeBlock.vue +45 -0
  25. package/src/components/Collapsible/Collapsible.vue +59 -0
  26. package/src/components/Container/Container.vue +22 -0
  27. package/src/components/CopyButton/CopyButton.tsx +3 -49
  28. package/src/components/CopyButton/CopyButton.vue +116 -0
  29. package/src/components/Dial/Dial.tsx +12 -120
  30. package/src/components/Dial/Dial.vue +228 -0
  31. package/src/components/Dialog/Dialog.vue +139 -0
  32. package/src/components/Divider/Divider.vue +27 -0
  33. package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
  34. package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
  35. package/src/components/EmptyState/EmptyState.vue +33 -0
  36. package/src/components/Fab/Fab.tsx +4 -14
  37. package/src/components/Fab/Fab.vue +115 -0
  38. package/src/components/Fieldset/Fieldset.vue +42 -0
  39. package/src/components/FormField/FormField.tsx +2 -17
  40. package/src/components/FormField/FormField.vue +64 -0
  41. package/src/components/Grid/Grid.vue +43 -0
  42. package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -143
  43. package/src/components/HoldToConfirm/HoldToConfirm.vue +281 -0
  44. package/src/components/IconButton/IconButton.vue +25 -0
  45. package/src/components/Input/Input.vue +112 -0
  46. package/src/components/InputGroup/InputGroup.vue +102 -0
  47. package/src/components/Kbd/Kbd.tsx +2 -12
  48. package/src/components/Kbd/Kbd.vue +21 -0
  49. package/src/components/Link/Link.vue +38 -0
  50. package/src/components/LivePill/LivePill.tsx +11 -39
  51. package/src/components/LivePill/LivePill.vue +152 -0
  52. package/src/components/Meter/Meter.vue +88 -0
  53. package/src/components/MorphButton/MorphButton.tsx +11 -67
  54. package/src/components/MorphButton/MorphButton.vue +120 -0
  55. package/src/components/NumberInput/NumberInput.tsx +11 -48
  56. package/src/components/NumberInput/NumberInput.vue +184 -0
  57. package/src/components/Pagination/Pagination.tsx +2 -15
  58. package/src/components/Pagination/Pagination.vue +87 -0
  59. package/src/components/Pagination/range.ts +15 -0
  60. package/src/components/PasswordInput/PasswordInput.vue +53 -0
  61. package/src/components/PickerWheel/DateWheel.vue +48 -0
  62. package/src/components/PickerWheel/PickerWheel.tsx +15 -129
  63. package/src/components/PickerWheel/PickerWheel.vue +77 -0
  64. package/src/components/PickerWheel/TimeWheel.vue +50 -0
  65. package/src/components/PickerWheel/WheelColumn.vue +70 -0
  66. package/src/components/Popover/Popover.vue +108 -0
  67. package/src/components/ProgressBar/ProgressBar.vue +48 -0
  68. package/src/components/Prose/Prose.vue +18 -0
  69. package/src/components/Radio/Radio.vue +49 -0
  70. package/src/components/Radio/RadioGroup.vue +69 -0
  71. package/src/components/Retime/Retime.tsx +7 -40
  72. package/src/components/Retime/Retime.vue +64 -0
  73. package/src/components/RulerScrubber/RulerScrubber.tsx +9 -38
  74. package/src/components/RulerScrubber/RulerScrubber.vue +166 -0
  75. package/src/components/ScrollArea/ScrollArea.tsx +17 -129
  76. package/src/components/ScrollArea/ScrollArea.vue +58 -0
  77. package/src/components/SearchInput/SearchInput.tsx +2 -15
  78. package/src/components/SearchInput/SearchInput.vue +70 -0
  79. package/src/components/SegmentedControl/SegmentedControl.tsx +7 -51
  80. package/src/components/SegmentedControl/SegmentedControl.vue +111 -0
  81. package/src/components/Select/Select.tsx +16 -47
  82. package/src/components/Select/Select.vue +236 -0
  83. package/src/components/Sheet/Sheet.tsx +52 -231
  84. package/src/components/Sheet/Sheet.vue +249 -0
  85. package/src/components/Skeleton/Skeleton.vue +38 -0
  86. package/src/components/Slider/Slider.tsx +25 -84
  87. package/src/components/Slider/Slider.vue +113 -0
  88. package/src/components/Spinner/Spinner.vue +30 -0
  89. package/src/components/Split/Split.vue +34 -0
  90. package/src/components/SplitFlap/SplitFlap.tsx +11 -175
  91. package/src/components/SplitFlap/SplitFlap.vue +52 -0
  92. package/src/components/Stack/Stack.vue +22 -0
  93. package/src/components/SwipeDeck/SwipeDeck.tsx +30 -194
  94. package/src/components/SwipeDeck/SwipeDeck.vue +168 -0
  95. package/src/components/Switch/Switch.tsx +6 -75
  96. package/src/components/Switch/Switch.vue +66 -0
  97. package/src/components/Table/Table.module.css +12 -3
  98. package/src/components/Table/Table.tsx +8 -59
  99. package/src/components/Table/Table.vue +274 -0
  100. package/src/components/TableOfContents/TableOfContents.vue +90 -0
  101. package/src/components/Tabs/Tab.vue +37 -0
  102. package/src/components/Tabs/TabList.vue +47 -0
  103. package/src/components/Tabs/TabPanel.vue +26 -0
  104. package/src/components/Tabs/Tabs.module.css +2 -2
  105. package/src/components/Tabs/Tabs.tsx +11 -84
  106. package/src/components/Tabs/Tabs.vue +33 -0
  107. package/src/components/Tabs/tabsContext.ts +15 -0
  108. package/src/components/Tag/Tag.tsx +6 -26
  109. package/src/components/Tag/Tag.vue +63 -0
  110. package/src/components/Textarea/Textarea.tsx +2 -22
  111. package/src/components/Textarea/Textarea.vue +120 -0
  112. package/src/components/ThemeToggle/ThemeToggle.tsx +10 -84
  113. package/src/components/ThemeToggle/ThemeToggle.vue +90 -0
  114. package/src/components/Toast/Toast.tsx +34 -264
  115. package/src/components/Toast/Toast.vue +138 -0
  116. package/src/components/Toast/ToastItem.vue +108 -0
  117. package/src/components/Toggle/Toggle.vue +52 -0
  118. package/src/components/Tooltip/Tooltip.tsx +17 -48
  119. package/src/components/Tooltip/Tooltip.vue +95 -0
  120. package/src/components/VisuallyHidden/VisuallyHidden.vue +17 -0
  121. package/src/internal/FieldMessage.vue +41 -0
  122. package/src/internal/Roll.vue +20 -0
  123. package/src/internal/focus.ts +4 -47
  124. package/src/internal/icons.module.css +39 -27
  125. package/src/internal/layers.ts +7 -61
  126. package/src/internal/motion/alert.ts +17 -0
  127. package/src/internal/motion/buttonGroup.ts +21 -0
  128. package/src/internal/motion/checkbox.ts +39 -0
  129. package/src/internal/motion/collapseOut.ts +26 -0
  130. package/src/internal/motion/copyButton.ts +48 -0
  131. package/src/internal/motion/dial.ts +137 -0
  132. package/src/internal/motion/fab.ts +14 -0
  133. package/src/internal/motion/floating.ts +58 -0
  134. package/src/internal/motion/focus.ts +53 -0
  135. package/src/internal/motion/formField.ts +19 -0
  136. package/src/internal/motion/holdToConfirm.ts +147 -0
  137. package/src/internal/motion/keys.ts +42 -0
  138. package/src/internal/motion/layers.ts +78 -0
  139. package/src/internal/motion/listbox.ts +69 -0
  140. package/src/internal/motion/livePill.ts +71 -0
  141. package/src/internal/motion/menuKeys.ts +58 -0
  142. package/src/internal/motion/morphButton.ts +71 -0
  143. package/src/internal/motion/numberInput.ts +57 -0
  144. package/src/internal/motion/pickerWheel.ts +166 -0
  145. package/src/internal/motion/retime.ts +45 -0
  146. package/src/internal/motion/ruler.ts +57 -0
  147. package/src/internal/motion/scrollArea.ts +133 -0
  148. package/src/internal/motion/scrubLook.ts +15 -0
  149. package/src/internal/motion/segmented.ts +63 -0
  150. package/src/internal/motion/sheetDetents.ts +271 -0
  151. package/src/internal/motion/slider.ts +108 -0
  152. package/src/internal/motion/splitFlap.ts +205 -0
  153. package/src/internal/motion/swipeDeck.ts +236 -0
  154. package/src/internal/motion/switchThumb.ts +83 -0
  155. package/src/internal/motion/table.ts +77 -0
  156. package/src/internal/motion/tabs.ts +105 -0
  157. package/src/internal/motion/textarea.ts +22 -0
  158. package/src/internal/motion/themeIcon.ts +91 -0
  159. package/src/internal/motion/themeReveal.ts +71 -0
  160. package/src/internal/motion/toast.ts +291 -0
  161. package/src/internal/motion/tooltip.ts +58 -0
  162. package/src/internal/themeReveal.ts +3 -61
  163. package/src/internal/useFloating.ts +9 -49
  164. package/src/internal/useScrub.ts +1 -13
  165. package/src/internal/vue/trigger.ts +18 -0
  166. package/src/internal/vue/useFloating.ts +38 -0
  167. package/src/internal/vue/usePresence.ts +21 -0
  168. package/src/internal/vue/useScrub.ts +40 -0
  169. package/src/internal/vueIcons.ts +134 -0
  170. package/src/tokens.css +5 -0
  171. package/src/internal/relativeTime.ts +0 -26
@@ -0,0 +1,59 @@
1
+ <script setup lang="ts">
2
+ import { computed, ref, useId } from 'vue'
3
+ import { ChevronDownIcon } from '../../internal/vueIcons'
4
+ import styles from './Collapsible.module.css'
5
+
6
+ // The Vue Collapsible: the same look (Collapsible.module.css) as the React one. For your own button, use the
7
+ // trigger slot and bind what it gives: <template #trigger="t"><Button v-bind="t">Details</Button></template>.
8
+ const props = withDefaults(
9
+ defineProps<{
10
+ /** Text for the built-in toggle button. The label slot replaces it. */
11
+ label?: string
12
+ /** Starting open state when there is no v-model:open. */
13
+ defaultOpen?: boolean
14
+ /** Stops the panel from opening or closing. */
15
+ disabled?: boolean
16
+ }>(),
17
+ { defaultOpen: false },
18
+ )
19
+ /** Open state. Use v-model:open, or leave it out and the part keeps its own state. */
20
+ const model = defineModel<boolean | undefined>('open', { default: undefined })
21
+ const slots = defineSlots<{
22
+ default?: () => unknown
23
+ label?: () => unknown
24
+ trigger?: (props: { 'aria-expanded': boolean; 'aria-controls': string; disabled?: boolean; 'aria-disabled'?: boolean; onClick: () => void }) => unknown
25
+ }>()
26
+
27
+ const id = useId()
28
+ const local = ref(props.defaultOpen)
29
+ const open = computed(() => model.value ?? local.value)
30
+ const toggle = () => {
31
+ if (props.disabled) return
32
+ const next = !open.value
33
+ local.value = next
34
+ model.value = next
35
+ }
36
+ // Only set when disabled, so a trigger's own disabled attribute is kept otherwise.
37
+ const triggerProps = computed(() => ({
38
+ 'aria-expanded': open.value,
39
+ 'aria-controls': `${id}-panel`,
40
+ ...(props.disabled ? { disabled: true, 'aria-disabled': true } : null),
41
+ onClick: toggle,
42
+ }))
43
+ </script>
44
+
45
+ <template>
46
+ <div :class="styles.root" :data-open="open || undefined">
47
+ <slot v-if="slots.trigger" name="trigger" v-bind="triggerProps" />
48
+ <button v-else type="button" :class="styles.trigger" :aria-expanded="open" :aria-controls="`${id}-panel`" :disabled="disabled" @click="toggle">
49
+ <span :class="styles.label"><slot name="label">{{ label }}</slot></span>
50
+ <ChevronDownIcon :class="styles.chevron" />
51
+ </button>
52
+ <!-- Grid rows animate from 0fr to 1fr, so the height follows the content without measuring. -->
53
+ <div :id="`${id}-panel`" :class="styles.panel" :inert="!open">
54
+ <div :class="styles.inner">
55
+ <div :class="styles.content"><slot /></div>
56
+ </div>
57
+ </div>
58
+ </div>
59
+ </template>
@@ -0,0 +1,22 @@
1
+ <script setup lang="ts">
2
+ import { space, type Space } from '../../internal/space'
3
+ import styles from './Container.module.css'
4
+
5
+ // The Vue Container: the same look (Container.module.css) as the React one. Other attributes go to the element.
6
+ withDefaults(
7
+ defineProps<{
8
+ /** Widest the content gets, from the --gooee-container-* tokens. "full" never stops growing. */
9
+ size?: 'sm' | 'md' | 'lg' | 'xl' | 'full'
10
+ /** Space on the left and right, as a step of the space scale. The notch area on a phone wins when it is wider. */
11
+ padding?: Space
12
+ /** Element to render, such as main for the page body. */
13
+ as?: 'div' | 'main' | 'section' | 'article' | 'header' | 'footer'
14
+ }>(),
15
+ { size: 'lg', padding: 5, as: 'div' },
16
+ )
17
+ defineSlots<{ default?: () => unknown }>()
18
+ </script>
19
+
20
+ <template>
21
+ <component :is="as" :class="[styles.root, styles[size]]" :style="{ '--container-pad': space(padding) }"><slot /></component>
22
+ </template>
@@ -4,6 +4,7 @@ import { useEffect, useLayoutEffect, useRef, useState } from 'react'
4
4
  import { cx } from '../../internal/cx'
5
5
  import { CopyIcon, XIcon } from '../../internal/icons'
6
6
  import { copyRun, type CopyRun } from '../../internal/liquidMore'
7
+ import { capShift, writeText } from '../../internal/motion/copyButton'
7
8
  import styles from './CopyButton.module.css'
8
9
 
9
10
  export type CopyButtonProps = {
@@ -27,27 +28,6 @@ export type CopyButtonProps = {
27
28
  className?: string
28
29
  }
29
30
 
30
- async function writeText(text: string) {
31
- try {
32
- await navigator.clipboard.writeText(text)
33
- return true
34
- } catch {
35
- // Fallback for http pages and older browsers.
36
- const ta = document.createElement('textarea')
37
- ta.value = text
38
- ta.setAttribute('readonly', '')
39
- ta.style.position = 'fixed'
40
- ta.style.opacity = '0'
41
- const prev = document.activeElement as HTMLElement | null
42
- document.body.appendChild(ta)
43
- ta.select()
44
- const ok = document.execCommand('copy')
45
- ta.remove()
46
- prev?.focus()
47
- return ok
48
- }
49
- }
50
-
51
31
  export function CopyButton({
52
32
  value,
53
33
  label = 'Copy',
@@ -88,34 +68,8 @@ export function CopyButton({
88
68
  }, [copied, status])
89
69
  useLayoutEffect(() => () => run.current?.stop(), [])
90
70
 
91
- // With a label, the icon sits on the middle of the label's capital letters, not of its line.
92
- useLayoutEffect(() => {
93
- const root = rootRef.current
94
- const icons = iconsRef.current
95
- const text = labelRef.current?.firstChild
96
- root?.style.removeProperty('--cap-shift')
97
- if (!root || !icons || !text) return
98
- const place = () => {
99
- root.style.removeProperty('--cap-shift')
100
- const range = document.createRange()
101
- range.selectNodeContents(text)
102
- const box = range.getBoundingClientRect()
103
- const look = getComputedStyle(root)
104
- const pen = document.createElement('canvas').getContext('2d')
105
- if (!pen || !box.height) return
106
- pen.font = `${look.fontWeight} ${look.fontSize} ${look.fontFamily}`
107
- const m = pen.measureText('H')
108
- const cap = box.top + m.fontBoundingBoxAscent - m.actualBoundingBoxAscent / 2
109
- const r = icons.getBoundingClientRect()
110
- root.style.setProperty('--cap-shift', `${Math.round((cap - r.top - r.height / 2) * 100) / 100}px`)
111
- }
112
- place()
113
- let live = true
114
- document.fonts?.ready.then(() => live && place())
115
- return () => {
116
- live = false
117
- }
118
- }, [showLabel, label, size])
71
+ // With a label, the icon sits on the middle of the label's capital letters (motion/copyButton.ts).
72
+ useLayoutEffect(() => capShift(rootRef.current, iconsRef.current, labelRef.current?.firstChild), [showLabel, label, size])
119
73
 
120
74
  const copy = async () => {
121
75
  const ok = await writeText(value)
@@ -0,0 +1,116 @@
1
+ <script setup lang="ts">
2
+ import { computed, onBeforeUnmount, ref, watch } from 'vue'
3
+ import { CopyIcon, XIcon } from '../../internal/vueIcons'
4
+ import { copyRun, type CopyRun } from '../../internal/motion/liquidMore'
5
+ import { capShift, writeText } from '../../internal/motion/copyButton'
6
+ import styles from './CopyButton.module.css'
7
+
8
+ // The Vue CopyButton: the same look (CopyButton.module.css), drop and check as the React one.
9
+ const props = withDefaults(
10
+ defineProps<{
11
+ /** Text to put on the clipboard. */
12
+ value: string
13
+ /** Accessible name, and the visible text when showLabel is on. */
14
+ label?: string
15
+ /** Shown and spoken after a successful copy. */
16
+ copiedLabel?: string
17
+ /** Shown and spoken when the browser blocks the copy. */
18
+ failedLabel?: string
19
+ /** Shows the label as text next to the icon. */
20
+ showLabel?: boolean
21
+ /** Size of the button. */
22
+ size?: 'sm' | 'md'
23
+ /** How long the check or the failed state stays, in ms. */
24
+ resetAfter?: number
25
+ }>(),
26
+ { label: 'Copy', copiedLabel: 'Copied', failedLabel: 'Could not copy', showLabel: false, size: 'md', resetAfter: 1800 },
27
+ )
28
+ /** Fires after the text is copied, with the copied text. Not fired if copying fails. */
29
+ const emit = defineEmits<{ copy: [value: string] }>()
30
+
31
+ const status = ref<'idle' | 'copied' | 'failed'>('idle')
32
+ let timer: number | undefined
33
+
34
+ const rootEl = ref<HTMLButtonElement | null>(null)
35
+ const iconsEl = ref<HTMLSpanElement | null>(null)
36
+ const labelEl = ref<HTMLSpanElement | null>(null)
37
+ let pressedAt: [number, number] | null = null
38
+ let run: CopyRun | null = null
39
+
40
+ // A copy pours a drop from where it was pressed into the icon (motion/liquidMore.ts); going idle plays it back.
41
+ const copied = computed(() => status.value === 'copied')
42
+ watch(
43
+ copied,
44
+ (now) => {
45
+ const icons = iconsEl.value
46
+ // A failed copy shows its cross at once, with nothing poured.
47
+ if (!icons || status.value === 'failed') {
48
+ run?.stop()
49
+ run = null
50
+ return
51
+ }
52
+ const at = pressedAt
53
+ pressedAt = null
54
+ run = copyRun(icons, now ? 1 : 0, at, styles.pour, run)
55
+ },
56
+ { flush: 'post' },
57
+ )
58
+
59
+ // With a label, the icon sits on the middle of the label's capital letters (motion/copyButton.ts).
60
+ let unplace = () => {}
61
+ watch(
62
+ [() => props.showLabel, () => props.label, () => props.size, labelEl],
63
+ () => {
64
+ unplace()
65
+ unplace = capShift(rootEl.value, iconsEl.value, labelEl.value?.firstChild)
66
+ },
67
+ { flush: 'post' },
68
+ )
69
+
70
+ onBeforeUnmount(() => {
71
+ window.clearTimeout(timer)
72
+ unplace()
73
+ run?.stop()
74
+ })
75
+
76
+ const copy = async () => {
77
+ const ok = await writeText(props.value)
78
+ status.value = ok ? 'copied' : 'failed'
79
+ if (ok) emit('copy', props.value)
80
+ window.clearTimeout(timer)
81
+ timer = window.setTimeout(() => (status.value = 'idle'), props.resetAfter)
82
+ }
83
+
84
+ const onClick = (e: MouseEvent) => {
85
+ const r = iconsEl.value?.getBoundingClientRect()
86
+ // A key press (no pointer) pours from the icon's middle.
87
+ pressedAt = r && e.detail ? [e.clientX - r.left, e.clientY - r.top] : null
88
+ copy()
89
+ }
90
+ </script>
91
+
92
+ <template>
93
+ <button
94
+ ref="rootEl"
95
+ type="button"
96
+ :class="[styles.root, styles[size], showLabel && styles.withLabel]"
97
+ :data-copied="status === 'copied' || undefined"
98
+ :data-failed="status === 'failed' || undefined"
99
+ :aria-label="showLabel ? undefined : label"
100
+ @click="onClick"
101
+ >
102
+ <span ref="iconsEl" :class="styles.icons" aria-hidden="true">
103
+ <CopyIcon :class="styles.copy" />
104
+ <svg viewBox="0 0 24 24" :class="styles.check">
105
+ <path d="M5 12.5l4.5 4.5L19 7.5" pathLength="1" />
106
+ </svg>
107
+ <XIcon :class="styles.fail" />
108
+ </span>
109
+ <span v-if="showLabel" :class="styles.labels">
110
+ <span ref="labelEl" :class="styles.labelIdle">{{ label }}</span>
111
+ <span :class="styles.labelDone" aria-hidden="true">{{ copiedLabel }}</span>
112
+ <span :class="styles.labelFailed" aria-hidden="true">{{ failedLabel }}</span>
113
+ </span>
114
+ <span :class="styles.srOnly" aria-live="polite">{{ status === 'copied' ? copiedLabel : status === 'failed' ? failedLabel : '' }}</span>
115
+ </button>
116
+ </template>
@@ -1,11 +1,12 @@
1
1
  'use client'
2
2
 
3
- import { useEffect, useId, useLayoutEffect, useRef, useState, type ComponentPropsWithRef, type PointerEvent, type ReactNode } from 'react'
3
+ import { useEffect, useId, useLayoutEffect, useRef, useState, type ComponentPropsWithRef, type ReactNode } from 'react'
4
4
  import { cx } from '../../internal/cx'
5
- import { clamp, lastStep, pull, roundTo, steps, wheelSteps, stepKey } from '../../internal/feel'
5
+ import { clamp, pull, roundTo, wheelSteps } from '../../internal/feel'
6
+ import { arcTouch, CX, CY, DOT, dialScale, dialScrub, H, KNOB, polar, SWEEP, W, type Rim } from '../../internal/motion/dial'
6
7
  import { Roll } from '../../internal/Roll'
7
8
  import { useControllable } from '../../internal/useControllable'
8
- import { ink, sideways, tokenNumber, useOnChange, useScrub } from '../../internal/useScrub'
9
+ import { ink, tokenNumber, useOnChange, useScrub } from '../../internal/useScrub'
9
10
  import styles from './Dial.module.css'
10
11
 
11
12
  export type DialProps = Omit<ComponentPropsWithRef<'div'>, 'defaultValue' | 'onChange' | 'children'> & {
@@ -43,41 +44,6 @@ export type DialProps = Omit<ComponentPropsWithRef<'div'>, 'defaultValue' | 'onC
43
44
  haptics?: boolean
44
45
  }
45
46
 
46
- // The printed and mercury dials sweep 270 degrees, with the dead part at the bottom.
47
- const SWEEP = 270
48
-
49
- // Mercury is drawn in this box (SVG units): knob, bump and level dots.
50
- const W = 280
51
- const H = 252
52
- const CX = W / 2
53
- const CY = 128
54
- const KNOB = 58
55
- const DOT = 84
56
- const polar = (deg: number, r: number) => [CX + r * Math.sin((deg * Math.PI) / 180), CY - r * Math.cos((deg * Math.PI) / 180)] as const
57
-
58
- /** A drag around a centre: the change in angle since the last move, never the angle itself, so a press never jumps. */
59
- function turn(unitsPerDeg: number, centre: (r: DOMRect) => { x: number; y: number }) {
60
- return (e: PointerEvent<HTMLElement>, last: { x: number; y: number }, el: HTMLElement) => {
61
- const c = centre(el.getBoundingClientRect())
62
- // Near the centre the angle swings wildly, so it does not count.
63
- if (Math.hypot(e.clientX - c.x, e.clientY - c.y) < 16) return 0
64
- let d = ((Math.atan2(e.clientY - c.y, e.clientX - c.x) - Math.atan2(last.y - c.y, last.x - c.x)) * 180) / Math.PI
65
- if (d > 180) d -= 360
66
- if (d < -180) d += 360
67
- return d * unitsPerDeg * (e.shiftKey ? 0.25 : 1)
68
- }
69
- }
70
-
71
- /** Where a tap sits around a centre, in degrees clockwise from the top, or null too near the centre to tell. */
72
- function angleAt(e: PointerEvent<HTMLElement>, centre: (r: DOMRect) => { x: number; y: number }) {
73
- const c = centre(e.currentTarget.getBoundingClientRect())
74
- if (Math.hypot(e.clientX - c.x, e.clientY - c.y) < 16) return null
75
- return (Math.atan2(e.clientX - c.x, c.y - e.clientY) * 180) / Math.PI
76
- }
77
-
78
- const mercuryCentre = (r: DOMRect) => ({ x: r.left + r.width / 2, y: r.top + (r.height * CY) / H })
79
- const knobCentre = (r: DOMRect) => ({ x: r.left + r.width / 2, y: r.top + r.height / 2 })
80
-
81
47
  export function Dial({
82
48
  label,
83
49
  value: valueProp,
@@ -101,68 +67,18 @@ export function Dial({
101
67
  const labelId = useId()
102
68
  const detailId = useId()
103
69
  const goo = useId().replace(/:/g, '')
104
- const top = lastStep(min, max, step)
70
+ const scale = { min, max, step, marks, variant }
71
+ const { top, all, labelled, land, stepDeg } = dialScale(scale)
105
72
  const [raw, setValue] = useControllable(valueProp, defaultValue ?? min, onValueChange)
106
73
  const value = clamp(raw, min, top)
107
- const all = steps(min, max, step)
108
- const labelled = (marks ?? [min, max]).map((v) => roundTo(v, step, min))
109
74
  const strongSet = new Set(strong.map((v) => roundTo(v, step, min)))
110
- const land = (p: number) => clamp(roundTo(p, step, min), min, top)
111
75
  const markText = formatMark ?? formatValue
112
- const stepDeg = SWEEP / Math.max(1, all.length - 1)
113
76
  // The arc's rim: its radius and the length of one step along it, both tokens.
114
- const [rim, setRim] = useState<{ r: number; gap: number } | null>(null)
77
+ const [rim, setRim] = useState<Rim | null>(null)
115
78
  const wheel = useRef(wheelSteps())
116
79
 
117
- const sc = useScrub({
118
- value,
119
- min,
120
- max: top,
121
- edge: step * (variant === 'arc' ? 0.8 : variant === 'mercury' ? 0.12 : 0.6),
122
- land,
123
- coast: variant === 'arc' ? 0.2 : 0,
124
- maxSpeed: 160 * step,
125
- move:
126
- variant === 'arc'
127
- ? (e, last) => (-(e.clientX - last.x) / (rim?.gap || 1)) * step * (e.shiftKey ? 0.25 : 1)
128
- : variant === 'mercury'
129
- ? turn(step / stepDeg, mercuryCentre)
130
- : turn((max - min) / SWEEP, knobCentre),
131
- key: (k, t) => stepKey(k, t, { min, max, step, marks: labelled }),
132
- // A tap without a drag goes to the mark under it, so the dial works with one plain press.
133
- tap: (e, at) => {
134
- if (variant === 'arc') {
135
- if (!rim?.gap || !rim.r) return null
136
- const r = e.currentTarget.getBoundingClientRect()
137
- // The rim curves, so the tap's sideways distance becomes a length along the arc.
138
- const along = rim.r * Math.asin(clamp((e.clientX - (r.left + r.width / 2)) / rim.r, -1, 1))
139
- return land(at + (along / rim.gap) * step)
140
- }
141
- if (variant === 'mercury') {
142
- const a = angleAt(e, mercuryCentre)
143
- // Past either end of the sweep, land clamps to the nearer end (the dead part is split at the bottom).
144
- return a === null ? null : land(min + ((a + 135) / stepDeg) * step)
145
- }
146
- const a = angleAt(e, knobCentre)
147
- if (a === null) return null
148
- // The printed face turns, so the tap is read on the face: min at 0 and max 270 degrees back from it.
149
- const back = (((((at - min) / (max - min || 1)) * SWEEP - a) % 360) + 360) % 360
150
- if (back > SWEEP) return back < SWEEP + (360 - SWEEP) / 2 ? top : min
151
- return land(min + (back / SWEEP) * (max - min))
152
- },
153
- wheel:
154
- variant === 'arc'
155
- ? (e, t) => {
156
- const dx = sideways(e)
157
- if (dx === null || !rim?.gap) return null
158
- const n = wheel.current(dx, rim.gap, e.timeStamp)
159
- const to = clamp(roundTo(t + n * step, step, min), min, top)
160
- return to === t && (t === min || t === top) ? null : to
161
- }
162
- : undefined,
163
- onSettle: setValue,
164
- haptics,
165
- })
80
+ // The drag, keys, tap and wheel (motion/dial.ts) on the shared scrubber.
81
+ const sc = useScrub({ value, ...dialScrub(scale, rim, wheel.current), onSettle: setValue, haptics })
166
82
 
167
83
  useLayoutEffect(() => {
168
84
  if (variant !== 'arc') return
@@ -170,35 +86,11 @@ export function Dial({
170
86
  setRim({ r: tokenNumber(el, '--gooee-dial-arc-radius'), gap: tokenNumber(el, '--gooee-dial-arc-gap') })
171
87
  }, [variant, sc.ref])
172
88
 
173
- // The arc lets an up-and-down swipe scroll the page, but a finger following the curve also moves up or
174
- // down. Once a touch moves more sideways than half its rise, it is a turn: the page stops taking it.
89
+ // The arc takes only sideways touches, so the page still scrolls under a finger on it (motion/dial.ts).
175
90
  useEffect(() => {
176
- if (variant !== 'arc') return
177
91
  const el = sc.ref.current
178
- if (!el) return
179
- let from: { x: number; y: number } | null = null
180
- let turning = false
181
- const start = (e: TouchEvent) => {
182
- from = { x: e.touches[0].clientX, y: e.touches[0].clientY }
183
- turning = false
184
- }
185
- const move = (e: TouchEvent) => {
186
- if (!from) return
187
- if (!turning) {
188
- const dx = Math.abs(e.touches[0].clientX - from.x)
189
- const dy = Math.abs(e.touches[0].clientY - from.y)
190
- if (dx + dy < 2) return
191
- turning = dx > dy / 2
192
- if (!turning) from = null
193
- }
194
- if (turning && e.cancelable) e.preventDefault()
195
- }
196
- el.addEventListener('touchstart', start, { passive: true })
197
- el.addEventListener('touchmove', move, { passive: false })
198
- return () => {
199
- el.removeEventListener('touchstart', start)
200
- el.removeEventListener('touchmove', move)
201
- }
92
+ if (variant !== 'arc' || !el) return
93
+ return arcTouch(el)
202
94
  }, [variant, sc.ref])
203
95
 
204
96
  const shown = land(sc.at)
@@ -0,0 +1,228 @@
1
+ <script setup lang="ts">
2
+ import { computed, onBeforeUnmount, onMounted, ref, useId, useSlots, watch } from 'vue'
3
+ import { clamp, pull, roundTo, wheelSteps } from '../../internal/feel'
4
+ import { arcTouch, CX, CY, DOT, dialScale, dialScrub, H, KNOB, polar, SWEEP, W, type Rim } from '../../internal/motion/dial'
5
+ import { ink, tokenNumber } from '../../internal/motion/scrubLook'
6
+ import Roll from '../../internal/Roll.vue'
7
+ import { useScrub } from '../../internal/vue/useScrub'
8
+ import styles from './Dial.module.css'
9
+
10
+ // The Vue Dial: the same look (Dial.module.css), turn and landing as the React one.
11
+
12
+ const props = withDefaults(
13
+ defineProps<{
14
+ /** Visible name of the dial, also read by screen readers. */
15
+ label: string
16
+ /** Starting value when there is no v-model. Undefined starts at min. */
17
+ defaultValue?: number
18
+ /** Lowest value. */
19
+ min?: number
20
+ /** Highest value. */
21
+ max?: number
22
+ /** Size of one step. Every step is a mark the dial is drawn onto. */
23
+ step?: number
24
+ /** Values with a printed label. Page Up and Page Down jump between them. Defaults to min and max. */
25
+ marks?: number[]
26
+ /** Values drawn stronger, such as the usual setting. */
27
+ strong?: number[]
28
+ /** "printed": the scale is printed on the knob and turns under a fixed index. "mercury": a solid knob with a few named levels. "arc": the rim of a large dial, swiped sideways. */
29
+ variant?: 'printed' | 'mercury' | 'arc'
30
+ /** Turns the value into the readout and what screen readers hear. */
31
+ formatValue?: (value: number) => string
32
+ /** Turns a mark into its printed label. Defaults to formatValue. */
33
+ formatMark?: (value: number) => string
34
+ /** A line under the readout, such as what the value means. The detail slot replaces it. */
35
+ detail?: string
36
+ /** A double click goes back to this value. */
37
+ resetValue?: number
38
+ /** A short buzz on each step while it turns, on phones that can (Android). */
39
+ haptics?: boolean
40
+ }>(),
41
+ { min: 0, max: 100, step: 1, strong: () => [], variant: 'printed', formatValue: String, haptics: false },
42
+ )
43
+ const emit = defineEmits<{ /** The value under the index while the dial still turns. */ scrub: [value: number] }>()
44
+ const slots = useSlots()
45
+
46
+ /** The value, set once the dial comes to rest. Use v-model, or leave it out and the dial keeps its own value. */
47
+ const model = defineModel<number>()
48
+ const local = ref(props.defaultValue ?? props.min)
49
+ const raw = computed({
50
+ get: () => model.value ?? local.value,
51
+ set: (next: number) => {
52
+ local.value = next
53
+ model.value = next
54
+ },
55
+ })
56
+
57
+ const labelId = useId()
58
+ const detailId = useId()
59
+ const goo = useId().replace(/:/g, '')
60
+ const scale = computed(() => ({ min: props.min, max: props.max, step: props.step, marks: props.marks, variant: props.variant }))
61
+ const dial = computed(() => dialScale(scale.value))
62
+ const value = computed(() => clamp(raw.value, props.min, dial.value.top))
63
+ const strongSet = computed(() => new Set(props.strong.map((v) => roundTo(v, props.step, props.min))))
64
+ const markText = (v: number) => (props.formatMark ?? props.formatValue)(v)
65
+ // The arc's rim: its radius and the length of one step along it, both tokens.
66
+ const rim = ref<Rim | null>(null)
67
+ const wheel = wheelSteps()
68
+
69
+ // The drag, keys, tap and wheel (motion/dial.ts) on the shared scrubber.
70
+ const sc = useScrub(() => ({ value: value.value, ...dialScrub(scale.value, rim.value, wheel), onSettle: (v: number) => (raw.value = v), haptics: props.haptics }))
71
+ const scrubEl = sc.el
72
+
73
+ // The arc takes only sideways touches, so the page still scrolls under a finger on it (motion/dial.ts).
74
+ let stopTouch = () => {}
75
+ const measureRim = () => {
76
+ stopTouch()
77
+ stopTouch = () => {}
78
+ if (props.variant !== 'arc' || !scrubEl.value) return
79
+ rim.value = { r: tokenNumber(scrubEl.value, '--gooee-dial-arc-radius'), gap: tokenNumber(scrubEl.value, '--gooee-dial-arc-gap') }
80
+ stopTouch = arcTouch(scrubEl.value)
81
+ }
82
+ onMounted(measureRim)
83
+ watch(() => props.variant, measureRim, { flush: 'post' })
84
+ onBeforeUnmount(() => stopTouch())
85
+
86
+ const shown = computed(() => dial.value.land(sc.at.value))
87
+ watch(shown, (v) => emit('scrub', v))
88
+
89
+ const hasDetail = computed(() => !!(props.detail || slots.detail))
90
+ const index = computed(() => dial.value.all.indexOf(value.value))
91
+ const sliderAttrs = computed(() => ({
92
+ role: 'slider',
93
+ tabindex: 0,
94
+ 'aria-labelledby': labelId,
95
+ 'aria-describedby': hasDetail.value ? detailId : undefined,
96
+ 'aria-valuemin': props.min,
97
+ 'aria-valuemax': dial.value.top,
98
+ 'aria-valuenow': value.value,
99
+ 'aria-valuetext': props.variant === 'mercury' && index.value >= 0 ? `${props.formatValue(value.value)}, ${index.value + 1} of ${dial.value.all.length}` : props.formatValue(value.value),
100
+ 'data-moving': sc.moving.value || undefined,
101
+ }))
102
+ const onDblclick = () => props.resetValue !== undefined && sc.go(dial.value.land(props.resetValue))
103
+
104
+ // Mercury: the bump on the knob and each level's dot and drop.
105
+ const mercury = computed(() => {
106
+ const { all, stepDeg } = dial.value
107
+ const at = sc.at.value
108
+ const [bx, by] = polar(-135 + ((at - props.min) / props.step) * stepDeg, KNOB)
109
+ const levels = all.map((v, i) => {
110
+ const a = -135 + i * stepDeg
111
+ const [x, y] = polar(a, DOT)
112
+ const [lx, ly] = polar(a, DOT + 22)
113
+ const p = pull(at - v, props.step)
114
+ const [dx, dy] = polar(a, DOT - 12 * p)
115
+ return { v, x, y, lx, ly, anchor: Math.abs(a) < 1 ? 'middle' : a < 0 ? 'end' : 'start', on: v === shown.value, dx, dy, dr: p * 9 }
116
+ })
117
+ return { bx, by, levels }
118
+ })
119
+
120
+ // Arc: the marks near the index, each leaning in by its pull.
121
+ const arcMarks = computed(() => {
122
+ const r = rim.value
123
+ const degPerStep = r && r.r ? (r.gap / r.r) * (180 / Math.PI) : 0
124
+ if (degPerStep <= 0) return []
125
+ const at = sc.at.value
126
+ return dial.value.all.flatMap((v, i) => {
127
+ const a = ((v - at) / props.step) * degPerStep
128
+ if (Math.abs(a) > 22) return []
129
+ const p = pull(at - v, props.step * 1.2)
130
+ const tier = dial.value.labelled.includes(v) ? 'major' : i % 2 === 0 ? 'mid' : 'minor'
131
+ return [{ v, a, p, tier }]
132
+ })
133
+ })
134
+
135
+ // Printed: the face turns under the index.
136
+ const printed = computed(() => {
137
+ const { min, max, step, marks } = props
138
+ const { all, labelled, top } = dial.value
139
+ const turned = ((sc.at.value - min) / (max - min || 1)) * SWEEP
140
+ // The face prints min, the middle and max unless marks say otherwise; the keys still jump by marks.
141
+ const nums = marks ? labelled : [...new Set([min, roundTo((min + max) / 2, step, min), top])]
142
+ // A longer tick every ten steps on a long scale, so the amount can be counted at a glance.
143
+ const tenth = all.length > 20 ? step * 10 : 0
144
+ const isMajor = (v: number) => nums.includes(v) || (tenth > 0 && Math.abs((v - min) / tenth - Math.round((v - min) / tenth)) < 1e-6)
145
+ // Every step gets a tick while they fit round the knob (thinner past 49); past that only the long ones.
146
+ const dense = all.length > 49
147
+ const ticks = (all.length <= 121 ? all : all.filter((v) => isMajor(v) || strongSet.value.has(v))).map((v) => {
148
+ const at = (-(v - min) / (max - min || 1)) * SWEEP
149
+ const major = isMajor(v)
150
+ return { v, at, p: pull(sc.at.value - v, step), major, fine: dense && !major, num: nums.includes(v) }
151
+ })
152
+ return { turned, ticks }
153
+ })
154
+ </script>
155
+
156
+ <template>
157
+ <div :class="[styles.root, styles[variant]]">
158
+ <div :class="styles.head">
159
+ <span :id="labelId" :class="styles.label">{{ label }}</span>
160
+ <Roll :text="formatValue(shown)" :class="styles.readout" :still="sc.moving.value" />
161
+ <span v-if="hasDetail" :id="detailId" :class="styles.detail"><slot name="detail">{{ detail }}</slot></span>
162
+ </div>
163
+ <div v-if="variant === 'mercury'" ref="scrubEl" :class="styles.mercuryHit" v-bind="sliderAttrs" v-on="sc.handlers" @dblclick="onDblclick">
164
+ <svg :viewBox="`0 0 ${W} ${H}`" :class="styles.mercurySvg" aria-hidden="true">
165
+ <defs>
166
+ <filter :id="goo" x="-20%" y="-20%" width="140%" height="140%">
167
+ <feGaussianBlur in="SourceGraphic" stdDeviation="5" result="blur" />
168
+ <feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 22 -9" result="goo" />
169
+ <feComposite in="SourceGraphic" in2="goo" operator="atop" />
170
+ </filter>
171
+ </defs>
172
+ <g v-for="l in mercury.levels" :key="l.v">
173
+ <circle :cx="l.x" :cy="l.y" :r="3.5" :class="styles.dot" :data-on="l.on || undefined" />
174
+ <text :x="l.lx" :y="l.ly" :text-anchor="l.anchor" dominant-baseline="middle" :class="styles.level" :data-on="l.on || undefined">{{ markText(l.v) }}</text>
175
+ </g>
176
+ <g :filter="`url(#${goo})`" :class="styles.metal">
177
+ <circle :cx="CX" :cy="CY" :r="KNOB" />
178
+ <circle :cx="mercury.bx" :cy="mercury.by" :r="10" />
179
+ <circle v-for="l in mercury.levels" :key="l.v" :cx="l.dx" :cy="l.dy" :r="l.dr" />
180
+ </g>
181
+ <circle :cx="CX" :cy="CY" :r="KNOB - 14" :class="styles.groove" />
182
+ </svg>
183
+ </div>
184
+ <div v-else-if="variant === 'arc'" ref="scrubEl" :class="styles.arcStage" v-bind="sliderAttrs" v-on="sc.handlers" @dblclick="onDblclick">
185
+ <span :class="styles.arcIndex" aria-hidden="true" />
186
+ <div :class="styles.arcClip" aria-hidden="true">
187
+ <div :class="styles.hub">
188
+ <span :class="styles.disc" />
189
+ <span v-for="m in arcMarks" :key="m.v" :class="styles.arcMark" :style="{ transform: `rotate(${m.a}deg) translateY(calc(var(--gooee-dial-arc-radius) * -1))` }">
190
+ <i :class="styles.arcTick" :data-tier="m.tier" :style="{ background: ink(m.p, 'var(--gooee-color-border-strong)', 'var(--gooee-color-accent)'), transform: `scaleY(${1 + m.p * 0.7})` }" />
191
+ <span v-if="m.tier === 'major'" :class="styles.arcNum" :style="{ color: ink(m.p), transform: `translateX(-50%) scale(${1 + m.p * 0.3})` }">{{ markText(m.v) }}</span>
192
+ <span v-if="strongSet.has(m.v)" :class="styles.arcStrong" />
193
+ </span>
194
+ </div>
195
+ </div>
196
+ </div>
197
+ <div v-else :class="styles.bezel">
198
+ <span :class="styles.index" aria-hidden="true" />
199
+ <!-- How much: the ring fills from min (where the face has turned it) up to the index. -->
200
+ <svg :class="styles.fill" aria-hidden="true">
201
+ <!-- A negative dash length is invalid and draws the whole ring, so the arc is clamped and left out at min. -->
202
+ <circle v-if="printed.turned > 0.5" :pathLength="360" :stroke-dasharray="`${Math.min(printed.turned, 360)} 360`" />
203
+ </svg>
204
+ <div ref="scrubEl" :class="styles.knob" v-bind="sliderAttrs" v-on="sc.handlers" @dblclick="onDblclick">
205
+ <div :class="styles.face" :style="{ transform: `rotate(${printed.turned}deg)` }" aria-hidden="true">
206
+ <span v-for="t in printed.ticks" :key="t.v" :class="styles.spoke" :style="{ transform: `rotate(${t.at}deg)` }">
207
+ <i
208
+ :class="styles.tick"
209
+ :data-major="t.major || undefined"
210
+ :data-fine="t.fine || undefined"
211
+ :data-strong="strongSet.has(t.v) || undefined"
212
+ :data-current="t.v === shown || undefined"
213
+ :style="{ background: ink(t.p, 'var(--gooee-color-border-strong)', 'var(--gooee-color-accent)'), transform: `scaleY(${1 + t.p * 0.5})` }"
214
+ />
215
+ <span v-if="t.num" :class="styles.numAt" :style="{ transform: `translateY(var(--num-r)) rotate(${-(t.at + printed.turned)}deg)` }">
216
+ <span :class="styles.num" :style="{ color: ink(t.p), transform: `translate(-50%, -50%) scale(${1 + t.p * 0.45})`, fontWeight: `calc(var(--gooee-weight-medium) + (var(--gooee-weight-semibold) - var(--gooee-weight-medium)) * ${t.p.toFixed(3)})` }">{{
217
+ markText(t.v)
218
+ }}</span>
219
+ </span>
220
+ </span>
221
+ <span :class="styles.cap" />
222
+ </div>
223
+ <!-- The grip: an indent on the cap that points at the value under the index, so it reads as a knob to turn. -->
224
+ <span :class="styles.grip" aria-hidden="true" />
225
+ </div>
226
+ </div>
227
+ </div>
228
+ </template>