gooee 0.50.0 → 0.51.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -4
- package/package.json +5 -3
- package/scripts/add.js +28 -6
- package/src/components/Accordion/Accordion.vue +62 -0
- package/src/components/Accordion/AccordionItem.vue +45 -0
- package/src/components/Accordion/accordionContext.ts +6 -0
- package/src/components/Alert/Alert.tsx +7 -27
- package/src/components/Alert/Alert.vue +59 -0
- package/src/components/AlertDialog/AlertDialog.vue +126 -0
- package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
- package/src/components/AspectRatio/AspectRatio.vue +17 -0
- package/src/components/Avatar/Avatar.vue +56 -0
- package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
- package/src/components/Badge/Badge.vue +32 -0
- package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
- package/src/components/Button/Button.vue +130 -0
- package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
- package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
- package/src/components/Card/Card.vue +34 -0
- package/src/components/Center/Center.vue +32 -0
- package/src/components/Checkbox/Checkbox.tsx +8 -25
- package/src/components/Checkbox/Checkbox.vue +108 -0
- package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
- package/src/components/Cluster/Cluster.vue +34 -0
- package/src/components/CodeBlock/CodeBlock.vue +45 -0
- package/src/components/Collapsible/Collapsible.vue +59 -0
- package/src/components/Container/Container.vue +22 -0
- package/src/components/CopyButton/CopyButton.tsx +3 -49
- package/src/components/CopyButton/CopyButton.vue +116 -0
- package/src/components/Dial/Dial.tsx +12 -120
- package/src/components/Dial/Dial.vue +228 -0
- package/src/components/Dialog/Dialog.vue +139 -0
- package/src/components/Divider/Divider.vue +27 -0
- package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
- package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
- package/src/components/EmptyState/EmptyState.vue +33 -0
- package/src/components/Fab/Fab.tsx +4 -14
- package/src/components/Fab/Fab.vue +115 -0
- package/src/components/Fieldset/Fieldset.vue +42 -0
- package/src/components/FormField/FormField.tsx +2 -17
- package/src/components/FormField/FormField.vue +64 -0
- package/src/components/Grid/Grid.vue +43 -0
- package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -143
- package/src/components/HoldToConfirm/HoldToConfirm.vue +281 -0
- package/src/components/IconButton/IconButton.vue +25 -0
- package/src/components/Input/Input.vue +112 -0
- package/src/components/InputGroup/InputGroup.vue +102 -0
- package/src/components/Kbd/Kbd.tsx +2 -12
- package/src/components/Kbd/Kbd.vue +21 -0
- package/src/components/Link/Link.vue +38 -0
- package/src/components/LivePill/LivePill.tsx +11 -39
- package/src/components/LivePill/LivePill.vue +152 -0
- package/src/components/Meter/Meter.vue +88 -0
- package/src/components/MorphButton/MorphButton.tsx +11 -67
- package/src/components/MorphButton/MorphButton.vue +120 -0
- package/src/components/NumberInput/NumberInput.tsx +11 -48
- package/src/components/NumberInput/NumberInput.vue +184 -0
- package/src/components/Pagination/Pagination.tsx +2 -15
- package/src/components/Pagination/Pagination.vue +87 -0
- package/src/components/Pagination/range.ts +15 -0
- package/src/components/PasswordInput/PasswordInput.vue +53 -0
- package/src/components/PickerWheel/DateWheel.vue +48 -0
- package/src/components/PickerWheel/PickerWheel.tsx +15 -129
- package/src/components/PickerWheel/PickerWheel.vue +77 -0
- package/src/components/PickerWheel/TimeWheel.vue +50 -0
- package/src/components/PickerWheel/WheelColumn.vue +70 -0
- package/src/components/Popover/Popover.vue +108 -0
- package/src/components/ProgressBar/ProgressBar.vue +48 -0
- package/src/components/Prose/Prose.vue +18 -0
- package/src/components/Radio/Radio.vue +49 -0
- package/src/components/Radio/RadioGroup.vue +69 -0
- package/src/components/Retime/Retime.tsx +7 -40
- package/src/components/Retime/Retime.vue +64 -0
- package/src/components/RulerScrubber/RulerScrubber.tsx +9 -38
- package/src/components/RulerScrubber/RulerScrubber.vue +166 -0
- package/src/components/ScrollArea/ScrollArea.tsx +17 -129
- package/src/components/ScrollArea/ScrollArea.vue +58 -0
- package/src/components/SearchInput/SearchInput.tsx +2 -15
- package/src/components/SearchInput/SearchInput.vue +70 -0
- package/src/components/SegmentedControl/SegmentedControl.tsx +7 -51
- package/src/components/SegmentedControl/SegmentedControl.vue +111 -0
- package/src/components/Select/Select.tsx +16 -47
- package/src/components/Select/Select.vue +236 -0
- package/src/components/Sheet/Sheet.tsx +52 -231
- package/src/components/Sheet/Sheet.vue +249 -0
- package/src/components/Skeleton/Skeleton.vue +38 -0
- package/src/components/Slider/Slider.tsx +25 -84
- package/src/components/Slider/Slider.vue +113 -0
- package/src/components/Spinner/Spinner.vue +30 -0
- package/src/components/Split/Split.vue +34 -0
- package/src/components/SplitFlap/SplitFlap.tsx +11 -175
- package/src/components/SplitFlap/SplitFlap.vue +52 -0
- package/src/components/Stack/Stack.vue +22 -0
- package/src/components/SwipeDeck/SwipeDeck.tsx +30 -194
- package/src/components/SwipeDeck/SwipeDeck.vue +168 -0
- package/src/components/Switch/Switch.tsx +6 -75
- package/src/components/Switch/Switch.vue +66 -0
- package/src/components/Table/Table.module.css +12 -3
- package/src/components/Table/Table.tsx +8 -59
- package/src/components/Table/Table.vue +274 -0
- package/src/components/TableOfContents/TableOfContents.vue +90 -0
- package/src/components/Tabs/Tab.vue +37 -0
- package/src/components/Tabs/TabList.vue +47 -0
- package/src/components/Tabs/TabPanel.vue +26 -0
- package/src/components/Tabs/Tabs.module.css +2 -2
- package/src/components/Tabs/Tabs.tsx +11 -84
- package/src/components/Tabs/Tabs.vue +33 -0
- package/src/components/Tabs/tabsContext.ts +15 -0
- package/src/components/Tag/Tag.tsx +6 -26
- package/src/components/Tag/Tag.vue +63 -0
- package/src/components/Textarea/Textarea.tsx +2 -22
- package/src/components/Textarea/Textarea.vue +120 -0
- package/src/components/ThemeToggle/ThemeToggle.tsx +10 -84
- package/src/components/ThemeToggle/ThemeToggle.vue +90 -0
- package/src/components/Toast/Toast.tsx +34 -264
- package/src/components/Toast/Toast.vue +138 -0
- package/src/components/Toast/ToastItem.vue +108 -0
- package/src/components/Toggle/Toggle.vue +52 -0
- package/src/components/Tooltip/Tooltip.tsx +17 -48
- package/src/components/Tooltip/Tooltip.vue +95 -0
- package/src/components/VisuallyHidden/VisuallyHidden.vue +17 -0
- package/src/internal/FieldMessage.vue +41 -0
- package/src/internal/Roll.vue +20 -0
- package/src/internal/focus.ts +4 -47
- package/src/internal/icons.module.css +39 -27
- package/src/internal/layers.ts +7 -61
- package/src/internal/motion/alert.ts +17 -0
- package/src/internal/motion/buttonGroup.ts +21 -0
- package/src/internal/motion/checkbox.ts +39 -0
- package/src/internal/motion/collapseOut.ts +26 -0
- package/src/internal/motion/copyButton.ts +48 -0
- package/src/internal/motion/dial.ts +137 -0
- package/src/internal/motion/fab.ts +14 -0
- package/src/internal/motion/floating.ts +58 -0
- package/src/internal/motion/focus.ts +53 -0
- package/src/internal/motion/formField.ts +19 -0
- package/src/internal/motion/holdToConfirm.ts +147 -0
- package/src/internal/motion/keys.ts +42 -0
- package/src/internal/motion/layers.ts +78 -0
- package/src/internal/motion/listbox.ts +69 -0
- package/src/internal/motion/livePill.ts +71 -0
- package/src/internal/motion/menuKeys.ts +58 -0
- package/src/internal/motion/morphButton.ts +71 -0
- package/src/internal/motion/numberInput.ts +57 -0
- package/src/internal/motion/pickerWheel.ts +166 -0
- package/src/internal/motion/retime.ts +45 -0
- package/src/internal/motion/ruler.ts +57 -0
- package/src/internal/motion/scrollArea.ts +133 -0
- package/src/internal/motion/scrubLook.ts +15 -0
- package/src/internal/motion/segmented.ts +63 -0
- package/src/internal/motion/sheetDetents.ts +271 -0
- package/src/internal/motion/slider.ts +108 -0
- package/src/internal/motion/splitFlap.ts +205 -0
- package/src/internal/motion/swipeDeck.ts +236 -0
- package/src/internal/motion/switchThumb.ts +83 -0
- package/src/internal/motion/table.ts +77 -0
- package/src/internal/motion/tabs.ts +105 -0
- package/src/internal/motion/textarea.ts +22 -0
- package/src/internal/motion/themeIcon.ts +91 -0
- package/src/internal/motion/themeReveal.ts +71 -0
- package/src/internal/motion/toast.ts +291 -0
- package/src/internal/motion/tooltip.ts +58 -0
- package/src/internal/themeReveal.ts +3 -61
- package/src/internal/useFloating.ts +9 -49
- package/src/internal/useScrub.ts +1 -13
- package/src/internal/vue/trigger.ts +18 -0
- package/src/internal/vue/useFloating.ts +38 -0
- package/src/internal/vue/usePresence.ts +21 -0
- package/src/internal/vue/useScrub.ts +40 -0
- package/src/internal/vueIcons.ts +134 -0
- package/src/tokens.css +5 -0
- package/src/internal/relativeTime.ts +0 -26
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
|
3
|
+
import { createScrollArea } from '../../internal/motion/scrollArea'
|
|
4
|
+
import styles from './ScrollArea.module.css'
|
|
5
|
+
|
|
6
|
+
// The Vue ScrollArea: the same look (ScrollArea.module.css) and drawn scrollbar as the React one.
|
|
7
|
+
|
|
8
|
+
const props = withDefaults(
|
|
9
|
+
defineProps<{
|
|
10
|
+
/** Name of the scrollable region. Required, because the region takes keyboard focus. */
|
|
11
|
+
label: string
|
|
12
|
+
/** A number in pixels, or any CSS length such as "50vh". */
|
|
13
|
+
maxHeight?: number | string
|
|
14
|
+
/** Which ways the content can scroll, and which scrollbars show. */
|
|
15
|
+
orientation?: 'vertical' | 'horizontal' | 'both'
|
|
16
|
+
}>(),
|
|
17
|
+
{ orientation: 'vertical' },
|
|
18
|
+
)
|
|
19
|
+
defineSlots<{ default?: () => unknown }>()
|
|
20
|
+
|
|
21
|
+
const root = ref<HTMLElement | null>(null)
|
|
22
|
+
const viewport = ref<HTMLElement | null>(null)
|
|
23
|
+
const thumbY = ref<HTMLElement | null>(null)
|
|
24
|
+
const thumbX = ref<HTMLElement | null>(null)
|
|
25
|
+
const axes = computed(() => [props.orientation !== 'horizontal' && ('y' as const), props.orientation !== 'vertical' && ('x' as const)].filter((a) => a !== false))
|
|
26
|
+
const height = computed(() => (typeof props.maxHeight === 'number' ? `${props.maxHeight}px` : props.maxHeight))
|
|
27
|
+
|
|
28
|
+
// The thumbs, their idle fade and drags (motion/scrollArea.ts): direct style writes, so scrolling never re-renders.
|
|
29
|
+
const area = createScrollArea({ root: () => root.value, viewport: () => viewport.value, thumb: (axis) => (axis === 'y' ? thumbY.value : thumbX.value) })
|
|
30
|
+
let stop = () => {}
|
|
31
|
+
onMounted(() => (stop = area.observe()))
|
|
32
|
+
onBeforeUnmount(() => {
|
|
33
|
+
stop()
|
|
34
|
+
area.destroy()
|
|
35
|
+
})
|
|
36
|
+
const setThumb = (axis: 'x' | 'y') => (el: unknown) => {
|
|
37
|
+
if (axis === 'y') thumbY.value = el as HTMLElement | null
|
|
38
|
+
else thumbX.value = el as HTMLElement | null
|
|
39
|
+
}
|
|
40
|
+
</script>
|
|
41
|
+
|
|
42
|
+
<template>
|
|
43
|
+
<div ref="root" :class="styles.root" :data-orientation="orientation" @pointerenter="area.wake" @pointermove="area.wake">
|
|
44
|
+
<div ref="viewport" :class="styles.viewport" :style="{ maxHeight: height }" role="region" :aria-label="label" tabindex="0" @scroll="area.onScroll">
|
|
45
|
+
<div :class="styles.content"><slot /></div>
|
|
46
|
+
</div>
|
|
47
|
+
<div v-for="axis in axes" :key="axis" :class="styles.track" :data-axis="axis" aria-hidden="true" @pointerdown="area.trackDown(axis, $event)">
|
|
48
|
+
<div
|
|
49
|
+
:ref="setThumb(axis)"
|
|
50
|
+
:class="styles.thumb"
|
|
51
|
+
@pointerdown="area.thumbDown(axis, $event)"
|
|
52
|
+
@pointermove="area.thumbMove"
|
|
53
|
+
@pointerup="area.thumbUp"
|
|
54
|
+
@pointercancel="area.thumbUp"
|
|
55
|
+
/>
|
|
56
|
+
</div>
|
|
57
|
+
</div>
|
|
58
|
+
</template>
|
|
@@ -4,6 +4,7 @@ import { useEffect, useRef } from 'react'
|
|
|
4
4
|
import { cx } from '../../internal/cx'
|
|
5
5
|
import { SearchIcon, XIcon } from '../../internal/icons'
|
|
6
6
|
import { mergeRefs } from '../../internal/mergeRefs'
|
|
7
|
+
import { shortcutFocus } from '../../internal/motion/keys'
|
|
7
8
|
import { useControllable } from '../../internal/useControllable'
|
|
8
9
|
import { Input, type InputProps } from '../Input'
|
|
9
10
|
import { Kbd } from '../Kbd'
|
|
@@ -24,9 +25,6 @@ export type SearchInputProps = Omit<InputProps, 'type' | 'leading' | 'trailing'
|
|
|
24
25
|
clearLabel?: string
|
|
25
26
|
}
|
|
26
27
|
|
|
27
|
-
const isTyping = (el: Element | null) =>
|
|
28
|
-
!!el && (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement || el instanceof HTMLSelectElement || (el as HTMLElement).isContentEditable)
|
|
29
|
-
|
|
30
28
|
export function SearchInput({
|
|
31
29
|
value: valueProp,
|
|
32
30
|
defaultValue = '',
|
|
@@ -42,20 +40,9 @@ export function SearchInput({
|
|
|
42
40
|
const [value, setValue] = useControllable(valueProp, defaultValue, onValueChange)
|
|
43
41
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
44
42
|
|
|
45
|
-
// Ignored while another field has focus, so it never steals typed text, and while a modal
|
|
46
|
-
// this field is not in is open, so focus never leaves the modal.
|
|
47
43
|
useEffect(() => {
|
|
48
44
|
if (!shortcut) return
|
|
49
|
-
|
|
50
|
-
if (e.key !== shortcut || e.metaKey || e.ctrlKey || e.altKey || isTyping(document.activeElement)) return
|
|
51
|
-
const modals = document.querySelectorAll('[aria-modal="true"]')
|
|
52
|
-
if (modals.length && !modals[modals.length - 1].contains(inputRef.current)) return
|
|
53
|
-
e.preventDefault()
|
|
54
|
-
inputRef.current?.focus()
|
|
55
|
-
inputRef.current?.select()
|
|
56
|
-
}
|
|
57
|
-
window.addEventListener('keydown', onKey)
|
|
58
|
-
return () => window.removeEventListener('keydown', onKey)
|
|
45
|
+
return shortcutFocus(shortcut, () => inputRef.current)
|
|
59
46
|
}, [shortcut])
|
|
60
47
|
|
|
61
48
|
return (
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
3
|
+
import { SearchIcon, XIcon } from '../../internal/vueIcons'
|
|
4
|
+
import { shortcutFocus } from '../../internal/motion/keys'
|
|
5
|
+
import Input from '../Input/Input.vue'
|
|
6
|
+
import Kbd from '../Kbd/Kbd.vue'
|
|
7
|
+
import styles from './SearchInput.module.css'
|
|
8
|
+
|
|
9
|
+
// The Vue SearchInput: Input with a search icon, a clear button and an optional key shortcut, like the React one.
|
|
10
|
+
// Other attributes, such as label, size or placeholder, go to the Input.
|
|
11
|
+
const props = withDefaults(
|
|
12
|
+
defineProps<{
|
|
13
|
+
/** Always required. A placeholder is only an example, never the label. */
|
|
14
|
+
label: string
|
|
15
|
+
/** A single key, such as "/". Pressing it anywhere on the page focuses the field. */
|
|
16
|
+
shortcut?: string
|
|
17
|
+
/** Screen reader name for the clear button. */
|
|
18
|
+
clearLabel?: string
|
|
19
|
+
}>(),
|
|
20
|
+
{ clearLabel: 'Clear search' },
|
|
21
|
+
)
|
|
22
|
+
/** The search text. Use v-model, or leave it out and the field keeps its own text. Also changes on clear. */
|
|
23
|
+
const model = defineModel<string>({ default: '' })
|
|
24
|
+
const emit = defineEmits<{ search: [value: string] }>()
|
|
25
|
+
|
|
26
|
+
const root = ref<InstanceType<typeof Input> | null>(null)
|
|
27
|
+
const field = () => root.value?.input ?? null
|
|
28
|
+
|
|
29
|
+
let stop = () => {}
|
|
30
|
+
onMounted(() =>
|
|
31
|
+
watch(
|
|
32
|
+
() => props.shortcut,
|
|
33
|
+
(key) => {
|
|
34
|
+
stop()
|
|
35
|
+
stop = key ? shortcutFocus(key, field) : () => {}
|
|
36
|
+
},
|
|
37
|
+
{ immediate: true },
|
|
38
|
+
),
|
|
39
|
+
)
|
|
40
|
+
onBeforeUnmount(() => stop())
|
|
41
|
+
|
|
42
|
+
const onKeydown = (e: KeyboardEvent) => {
|
|
43
|
+
// Enter during IME composition picks a word, not a search
|
|
44
|
+
if (e.isComposing || e.keyCode === 229) return
|
|
45
|
+
if (e.key === 'Enter') emit('search', model.value)
|
|
46
|
+
else if (e.key === 'Escape' && model.value) {
|
|
47
|
+
e.preventDefault()
|
|
48
|
+
e.stopPropagation()
|
|
49
|
+
model.value = ''
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const clear = () => {
|
|
53
|
+
model.value = ''
|
|
54
|
+
field()?.focus()
|
|
55
|
+
}
|
|
56
|
+
</script>
|
|
57
|
+
|
|
58
|
+
<template>
|
|
59
|
+
<Input ref="root" v-model="model" :label="label" :class="styles.root" type="search" enterkeyhint="search" :aria-keyshortcuts="shortcut" @keydown="onKeydown">
|
|
60
|
+
<template #leading><SearchIcon /></template>
|
|
61
|
+
<template v-if="model" #trailing>
|
|
62
|
+
<button type="button" :class="styles.clear" :aria-label="clearLabel" @click="clear"><XIcon /></button>
|
|
63
|
+
</template>
|
|
64
|
+
<template v-else-if="shortcut" #trailing>
|
|
65
|
+
<span :class="styles.hint" aria-hidden="true">
|
|
66
|
+
<Kbd :listen="shortcut">{{ shortcut }}</Kbd>
|
|
67
|
+
</span>
|
|
68
|
+
</template>
|
|
69
|
+
</Input>
|
|
70
|
+
</template>
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from 'react'
|
|
4
4
|
import { cx } from '../../internal/cx'
|
|
5
5
|
import { useFlow } from '../../internal/liquidMatter'
|
|
6
|
+
import { measureSegments, segmentInk, segmentKey } from '../../internal/motion/segmented'
|
|
6
7
|
import { useControllable } from '../../internal/useControllable'
|
|
7
8
|
import styles from './SegmentedControl.module.css'
|
|
8
9
|
|
|
@@ -49,14 +50,8 @@ export function SegmentedControl({
|
|
|
49
50
|
|
|
50
51
|
useLayoutEffect(() => {
|
|
51
52
|
const measure = () => {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (root) {
|
|
55
|
-
root.removeAttribute('data-tight')
|
|
56
|
-
if (root.scrollWidth > root.clientWidth) root.setAttribute('data-tight', '')
|
|
57
|
-
}
|
|
58
|
-
const el = refs.current[index]
|
|
59
|
-
if (el) setPill({ x: el.offsetLeft, w: el.offsetWidth })
|
|
53
|
+
const place = measureSegments(rootRef.current, refs.current[index])
|
|
54
|
+
if (place) setPill(place)
|
|
60
55
|
}
|
|
61
56
|
measure()
|
|
62
57
|
const ro = new ResizeObserver(measure)
|
|
@@ -73,51 +68,12 @@ export function SegmentedControl({
|
|
|
73
68
|
|
|
74
69
|
// The pill flows to the new choice. The labels stay put; under the liquid they take the chosen
|
|
75
70
|
// colour, from a copy of the row clipped to the same outline.
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (!outline || !root) {
|
|
80
|
-
inkRef.current?.remove()
|
|
81
|
-
inkRef.current = null
|
|
82
|
-
return
|
|
83
|
-
}
|
|
84
|
-
if (!inkRef.current) {
|
|
85
|
-
const layer = document.createElement('div')
|
|
86
|
-
layer.className = styles.ink
|
|
87
|
-
layer.setAttribute('aria-hidden', 'true')
|
|
88
|
-
layer.inert = true
|
|
89
|
-
for (const el of refs.current) {
|
|
90
|
-
if (!el) continue
|
|
91
|
-
const copy = el.cloneNode(true) as HTMLElement
|
|
92
|
-
copy.removeAttribute('id')
|
|
93
|
-
copy.style.left = `${el.offsetLeft}px`
|
|
94
|
-
copy.style.top = `${el.offsetTop}px`
|
|
95
|
-
copy.style.width = `${el.offsetWidth}px`
|
|
96
|
-
copy.style.height = `${el.offsetHeight}px`
|
|
97
|
-
layer.append(copy)
|
|
98
|
-
}
|
|
99
|
-
root.append(layer)
|
|
100
|
-
inkRef.current = layer
|
|
101
|
-
}
|
|
102
|
-
inkRef.current.style.clipPath = `path('${outline}')`
|
|
103
|
-
}
|
|
104
|
-
useFlow(rootRef, pillRef, pill, index, ready, styles.liquid, false, ink)
|
|
105
|
-
|
|
106
|
-
const move = (from: number, dir: 1 | -1) => {
|
|
107
|
-
let i = from
|
|
108
|
-
for (let n = 0; n < options.length; n++) {
|
|
109
|
-
i = (i + dir + options.length) % options.length
|
|
110
|
-
if (!options[i].disabled) return i
|
|
111
|
-
}
|
|
112
|
-
return from
|
|
113
|
-
}
|
|
71
|
+
const ink = useRef<ReturnType<typeof segmentInk>>(null)
|
|
72
|
+
ink.current ??= segmentInk(() => rootRef.current, () => refs.current, styles.ink)
|
|
73
|
+
useFlow(rootRef, pillRef, pill, index, ready, styles.liquid, false, ink.current)
|
|
114
74
|
|
|
115
75
|
const onKeyDown = (e: KeyboardEvent) => {
|
|
116
|
-
|
|
117
|
-
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') next = move(tabStop, 1)
|
|
118
|
-
else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') next = move(tabStop, -1)
|
|
119
|
-
else if (e.key === 'Home') next = move(-1, 1)
|
|
120
|
-
else if (e.key === 'End') next = move(options.length, -1)
|
|
76
|
+
const next = segmentKey(e.key, tabStop, options.map((o) => !!o.disabled))
|
|
121
77
|
if (next < 0) return
|
|
122
78
|
e.preventDefault()
|
|
123
79
|
setValue(options[next].value)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch, type Component } from 'vue'
|
|
3
|
+
import { createFlow } from '../../internal/motion/liquidMatter'
|
|
4
|
+
import { measureSegments, segmentInk, segmentKey } from '../../internal/motion/segmented'
|
|
5
|
+
import styles from './SegmentedControl.module.css'
|
|
6
|
+
|
|
7
|
+
// The Vue SegmentedControl: the same look (SegmentedControl.module.css), flowing pill and keys as the React one.
|
|
8
|
+
|
|
9
|
+
export type SegmentOption = { value: string; label: string; icon?: Component; disabled?: boolean; ariaLabel?: string }
|
|
10
|
+
|
|
11
|
+
const props = withDefaults(
|
|
12
|
+
defineProps<{
|
|
13
|
+
/** Accessible name for the group. */
|
|
14
|
+
label: string
|
|
15
|
+
/** The segments, in order. */
|
|
16
|
+
options: SegmentOption[]
|
|
17
|
+
/** Height and text size of the segments. */
|
|
18
|
+
size?: 'sm' | 'md'
|
|
19
|
+
/** Stretches the control to fill its container, with equal segments. */
|
|
20
|
+
fullWidth?: boolean
|
|
21
|
+
}>(),
|
|
22
|
+
{ size: 'md' },
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
/** The selected value. Use v-model, or leave it out and the first option starts selected. */
|
|
26
|
+
const model = defineModel<string>()
|
|
27
|
+
const local = ref(props.options[0]?.value ?? '')
|
|
28
|
+
const value = computed({
|
|
29
|
+
get: () => model.value ?? local.value,
|
|
30
|
+
set: (next: string) => {
|
|
31
|
+
local.value = next
|
|
32
|
+
model.value = next
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const root = ref<HTMLElement | null>(null)
|
|
37
|
+
const items = ref<Array<HTMLElement | null>>([])
|
|
38
|
+
const pillEl = ref<HTMLElement | null>(null)
|
|
39
|
+
const pill = ref<{ x: number; w: number } | null>(null)
|
|
40
|
+
const ready = ref(false)
|
|
41
|
+
const index = computed(() => props.options.findIndex((o) => o.value === value.value))
|
|
42
|
+
// With no match, the first enabled segment takes the tab stop.
|
|
43
|
+
const tabStop = computed(() => (index.value >= 0 && !props.options[index.value].disabled ? index.value : props.options.findIndex((o) => !o.disabled)))
|
|
44
|
+
|
|
45
|
+
const measure = () => {
|
|
46
|
+
const place = measureSegments(root.value, items.value[index.value])
|
|
47
|
+
if (place) pill.value = place
|
|
48
|
+
}
|
|
49
|
+
let ro: ResizeObserver | null = null
|
|
50
|
+
onMounted(() => {
|
|
51
|
+
measure()
|
|
52
|
+
ro = new ResizeObserver(measure)
|
|
53
|
+
if (root.value) ro.observe(root.value)
|
|
54
|
+
requestAnimationFrame(() => (ready.value = true))
|
|
55
|
+
})
|
|
56
|
+
watch([index, () => props.options.length], measure, { flush: 'post' })
|
|
57
|
+
|
|
58
|
+
// The pill flows to the new choice (motion/liquidMatter.ts); the labels under it take the chosen colour.
|
|
59
|
+
const flow = createFlow(
|
|
60
|
+
() => root.value,
|
|
61
|
+
() => pillEl.value,
|
|
62
|
+
{ cls: styles.liquid, ink: segmentInk(() => root.value, () => items.value, styles.ink) },
|
|
63
|
+
)
|
|
64
|
+
watch(
|
|
65
|
+
() => [index.value, pill.value?.x, pill.value?.w, ready.value] as const,
|
|
66
|
+
() => nextTick(() => flow.update(pill.value, index.value, ready.value)),
|
|
67
|
+
{ flush: 'post', immediate: true },
|
|
68
|
+
)
|
|
69
|
+
onBeforeUnmount(() => {
|
|
70
|
+
ro?.disconnect()
|
|
71
|
+
flow.destroy()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
75
|
+
const next = segmentKey(e.key, tabStop.value, props.options.map((o) => !!o.disabled))
|
|
76
|
+
if (next < 0) return
|
|
77
|
+
e.preventDefault()
|
|
78
|
+
value.value = props.options[next].value
|
|
79
|
+
items.value[next]?.focus()
|
|
80
|
+
}
|
|
81
|
+
</script>
|
|
82
|
+
|
|
83
|
+
<template>
|
|
84
|
+
<div ref="root" role="radiogroup" :aria-label="label" :class="[styles.root, styles[size], fullWidth && styles.full]" @keydown="onKeyDown">
|
|
85
|
+
<span
|
|
86
|
+
v-if="pill"
|
|
87
|
+
ref="pillEl"
|
|
88
|
+
:class="styles.pill"
|
|
89
|
+
:data-ready="ready || undefined"
|
|
90
|
+
:style="{ transform: `translateX(${pill.x}px)`, width: `${pill.w}px` }"
|
|
91
|
+
aria-hidden="true"
|
|
92
|
+
/>
|
|
93
|
+
<button
|
|
94
|
+
v-for="(o, i) in options"
|
|
95
|
+
:key="o.value"
|
|
96
|
+
:ref="(el) => (items[i] = el as HTMLElement | null)"
|
|
97
|
+
type="button"
|
|
98
|
+
role="radio"
|
|
99
|
+
:aria-checked="o.value === value"
|
|
100
|
+
:aria-label="o.ariaLabel"
|
|
101
|
+
:tabindex="i === tabStop ? 0 : -1"
|
|
102
|
+
:disabled="o.disabled"
|
|
103
|
+
:class="styles.item"
|
|
104
|
+
:data-on="o.value === value || undefined"
|
|
105
|
+
@click="value = o.value"
|
|
106
|
+
>
|
|
107
|
+
<span v-if="o.icon" :class="styles.icon"><component :is="o.icon" /></span>
|
|
108
|
+
<span v-if="o.label" :class="styles.text">{{ o.label }}</span>
|
|
109
|
+
</button>
|
|
110
|
+
</div>
|
|
111
|
+
</template>
|
|
@@ -10,6 +10,7 @@ import { Portal } from '../../internal/Portal'
|
|
|
10
10
|
import { useControllable } from '../../internal/useControllable'
|
|
11
11
|
import { useFloating } from '../../internal/useFloating'
|
|
12
12
|
import { usePresence } from '../../internal/usePresence'
|
|
13
|
+
import { createListbox } from '../../internal/motion/listbox'
|
|
13
14
|
import { FieldMessage } from '../../internal/FieldMessage'
|
|
14
15
|
import styles from './Select.module.css'
|
|
15
16
|
|
|
@@ -76,7 +77,6 @@ export function Select({
|
|
|
76
77
|
const triggerRef = useRef<HTMLButtonElement>(null)
|
|
77
78
|
const listRef = useRef<HTMLDivElement>(null)
|
|
78
79
|
const optionRefs = useRef<Array<HTMLDivElement | null>>([])
|
|
79
|
-
const typeahead = useRef({ text: '', at: 0 })
|
|
80
80
|
const { mounted, state } = usePresence(open, 'morph-out')
|
|
81
81
|
const { style, side, ready } = useFloating(triggerRef, listRef, { open: mounted, side: 'bottom', align: 'start', offset: 6, matchWidth: true })
|
|
82
82
|
// The list grows out of the field and folds back into it.
|
|
@@ -88,17 +88,12 @@ export function Select({
|
|
|
88
88
|
const selectedIndex = options.findIndex((o) => o.value === value)
|
|
89
89
|
const selected = selectedIndex >= 0 ? options[selectedIndex] : null
|
|
90
90
|
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
return from
|
|
99
|
-
}
|
|
100
|
-
const first = () => step(-1, 1)
|
|
101
|
-
const last = () => step(options.length, -1)
|
|
91
|
+
// Arrows, Home, End, Enter, Space, Tab and type-ahead (motion/listbox.ts).
|
|
92
|
+
const optionsRef = useRef(options)
|
|
93
|
+
optionsRef.current = options
|
|
94
|
+
const list = useRef<ReturnType<typeof createListbox>>(null)
|
|
95
|
+
list.current ??= createListbox(() => optionsRef.current)
|
|
96
|
+
const { enabled, first } = list.current
|
|
102
97
|
|
|
103
98
|
const openList = (at?: number) => {
|
|
104
99
|
if (disabled) return
|
|
@@ -129,41 +124,15 @@ export function Select({
|
|
|
129
124
|
}, [open])
|
|
130
125
|
|
|
131
126
|
const onKeyDown = (e: KeyboardEvent<HTMLButtonElement>) => {
|
|
132
|
-
const
|
|
133
|
-
if (!
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
openList()
|
|
142
|
-
runTypeahead(k)
|
|
143
|
-
}
|
|
144
|
-
return
|
|
145
|
-
}
|
|
146
|
-
if (k === 'ArrowDown') { e.preventDefault(); setActive((a) => step(a, 1)) }
|
|
147
|
-
else if (k === 'ArrowUp') { e.preventDefault(); setActive((a) => step(a, -1)) }
|
|
148
|
-
else if (k === 'Home') { e.preventDefault(); setActive(first()) }
|
|
149
|
-
else if (k === 'End') { e.preventDefault(); setActive(last()) }
|
|
150
|
-
else if (k === 'Enter' || k === ' ') { e.preventDefault(); choose(active) }
|
|
151
|
-
else if (k === 'Tab') { if (enabled(active)) setValue(options[active].value); setOpen(false) }
|
|
152
|
-
else if (k.length === 1) runTypeahead(k)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const runTypeahead = (ch: string) => {
|
|
156
|
-
const now = Date.now()
|
|
157
|
-
const t = typeahead.current
|
|
158
|
-
t.text = now - t.at > 600 ? ch.toLowerCase() : t.text + ch.toLowerCase()
|
|
159
|
-
t.at = now
|
|
160
|
-
const start = Math.max(active, 0)
|
|
161
|
-
for (let n = 0; n < options.length; n++) {
|
|
162
|
-
const i = (start + n + (t.text.length === 1 ? 1 : 0)) % options.length
|
|
163
|
-
if (enabled(i) && options[i].label.toLowerCase().startsWith(t.text)) {
|
|
164
|
-
setActive(i)
|
|
165
|
-
return
|
|
166
|
-
}
|
|
127
|
+
const r = list.current!.key(e, open, active, selectedIndex)
|
|
128
|
+
if (!r) return
|
|
129
|
+
if (r.prevent) e.preventDefault()
|
|
130
|
+
if (r.open) openList(r.active)
|
|
131
|
+
else if (r.active !== undefined) setActive(r.active)
|
|
132
|
+
if (r.choose) choose(active)
|
|
133
|
+
if (r.tab) {
|
|
134
|
+
if (enabled(active)) setValue(options[active].value)
|
|
135
|
+
setOpen(false)
|
|
167
136
|
}
|
|
168
137
|
}
|
|
169
138
|
|