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.
- package/package.json +4 -2
- package/scripts/add.js +28 -6
- package/src/components/Accordion/Accordion.vue +62 -0
- package/src/components/Accordion/AccordionItem.vue +45 -0
- package/src/components/Accordion/accordionContext.ts +6 -0
- package/src/components/Alert/Alert.tsx +7 -27
- package/src/components/Alert/Alert.vue +59 -0
- package/src/components/AlertDialog/AlertDialog.vue +126 -0
- package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
- package/src/components/AspectRatio/AspectRatio.vue +17 -0
- package/src/components/Avatar/Avatar.vue +56 -0
- package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
- package/src/components/Badge/Badge.vue +32 -0
- package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
- package/src/components/Button/Button.vue +130 -0
- package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
- package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
- package/src/components/Card/Card.vue +34 -0
- package/src/components/Center/Center.vue +32 -0
- package/src/components/Checkbox/Checkbox.tsx +8 -25
- package/src/components/Checkbox/Checkbox.vue +108 -0
- package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
- package/src/components/Cluster/Cluster.vue +34 -0
- package/src/components/CodeBlock/CodeBlock.vue +45 -0
- package/src/components/Collapsible/Collapsible.vue +59 -0
- package/src/components/Container/Container.vue +22 -0
- package/src/components/CopyButton/CopyButton.tsx +3 -49
- package/src/components/CopyButton/CopyButton.vue +116 -0
- package/src/components/Dial/Dial.tsx +12 -120
- package/src/components/Dial/Dial.vue +228 -0
- package/src/components/Dialog/Dialog.vue +139 -0
- package/src/components/Divider/Divider.vue +27 -0
- package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
- package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
- package/src/components/EmptyState/EmptyState.vue +33 -0
- package/src/components/Fab/Fab.tsx +4 -14
- package/src/components/Fab/Fab.vue +115 -0
- package/src/components/Fieldset/Fieldset.vue +42 -0
- package/src/components/FormField/FormField.tsx +2 -17
- package/src/components/FormField/FormField.vue +64 -0
- package/src/components/Grid/Grid.vue +43 -0
- package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -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,168 @@
|
|
|
1
|
+
<script setup lang="ts" generic="T">
|
|
2
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
|
|
3
|
+
import { createSwipeDeck, type DeckDir, type DeckSide } from '../../internal/motion/swipeDeck'
|
|
4
|
+
import { UndoIcon } from '../../internal/vueIcons'
|
|
5
|
+
import styles from './SwipeDeck.module.css'
|
|
6
|
+
|
|
7
|
+
export type SwipeDeckSide = DeckDir
|
|
8
|
+
|
|
9
|
+
// The Vue SwipeDeck: the same look (SwipeDeck.module.css), drag, throw and undo as the React one. Each
|
|
10
|
+
// card is drawn by the card slot, which gets the item. Other attributes go to the outer box.
|
|
11
|
+
const props = withDefaults(
|
|
12
|
+
defineProps<{
|
|
13
|
+
/** The cards, top card first. The top card is the first whose key is not decided yet, so you may remove decided ones. */
|
|
14
|
+
items: T[]
|
|
15
|
+
/** A key that stays the same for each item. */
|
|
16
|
+
getKey: (item: T) => string
|
|
17
|
+
/** A short name for an item, said by screen readers after each decision. */
|
|
18
|
+
getName: (item: T) => string
|
|
19
|
+
/** Screen reader name of the deck, such as "Flats to rent". */
|
|
20
|
+
label: string
|
|
21
|
+
/** The two button words, for the start side then the end side. */
|
|
22
|
+
actions?: [string, string]
|
|
23
|
+
/** What screen readers say was done, start then end, such as "Passed" and "Saved". */
|
|
24
|
+
decided?: [string, string]
|
|
25
|
+
/** Screen reader name of the undo button. */
|
|
26
|
+
undoLabel?: string
|
|
27
|
+
/** Shown when every card has been decided. The empty slot replaces it. */
|
|
28
|
+
empty?: string
|
|
29
|
+
}>(),
|
|
30
|
+
{ actions: () => ['Pass', 'Save'], decided: () => ['Passed', 'Saved'], undoLabel: 'Undo the last decision', empty: 'That was the last card.' },
|
|
31
|
+
)
|
|
32
|
+
/** decide: once for each card, when it has been thrown or a button or key decided it. undo: Undo brought a card back; if you removed it from items, put it back here. */
|
|
33
|
+
const emit = defineEmits<{ decide: [item: T, side: SwipeDeckSide]; undo: [item: T] }>()
|
|
34
|
+
defineSlots<{ card(props: { item: T }): unknown; empty?: () => unknown }>()
|
|
35
|
+
|
|
36
|
+
type Done = { item: T; side: DeckSide; dir: SwipeDeckSide }
|
|
37
|
+
|
|
38
|
+
const done = shallowRef<Done[]>([])
|
|
39
|
+
const news = ref('')
|
|
40
|
+
const armed = ref<DeckSide | 0>(0)
|
|
41
|
+
const root = ref<HTMLElement | null>(null)
|
|
42
|
+
const deck = ref<HTMLElement | null>(null)
|
|
43
|
+
const knob = ref<HTMLElement | null>(null)
|
|
44
|
+
let top: HTMLElement | null = null
|
|
45
|
+
const buttons: Record<DeckSide, HTMLElement | null> = { [-1]: null, [1]: null }
|
|
46
|
+
// The top card is the first one not yet decided, found by key, so the app may trim or replace items.
|
|
47
|
+
const left = computed(() => {
|
|
48
|
+
const gone = new Set(done.value.map((d) => props.getKey(d.item)))
|
|
49
|
+
return props.items.filter((item) => !gone.has(props.getKey(item)))
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const commit = (side: DeckSide) => {
|
|
53
|
+
const [item, next] = left.value
|
|
54
|
+
if (!item) return
|
|
55
|
+
const count = done.value.length
|
|
56
|
+
const n = left.value.length
|
|
57
|
+
const dir = swipe.named(side)
|
|
58
|
+
done.value = [...done.value, { item, side, dir }]
|
|
59
|
+
const said = `${props.decided[dir === 'start' ? 0 : 1]}: ${props.getName(item)}.`
|
|
60
|
+
news.value = next ? `${said} Card ${count + 2} of ${count + n}: ${props.getName(next)}.` : `${said} No more cards.`
|
|
61
|
+
emit('decide', item, dir)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// The drag, the throw and undo's way back (motion/swipeDeck.ts).
|
|
65
|
+
const swipe = createSwipeDeck({
|
|
66
|
+
root: () => root.value,
|
|
67
|
+
deck: () => deck.value,
|
|
68
|
+
top: () => top,
|
|
69
|
+
knob: () => knob.value,
|
|
70
|
+
button: (side) => buttons[side],
|
|
71
|
+
setArmed: (side) => (armed.value = side),
|
|
72
|
+
hasCards: () => left.value.length > 0,
|
|
73
|
+
// The next card must be in place before the top card's transform resets, or the thrown card
|
|
74
|
+
// shows in the middle for a frame.
|
|
75
|
+
commit: (side) => {
|
|
76
|
+
commit(side)
|
|
77
|
+
return nextTick()
|
|
78
|
+
},
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const undo = () => {
|
|
82
|
+
if (!done.value.length) return
|
|
83
|
+
const last = done.value[done.value.length - 1]
|
|
84
|
+
if (!swipe.undo(last.side)) return
|
|
85
|
+
const count = done.value.length - 1
|
|
86
|
+
const n = left.value.length
|
|
87
|
+
done.value = done.value.slice(0, -1)
|
|
88
|
+
news.value = `Brought back: ${props.getName(last.item)}. Card ${count + 1} of ${count + n + 1}.`
|
|
89
|
+
emit('undo', last.item)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Undo plays the throw backwards, and a key pressed mid-move plays once the move has ended.
|
|
93
|
+
watch(() => done.value.length, swipe.changed, { flush: 'post' })
|
|
94
|
+
onMounted(swipe.mount)
|
|
95
|
+
onBeforeUnmount(swipe.destroy)
|
|
96
|
+
|
|
97
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
98
|
+
if (e.target !== e.currentTarget || e.altKey || e.ctrlKey || e.metaKey) return
|
|
99
|
+
if (e.key === 'ArrowLeft') swipe.fly(-1, 0)
|
|
100
|
+
else if (e.key === 'ArrowRight') swipe.fly(1, 0)
|
|
101
|
+
else if (e.key === 'Backspace') undo()
|
|
102
|
+
else return
|
|
103
|
+
e.preventDefault()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const shown = computed(() =>
|
|
107
|
+
left.value
|
|
108
|
+
.slice(0, 3)
|
|
109
|
+
.map((item, depth) => ({ item, depth }))
|
|
110
|
+
.reverse(),
|
|
111
|
+
)
|
|
112
|
+
const over = computed(() => !left.value.length)
|
|
113
|
+
const armedDir = computed(() => (armed.value ? swipe.named(armed.value) : undefined))
|
|
114
|
+
const setTop = (el: unknown) => (top = el as HTMLElement | null)
|
|
115
|
+
const buttonRef = (dir: SwipeDeckSide) => (el: unknown) => (buttons[dir === 'start' ? -1 : 1] = el as HTMLElement | null)
|
|
116
|
+
const choose = (dir: SwipeDeckSide) => !over.value && swipe.fly(swipe.physical(dir), 0)
|
|
117
|
+
</script>
|
|
118
|
+
|
|
119
|
+
<template>
|
|
120
|
+
<div ref="root" :class="styles.root" :data-armed="armedDir">
|
|
121
|
+
<div :class="styles.stage">
|
|
122
|
+
<div ref="deck" :class="styles.deck" role="group" aria-roledescription="card deck" :aria-label="label" aria-keyshortcuts="ArrowLeft ArrowRight Backspace" tabindex="0" @keydown="onKeyDown">
|
|
123
|
+
<div v-if="over" :class="styles.empty"><slot name="empty">{{ empty }}</slot></div>
|
|
124
|
+
<div
|
|
125
|
+
v-for="{ item, depth } in shown"
|
|
126
|
+
:key="getKey(item)"
|
|
127
|
+
:ref="depth === 0 ? setTop : undefined"
|
|
128
|
+
:class="styles.card"
|
|
129
|
+
:data-depth="depth"
|
|
130
|
+
:inert="depth > 0"
|
|
131
|
+
:aria-hidden="depth > 0 || undefined"
|
|
132
|
+
@pointerdown="depth === 0 && swipe.down($event)"
|
|
133
|
+
@pointermove="depth === 0 && swipe.move($event)"
|
|
134
|
+
@pointerup="depth === 0 && swipe.up($event)"
|
|
135
|
+
@pointercancel="depth === 0 && swipe.cancel($event)"
|
|
136
|
+
@click.capture="swipe.clickCapture"
|
|
137
|
+
@dragstart.prevent
|
|
138
|
+
>
|
|
139
|
+
<slot name="card" :item="item" />
|
|
140
|
+
</div>
|
|
141
|
+
</div>
|
|
142
|
+
</div>
|
|
143
|
+
<div :class="styles.ruler" aria-hidden="true">
|
|
144
|
+
<span :class="styles.mark" data-side="left" :data-armed="armed === -1 || undefined" />
|
|
145
|
+
<span :class="styles.centre" />
|
|
146
|
+
<span :class="styles.mark" data-side="right" :data-armed="armed === 1 || undefined" />
|
|
147
|
+
<span ref="knob" :class="styles.knob" :data-armed="armed !== 0 || undefined" />
|
|
148
|
+
</div>
|
|
149
|
+
<div :class="styles.bar">
|
|
150
|
+
<button :ref="buttonRef('start')" type="button" :class="styles.choice" data-side="start" :aria-disabled="over || undefined" @click="choose('start')">
|
|
151
|
+
<span :class="styles.word">{{ actions[0] }}</span>
|
|
152
|
+
<span :class="styles.fill" aria-hidden="true">
|
|
153
|
+
<span :class="styles.word">{{ actions[0] }}</span>
|
|
154
|
+
</span>
|
|
155
|
+
</button>
|
|
156
|
+
<button type="button" :class="styles.undo" :aria-label="undoLabel" :aria-disabled="!done.length || undefined" @click="undo">
|
|
157
|
+
<UndoIcon />
|
|
158
|
+
</button>
|
|
159
|
+
<button :ref="buttonRef('end')" type="button" :class="styles.choice" data-side="end" :aria-disabled="over || undefined" @click="choose('end')">
|
|
160
|
+
<span :class="styles.word">{{ actions[1] }}</span>
|
|
161
|
+
<span :class="styles.fill" aria-hidden="true">
|
|
162
|
+
<span :class="styles.word">{{ actions[1] }}</span>
|
|
163
|
+
</span>
|
|
164
|
+
</button>
|
|
165
|
+
</div>
|
|
166
|
+
<span :class="styles.sr" role="status" aria-live="polite" aria-atomic="true">{{ news }}</span>
|
|
167
|
+
</div>
|
|
168
|
+
</template>
|
|
@@ -2,15 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
import { useId, useLayoutEffect, useRef, type ComponentPropsWithRef, type ReactNode } from 'react'
|
|
4
4
|
import { cx } from '../../internal/cx'
|
|
5
|
-
import {
|
|
5
|
+
import { createSwitchThumb } from '../../internal/motion/switchThumb'
|
|
6
6
|
import { useControllable } from '../../internal/useControllable'
|
|
7
7
|
import styles from './Switch.module.css'
|
|
8
8
|
|
|
9
|
-
// The front edge moves over this share of the time; the back edge starts the rest of the way in.
|
|
10
|
-
const LEAD = 0.75
|
|
11
|
-
// The longest the thumb gets, as a share of its round size.
|
|
12
|
-
const STRETCH = 1.4
|
|
13
|
-
|
|
14
9
|
export type SwitchProps = Omit<ComponentPropsWithRef<'button'>, 'onChange' | 'value'> & {
|
|
15
10
|
/** Visible text next to the switch, also its accessible name. */
|
|
16
11
|
label: ReactNode
|
|
@@ -47,76 +42,12 @@ export function Switch({
|
|
|
47
42
|
const switchId = id ?? auto
|
|
48
43
|
const [checked, setChecked] = useControllable(checkedProp, defaultChecked, onCheckedChange)
|
|
49
44
|
const describedBy = [description && `${switchId}-desc`, describedByProp].filter(Boolean).join(' ')
|
|
50
|
-
// The thumb
|
|
51
|
-
// shape. The on fill's edge rides on the thumb's edge, so the colour never sits apart from it.
|
|
45
|
+
// The thumb's stretch (motion/switchThumb.ts).
|
|
52
46
|
const thumbRef = useRef<HTMLSpanElement>(null)
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
run.current?.stop()
|
|
58
|
-
run.current = null
|
|
59
|
-
if (!thumb) return
|
|
60
|
-
thumb.style.removeProperty('transform')
|
|
61
|
-
thumb.style.removeProperty('width')
|
|
62
|
-
const track = thumb.parentElement
|
|
63
|
-
if (track) {
|
|
64
|
-
delete track.dataset.moving
|
|
65
|
-
track.style.removeProperty('--fill')
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
useLayoutEffect(() => {
|
|
69
|
-
const thumb = thumbRef.current
|
|
70
|
-
const track = thumb?.parentElement
|
|
71
|
-
if (was.current === checked || !thumb || !track) return
|
|
72
|
-
was.current = checked
|
|
73
|
-
const ms = motionFor('slow')
|
|
74
|
-
if (!ms) {
|
|
75
|
-
end()
|
|
76
|
-
return
|
|
77
|
-
}
|
|
78
|
-
const s = getComputedStyle(track)
|
|
79
|
-
const line = parseFloat(s.borderLeftWidth) || 0
|
|
80
|
-
const pad = parseFloat(s.getPropertyValue('--pad')) || 0
|
|
81
|
-
const size = thumb.offsetHeight
|
|
82
|
-
const rest = thumb.offsetLeft
|
|
83
|
-
const offX = rest
|
|
84
|
-
const onX = track.clientWidth - rest - size
|
|
85
|
-
const to = checked ? onX : offX
|
|
86
|
-
const from = run.current ? { a: run.current.a, b: run.current.b } : { a: checked ? offX : onX, b: (checked ? offX : onX) + size }
|
|
87
|
-
run.current?.stop()
|
|
88
|
-
const ahead = to + size / 2 >= (from.a + from.b) / 2
|
|
89
|
-
const e = morphEase()
|
|
90
|
-
const outer = track.offsetWidth
|
|
91
|
-
const live = { ...from, stop: () => {} }
|
|
92
|
-
run.current = live
|
|
93
|
-
track.dataset.moving = ''
|
|
94
|
-
live.stop = frames(
|
|
95
|
-
ms,
|
|
96
|
-
(u) => {
|
|
97
|
-
const lead = e(Math.min(1, u / LEAD))
|
|
98
|
-
const trail = e(Math.max(0, (u - 1 + LEAD) / LEAD))
|
|
99
|
-
live.a = from.a + (to - from.a) * (ahead ? trail : lead)
|
|
100
|
-
live.b = from.b + (to + size - from.b) * (ahead ? lead : trail)
|
|
101
|
-
// A short pill at most, so the fill behind it stays in view.
|
|
102
|
-
const long = live.b - live.a - size * STRETCH
|
|
103
|
-
if (long > 0) {
|
|
104
|
-
if (ahead) live.a += long
|
|
105
|
-
else live.b -= long
|
|
106
|
-
}
|
|
107
|
-
thumb.style.transform = `translateX(${live.a - rest}px)`
|
|
108
|
-
thumb.style.width = `${live.b - live.a}px`
|
|
109
|
-
// How far on the thumb is, 0 to 1: the fill opens out to the track's edge as it goes.
|
|
110
|
-
const p = Math.min(1, Math.max(0, ((live.a + live.b) / 2 - offX - size / 2) / (onX - offX)))
|
|
111
|
-
const side = (pad + line) * (1 - p)
|
|
112
|
-
const right = outer - live.b - line + line * (1 - p) - pad * p
|
|
113
|
-
track.style.setProperty('--fill', `inset(${side}px ${Math.max(0, right)}px ${side}px ${side}px round var(--gooee-radius-full))`)
|
|
114
|
-
},
|
|
115
|
-
end,
|
|
116
|
-
)
|
|
117
|
-
}, [checked])
|
|
118
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
119
|
-
useLayoutEffect(() => () => end(), [])
|
|
47
|
+
const thumb = useRef<ReturnType<typeof createSwitchThumb>>(null)
|
|
48
|
+
thumb.current ??= createSwitchThumb(() => thumbRef.current, checked)
|
|
49
|
+
useLayoutEffect(() => thumb.current!.set(checked), [checked])
|
|
50
|
+
useLayoutEffect(() => () => thumb.current!.destroy(), [])
|
|
120
51
|
return (
|
|
121
52
|
<div className={cx(styles.root, labelPosition === 'start' && styles.start, className)} data-disabled={disabled || undefined}>
|
|
122
53
|
<button
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onBeforeUnmount, ref, useAttrs, useId, watch } from 'vue'
|
|
3
|
+
import { createSwitchThumb } from '../../internal/motion/switchThumb'
|
|
4
|
+
import styles from './Switch.module.css'
|
|
5
|
+
|
|
6
|
+
// The Vue Switch: the same look (Switch.module.css) and thumb motion as the React one.
|
|
7
|
+
// Other attributes, such as aria-label or a click listener, go to the switch button.
|
|
8
|
+
defineOptions({ inheritAttrs: false })
|
|
9
|
+
|
|
10
|
+
const props = withDefaults(
|
|
11
|
+
defineProps<{
|
|
12
|
+
/** Visible text next to the switch, also its accessible name. The label slot replaces it. */
|
|
13
|
+
label?: string
|
|
14
|
+
/** Extra text under the label, also read by screen readers. The description slot replaces it. */
|
|
15
|
+
description?: string
|
|
16
|
+
/** Puts the label before the switch, like a settings row. */
|
|
17
|
+
labelPosition?: 'start' | 'end'
|
|
18
|
+
/** Form field name. The form gets "on" while the switch is on, and nothing while it is off. */
|
|
19
|
+
name?: string
|
|
20
|
+
disabled?: boolean
|
|
21
|
+
id?: string
|
|
22
|
+
}>(),
|
|
23
|
+
{ labelPosition: 'end' },
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
/** Whether the switch is on. Use v-model:checked, or leave it out and the switch keeps its own state. */
|
|
27
|
+
const checked = defineModel<boolean>('checked', { default: false })
|
|
28
|
+
const slots = defineSlots<{ label?: () => unknown; description?: () => unknown }>()
|
|
29
|
+
|
|
30
|
+
const auto = useId()
|
|
31
|
+
const switchId = computed(() => props.id ?? auto)
|
|
32
|
+
const hasDescription = computed(() => !!(props.description || slots.description))
|
|
33
|
+
const attrs = useAttrs()
|
|
34
|
+
const describedBy = computed(() => [hasDescription.value && `${switchId.value}-desc`, attrs['aria-describedby']].filter(Boolean).join(' ') || undefined)
|
|
35
|
+
|
|
36
|
+
// The thumb's stretch (motion/switchThumb.ts), played after the DOM has the new state.
|
|
37
|
+
const thumbEl = ref<HTMLElement | null>(null)
|
|
38
|
+
const thumb = createSwitchThumb(() => thumbEl.value, checked.value)
|
|
39
|
+
watch(checked, (on) => thumb.set(on), { flush: 'post' })
|
|
40
|
+
onBeforeUnmount(thumb.destroy)
|
|
41
|
+
</script>
|
|
42
|
+
|
|
43
|
+
<template>
|
|
44
|
+
<div :class="[styles.root, labelPosition === 'start' && styles.start]" :data-disabled="disabled || undefined">
|
|
45
|
+
<button
|
|
46
|
+
v-bind="$attrs"
|
|
47
|
+
:id="switchId"
|
|
48
|
+
type="button"
|
|
49
|
+
role="switch"
|
|
50
|
+
:aria-checked="checked"
|
|
51
|
+
:aria-describedby="describedBy"
|
|
52
|
+
:class="styles.track"
|
|
53
|
+
:data-on="checked || undefined"
|
|
54
|
+
:disabled="disabled"
|
|
55
|
+
@click="checked = !checked"
|
|
56
|
+
>
|
|
57
|
+
<span ref="thumbEl" :class="styles.thumb" aria-hidden="true" />
|
|
58
|
+
</button>
|
|
59
|
+
<span :class="styles.text">
|
|
60
|
+
<label :for="switchId" :class="styles.label"><slot name="label">{{ label }}</slot></label>
|
|
61
|
+
<span v-if="hasDescription" :id="`${switchId}-desc`" :class="styles.description"><slot name="description">{{ description }}</slot></span>
|
|
62
|
+
</span>
|
|
63
|
+
<!-- Submits like a checkbox: "on" when on, nothing when off -->
|
|
64
|
+
<input v-if="name && checked" type="hidden" :name="name" value="on" />
|
|
65
|
+
</div>
|
|
66
|
+
</template>
|
|
@@ -4,14 +4,23 @@
|
|
|
4
4
|
position: relative;
|
|
5
5
|
container-type: inline-size;
|
|
6
6
|
min-width: 0;
|
|
7
|
-
|
|
8
|
-
outline: var(--gooee-line) solid var(--gooee-color-border);
|
|
9
|
-
outline-offset: calc(var(--gooee-line) * -1);
|
|
7
|
+
isolation: isolate;
|
|
10
8
|
border-radius: var(--gooee-radius-panel);
|
|
11
9
|
background: var(--gooee-color-surface);
|
|
12
10
|
overflow: hidden;
|
|
13
11
|
}
|
|
14
12
|
|
|
13
|
+
/* The frame is a layer over the rows, so sticky cells and the fade (::after) never cover it. Inside the edge, it takes no space. */
|
|
14
|
+
.root::before {
|
|
15
|
+
content: "";
|
|
16
|
+
position: absolute;
|
|
17
|
+
inset: 0;
|
|
18
|
+
z-index: 3;
|
|
19
|
+
border: var(--gooee-line) solid var(--gooee-color-border);
|
|
20
|
+
border-radius: inherit;
|
|
21
|
+
pointer-events: none;
|
|
22
|
+
}
|
|
23
|
+
|
|
15
24
|
.scroll {
|
|
16
25
|
overflow: auto;
|
|
17
26
|
/* Room for the sticky header row and first column, so focus never lands under them. */
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState, type CSSProperties, type FocusEvent, type ReactNode } from 'react'
|
|
4
4
|
import { cx } from '../../internal/cx'
|
|
5
|
-
import {
|
|
5
|
+
import { flipRows, groupRows, nextSort, readCell as read, revealCell, scrollEdges, sortRows, type TableSort } from '../../internal/motion/table'
|
|
6
6
|
import { useControllable } from '../../internal/useControllable'
|
|
7
7
|
import { Skeleton } from '../Skeleton'
|
|
8
8
|
import styles from './Table.module.css'
|
|
@@ -21,7 +21,7 @@ export type TableColumn<T> = {
|
|
|
21
21
|
width?: CSSProperties['width']
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export type
|
|
24
|
+
export type { TableSort }
|
|
25
25
|
|
|
26
26
|
export type TableLabels = {
|
|
27
27
|
/** Name of the checkbox in the header. */
|
|
@@ -112,8 +112,6 @@ function Check({ checked, mixed, label, disabled, onChange }: { checked: boolean
|
|
|
112
112
|
)
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
const read = (row: unknown, key: string) => (row as Record<string, unknown>)[key]
|
|
116
|
-
|
|
117
115
|
export function Table<T>({
|
|
118
116
|
columns,
|
|
119
117
|
rows,
|
|
@@ -145,24 +143,11 @@ export function Table<T>({
|
|
|
145
143
|
const measure = () => {
|
|
146
144
|
const el = scrollRef.current
|
|
147
145
|
if (!el) return
|
|
148
|
-
const start = el
|
|
149
|
-
const end = el.scrollLeft + el.clientWidth < el.scrollWidth - 1
|
|
146
|
+
const { start, end } = scrollEdges(el)
|
|
150
147
|
setMore((m) => (m.start === start && m.end === end ? m : { start, end }))
|
|
151
|
-
// The sticky first column's width, so a cell that takes focus scrolls clear of it (WCAG 2.4.11).
|
|
152
|
-
const first = el.querySelector<HTMLElement>('thead th')
|
|
153
|
-
el.style.setProperty('--first-w', first && getComputedStyle(first).left === '0px' ? `${first.offsetWidth}px` : '0px')
|
|
154
148
|
}
|
|
155
|
-
// Browsers do not scroll a sticky header cell into view on focus, so its button could stay under the
|
|
156
|
-
// sticky first column. Nudge the sideways scroll until the focused cell is clear on both sides.
|
|
157
149
|
const reveal = (e: FocusEvent<HTMLDivElement>) => {
|
|
158
|
-
|
|
159
|
-
const cell = e.target.closest('th, td')
|
|
160
|
-
if (!el || !cell || cell === cell.parentElement?.firstElementChild) return
|
|
161
|
-
const box = el.getBoundingClientRect()
|
|
162
|
-
const r = cell.getBoundingClientRect()
|
|
163
|
-
const under = r.left - box.left - (parseFloat(el.style.getPropertyValue('--first-w')) || 0)
|
|
164
|
-
if (under < 0) el.scrollLeft += under
|
|
165
|
-
else if (r.right > box.right) el.scrollLeft += r.right - box.right
|
|
150
|
+
if (scrollRef.current) revealCell(scrollRef.current, e.target)
|
|
166
151
|
}
|
|
167
152
|
useEffect(() => {
|
|
168
153
|
const el = scrollRef.current
|
|
@@ -180,28 +165,9 @@ export function Table<T>({
|
|
|
180
165
|
const lastShape = useRef('')
|
|
181
166
|
const groupOrder = useRef<string[]>([])
|
|
182
167
|
|
|
183
|
-
const sorted = useMemo(() =>
|
|
184
|
-
if (!sort) return rows
|
|
185
|
-
const col = columns.find((c) => c.key === sort.key)
|
|
186
|
-
if (!col) return rows
|
|
187
|
-
const value = (r: T) => (col.sortValue ? col.sortValue(r) : (read(r, col.key) as string | number))
|
|
188
|
-
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' })
|
|
189
|
-
const out = [...rows].sort((a, b) => {
|
|
190
|
-
const va = value(a)
|
|
191
|
-
const vb = value(b)
|
|
192
|
-
const c = typeof va === 'number' && typeof vb === 'number' ? va - vb : collator.compare(String(va ?? ''), String(vb ?? ''))
|
|
193
|
-
return sort.direction === 'ascending' ? c : -c
|
|
194
|
-
})
|
|
195
|
-
return out
|
|
196
|
-
}, [rows, columns, sort])
|
|
168
|
+
const sorted = useMemo(() => sortRows(rows, columns, sort), [rows, columns, sort])
|
|
197
169
|
|
|
198
|
-
|
|
199
|
-
// never shuffles the groups themselves. A group seen for the first time joins at the end.
|
|
200
|
-
let groups: Array<{ key: string; rows: T[] }> | null = null
|
|
201
|
-
if (groupBy) {
|
|
202
|
-
for (const key of new Set(rows.map(groupBy))) if (!groupOrder.current.includes(key)) groupOrder.current.push(key)
|
|
203
|
-
groups = groupOrder.current.map((key) => ({ key, rows: sorted.filter((r) => groupBy(r) === key) })).filter((g) => g.rows.length)
|
|
204
|
-
}
|
|
170
|
+
const groups = groupBy ? groupRows(rows, sorted, groupBy, groupOrder.current) : null
|
|
205
171
|
const shape = groups ? groups.map((g) => g.key).join('|') : ''
|
|
206
172
|
const order = groups ? groups.map((g) => `${g.key}:${g.rows.map(getRowId).join(',')}`).join('|') : sorted.map(getRowId).join('|')
|
|
207
173
|
|
|
@@ -210,31 +176,14 @@ export function Table<T>({
|
|
|
210
176
|
useLayoutEffect(() => {
|
|
211
177
|
const table = tableRef.current
|
|
212
178
|
if (!table) return
|
|
213
|
-
|
|
214
|
-
const ease = easing(shape !== lastShape.current ? 'spring' : 'out')
|
|
179
|
+
lastTops.current = flipRows(table, lastTops.current, shape !== lastShape.current)
|
|
215
180
|
lastShape.current = shape
|
|
216
|
-
const now = new Map<string, number>()
|
|
217
|
-
table.querySelectorAll<HTMLElement>('tbody [data-row-id]').forEach((tr) => {
|
|
218
|
-
const rid = tr.dataset.rowId!
|
|
219
|
-
const top = tr.offsetTop
|
|
220
|
-
now.set(rid, top)
|
|
221
|
-
const before = lastTops.current.get(rid)
|
|
222
|
-
if (before !== undefined && before !== top && duration > 0) {
|
|
223
|
-
tr.animate([{ transform: `translateY(${before - top}px)` }, { transform: 'none' }], { duration, easing: ease })
|
|
224
|
-
}
|
|
225
|
-
})
|
|
226
|
-
lastTops.current = now
|
|
227
181
|
}, [order])
|
|
228
182
|
|
|
229
183
|
const labelOf = (c: TableColumn<T>) => c.label ?? (typeof c.header === 'string' ? c.header : c.key)
|
|
230
184
|
|
|
231
185
|
const toggleSort = (key: string) => {
|
|
232
|
-
const
|
|
233
|
-
const next: TableSort | null = !same
|
|
234
|
-
? { key, direction: 'ascending' }
|
|
235
|
-
: sort.direction === 'ascending'
|
|
236
|
-
? { key, direction: 'descending' }
|
|
237
|
-
: null
|
|
186
|
+
const next = nextSort(sort, key)
|
|
238
187
|
setSort(next)
|
|
239
188
|
const col = columns.find((c) => c.key === key)
|
|
240
189
|
const name = col ? labelOf(col) : key
|