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,236 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, useId, watch } from 'vue'
|
|
3
|
+
import FieldMessage from '../../internal/FieldMessage.vue'
|
|
4
|
+
import { dismissable } from '../../internal/motion/layers'
|
|
5
|
+
import { createLiquid } from '../../internal/motion/liquid'
|
|
6
|
+
import { edgeFlow } from '../../internal/motion/liquidMore'
|
|
7
|
+
import { createListbox } from '../../internal/motion/listbox'
|
|
8
|
+
import { useFloating } from '../../internal/vue/useFloating'
|
|
9
|
+
import { usePresence } from '../../internal/vue/usePresence'
|
|
10
|
+
import { CheckIcon, ChevronDownIcon } from '../../internal/vueIcons'
|
|
11
|
+
import styles from './Select.module.css'
|
|
12
|
+
|
|
13
|
+
// The Vue Select: the same look (Select.module.css), list morph, focus edge and keys as the React one.
|
|
14
|
+
|
|
15
|
+
export type SelectOption = { value: string; label: string; disabled?: boolean; hint?: string }
|
|
16
|
+
|
|
17
|
+
const props = withDefaults(
|
|
18
|
+
defineProps<{
|
|
19
|
+
/** Visible name of the field, also read by screen readers. */
|
|
20
|
+
label: string
|
|
21
|
+
/** Keeps the label for screen readers only, for a list where a column heading already names the field. */
|
|
22
|
+
hideLabel?: boolean
|
|
23
|
+
/** Choices in the list, in order. */
|
|
24
|
+
options: SelectOption[]
|
|
25
|
+
/** Starting value when there is no v-model. */
|
|
26
|
+
defaultValue?: string | null
|
|
27
|
+
/** Shown when nothing is chosen yet. */
|
|
28
|
+
placeholder?: string
|
|
29
|
+
/** Hint under the field. Hidden while an error shows. */
|
|
30
|
+
help?: string
|
|
31
|
+
/** Error message under the field. Also marks the field invalid. */
|
|
32
|
+
error?: string
|
|
33
|
+
/** Shown in the open list when there are no options. */
|
|
34
|
+
emptyText?: string
|
|
35
|
+
/** Stops the list from opening. */
|
|
36
|
+
disabled?: boolean
|
|
37
|
+
/** Submits the selected value in a hidden field with this name. */
|
|
38
|
+
name?: string
|
|
39
|
+
/** Id of the button, so a label's `for` can point at it. Inner ids add a suffix to it. */
|
|
40
|
+
id?: string
|
|
41
|
+
}>(),
|
|
42
|
+
{ hideLabel: false, defaultValue: null, placeholder: 'Choose one', emptyText: 'No options' },
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
/** The selected value. Use v-model, or leave it out and the field keeps its own. Null means nothing is chosen. */
|
|
46
|
+
const model = defineModel<string | null>()
|
|
47
|
+
const local = ref(props.defaultValue)
|
|
48
|
+
const value = computed({
|
|
49
|
+
get: () => (model.value === undefined ? local.value : model.value),
|
|
50
|
+
set: (next: string | null) => {
|
|
51
|
+
local.value = next
|
|
52
|
+
model.value = next
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const auto = useId()
|
|
57
|
+
const baseId = computed(() => props.id ?? auto)
|
|
58
|
+
const labelId = computed(() => `${baseId.value}-label`)
|
|
59
|
+
const listId = computed(() => `${baseId.value}-list`)
|
|
60
|
+
const helpId = computed(() => `${baseId.value}-help`)
|
|
61
|
+
const errorId = computed(() => `${baseId.value}-error`)
|
|
62
|
+
const emptyId = computed(() => `${baseId.value}-empty`)
|
|
63
|
+
const triggerId = computed(() => props.id ?? `${auto}-trigger`)
|
|
64
|
+
|
|
65
|
+
const open = ref(false)
|
|
66
|
+
const active = ref(-1)
|
|
67
|
+
const trigger = ref<HTMLButtonElement | null>(null)
|
|
68
|
+
const listEl = ref<HTMLElement | null>(null)
|
|
69
|
+
const optionEls: Array<HTMLElement | null> = []
|
|
70
|
+
const { mounted, state } = usePresence(open, 'morph-out')
|
|
71
|
+
const { style, side, ready } = useFloating(trigger, listEl, mounted, { side: 'bottom', align: 'start', offset: 6, matchWidth: true })
|
|
72
|
+
// Vue, unlike React, adds no px to a number in a style.
|
|
73
|
+
const selectedIndex = computed(() => props.options.findIndex((o) => o.value === value.value))
|
|
74
|
+
const selected = computed(() => (selectedIndex.value >= 0 ? props.options[selectedIndex.value] : null))
|
|
75
|
+
|
|
76
|
+
// Arrows, Home, End, Enter, Space, Tab and type-ahead (motion/listbox.ts).
|
|
77
|
+
const list = createListbox(() => props.options)
|
|
78
|
+
const { enabled, first } = list
|
|
79
|
+
|
|
80
|
+
const openList = (at?: number) => {
|
|
81
|
+
if (props.disabled) return
|
|
82
|
+
open.value = true
|
|
83
|
+
active.value = at ?? (enabled(selectedIndex.value) ? selectedIndex.value : first())
|
|
84
|
+
}
|
|
85
|
+
const close = (focusTrigger = true) => {
|
|
86
|
+
open.value = false
|
|
87
|
+
if (focusTrigger) trigger.value?.focus()
|
|
88
|
+
}
|
|
89
|
+
const choose = (i: number) => {
|
|
90
|
+
if (!enabled(i)) return
|
|
91
|
+
value.value = props.options[i].value
|
|
92
|
+
close()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
96
|
+
const r = list.key(e, open.value, active.value, selectedIndex.value)
|
|
97
|
+
if (!r) return
|
|
98
|
+
if (r.prevent) e.preventDefault()
|
|
99
|
+
if (r.open) openList(r.active)
|
|
100
|
+
else if (r.active !== undefined) active.value = r.active
|
|
101
|
+
if (r.choose) choose(active.value)
|
|
102
|
+
if (r.tab) {
|
|
103
|
+
if (enabled(active.value)) value.value = props.options[active.value].value
|
|
104
|
+
open.value = false
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// The list grows out of the field and folds back into it (motion/liquid.ts).
|
|
109
|
+
const liquid = createLiquid(
|
|
110
|
+
() => listEl.value,
|
|
111
|
+
() => trigger.value,
|
|
112
|
+
)
|
|
113
|
+
// Waits for the render that puts the list in its place, so the grow starts from the right shape.
|
|
114
|
+
watch(
|
|
115
|
+
[state, ready],
|
|
116
|
+
async () => {
|
|
117
|
+
if (!ready.value) return
|
|
118
|
+
await nextTick()
|
|
119
|
+
liquid.play(state.value)
|
|
120
|
+
},
|
|
121
|
+
{ flush: 'post' },
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
// Esc and a press outside close the list; only Esc puts focus back on the field.
|
|
125
|
+
let undismiss = () => {}
|
|
126
|
+
watch(
|
|
127
|
+
open,
|
|
128
|
+
(now) => {
|
|
129
|
+
undismiss()
|
|
130
|
+
undismiss = now ? dismissable(() => [trigger.value, listEl.value], (reason) => close(reason === 'escape')) : () => {}
|
|
131
|
+
},
|
|
132
|
+
{ flush: 'post' },
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
// One highlight glides between the options.
|
|
136
|
+
const hl = ref<{ top: number; height: number } | null>(null)
|
|
137
|
+
watch(
|
|
138
|
+
[active, open, mounted],
|
|
139
|
+
() => {
|
|
140
|
+
if (!open.value) return (hl.value = null)
|
|
141
|
+
const el = optionEls[active.value]
|
|
142
|
+
if (!el) return
|
|
143
|
+
hl.value = { top: el.offsetTop, height: el.offsetHeight }
|
|
144
|
+
el.scrollIntoView({ block: 'nearest' })
|
|
145
|
+
},
|
|
146
|
+
{ flush: 'post' },
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
// Open or keyboard focused, the field's dark edge flows round it from where it was pressed (motion/liquidMore.ts).
|
|
150
|
+
let stopEdge = () => {}
|
|
151
|
+
onMounted(() => {
|
|
152
|
+
if (trigger.value) stopEdge = edgeFlow(trigger.value, trigger.value, ':is([data-open], :focus-visible)', styles.edge)
|
|
153
|
+
})
|
|
154
|
+
onBeforeUnmount(() => {
|
|
155
|
+
stopEdge()
|
|
156
|
+
undismiss()
|
|
157
|
+
liquid.destroy()
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
const empty = computed(() => props.options.length === 0)
|
|
161
|
+
const describedBy = computed(
|
|
162
|
+
() => [props.help && !props.error ? helpId.value : null, props.error ? errorId.value : null, open.value && empty.value ? emptyId.value : null].filter(Boolean).join(' ') || undefined,
|
|
163
|
+
)
|
|
164
|
+
</script>
|
|
165
|
+
|
|
166
|
+
<template>
|
|
167
|
+
<div :class="styles.root">
|
|
168
|
+
<span :id="labelId" :class="[styles.label, hideLabel && styles.hiddenLabel]" @click="trigger?.focus()">
|
|
169
|
+
{{ label }}
|
|
170
|
+
</span>
|
|
171
|
+
<button
|
|
172
|
+
:id="triggerId"
|
|
173
|
+
ref="trigger"
|
|
174
|
+
type="button"
|
|
175
|
+
role="combobox"
|
|
176
|
+
:class="styles.trigger"
|
|
177
|
+
:data-open="open || undefined"
|
|
178
|
+
:data-invalid="error ? true : undefined"
|
|
179
|
+
:aria-labelledby="`${labelId} ${triggerId}`"
|
|
180
|
+
aria-haspopup="listbox"
|
|
181
|
+
:aria-expanded="open"
|
|
182
|
+
:aria-controls="listId"
|
|
183
|
+
:aria-activedescendant="open && enabled(active) ? `${baseId}-opt-${active}` : undefined"
|
|
184
|
+
:aria-invalid="error ? true : undefined"
|
|
185
|
+
:aria-describedby="describedBy"
|
|
186
|
+
:disabled="disabled"
|
|
187
|
+
@click="open ? close() : openList()"
|
|
188
|
+
@keydown="onKeyDown"
|
|
189
|
+
>
|
|
190
|
+
<span :class="[styles.value, !selected && styles.placeholder]">
|
|
191
|
+
<span :key="selected?.value ?? 'none'" :class="styles.valueText" :title="selected ? selected.label : placeholder">
|
|
192
|
+
{{ selected ? selected.label : placeholder }}
|
|
193
|
+
</span>
|
|
194
|
+
</span>
|
|
195
|
+
<ChevronDownIcon :class="styles.chevron" />
|
|
196
|
+
</button>
|
|
197
|
+
<input v-if="name" type="hidden" :name="name" :value="value ?? ''" />
|
|
198
|
+
<FieldMessage :help-id="helpId" :error-id="errorId" :help="help" :error="error" />
|
|
199
|
+
<Teleport v-if="mounted" to="body">
|
|
200
|
+
<div
|
|
201
|
+
:id="listId"
|
|
202
|
+
ref="listEl"
|
|
203
|
+
role="listbox"
|
|
204
|
+
:aria-labelledby="labelId"
|
|
205
|
+
tabindex="-1"
|
|
206
|
+
:class="styles.list"
|
|
207
|
+
:data-state="state"
|
|
208
|
+
:data-side="side"
|
|
209
|
+
:style="style"
|
|
210
|
+
@mousedown.prevent
|
|
211
|
+
>
|
|
212
|
+
<span v-if="hl" :class="styles.highlight" :style="{ transform: `translateY(${hl.top}px)`, height: `${hl.height}px` }" aria-hidden="true" />
|
|
213
|
+
<div
|
|
214
|
+
v-for="(o, i) in options"
|
|
215
|
+
:id="`${baseId}-opt-${i}`"
|
|
216
|
+
:key="o.value"
|
|
217
|
+
:ref="(el) => (optionEls[i] = el as HTMLElement | null)"
|
|
218
|
+
role="option"
|
|
219
|
+
:aria-selected="o.value === value"
|
|
220
|
+
:aria-disabled="o.disabled || undefined"
|
|
221
|
+
:class="styles.option"
|
|
222
|
+
:data-active="i === active || undefined"
|
|
223
|
+
:style="{ '--i': i }"
|
|
224
|
+
@pointermove="enabled(i) && active !== i && (active = i)"
|
|
225
|
+
@click="choose(i)"
|
|
226
|
+
>
|
|
227
|
+
<span :class="styles.optionText">{{ o.label }}<span v-if="o.hint" :class="styles.hint">{{ o.hint }}</span></span>
|
|
228
|
+
<CheckIcon :class="styles.check" :data-on="o.value === value || undefined" />
|
|
229
|
+
</div>
|
|
230
|
+
<div v-if="empty" :id="emptyId" :class="styles.empty">
|
|
231
|
+
{{ emptyText }}
|
|
232
|
+
</div>
|
|
233
|
+
</div>
|
|
234
|
+
</Teleport>
|
|
235
|
+
</div>
|
|
236
|
+
</template>
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
-
import { useEffect, useId, useLayoutEffect, useRef, useState, type
|
|
3
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState, type ReactNode, type RefObject } from 'react'
|
|
4
4
|
import { cx } from '../../internal/cx'
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { useDragClose } from '../../internal/fling'
|
|
6
|
+
import { useFocusTrap } from '../../internal/focus'
|
|
7
7
|
import { XIcon } from '../../internal/icons'
|
|
8
|
+
import { createDetentSheet, DETENT_NAME, detentHeights, detentOrder, type SheetDetent } from '../../internal/motion/sheetDetents'
|
|
8
9
|
import { useDismiss, useScrollLock } from '../../internal/layers'
|
|
9
10
|
import { Portal } from '../../internal/Portal'
|
|
10
11
|
import { usePresence } from '../../internal/usePresence'
|
|
11
12
|
import styles from './Sheet.module.css'
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
export type SheetDetent = 'peek' | 'half' | 'full'
|
|
14
|
+
export type { SheetDetent }
|
|
15
15
|
|
|
16
16
|
export type SheetProps = {
|
|
17
17
|
/** Whether the sheet is open. You always control it. */
|
|
@@ -117,38 +117,18 @@ function PlainSheet({ open, onOpenChange, title, description, children, footer,
|
|
|
117
117
|
)
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
// Shares of the screen, from the heights Apple Maps on the web and Vaul rest at.
|
|
121
|
-
const SHARE: Record<SheetDetent, number> = { peek: 0.19, half: 0.5, full: 0.96 }
|
|
122
|
-
const ORDER: SheetDetent[] = ['peek', 'half', 'full']
|
|
123
|
-
const NAME: Record<SheetDetent, string> = { peek: 'Low', half: 'Half', full: 'Full' }
|
|
124
|
-
// px per second: faster than this, a let-go is a flick and moves one height its way.
|
|
125
|
-
const FLICK = 400
|
|
126
|
-
// How far ahead, in seconds, a slow let-go is carried before the nearest height is picked.
|
|
127
|
-
const PROJECT = 0.2
|
|
128
|
-
// A press is not a drag until it moves this far, so taps on the content still work.
|
|
129
|
-
const SLOP = 6
|
|
130
|
-
|
|
131
|
-
const nearest = (v: number, hs: number[]) => hs.reduce((best, h, i) => (Math.abs(h - v) < Math.abs(hs[best] - v) ? i : best), 0)
|
|
132
|
-
|
|
133
|
-
// A flick moves one height from where the drag began, never two, even when the hand has already
|
|
134
|
-
// passed the next one. Real sheets measured all do this.
|
|
135
|
-
function pick(pos: number, v: number, start: number, hs: number[]) {
|
|
136
|
-
if (Math.abs(v) <= FLICK) return nearest(pos + v * PROJECT, hs)
|
|
137
|
-
return Math.max(0, Math.min(hs.length - 1, start + Math.sign(v)))
|
|
138
|
-
}
|
|
139
|
-
|
|
140
120
|
type DetentProps = BaseProps & Required<Pick<SheetProps, 'detents' | 'heightLabel'>> & Pick<SheetProps, 'detent' | 'defaultDetent' | 'onDetentChange'>
|
|
141
121
|
|
|
142
122
|
function DetentSheet({ open, onOpenChange, title, description, children, footer, closeLabel, initialFocus, className, detents, detent, defaultDetent, onDetentChange, heightLabel }: DetentProps) {
|
|
143
123
|
const id = useId()
|
|
144
|
-
const order =
|
|
124
|
+
const order = detentOrder(detents)
|
|
145
125
|
const top = order.length - 1
|
|
146
126
|
const [inner, setInner] = useState<SheetDetent>(defaultDetent ?? order[0])
|
|
147
127
|
const index = Math.max(0, order.indexOf(detent ?? inner))
|
|
148
128
|
const atTop = index === top
|
|
149
129
|
const { mounted, state } = usePresence(open, 'morph-out')
|
|
150
130
|
const [vh, setVh] = useState(() => (typeof window === 'undefined' ? 0 : window.innerHeight))
|
|
151
|
-
const heights = order
|
|
131
|
+
const heights = detentHeights(order, vh)
|
|
152
132
|
const [dragging, setDragging] = useState(false)
|
|
153
133
|
const [target, setTarget] = useState(index)
|
|
154
134
|
const [said, setSaid] = useState('')
|
|
@@ -157,69 +137,38 @@ function DetentSheet({ open, onOpenChange, title, description, children, footer,
|
|
|
157
137
|
const overlayRef = useRef<HTMLDivElement>(null)
|
|
158
138
|
const grabRef = useRef<HTMLDivElement>(null)
|
|
159
139
|
const bodyRef = useRef<HTMLDivElement>(null)
|
|
160
|
-
const at = useRef(0)
|
|
161
|
-
const aimed = useRef(-1)
|
|
162
|
-
const stopGlide = useRef(() => {})
|
|
163
|
-
const unlisten = useRef(() => {})
|
|
164
|
-
const opener = useRef<HTMLElement | null>(null)
|
|
165
140
|
const trapStart = useRef<HTMLElement | null>(null)
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const hs = live.current.heights
|
|
178
|
-
const panel = panelRef.current
|
|
179
|
-
if (!panel) return
|
|
180
|
-
panel.style.transform = `translateY(${hs[hs.length - 1] - v}px)`
|
|
181
|
-
// The dim follows the height between the two highest, so it never jumps.
|
|
182
|
-
const lo = hs.length > 1 ? hs[hs.length - 2] : 0
|
|
183
|
-
const dim = Math.max(0, Math.min(1, (v - lo) / (hs[hs.length - 1] - lo || 1)))
|
|
184
|
-
overlayRef.current?.style.setProperty('opacity', String(dim))
|
|
141
|
+
const live = useRef({ order, heights, index, open, onOpenChange, change: (_: number) => {} })
|
|
142
|
+
live.current = {
|
|
143
|
+
order,
|
|
144
|
+
heights,
|
|
145
|
+
index,
|
|
146
|
+
open,
|
|
147
|
+
onOpenChange,
|
|
148
|
+
change: (i: number) => {
|
|
149
|
+
if (detent === undefined) setInner(order[i])
|
|
150
|
+
onDetentChange?.(order[i])
|
|
151
|
+
},
|
|
185
152
|
}
|
|
186
153
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
if (live.current.open) {
|
|
206
|
-
panelRef.current?.removeAttribute('data-fluid')
|
|
207
|
-
settle(hs[live.current.index], 0)
|
|
208
|
-
}
|
|
209
|
-
})
|
|
210
|
-
return
|
|
211
|
-
}
|
|
212
|
-
aimed.current = i
|
|
213
|
-
setTarget(i)
|
|
214
|
-
if (i !== live.current.index) {
|
|
215
|
-
change(i)
|
|
216
|
-
if (spoken) setSaid(`${NAME[order[i]]} height`)
|
|
217
|
-
}
|
|
218
|
-
// Shrunk below a focused control: focus goes to the grabber, so it never sits on something hidden.
|
|
219
|
-
settle(hs[i], v, () => shownHidden() && grabRef.current?.focus({ preventScroll: true }))
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const stepDown = () => (live.current.index > 0 ? go(live.current.index - 1) : onOpenChange(false))
|
|
154
|
+
// Drag, flick, the grabber's keys and where focus goes (motion/sheetDetents.ts).
|
|
155
|
+
const core = useRef<ReturnType<typeof createDetentSheet>>(null)
|
|
156
|
+
core.current ??= createDetentSheet({
|
|
157
|
+
panel: () => panelRef.current,
|
|
158
|
+
overlay: () => overlayRef.current,
|
|
159
|
+
grabber: () => grabRef.current,
|
|
160
|
+
body: () => bodyRef.current,
|
|
161
|
+
order: () => live.current.order,
|
|
162
|
+
heights: () => live.current.heights,
|
|
163
|
+
index: () => live.current.index,
|
|
164
|
+
open: () => live.current.open,
|
|
165
|
+
change: (i) => live.current.change(i),
|
|
166
|
+
close: () => live.current.onOpenChange(false),
|
|
167
|
+
target: setTarget,
|
|
168
|
+
dragging: setDragging,
|
|
169
|
+
say: setSaid,
|
|
170
|
+
})
|
|
171
|
+
const sheet = core.current
|
|
223
172
|
|
|
224
173
|
useEffect(() => {
|
|
225
174
|
const onResize = () => setVh(window.innerHeight)
|
|
@@ -228,38 +177,24 @@ function DetentSheet({ open, onOpenChange, title, description, children, footer,
|
|
|
228
177
|
}, [])
|
|
229
178
|
|
|
230
179
|
useLayoutEffect(() => {
|
|
231
|
-
if (
|
|
232
|
-
|
|
233
|
-
aimed.current = -1
|
|
234
|
-
}, [open])
|
|
180
|
+
if (open) sheet.opening()
|
|
181
|
+
}, [open, sheet])
|
|
235
182
|
|
|
236
183
|
useLayoutEffect(() => {
|
|
237
|
-
if (
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
setTarget(index)
|
|
241
|
-
paint(heights[index])
|
|
242
|
-
if (!atTop) (initialFocus?.current ?? grabRef.current)?.focus({ preventScroll: true })
|
|
243
|
-
return
|
|
244
|
-
}
|
|
245
|
-
if (drag.current?.live) return
|
|
246
|
-
if (aimed.current !== index) {
|
|
247
|
-
aimed.current = index
|
|
248
|
-
setTarget(index)
|
|
249
|
-
settle(heights[index], 0)
|
|
250
|
-
} else paint(heights[index])
|
|
184
|
+
if (open && mounted) sheet.sync(initialFocus?.current)
|
|
185
|
+
// Synced when the height or the screen changes; initialFocus is read at that moment.
|
|
186
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
251
187
|
}, [open, mounted, index, vh])
|
|
252
188
|
|
|
253
189
|
// Closed, it gives focus back to its opener, and opens next time at the default height.
|
|
254
190
|
useEffect(() => {
|
|
255
191
|
if (open) return
|
|
256
192
|
setInner(defaultDetent ?? order[0])
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
if (panel && opener.current?.isConnected && (!active || active === document.body || panel.contains(active))) opener.current.focus({ preventScroll: true })
|
|
193
|
+
sheet.closed()
|
|
194
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
260
195
|
}, [open])
|
|
261
196
|
|
|
262
|
-
useEffect(() => () =>
|
|
197
|
+
useEffect(() => () => sheet.destroy(), [sheet])
|
|
263
198
|
|
|
264
199
|
// Before the trap's own effect: at the top, focus stays where it is in the sheet.
|
|
265
200
|
useLayoutEffect(() => {
|
|
@@ -269,127 +204,13 @@ function DetentSheet({ open, onOpenChange, title, description, children, footer,
|
|
|
269
204
|
}, [atTop, open])
|
|
270
205
|
useScrollLock(open && atTop)
|
|
271
206
|
useFocusTrap(panelRef, open && mounted && atTop, { initialFocus: trapStart, restore: false })
|
|
272
|
-
useDismiss(open, [panelRef], stepDown, { outside: atTop })
|
|
207
|
+
useDismiss(open, [panelRef], sheet.stepDown, { outside: atTop })
|
|
273
208
|
|
|
274
|
-
const onPointerMove = (e: globalThis.PointerEvent) => {
|
|
275
|
-
const d = drag.current
|
|
276
|
-
const hs = live.current.heights
|
|
277
|
-
if (!d || d.pointer !== e.pointerId) return
|
|
278
|
-
const dy = d.y0 - e.clientY
|
|
279
|
-
if (!d.live) {
|
|
280
|
-
if (Math.abs(dy) < SLOP) return
|
|
281
|
-
if (bodyRef.current?.contains(e.target as Node) && live.current.index === top && dy > 0) return endDrag()
|
|
282
|
-
// A tap must not stop a settle half way, so the glide is caught only once it is a drag.
|
|
283
|
-
stopGlide.current()
|
|
284
|
-
d.live = true
|
|
285
|
-
d.at0 = at.current
|
|
286
|
-
d.start = nearest(at.current, hs)
|
|
287
|
-
d.speed.add(at.current)
|
|
288
|
-
d.y0 = e.clientY + (dy > 0 ? SLOP : -SLOP)
|
|
289
|
-
// Only now, so a tap on a button inside still clicks it.
|
|
290
|
-
if (e.target instanceof Element && panelRef.current?.contains(e.target)) panelRef.current.setPointerCapture?.(e.pointerId)
|
|
291
|
-
window.addEventListener('selectstart', block)
|
|
292
|
-
setDragging(true)
|
|
293
|
-
}
|
|
294
|
-
// A drag moves the sheet, never selects the page's text: a selection left over from one drag
|
|
295
|
-
// would turn the next press on it into the browser's own drag, which cancels the pointer. Safari
|
|
296
|
-
// keeps growing the selection it began on the press, so it is cleared on every move.
|
|
297
|
-
const selection = window.getSelection()
|
|
298
|
-
if (selection && !selection.isCollapsed) selection.removeAllRanges()
|
|
299
|
-
let raw = d.at0 + (d.y0 - e.clientY)
|
|
300
|
-
const full = hs[hs.length - 1]
|
|
301
|
-
if (raw > full) raw = full + rubber(raw - full, window.innerHeight - full)
|
|
302
|
-
if (raw < 0) raw = 0
|
|
303
|
-
d.speed.add(raw)
|
|
304
|
-
paint(raw)
|
|
305
|
-
const next = pick(raw, d.speed.speed(), d.start, hs)
|
|
306
|
-
setTarget((t) => (t === next ? t : next))
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
const onPointerUp = (e: globalThis.PointerEvent) => {
|
|
310
|
-
const d = drag.current
|
|
311
|
-
if (!d || d.pointer !== e.pointerId) return
|
|
312
|
-
endDrag()
|
|
313
|
-
if (!d.live) return
|
|
314
|
-
setDragging(false)
|
|
315
|
-
const hs = live.current.heights
|
|
316
|
-
const cancelled = e.type === 'pointercancel'
|
|
317
|
-
const v = cancelled ? 0 : d.speed.speed()
|
|
318
|
-
const pos = at.current
|
|
319
|
-
if (!cancelled && d.start === 0 && (v < -FLICK || pos < hs[0] * 0.5)) return go(-1, v)
|
|
320
|
-
go(pick(pos, v, d.start, hs), v)
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
// The hand often leaves the sheet before the drag counts as one, so the moves are followed on the window.
|
|
324
|
-
const block = (e: Event) => e.preventDefault()
|
|
325
|
-
|
|
326
|
-
const endDrag = () => {
|
|
327
|
-
drag.current = null
|
|
328
|
-
unlisten.current()
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
const onPointerDown = (e: PointerEvent<HTMLDivElement>) => {
|
|
332
|
-
if ((e.pointerType === 'mouse' && e.button !== 0) || drag.current) return
|
|
333
|
-
const el = e.target as Element
|
|
334
|
-
if (el.closest('input, textarea, select, [contenteditable="true"]')) return
|
|
335
|
-
const hs = live.current.heights
|
|
336
|
-
// At the top the content scrolls; a drag on it moves the sheet only from the top of the content.
|
|
337
|
-
if (bodyRef.current?.contains(el) && live.current.index === top && (bodyRef.current?.scrollTop ?? 0) > 0) return
|
|
338
|
-
drag.current = { y0: e.clientY, at0: at.current, start: nearest(at.current, hs), pointer: e.pointerId, live: false, speed: speedTracker() }
|
|
339
|
-
const move = onPointerMove
|
|
340
|
-
const up = onPointerUp
|
|
341
|
-
window.addEventListener('pointermove', move)
|
|
342
|
-
window.addEventListener('pointerup', up)
|
|
343
|
-
window.addEventListener('pointercancel', up)
|
|
344
|
-
window.addEventListener('dragstart', block)
|
|
345
|
-
// Safari starts a text selection on the press itself, so a press outside the content (the grabber,
|
|
346
|
-
// the edges) blocks it at once. Text in the content can still be selected by dragging inside it.
|
|
347
|
-
if (!bodyRef.current?.contains(el)) window.addEventListener('selectstart', block)
|
|
348
|
-
unlisten.current = () => {
|
|
349
|
-
window.removeEventListener('pointermove', move)
|
|
350
|
-
window.removeEventListener('pointerup', up)
|
|
351
|
-
window.removeEventListener('pointercancel', up)
|
|
352
|
-
window.removeEventListener('dragstart', block)
|
|
353
|
-
window.removeEventListener('selectstart', block)
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
useEffect(() => () => unlisten.current(), [])
|
|
358
|
-
|
|
359
|
-
// A touch at the top of the content, moving down, belongs to the sheet: the browser would scroll it.
|
|
360
209
|
useEffect(() => {
|
|
361
210
|
const body = bodyRef.current
|
|
362
211
|
if (!body) return
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
const move = (e: TouchEvent) => {
|
|
366
|
-
if (drag.current?.live || (live.current.index === live.current.heights.length - 1 && body.scrollTop <= 0 && e.touches[0].clientY > y)) e.preventDefault()
|
|
367
|
-
}
|
|
368
|
-
body.addEventListener('touchstart', start, { passive: true })
|
|
369
|
-
body.addEventListener('touchmove', move, { passive: false })
|
|
370
|
-
return () => {
|
|
371
|
-
body.removeEventListener('touchstart', start)
|
|
372
|
-
body.removeEventListener('touchmove', move)
|
|
373
|
-
}
|
|
374
|
-
}, [mounted])
|
|
375
|
-
|
|
376
|
-
const onGrabKey = (e: KeyboardEvent<HTMLDivElement>) => {
|
|
377
|
-
const i = live.current.index
|
|
378
|
-
if (e.key === 'Enter' || e.key === ' ') {
|
|
379
|
-
e.preventDefault()
|
|
380
|
-
return go(i === top ? 0 : i + 1, 0, false)
|
|
381
|
-
}
|
|
382
|
-
const keys: Record<string, number> = { ArrowUp: i + 1, ArrowRight: i + 1, ArrowDown: i - 1, ArrowLeft: i - 1, Home: 0, End: top, PageUp: top, PageDown: 0 }
|
|
383
|
-
if (!(e.key in keys)) return
|
|
384
|
-
e.preventDefault()
|
|
385
|
-
go(Math.max(0, Math.min(top, keys[e.key])), 0, false)
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// Tabbing onto a control hidden below the screen's edge grows the sheet to the top, so it shows.
|
|
389
|
-
const onFocus = (e: FocusEvent<HTMLDivElement>) => {
|
|
390
|
-
if (!bodyRef.current?.contains(e.target) || live.current.index === top) return
|
|
391
|
-
if (e.target.getBoundingClientRect().bottom > window.innerHeight - 4) go(top)
|
|
392
|
-
}
|
|
212
|
+
return sheet.holdTouch(body)
|
|
213
|
+
}, [mounted, sheet])
|
|
393
214
|
|
|
394
215
|
if (!mounted) return null
|
|
395
216
|
const shown = order[dragging ? target : index]
|
|
@@ -410,8 +231,8 @@ function DetentSheet({ open, onOpenChange, title, description, children, footer,
|
|
|
410
231
|
data-top={atTop || undefined}
|
|
411
232
|
data-dragging={dragging || undefined}
|
|
412
233
|
style={{ height: heights[top] }}
|
|
413
|
-
onPointerDown={
|
|
414
|
-
onFocus={
|
|
234
|
+
onPointerDown={sheet.down}
|
|
235
|
+
onFocus={(e) => sheet.focusIn(e.target)}
|
|
415
236
|
>
|
|
416
237
|
<div
|
|
417
238
|
ref={grabRef}
|
|
@@ -423,11 +244,11 @@ function DetentSheet({ open, onOpenChange, title, description, children, footer,
|
|
|
423
244
|
aria-valuemin={0}
|
|
424
245
|
aria-valuemax={top}
|
|
425
246
|
aria-valuenow={index}
|
|
426
|
-
aria-valuetext={
|
|
247
|
+
aria-valuetext={DETENT_NAME[order[index]]}
|
|
427
248
|
aria-controls={`${id}-sheet`}
|
|
428
249
|
data-detent={shown}
|
|
429
|
-
onKeyDown={
|
|
430
|
-
onClick={
|
|
250
|
+
onKeyDown={sheet.grabKey}
|
|
251
|
+
onClick={sheet.grabClick}
|
|
431
252
|
>
|
|
432
253
|
<span className={styles.pill} />
|
|
433
254
|
</div>
|