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
|
@@ -5,6 +5,7 @@ import { cx } from '../../internal/cx'
|
|
|
5
5
|
import { focusables } from '../../internal/focus'
|
|
6
6
|
import { XIcon } from '../../internal/icons'
|
|
7
7
|
import { useDismiss } from '../../internal/layers'
|
|
8
|
+
import { morphInsets, observeMorphButton, placeMorphButton, takesFocus, type MorphCorner } from '../../internal/motion/morphButton'
|
|
8
9
|
import { useControllable } from '../../internal/useControllable'
|
|
9
10
|
import styles from './MorphButton.module.css'
|
|
10
11
|
|
|
@@ -31,11 +32,6 @@ export type MorphButtonProps = {
|
|
|
31
32
|
className?: string
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
type Corner = 'tl' | 'tr' | 'bl' | 'br'
|
|
35
|
-
|
|
36
|
-
// The space kept between the panel and the edge of the screen.
|
|
37
|
-
const EDGE = 16
|
|
38
|
-
|
|
39
35
|
export function MorphButton({
|
|
40
36
|
label,
|
|
41
37
|
icon,
|
|
@@ -50,7 +46,7 @@ export function MorphButton({
|
|
|
50
46
|
}: MorphButtonProps) {
|
|
51
47
|
const id = useId()
|
|
52
48
|
const [open, setOpen] = useControllable(openProp, defaultOpen, onOpenChange)
|
|
53
|
-
const [corner, setCorner] = useState<
|
|
49
|
+
const [corner, setCorner] = useState<MorphCorner>('tl')
|
|
54
50
|
const [room, setRoom] = useState<number>()
|
|
55
51
|
const [box, setBox] = useState({ tw: 0, th: 0, pw: 0, ph: 0 })
|
|
56
52
|
const anchorRef = useRef<HTMLDivElement>(null)
|
|
@@ -58,60 +54,20 @@ export function MorphButton({
|
|
|
58
54
|
const triggerRef = useRef<HTMLButtonElement>(null)
|
|
59
55
|
const panelRef = useRef<HTMLDivElement>(null)
|
|
60
56
|
|
|
61
|
-
//
|
|
62
|
-
// window. The panel never grows past its sides, so it fits whatever it sits in, not only the screen.
|
|
63
|
-
const sides = () => {
|
|
64
|
-
let clip: Element | null = null
|
|
65
|
-
for (let e = anchorRef.current?.parentElement; e && e !== document.body; e = e.parentElement)
|
|
66
|
-
if (getComputedStyle(e).overflowX !== 'visible') {
|
|
67
|
-
clip = e
|
|
68
|
-
break
|
|
69
|
-
}
|
|
70
|
-
const c = clip?.getBoundingClientRect()
|
|
71
|
-
return { clip, left: Math.max(0, c?.left ?? 0), right: Math.min(window.innerWidth, c?.right ?? window.innerWidth), top: c?.top ?? 0, bottom: c?.bottom ?? window.innerHeight }
|
|
72
|
-
}
|
|
73
|
-
|
|
57
|
+
// Measured now and on every resize, and placed toward the side with room (motion/morphButton.ts).
|
|
74
58
|
useLayoutEffect(() => {
|
|
75
59
|
const t = triggerRef.current
|
|
76
60
|
const s = shellRef.current
|
|
77
61
|
if (!t || !s) return
|
|
78
|
-
|
|
79
|
-
// Shut, the panel's box is already no wider than the room it has, so it never overflows its box.
|
|
80
|
-
const fit = () => {
|
|
81
|
-
if (s.dataset.open !== undefined) return
|
|
82
|
-
const a = anchorRef.current!.getBoundingClientRect(), { left, right } = sides()
|
|
83
|
-
setRoom(Math.max(t.offsetWidth, s.dataset.corner?.endsWith('r') ? a.right - left - EDGE : right - EDGE - a.left))
|
|
84
|
-
}
|
|
85
|
-
measure()
|
|
86
|
-
fit()
|
|
87
|
-
const ro = new ResizeObserver(() => {
|
|
88
|
-
measure()
|
|
89
|
-
fit()
|
|
90
|
-
})
|
|
91
|
-
ro.observe(t)
|
|
92
|
-
ro.observe(s)
|
|
93
|
-
const clip = sides().clip
|
|
94
|
-
if (clip) ro.observe(clip)
|
|
95
|
-
return () => ro.disconnect()
|
|
62
|
+
return observeMorphButton(anchorRef.current!, t, s, setBox, setRoom)
|
|
96
63
|
}, [])
|
|
97
64
|
|
|
98
|
-
// It grows toward the side with room, decided before the move starts and never during it.
|
|
99
65
|
const place = () => {
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
if (!
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const left = a.right - L - EDGE
|
|
106
|
-
// Up or down: first the way that fits both its box and the screen, then the way that fits its box
|
|
107
|
-
// (a card that clips it would cut it; the page can still scroll), then the way with more room.
|
|
108
|
-
const below = { box: B - a.top, screen: window.innerHeight - EDGE - a.top }
|
|
109
|
-
const above = { box: a.bottom - T, screen: a.bottom - EDGE }
|
|
110
|
-
const fits = (r: { box: number; screen: number }, both: boolean) => r.box >= box.ph && (!both || r.screen >= box.ph)
|
|
111
|
-
const up = fits(below, true) ? false : fits(above, true) ? true : fits(below, false) ? false : fits(above, false) ? true : above.box > below.box
|
|
112
|
-
const flipX = right < box.pw && left > right
|
|
113
|
-
setCorner(`${up ? 'b' : 't'}${flipX ? 'r' : 'l'}` as Corner)
|
|
114
|
-
setRoom(Math.max(box.tw, flipX ? left : right))
|
|
66
|
+
if (!shellRef.current) return
|
|
67
|
+
const at = placeMorphButton(anchorRef.current, box)
|
|
68
|
+
if (!at) return
|
|
69
|
+
setCorner(at.corner)
|
|
70
|
+
setRoom(at.room)
|
|
115
71
|
}
|
|
116
72
|
|
|
117
73
|
const show = (next: boolean) => {
|
|
@@ -139,26 +95,14 @@ export function MorphButton({
|
|
|
139
95
|
[shellRef],
|
|
140
96
|
(reason, e) => {
|
|
141
97
|
setOpen(false)
|
|
142
|
-
const target = e.target instanceof Element ? e.target : null
|
|
143
98
|
if (reason === 'escape') backToButton()
|
|
144
|
-
else if (e.type === 'pointerdown' && !target
|
|
99
|
+
else if (e.type === 'pointerdown' && !takesFocus(e.target)) window.setTimeout(backToButton)
|
|
145
100
|
},
|
|
146
101
|
{ focusOut: true },
|
|
147
102
|
)
|
|
148
103
|
|
|
149
104
|
// Shut, the surface is cut back to the button's own box in the chosen corner.
|
|
150
|
-
const
|
|
151
|
-
const dy = Math.max(0, box.ph - box.th)
|
|
152
|
-
const shut = { tl: [0, dx, dy, 0], tr: [0, 0, dy, dx], bl: [dy, dx, 0, 0], br: [dy, 0, 0, dx] }[corner]
|
|
153
|
-
const [t, r, b, l] = open ? [0, 0, 0, 0] : shut
|
|
154
|
-
const vars = {
|
|
155
|
-
'--t': `${t}px`,
|
|
156
|
-
'--r': `${r}px`,
|
|
157
|
-
'--b': `${b}px`,
|
|
158
|
-
'--l': `${l}px`,
|
|
159
|
-
'--pill': `${box.th / 2}px`,
|
|
160
|
-
maxWidth: room,
|
|
161
|
-
} as CSSProperties
|
|
105
|
+
const vars = { ...morphInsets(corner, box, open), maxWidth: room } as CSSProperties
|
|
162
106
|
|
|
163
107
|
return (
|
|
164
108
|
<div ref={anchorRef} className={cx(styles.anchor, className)} style={{ width: box.tw || undefined, height: box.th || undefined }}>
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onBeforeUnmount, onMounted, ref, useId, useSlots, watch } from 'vue'
|
|
3
|
+
import { focusables } from '../../internal/motion/focus'
|
|
4
|
+
import { dismissable } from '../../internal/motion/layers'
|
|
5
|
+
import { morphInsets, observeMorphButton, placeMorphButton, takesFocus, type MorphBox, type MorphCorner } from '../../internal/motion/morphButton'
|
|
6
|
+
import { XIcon } from '../../internal/vueIcons'
|
|
7
|
+
import styles from './MorphButton.module.css'
|
|
8
|
+
|
|
9
|
+
// The Vue MorphButton: the same look (MorphButton.module.css), growth and dismiss as the React one. The
|
|
10
|
+
// panel's content goes in the default slot, the icon in the icon slot.
|
|
11
|
+
const props = withDefaults(
|
|
12
|
+
defineProps<{
|
|
13
|
+
/** The button's text. It stays on the same spot and becomes the panel's title and name. The label slot replaces it. */
|
|
14
|
+
label?: string
|
|
15
|
+
/** Starting open state when there is no v-model. */
|
|
16
|
+
defaultOpen?: boolean
|
|
17
|
+
/** How wide the panel grows. It never runs past the edge of the screen. */
|
|
18
|
+
width?: 'sm' | 'md'
|
|
19
|
+
/** Screen reader name for the close button. */
|
|
20
|
+
closeLabel?: string
|
|
21
|
+
/** Element to focus when it opens. Defaults to the first control in the panel. */
|
|
22
|
+
initialFocus?: HTMLElement | null
|
|
23
|
+
}>(),
|
|
24
|
+
{ defaultOpen: false, width: 'sm', closeLabel: 'Close' },
|
|
25
|
+
)
|
|
26
|
+
/** Whether the panel is open. Use v-model:open, or leave it out and the button keeps its own state. */
|
|
27
|
+
const model = defineModel<boolean | undefined>('open', { type: Boolean, default: undefined })
|
|
28
|
+
const slots = useSlots() as { default?: () => unknown; label?: () => unknown; icon?: () => unknown }
|
|
29
|
+
|
|
30
|
+
const id = useId()
|
|
31
|
+
const own = ref(props.defaultOpen)
|
|
32
|
+
const open = computed(() => model.value ?? own.value)
|
|
33
|
+
const setOpen = (next: boolean) => {
|
|
34
|
+
own.value = next
|
|
35
|
+
model.value = next
|
|
36
|
+
}
|
|
37
|
+
const corner = ref<MorphCorner>('tl')
|
|
38
|
+
const room = ref<number>()
|
|
39
|
+
const box = ref<MorphBox>({ tw: 0, th: 0, pw: 0, ph: 0 })
|
|
40
|
+
const anchorEl = ref<HTMLElement | null>(null)
|
|
41
|
+
const shellEl = ref<HTMLElement | null>(null)
|
|
42
|
+
const triggerEl = ref<HTMLButtonElement | null>(null)
|
|
43
|
+
const panelEl = ref<HTMLElement | null>(null)
|
|
44
|
+
|
|
45
|
+
// Measured now and on every resize, and placed toward the side with room (motion/morphButton.ts).
|
|
46
|
+
let stopObserve: (() => void) | undefined
|
|
47
|
+
onMounted(() => {
|
|
48
|
+
if (!triggerEl.value || !shellEl.value) return
|
|
49
|
+
stopObserve = observeMorphButton(anchorEl.value!, triggerEl.value, shellEl.value, (b) => (box.value = b), (r) => (room.value = r))
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const place = () => {
|
|
53
|
+
if (!shellEl.value) return
|
|
54
|
+
const at = placeMorphButton(anchorEl.value, box.value)
|
|
55
|
+
if (!at) return
|
|
56
|
+
corner.value = at.corner
|
|
57
|
+
room.value = at.room
|
|
58
|
+
}
|
|
59
|
+
const show = (next: boolean) => {
|
|
60
|
+
if (next) place()
|
|
61
|
+
setOpen(next)
|
|
62
|
+
}
|
|
63
|
+
const backToButton = () => triggerEl.value?.focus({ preventScroll: true })
|
|
64
|
+
|
|
65
|
+
// Esc, a press outside, or focus moving on closes it. A layer it opened, such as a Select's list, is
|
|
66
|
+
// above it, so picking there does not count. Focus goes back to the button unless the press landed on
|
|
67
|
+
// something that takes focus itself.
|
|
68
|
+
let stopDismiss: (() => void) | undefined
|
|
69
|
+
// A controlled open from outside picks its side too; then focus moves into the panel.
|
|
70
|
+
const onOpen = (now: boolean) => {
|
|
71
|
+
stopDismiss?.()
|
|
72
|
+
stopDismiss = undefined
|
|
73
|
+
if (!now) return
|
|
74
|
+
place()
|
|
75
|
+
stopDismiss = dismissable(
|
|
76
|
+
() => [shellEl.value],
|
|
77
|
+
(reason, e) => {
|
|
78
|
+
setOpen(false)
|
|
79
|
+
if (reason === 'escape') backToButton()
|
|
80
|
+
else if (e.type === 'pointerdown' && !takesFocus(e.target)) window.setTimeout(backToButton)
|
|
81
|
+
},
|
|
82
|
+
{ focusOut: true },
|
|
83
|
+
)
|
|
84
|
+
if (!panelEl.value) return
|
|
85
|
+
const first = props.initialFocus ?? focusables(panelEl.value).find((el) => !el.hasAttribute('data-skip-initial-focus')) ?? panelEl.value
|
|
86
|
+
first.focus({ preventScroll: true })
|
|
87
|
+
}
|
|
88
|
+
watch(open, onOpen, { flush: 'post' })
|
|
89
|
+
onMounted(() => open.value && onOpen(true))
|
|
90
|
+
onBeforeUnmount(() => {
|
|
91
|
+
stopObserve?.()
|
|
92
|
+
stopDismiss?.()
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
const vars = computed(() => ({ ...morphInsets(corner.value, box.value, open.value), maxWidth: room.value === undefined ? undefined : `${room.value}px` }))
|
|
96
|
+
const close = () => {
|
|
97
|
+
setOpen(false)
|
|
98
|
+
backToButton()
|
|
99
|
+
}
|
|
100
|
+
</script>
|
|
101
|
+
|
|
102
|
+
<template>
|
|
103
|
+
<div ref="anchorEl" :class="styles.anchor" :style="{ width: box.tw ? `${box.tw}px` : undefined, height: box.th ? `${box.th}px` : undefined }">
|
|
104
|
+
<div ref="shellEl" :class="styles.shell" :data-open="open || undefined" :data-corner="corner" :data-width="width" :style="vars">
|
|
105
|
+
<div :class="styles.surface" aria-hidden="true" />
|
|
106
|
+
<div :class="styles.content">
|
|
107
|
+
<button ref="triggerEl" type="button" :class="styles.trigger" :aria-expanded="open" aria-haspopup="dialog" :aria-controls="`${id}-panel`" @click="show(!open)">
|
|
108
|
+
<span v-if="slots.icon" :class="styles.icon" aria-hidden="true"><slot name="icon" /></span>
|
|
109
|
+
<span :id="`${id}-label`"><slot name="label">{{ label }}</slot></span>
|
|
110
|
+
</button>
|
|
111
|
+
<div ref="panelEl" :id="`${id}-panel`" role="dialog" :aria-labelledby="`${id}-label`" tabindex="-1" :class="styles.panel" :inert="!open" :aria-hidden="!open || undefined">
|
|
112
|
+
<button type="button" :class="styles.close" :aria-label="closeLabel" data-skip-initial-focus="true" @click="close">
|
|
113
|
+
<XIcon />
|
|
114
|
+
</button>
|
|
115
|
+
<slot />
|
|
116
|
+
</div>
|
|
117
|
+
</div>
|
|
118
|
+
</div>
|
|
119
|
+
</div>
|
|
120
|
+
</template>
|
|
@@ -4,16 +4,12 @@ import { useEffect, useId, useMemo, useRef, useState, type KeyboardEvent, type P
|
|
|
4
4
|
import { cx } from '../../internal/cx'
|
|
5
5
|
import { FieldMessage } from '../../internal/FieldMessage'
|
|
6
6
|
import { MinusIcon, PlusIcon } from '../../internal/icons'
|
|
7
|
+
import { createHold, parseNumber, type NumberInputLabels } from '../../internal/motion/numberInput'
|
|
7
8
|
import { useControllable } from '../../internal/useControllable'
|
|
8
9
|
import { AnimatedNumber } from '../AnimatedNumber'
|
|
9
10
|
import styles from './NumberInput.module.css'
|
|
10
11
|
|
|
11
|
-
export type NumberInputLabels
|
|
12
|
-
/** Name of the minus button. A function gets the field label; a string is used as it is. */
|
|
13
|
-
decrease: string | ((label: string) => string)
|
|
14
|
-
/** Name of the plus button. A function gets the field label; a string is used as it is. */
|
|
15
|
-
increase: string | ((label: string) => string)
|
|
16
|
-
}
|
|
12
|
+
export type { NumberInputLabels }
|
|
17
13
|
|
|
18
14
|
const defaultLabels: NumberInputLabels = {
|
|
19
15
|
decrease: (label) => `Decrease ${label}`,
|
|
@@ -55,32 +51,6 @@ export type NumberInputProps = {
|
|
|
55
51
|
className?: string
|
|
56
52
|
}
|
|
57
53
|
|
|
58
|
-
// Input timings, so reduced motion does not change them
|
|
59
|
-
function tokenMs(name: string, fallback: number): number {
|
|
60
|
-
if (typeof window === 'undefined') return fallback
|
|
61
|
-
const raw = getComputedStyle(document.documentElement).getPropertyValue(name).trim()
|
|
62
|
-
if (!raw) return fallback
|
|
63
|
-
// The browser may report 500ms as .5s
|
|
64
|
-
return raw.endsWith('ms') ? parseFloat(raw) : parseFloat(raw) * 1000
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function parseNumber(text: string, locale?: string): number | null {
|
|
68
|
-
const parts = new Intl.NumberFormat(locale).formatToParts(-12345.6)
|
|
69
|
-
const group = parts.find((p) => p.type === 'group')?.value ?? ','
|
|
70
|
-
const decimal = parts.find((p) => p.type === 'decimal')?.value ?? '.'
|
|
71
|
-
const minus = parts.find((p) => p.type === 'minusSign')?.value ?? '-'
|
|
72
|
-
// sv, nb and fi write minus as U+2212, which the cleanup below would drop
|
|
73
|
-
const clean = text
|
|
74
|
-
.split(minus).join('-')
|
|
75
|
-
.replace(/\u2212/g, '-')
|
|
76
|
-
.split(group).join('')
|
|
77
|
-
.split(decimal).join('.')
|
|
78
|
-
.replace(/[^\d.-]/g, '')
|
|
79
|
-
if (clean === '' || clean === '-' || clean === '.') return null
|
|
80
|
-
const n = Number(clean)
|
|
81
|
-
return Number.isFinite(n) ? n : null
|
|
82
|
-
}
|
|
83
|
-
|
|
84
54
|
export function NumberInput({
|
|
85
55
|
label,
|
|
86
56
|
value: valueProp,
|
|
@@ -112,7 +82,7 @@ export function NumberInput({
|
|
|
112
82
|
// The hold timer keeps calling the bump from its first render, so it reads the typed text from a ref
|
|
113
83
|
const typed = useRef<string | null>(null)
|
|
114
84
|
typed.current = editing ? text : null
|
|
115
|
-
const hold =
|
|
85
|
+
const [hold] = useState(createHold)
|
|
116
86
|
const decimals = (String(step).split('.')[1] ?? '').length
|
|
117
87
|
const fmt = useMemo(() => new Intl.NumberFormat(locale, format), [locale, format])
|
|
118
88
|
const plain = useMemo(() => new Intl.NumberFormat(locale, { useGrouping: false, maximumFractionDigits: 20 }), [locale])
|
|
@@ -136,25 +106,18 @@ export function NumberInput({
|
|
|
136
106
|
commit(current === null ? base : base + dir * size)
|
|
137
107
|
}
|
|
138
108
|
|
|
139
|
-
const stopHold =
|
|
140
|
-
window.clearTimeout(hold.current.timer)
|
|
141
|
-
hold.current.count = 0
|
|
142
|
-
}
|
|
109
|
+
const stopHold = hold.stop
|
|
143
110
|
const startHold = (dir: 1 | -1) => (e: PointerEvent<HTMLButtonElement>) => {
|
|
144
111
|
// Chromium still sends pointer events to a disabled button
|
|
145
112
|
if (e.button !== 0 || e.currentTarget.disabled) return
|
|
146
113
|
e.preventDefault()
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
bump(dir, hold.current.count > 12 ? step * 5 : step)
|
|
155
|
-
hold.current.timer = window.setTimeout(tick, tokenMs('--gooee-motion-repeat', 70))
|
|
156
|
-
}
|
|
157
|
-
hold.current.timer = window.setTimeout(tick, tokenMs('--gooee-motion-repeat-delay', 400))
|
|
114
|
+
hold.start(
|
|
115
|
+
(big) => bump(dir, big ? step * 5 : step),
|
|
116
|
+
() => {
|
|
117
|
+
const v = valueRef.current
|
|
118
|
+
return v !== null && (dir > 0 ? v >= max : v <= min)
|
|
119
|
+
},
|
|
120
|
+
)
|
|
158
121
|
}
|
|
159
122
|
useEffect(() => stopHold, [])
|
|
160
123
|
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onBeforeUnmount, ref, useId } from 'vue'
|
|
3
|
+
import FieldMessage from '../../internal/FieldMessage.vue'
|
|
4
|
+
import { MinusIcon, PlusIcon } from '../../internal/vueIcons'
|
|
5
|
+
import { createHold, parseNumber, type NumberInputLabels } from '../../internal/motion/numberInput'
|
|
6
|
+
import AnimatedNumber from '../AnimatedNumber/AnimatedNumber.vue'
|
|
7
|
+
import styles from './NumberInput.module.css'
|
|
8
|
+
|
|
9
|
+
// The Vue NumberInput: the same look (NumberInput.module.css), keys and hold to repeat as the React one.
|
|
10
|
+
const props = withDefaults(
|
|
11
|
+
defineProps<{
|
|
12
|
+
/** Visible name of the field, also read by screen readers. */
|
|
13
|
+
label: string
|
|
14
|
+
/** Starting value when there is no v-model. */
|
|
15
|
+
defaultValue?: number | null
|
|
16
|
+
/** Lowest allowed number. Typed values are clamped to it. */
|
|
17
|
+
min?: number
|
|
18
|
+
/** Highest allowed number. Typed values are clamped to it. */
|
|
19
|
+
max?: number
|
|
20
|
+
/** How much the buttons and arrow keys change the value. Its decimals set the rounding. */
|
|
21
|
+
step?: number
|
|
22
|
+
/** Step for Page Up and Page Down. Defaults to ten steps. */
|
|
23
|
+
largeStep?: number
|
|
24
|
+
/** Passed to Intl.NumberFormat, for currency, units or decimals. */
|
|
25
|
+
format?: Intl.NumberFormatOptions
|
|
26
|
+
/** Locale for showing and reading numbers. Undefined uses the browser locale. */
|
|
27
|
+
locale?: string
|
|
28
|
+
/** Hint under the field. Hidden while an error shows. */
|
|
29
|
+
help?: string
|
|
30
|
+
/** Error message under the field. Also marks the field invalid. */
|
|
31
|
+
error?: string
|
|
32
|
+
/** Stops typing and the step buttons. */
|
|
33
|
+
disabled?: boolean
|
|
34
|
+
/** Submits the plain number in a hidden field with this name. */
|
|
35
|
+
name?: string
|
|
36
|
+
/** Screen reader names for the step buttons. */
|
|
37
|
+
labels?: Partial<NumberInputLabels>
|
|
38
|
+
}>(),
|
|
39
|
+
{ defaultValue: null, min: -Infinity, max: Infinity, step: 1 },
|
|
40
|
+
)
|
|
41
|
+
/** The number, or null when the field is empty. Use v-model, or leave it out and the field keeps its own. */
|
|
42
|
+
const model = defineModel<number | null | undefined>({ default: undefined })
|
|
43
|
+
const inner = ref<number | null>(props.defaultValue)
|
|
44
|
+
const value = computed(() => (model.value !== undefined ? model.value : inner.value))
|
|
45
|
+
const setValue = (next: number | null) => {
|
|
46
|
+
inner.value = next
|
|
47
|
+
model.value = next
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const nameOf = (text: NumberInputLabels['decrease'] | undefined, fallback: (l: string) => string) => {
|
|
51
|
+
const t = text ?? fallback
|
|
52
|
+
return typeof t === 'function' ? t(props.label) : t
|
|
53
|
+
}
|
|
54
|
+
const decreaseName = computed(() => nameOf(props.labels?.decrease, (l) => `Decrease ${l}`))
|
|
55
|
+
const increaseName = computed(() => nameOf(props.labels?.increase, (l) => `Increase ${l}`))
|
|
56
|
+
const id = useId()
|
|
57
|
+
const helpId = `${id}-help`
|
|
58
|
+
const errorId = `${id}-error`
|
|
59
|
+
const editing = ref(false)
|
|
60
|
+
const text = ref('')
|
|
61
|
+
const decimals = computed(() => (String(props.step).split('.')[1] ?? '').length)
|
|
62
|
+
const fmt = computed(() => new Intl.NumberFormat(props.locale, props.format))
|
|
63
|
+
const plain = computed(() => new Intl.NumberFormat(props.locale, { useGrouping: false, maximumFractionDigits: 20 }))
|
|
64
|
+
|
|
65
|
+
const clamp = (n: number) => Number(Math.min(props.max, Math.max(props.min, n)).toFixed(decimals.value))
|
|
66
|
+
const commit = (n: number | null) => {
|
|
67
|
+
const next = n === null ? null : clamp(n)
|
|
68
|
+
if (next !== value.value) setValue(next)
|
|
69
|
+
if (editing.value) text.value = next === null ? '' : plain.value.format(next)
|
|
70
|
+
}
|
|
71
|
+
const bump = (dir: 1 | -1, size = props.step) => {
|
|
72
|
+
const current = (editing.value ? parseNumber(text.value, props.locale) : null) ?? value.value
|
|
73
|
+
const base = current ?? (props.min > -Infinity ? props.min : 0)
|
|
74
|
+
commit(current === null ? base : base + dir * size)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const hold = createHold()
|
|
78
|
+
const startHold = (dir: 1 | -1) => (e: PointerEvent) => {
|
|
79
|
+
// Chromium still sends pointer events to a disabled button
|
|
80
|
+
if (e.button !== 0 || (e.currentTarget as HTMLButtonElement).disabled) return
|
|
81
|
+
e.preventDefault()
|
|
82
|
+
hold.start(
|
|
83
|
+
(big) => bump(dir, big ? props.step * 5 : props.step),
|
|
84
|
+
() => {
|
|
85
|
+
const v = value.value
|
|
86
|
+
return v !== null && (dir > 0 ? v >= props.max : v <= props.min)
|
|
87
|
+
},
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
onBeforeUnmount(hold.stop)
|
|
91
|
+
|
|
92
|
+
const onKeydown = (e: KeyboardEvent) => {
|
|
93
|
+
const big = props.largeStep ?? props.step * 10
|
|
94
|
+
const k = e.key
|
|
95
|
+
if (k === 'ArrowUp') bump(1)
|
|
96
|
+
else if (k === 'ArrowDown') bump(-1)
|
|
97
|
+
else if (k === 'PageUp') bump(1, big)
|
|
98
|
+
else if (k === 'PageDown') bump(-1, big)
|
|
99
|
+
else if (k === 'Home' && props.min > -Infinity) commit(props.min)
|
|
100
|
+
else if (k === 'End' && props.max < Infinity) commit(props.max)
|
|
101
|
+
else if (k === 'Enter') commit(parseNumber(text.value, props.locale))
|
|
102
|
+
else return
|
|
103
|
+
e.preventDefault()
|
|
104
|
+
}
|
|
105
|
+
const onFocus = () => {
|
|
106
|
+
editing.value = true
|
|
107
|
+
text.value = value.value === null ? '' : plain.value.format(value.value)
|
|
108
|
+
}
|
|
109
|
+
const onBlur = () => {
|
|
110
|
+
commit(parseNumber(text.value, props.locale))
|
|
111
|
+
editing.value = false
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const atMin = computed(() => value.value !== null && value.value <= props.min)
|
|
115
|
+
const atMax = computed(() => value.value !== null && value.value >= props.max)
|
|
116
|
+
const describedBy = computed(() => [props.help && !props.error ? helpId : null, props.error ? errorId : null].filter(Boolean).join(' ') || undefined)
|
|
117
|
+
const shown = computed(() => (editing.value ? text.value : value.value === null ? '' : fmt.value.format(value.value)))
|
|
118
|
+
</script>
|
|
119
|
+
|
|
120
|
+
<template>
|
|
121
|
+
<div :class="styles.root">
|
|
122
|
+
<label :for="id" :class="styles.label">{{ label }}</label>
|
|
123
|
+
<div :class="styles.field" :data-invalid="error ? true : undefined" :data-disabled="disabled || undefined">
|
|
124
|
+
<button
|
|
125
|
+
type="button"
|
|
126
|
+
tabindex="-1"
|
|
127
|
+
:class="styles.step"
|
|
128
|
+
:aria-label="decreaseName"
|
|
129
|
+
:aria-controls="id"
|
|
130
|
+
:disabled="disabled || atMin"
|
|
131
|
+
@pointerdown="startHold(-1)($event)"
|
|
132
|
+
@pointerup="hold.stop"
|
|
133
|
+
@pointerleave="hold.stop"
|
|
134
|
+
@pointercancel="hold.stop"
|
|
135
|
+
@click="$event.detail === 0 && bump(-1)"
|
|
136
|
+
>
|
|
137
|
+
<MinusIcon />
|
|
138
|
+
</button>
|
|
139
|
+
<div :class="styles.control">
|
|
140
|
+
<input
|
|
141
|
+
:id="id"
|
|
142
|
+
:class="styles.input"
|
|
143
|
+
type="text"
|
|
144
|
+
:inputmode="decimals || min < 0 ? 'decimal' : 'numeric'"
|
|
145
|
+
role="spinbutton"
|
|
146
|
+
autocomplete="off"
|
|
147
|
+
:aria-valuenow="value ?? undefined"
|
|
148
|
+
:aria-valuemin="min > -Infinity ? min : undefined"
|
|
149
|
+
:aria-valuemax="max < Infinity ? max : undefined"
|
|
150
|
+
:aria-valuetext="value !== null ? fmt.format(value) : undefined"
|
|
151
|
+
:aria-invalid="error ? true : undefined"
|
|
152
|
+
:aria-describedby="describedBy"
|
|
153
|
+
:data-editing="editing || undefined"
|
|
154
|
+
:disabled="disabled"
|
|
155
|
+
:value="shown"
|
|
156
|
+
@focus="onFocus"
|
|
157
|
+
@blur="onBlur"
|
|
158
|
+
@input="text = ($event.target as HTMLInputElement).value"
|
|
159
|
+
@keydown="onKeydown"
|
|
160
|
+
/>
|
|
161
|
+
<span v-if="!editing && value !== null" :class="styles.rolling" aria-hidden="true">
|
|
162
|
+
<AnimatedNumber :value="value" :format="format" :locale="locale" />
|
|
163
|
+
</span>
|
|
164
|
+
</div>
|
|
165
|
+
<button
|
|
166
|
+
type="button"
|
|
167
|
+
tabindex="-1"
|
|
168
|
+
:class="styles.step"
|
|
169
|
+
:aria-label="increaseName"
|
|
170
|
+
:aria-controls="id"
|
|
171
|
+
:disabled="disabled || atMax"
|
|
172
|
+
@pointerdown="startHold(1)($event)"
|
|
173
|
+
@pointerup="hold.stop"
|
|
174
|
+
@pointerleave="hold.stop"
|
|
175
|
+
@pointercancel="hold.stop"
|
|
176
|
+
@click="$event.detail === 0 && bump(1)"
|
|
177
|
+
>
|
|
178
|
+
<PlusIcon />
|
|
179
|
+
</button>
|
|
180
|
+
</div>
|
|
181
|
+
<input v-if="name" type="hidden" :name="name" :value="value ?? ''" />
|
|
182
|
+
<FieldMessage :help-id="helpId" :error-id="errorId" :help="help" :error="error" />
|
|
183
|
+
</div>
|
|
184
|
+
</template>
|
|
@@ -4,6 +4,7 @@ import { useLayoutEffect, useRef, useState } from 'react'
|
|
|
4
4
|
import { cx } from '../../internal/cx'
|
|
5
5
|
import { ChevronLeftIcon, ChevronRightIcon } from '../../internal/icons'
|
|
6
6
|
import { useControllable } from '../../internal/useControllable'
|
|
7
|
+
import { pageRange } from './range'
|
|
7
8
|
import styles from './Pagination.module.css'
|
|
8
9
|
|
|
9
10
|
export type PaginationProps = {
|
|
@@ -29,20 +30,6 @@ export type PaginationProps = {
|
|
|
29
30
|
className?: string
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
const span = (from: number, to: number) => Array.from({ length: to - from + 1 }, (_, i) => from + i)
|
|
33
|
-
|
|
34
|
-
// Always the same number of slots, so the row keeps its width. A gap hides at least two pages.
|
|
35
|
-
function range(page: number, count: number, siblings: number): Array<number | 'gap'> {
|
|
36
|
-
const total = siblings * 2 + 5
|
|
37
|
-
if (count <= total) return span(1, count)
|
|
38
|
-
const side = total - 2
|
|
39
|
-
const leftGap = page - siblings > 3
|
|
40
|
-
const rightGap = page + siblings < count - 2
|
|
41
|
-
if (!leftGap) return [...span(1, side), 'gap', count]
|
|
42
|
-
if (!rightGap) return [1, 'gap', ...span(count - side + 1, count)]
|
|
43
|
-
return [1, 'gap', ...span(page - siblings, page + siblings), 'gap', count]
|
|
44
|
-
}
|
|
45
|
-
|
|
46
33
|
export function Pagination({
|
|
47
34
|
pageCount,
|
|
48
35
|
page: pageProp,
|
|
@@ -61,7 +48,7 @@ export function Pagination({
|
|
|
61
48
|
const listRef = useRef<HTMLUListElement>(null)
|
|
62
49
|
const [pill, setPill] = useState<{ x: number; w: number } | null>(null)
|
|
63
50
|
const [ready, setReady] = useState(false)
|
|
64
|
-
const pages =
|
|
51
|
+
const pages = pageRange(page, pageCount, siblings)
|
|
65
52
|
|
|
66
53
|
useLayoutEffect(() => {
|
|
67
54
|
const list = listRef.current
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
3
|
+
import { ChevronLeftIcon, ChevronRightIcon } from '../../internal/vueIcons'
|
|
4
|
+
import { pageRange } from './range'
|
|
5
|
+
import styles from './Pagination.module.css'
|
|
6
|
+
|
|
7
|
+
// The Vue Pagination: the same look (Pagination.module.css) and gliding pill as the React one.
|
|
8
|
+
|
|
9
|
+
const props = withDefaults(
|
|
10
|
+
defineProps<{
|
|
11
|
+
/** Total number of pages. Pages count from 1. */
|
|
12
|
+
pageCount: number
|
|
13
|
+
/** Starting page when there is no v-model:page. */
|
|
14
|
+
defaultPage?: number
|
|
15
|
+
/** Pages shown on each side of the current one. */
|
|
16
|
+
siblings?: number
|
|
17
|
+
/** Screen reader name for the nav landmark. */
|
|
18
|
+
label?: string
|
|
19
|
+
/** Screen reader name for the previous button. */
|
|
20
|
+
previousLabel?: string
|
|
21
|
+
/** Screen reader name for the next button. */
|
|
22
|
+
nextLabel?: string
|
|
23
|
+
/** Builds the screen reader name for each page button. */
|
|
24
|
+
pageLabel?: (page: number) => string
|
|
25
|
+
}>(),
|
|
26
|
+
{ defaultPage: 1, siblings: 1, label: 'Pagination', previousLabel: 'Previous page', nextLabel: 'Next page', pageLabel: (p: number) => `Page ${p}` },
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
/** Current page, counting from 1. Use v-model:page, or leave it out and the part keeps its own state. */
|
|
30
|
+
const model = defineModel<number | undefined>('page', { default: undefined })
|
|
31
|
+
const local = ref(props.defaultPage)
|
|
32
|
+
// A stored page can sit past the end after pageCount drops.
|
|
33
|
+
const page = computed(() => Math.min(Math.max(model.value ?? local.value, 1), Math.max(props.pageCount, 1)))
|
|
34
|
+
const pages = computed(() => pageRange(page.value, props.pageCount, props.siblings))
|
|
35
|
+
|
|
36
|
+
const list = ref<HTMLUListElement | null>(null)
|
|
37
|
+
const pill = ref<{ x: number; w: number } | null>(null)
|
|
38
|
+
const ready = ref(false)
|
|
39
|
+
|
|
40
|
+
const measure = () => {
|
|
41
|
+
const el = list.value?.querySelector<HTMLElement>('[aria-current="page"]')
|
|
42
|
+
pill.value = el ? { x: el.offsetLeft, w: el.offsetWidth } : null
|
|
43
|
+
}
|
|
44
|
+
// Keeps the current page in view when the list scrolls on a narrow screen.
|
|
45
|
+
const center = () => {
|
|
46
|
+
const el = list.value?.querySelector<HTMLElement>('[aria-current="page"]')
|
|
47
|
+
if (!list.value || !el || list.value.scrollWidth <= list.value.clientWidth) return
|
|
48
|
+
list.value.scrollLeft = el.offsetLeft - (list.value.clientWidth - el.offsetWidth) / 2
|
|
49
|
+
}
|
|
50
|
+
let ro: ResizeObserver | null = null
|
|
51
|
+
onMounted(() => {
|
|
52
|
+
measure()
|
|
53
|
+
center()
|
|
54
|
+
requestAnimationFrame(() => (ready.value = true))
|
|
55
|
+
ro = new ResizeObserver(measure)
|
|
56
|
+
if (list.value) ro.observe(list.value)
|
|
57
|
+
})
|
|
58
|
+
watch([page, () => props.pageCount, () => props.siblings], () => (measure(), center()), { flush: 'post' })
|
|
59
|
+
onBeforeUnmount(() => ro?.disconnect())
|
|
60
|
+
|
|
61
|
+
const go = (p: number) => {
|
|
62
|
+
const next = Math.min(Math.max(p, 1), props.pageCount)
|
|
63
|
+
if (next === page.value) return
|
|
64
|
+
local.value = next
|
|
65
|
+
model.value = next
|
|
66
|
+
}
|
|
67
|
+
</script>
|
|
68
|
+
|
|
69
|
+
<template>
|
|
70
|
+
<nav :aria-label="label" :class="styles.root">
|
|
71
|
+
<button type="button" :class="[styles.btn, styles.edge]" :aria-label="previousLabel" :disabled="page <= 1" @click="go(page - 1)">
|
|
72
|
+
<ChevronLeftIcon />
|
|
73
|
+
</button>
|
|
74
|
+
<ul ref="list" :class="styles.list">
|
|
75
|
+
<li v-if="pill" aria-hidden="true" :class="styles.pill" :data-ready="ready || undefined" :style="{ transform: `translateX(${pill.x}px)`, width: `${pill.w}px` }" />
|
|
76
|
+
<template v-for="(p, i) in pages" :key="p === 'gap' ? `gap-${i}` : p">
|
|
77
|
+
<li v-if="p === 'gap'" :class="styles.gap" aria-hidden="true">…</li>
|
|
78
|
+
<li v-else>
|
|
79
|
+
<button type="button" :class="styles.btn" :aria-label="pageLabel(p)" :aria-current="p === page ? 'page' : undefined" @click="go(p)">{{ p }}</button>
|
|
80
|
+
</li>
|
|
81
|
+
</template>
|
|
82
|
+
</ul>
|
|
83
|
+
<button type="button" :class="[styles.btn, styles.edge]" :aria-label="nextLabel" :disabled="page >= pageCount" @click="go(page + 1)">
|
|
84
|
+
<ChevronRightIcon />
|
|
85
|
+
</button>
|
|
86
|
+
</nav>
|
|
87
|
+
</template>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Which pages show, the same in every framework.
|
|
2
|
+
|
|
3
|
+
const span = (from: number, to: number) => Array.from({ length: to - from + 1 }, (_, i) => from + i)
|
|
4
|
+
|
|
5
|
+
/** Always the same number of slots, so the row keeps its width. A gap hides at least two pages. */
|
|
6
|
+
export function pageRange(page: number, count: number, siblings: number): Array<number | 'gap'> {
|
|
7
|
+
const total = siblings * 2 + 5
|
|
8
|
+
if (count <= total) return span(1, count)
|
|
9
|
+
const side = total - 2
|
|
10
|
+
const leftGap = page - siblings > 3
|
|
11
|
+
const rightGap = page + siblings < count - 2
|
|
12
|
+
if (!leftGap) return [...span(1, side), 'gap', count]
|
|
13
|
+
if (!rightGap) return [1, 'gap', ...span(count - side + 1, count)]
|
|
14
|
+
return [1, 'gap', ...span(page - siblings, page + siblings), 'gap', count]
|
|
15
|
+
}
|