gooee 0.48.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 -142
- 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 +12 -40
- 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/fling.ts +29 -106
- package/src/internal/focus.ts +4 -79
- package/src/internal/icons.module.css +39 -27
- package/src/internal/layers.ts +7 -61
- package/src/internal/liquid.ts +26 -213
- package/src/internal/liquidMatter.ts +9 -181
- package/src/internal/liquidMore.ts +6 -669
- 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/fling.ts +147 -0
- package/src/internal/motion/floating.ts +58 -0
- package/src/internal/motion/focus.ts +88 -0
- package/src/internal/motion/formField.ts +19 -0
- package/src/internal/motion/holdToConfirm.ts +147 -0
- package/src/internal/motion/index.ts +20 -0
- package/src/internal/motion/keys.ts +42 -0
- package/src/internal/motion/layers.ts +78 -0
- package/src/internal/motion/liquid.ts +241 -0
- package/src/internal/motion/liquidMatter.ts +198 -0
- package/src/internal/motion/liquidMore.ts +676 -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/scrollFrame.ts +152 -0
- package/src/internal/motion/scrubLook.ts +15 -0
- package/src/internal/motion/scrubber.ts +154 -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/softPress.ts +334 -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/scrollFrame.ts +4 -145
- package/src/internal/softPress.ts +27 -289
- package/src/internal/themeReveal.ts +3 -61
- package/src/internal/useFloating.ts +9 -49
- package/src/internal/useScrub.ts +15 -120
- package/src/internal/vue/trigger.ts +18 -0
- package/src/internal/vue/useFloating.ts +38 -0
- package/src/internal/vue/usePresence.ts +21 -0
- package/src/internal/vue/useScrub.ts +40 -0
- package/src/internal/vueIcons.ts +134 -0
- package/src/tokens.css +5 -0
- package/src/internal/relativeTime.ts +0 -26
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, nextTick, ref, watch } from 'vue'
|
|
3
|
+
import { ChevronRightIcon } from '../../internal/vueIcons'
|
|
4
|
+
import styles from './Breadcrumb.module.css'
|
|
5
|
+
|
|
6
|
+
// The Vue Breadcrumb: the same look (Breadcrumb.module.css) and unfold as the React one.
|
|
7
|
+
|
|
8
|
+
export type BreadcrumbItem = {
|
|
9
|
+
/** Text of the item. */
|
|
10
|
+
label: string
|
|
11
|
+
/** Where the link goes. Without it, the item is a button. */
|
|
12
|
+
href?: string
|
|
13
|
+
/** Called on click with the event, so a link can call preventDefault for client routing. */
|
|
14
|
+
onClick?: (event: MouseEvent) => void
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const props = withDefaults(
|
|
18
|
+
defineProps<{
|
|
19
|
+
/** Path from the top level down. The last item is the current page. */
|
|
20
|
+
items: BreadcrumbItem[]
|
|
21
|
+
/** Above this many items, the middle collapses into a "…" button. With 2 or less, only "…" and the current page show. */
|
|
22
|
+
maxItems?: number
|
|
23
|
+
/** Name of the nav landmark. */
|
|
24
|
+
label?: string
|
|
25
|
+
/** Screen reader name for the "…" button. */
|
|
26
|
+
expandLabel?: string
|
|
27
|
+
}>(),
|
|
28
|
+
{ maxItems: 4, label: 'Breadcrumb', expandLabel: 'Show full path' },
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
const expanded = ref(false)
|
|
32
|
+
const list = ref<HTMLOListElement | null>(null)
|
|
33
|
+
// Under 3 there is no room for the first item, so only "…" and the current page stay.
|
|
34
|
+
const head = computed(() => (props.maxItems < 3 ? 0 : 1))
|
|
35
|
+
const visible = computed(() => {
|
|
36
|
+
const tail = Math.max(1, props.maxItems - head.value - 1)
|
|
37
|
+
return !expanded.value && props.items.length > props.maxItems ? [...props.items.slice(0, head.value), null, ...props.items.slice(-tail)] : props.items
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
// The "…" button is gone after expanding, so focus moves to the first link it revealed.
|
|
41
|
+
watch(expanded, (on) => {
|
|
42
|
+
if (on) nextTick(() => list.value?.querySelector<HTMLElement>(`:scope > li:nth-child(${head.value + 1}) > :is(a, button)`)?.focus())
|
|
43
|
+
})
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
<template>
|
|
47
|
+
<nav :aria-label="label" :class="styles.root">
|
|
48
|
+
<ol ref="list" :class="styles.list">
|
|
49
|
+
<li v-for="(item, i) in visible" :key="item ? `${item.label}-${i}` : 'more'" :class="styles.item" :style="{ '--i': i }" :data-revealed="expanded || undefined">
|
|
50
|
+
<button v-if="item === null" type="button" :class="styles.more" :aria-label="expandLabel" @click="expanded = true">…</button>
|
|
51
|
+
<span v-else-if="i === visible.length - 1" :class="styles.current" aria-current="page" :title="item.label">{{ item.label }}</span>
|
|
52
|
+
<button v-else-if="item.href === undefined" type="button" :class="[styles.link, styles.button]" :title="item.label" @click="item.onClick?.($event)">{{ item.label }}</button>
|
|
53
|
+
<a v-else :class="styles.link" :href="item.href" :title="item.label" @click="item.onClick?.($event)">{{ item.label }}</a>
|
|
54
|
+
<ChevronRightIcon v-if="i !== visible.length - 1" :class="styles.sep" />
|
|
55
|
+
</li>
|
|
56
|
+
</ol>
|
|
57
|
+
</nav>
|
|
58
|
+
</template>
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onBeforeUnmount, ref, useAttrs, useId, useSlots, watch } from 'vue'
|
|
3
|
+
import { createSoftPress } from '../../internal/motion/softPress'
|
|
4
|
+
import Spinner from '../Spinner/Spinner.vue'
|
|
5
|
+
import styles from './Button.module.css'
|
|
6
|
+
|
|
7
|
+
// The Vue Button: the same look (Button.module.css) and soft press as the React one.
|
|
8
|
+
// Other attributes and listeners go to the button (or the link, with href).
|
|
9
|
+
defineOptions({ inheritAttrs: false })
|
|
10
|
+
|
|
11
|
+
const props = withDefaults(
|
|
12
|
+
defineProps<{
|
|
13
|
+
/** Visual weight. Use one primary per view and danger for destructive actions. */
|
|
14
|
+
variant?: 'primary' | 'secondary' | 'ghost' | 'danger'
|
|
15
|
+
/** Height and text size. */
|
|
16
|
+
size?: 'sm' | 'md' | 'lg'
|
|
17
|
+
/** Shows a spinner and ignores clicks, but keeps focus on the button. */
|
|
18
|
+
loading?: boolean
|
|
19
|
+
/** Spoken while loading, for example "Saving". */
|
|
20
|
+
loadingLabel?: string
|
|
21
|
+
/** Stretches the button to fill its container. */
|
|
22
|
+
fullWidth?: boolean
|
|
23
|
+
disabled?: boolean
|
|
24
|
+
/** Why the button is disabled. Read by screen readers and shown as a tooltip while it is disabled. */
|
|
25
|
+
disabledReason?: string
|
|
26
|
+
/** Renders a link with the same look, for navigation. */
|
|
27
|
+
href?: string
|
|
28
|
+
type?: 'button' | 'submit' | 'reset'
|
|
29
|
+
}>(),
|
|
30
|
+
{ variant: 'primary', size: 'md', loading: false, loadingLabel: 'Loading', type: 'button' },
|
|
31
|
+
)
|
|
32
|
+
// The rendered button or link, for a parent that moves focus to it (React's forwarded ref).
|
|
33
|
+
const el = ref<HTMLButtonElement | HTMLAnchorElement | null>(null)
|
|
34
|
+
defineExpose({ el })
|
|
35
|
+
const slots = useSlots() as { default?: () => unknown; leading?: () => unknown; trailing?: () => unknown }
|
|
36
|
+
const attrs = useAttrs()
|
|
37
|
+
|
|
38
|
+
const reasonId = useId()
|
|
39
|
+
const ariaDisabled = computed(() => attrs['aria-disabled'] === true || attrs['aria-disabled'] === 'true')
|
|
40
|
+
const inert = computed(() => props.loading || props.disabled || ariaDisabled.value)
|
|
41
|
+
const reason = computed(() => (props.disabled || ariaDisabled.value ? props.disabledReason : undefined))
|
|
42
|
+
const describedBy = computed(() => [attrs['aria-describedby'], reason.value ? reasonId : undefined].filter(Boolean).join(' ') || undefined)
|
|
43
|
+
|
|
44
|
+
// The edge is soft: it dents where pressed and springs back (motion/softPress.ts).
|
|
45
|
+
const soft = createSoftPress({
|
|
46
|
+
look: { layer: styles.soft, fill: styles.softFill, ring: styles.softRing, wet: styles.wet },
|
|
47
|
+
get keys() {
|
|
48
|
+
return props.href === undefined ? [' ', 'Enter'] : ['Enter']
|
|
49
|
+
},
|
|
50
|
+
get inert() {
|
|
51
|
+
return inert.value
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
watch(inert, (now) => now && soft.stop())
|
|
55
|
+
onBeforeUnmount(soft.destroy)
|
|
56
|
+
|
|
57
|
+
// The caller's click runs only while the button is not inert; preventDefault also stops a submit or a link.
|
|
58
|
+
const onClick = (e: MouseEvent) => {
|
|
59
|
+
if (inert.value) {
|
|
60
|
+
e.preventDefault()
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
const own = attrs.onClick as ((e: MouseEvent) => void) | Array<(e: MouseEvent) => void> | undefined
|
|
64
|
+
;(Array.isArray(own) ? own : own ? [own] : []).forEach((f) => f(e))
|
|
65
|
+
}
|
|
66
|
+
const rest = computed(() => {
|
|
67
|
+
const { onClick: _onClick, 'aria-describedby': _describedBy, 'aria-disabled': _ariaDisabled, class: _class, ...others } = attrs
|
|
68
|
+
return others
|
|
69
|
+
})
|
|
70
|
+
const shared = computed(() => ({
|
|
71
|
+
'aria-describedby': describedBy.value,
|
|
72
|
+
title: (attrs.title as string | undefined) ?? reason.value,
|
|
73
|
+
// The caller's class joins ours, as React's className does.
|
|
74
|
+
class: [styles.root, styles[props.variant], styles[props.size], props.fullWidth && styles.full, attrs.class],
|
|
75
|
+
'data-loading': props.loading || undefined,
|
|
76
|
+
'aria-busy': props.loading || undefined,
|
|
77
|
+
'aria-disabled': inert.value || undefined,
|
|
78
|
+
}))
|
|
79
|
+
</script>
|
|
80
|
+
|
|
81
|
+
<template>
|
|
82
|
+
<!-- A link has no disabled state; without href it leaves the tab order like a disabled button. -->
|
|
83
|
+
<a
|
|
84
|
+
v-if="href !== undefined"
|
|
85
|
+
ref="el"
|
|
86
|
+
v-bind="{ ...rest, ...shared }"
|
|
87
|
+
:href="disabled ? undefined : href"
|
|
88
|
+
:role="disabled ? 'link' : undefined"
|
|
89
|
+
@click="onClick"
|
|
90
|
+
@pointerdown="soft.down"
|
|
91
|
+
@pointermove="soft.hover"
|
|
92
|
+
@pointerleave="soft.leave"
|
|
93
|
+
@keydown="soft.keyDown"
|
|
94
|
+
@keyup="soft.keyUp"
|
|
95
|
+
@blur="soft.blur"
|
|
96
|
+
>
|
|
97
|
+
<span :class="styles.content">
|
|
98
|
+
<span v-if="slots.leading" :class="styles.icon"><slot name="leading" /></span>
|
|
99
|
+
<span :class="styles.label"><slot /></span>
|
|
100
|
+
<span v-if="slots.trailing" :class="styles.icon"><slot name="trailing" /></span>
|
|
101
|
+
</span>
|
|
102
|
+
<span :class="styles.spinner" aria-hidden="true"><Spinner v-if="loading" size="sm" :label="null" /></span>
|
|
103
|
+
<span :class="styles.srOnly" role="status">{{ loading ? loadingLabel : null }}</span>
|
|
104
|
+
<span v-if="reason" :id="reasonId" :class="styles.srOnly" aria-hidden="true">{{ reason }}</span>
|
|
105
|
+
</a>
|
|
106
|
+
<!-- With a reason the button is aria-disabled only: a natively disabled button cannot take focus, so its reason could not be reached. -->
|
|
107
|
+
<button
|
|
108
|
+
v-else
|
|
109
|
+
ref="el"
|
|
110
|
+
v-bind="{ ...rest, ...shared }"
|
|
111
|
+
:type="type"
|
|
112
|
+
:disabled="reason ? undefined : disabled"
|
|
113
|
+
@click="onClick"
|
|
114
|
+
@pointerdown="soft.down"
|
|
115
|
+
@pointermove="soft.hover"
|
|
116
|
+
@pointerleave="soft.leave"
|
|
117
|
+
@keydown="soft.keyDown"
|
|
118
|
+
@keyup="soft.keyUp"
|
|
119
|
+
@blur="soft.blur"
|
|
120
|
+
>
|
|
121
|
+
<span :class="styles.content">
|
|
122
|
+
<span v-if="slots.leading" :class="styles.icon"><slot name="leading" /></span>
|
|
123
|
+
<span :class="styles.label"><slot /></span>
|
|
124
|
+
<span v-if="slots.trailing" :class="styles.icon"><slot name="trailing" /></span>
|
|
125
|
+
</span>
|
|
126
|
+
<span :class="styles.spinner" aria-hidden="true"><Spinner v-if="loading" size="sm" :label="null" /></span>
|
|
127
|
+
<span :class="styles.srOnly" role="status">{{ loading ? loadingLabel : null }}</span>
|
|
128
|
+
<span v-if="reason" :id="reasonId" :class="styles.srOnly" aria-hidden="true">{{ reason }}</span>
|
|
129
|
+
</button>
|
|
130
|
+
</template>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useLayoutEffect, useRef, type ComponentPropsWithRef, type ReactNode } from 'react'
|
|
2
2
|
import { cx } from '../../internal/cx'
|
|
3
3
|
import { mergeRefs } from '../../internal/mergeRefs'
|
|
4
|
+
import { markRows } from '../../internal/motion/buttonGroup'
|
|
4
5
|
import styles from './ButtonGroup.module.css'
|
|
5
6
|
|
|
6
7
|
export type ButtonGroupProps = Omit<ComponentPropsWithRef<'div'>, 'children'> & {
|
|
@@ -12,30 +13,12 @@ export type ButtonGroupProps = Omit<ComponentPropsWithRef<'div'>, 'children'> &
|
|
|
12
13
|
orientation?: 'horizontal' | 'vertical'
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
// A row with no room wraps onto more rows. Each row stays joined, with round ends of its own, so the
|
|
16
|
-
// buttons that start and end a row are marked as they fall.
|
|
17
16
|
export function ButtonGroup({ label, children, orientation = 'horizontal', className, ref, ...rest }: ButtonGroupProps) {
|
|
18
17
|
const own = useRef<HTMLDivElement>(null)
|
|
18
|
+
// Each wrapped row keeps round ends of its own (motion/buttonGroup.ts).
|
|
19
19
|
useLayoutEffect(() => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const mark = () => {
|
|
23
|
-
const kids = [...root.children] as HTMLElement[]
|
|
24
|
-
kids.forEach((k, i) => {
|
|
25
|
-
const prev = kids[i - 1], next = kids[i + 1]
|
|
26
|
-
k.toggleAttribute('data-row-start', !prev || prev.offsetTop !== k.offsetTop)
|
|
27
|
-
k.toggleAttribute('data-row-end', !next || next.offsetTop !== k.offsetTop)
|
|
28
|
-
})
|
|
29
|
-
}
|
|
30
|
-
mark()
|
|
31
|
-
const ro = new ResizeObserver(mark)
|
|
32
|
-
ro.observe(root)
|
|
33
|
-
const mo = new MutationObserver(mark)
|
|
34
|
-
mo.observe(root, { childList: true })
|
|
35
|
-
return () => {
|
|
36
|
-
ro.disconnect()
|
|
37
|
-
mo.disconnect()
|
|
38
|
-
}
|
|
20
|
+
if (!own.current || orientation !== 'horizontal') return
|
|
21
|
+
return markRows(own.current)
|
|
39
22
|
}, [orientation])
|
|
40
23
|
return (
|
|
41
24
|
<div {...rest} ref={mergeRefs(own, ref)} role="group" aria-label={label} className={cx(styles.root, styles[orientation], className)} data-orientation={orientation}>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
3
|
+
import { markRows } from '../../internal/motion/buttonGroup'
|
|
4
|
+
import styles from './ButtonGroup.module.css'
|
|
5
|
+
|
|
6
|
+
// The Vue ButtonGroup: the same look (ButtonGroup.module.css) as the React one. The buttons go in the default slot.
|
|
7
|
+
const props = withDefaults(
|
|
8
|
+
defineProps<{
|
|
9
|
+
/** Accessible name for the group, such as "Text alignment". */
|
|
10
|
+
label: string
|
|
11
|
+
/** A row, or a column where each button takes the full width. */
|
|
12
|
+
orientation?: 'horizontal' | 'vertical'
|
|
13
|
+
}>(),
|
|
14
|
+
{ orientation: 'horizontal' },
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
const root = ref<HTMLElement | null>(null)
|
|
18
|
+
let stop: (() => void) | undefined
|
|
19
|
+
// Each wrapped row keeps round ends of its own (motion/buttonGroup.ts).
|
|
20
|
+
const watchRows = () => {
|
|
21
|
+
stop?.()
|
|
22
|
+
stop = root.value && props.orientation === 'horizontal' ? markRows(root.value) : undefined
|
|
23
|
+
}
|
|
24
|
+
onMounted(watchRows)
|
|
25
|
+
watch(() => props.orientation, watchRows, { flush: 'post' })
|
|
26
|
+
onBeforeUnmount(() => stop?.())
|
|
27
|
+
</script>
|
|
28
|
+
|
|
29
|
+
<template>
|
|
30
|
+
<div ref="root" role="group" :aria-label="label" :class="[styles.root, styles[orientation]]" :data-orientation="orientation">
|
|
31
|
+
<slot />
|
|
32
|
+
</div>
|
|
33
|
+
</template>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import styles from './Card.module.css'
|
|
3
|
+
|
|
4
|
+
// The Vue Card: the same look (Card.module.css) as the React one. Other attributes go to the root.
|
|
5
|
+
withDefaults(
|
|
6
|
+
defineProps<{
|
|
7
|
+
/** Heading at the top of the body, rendered as an h3. The title slot replaces it. */
|
|
8
|
+
title?: string
|
|
9
|
+
/** Muted text under the title. The description slot replaces it. */
|
|
10
|
+
description?: string
|
|
11
|
+
/** Lifts on hover. Use when the card holds a link or button that covers it. */
|
|
12
|
+
interactive?: boolean
|
|
13
|
+
/** Space inside the card. */
|
|
14
|
+
padding?: 'md' | 'lg'
|
|
15
|
+
/** Lets the body grow when the card is stretched (a grid cell or flex row), so the footer or a last button sits at the bottom. */
|
|
16
|
+
fill?: boolean
|
|
17
|
+
}>(),
|
|
18
|
+
{ padding: 'md' },
|
|
19
|
+
)
|
|
20
|
+
/** media: shown above the body, edge to edge. footer: a row under the body, set apart by a border, such as actions. */
|
|
21
|
+
const slots = defineSlots<{ default?: () => unknown; title?: () => unknown; description?: () => unknown; media?: () => unknown; footer?: () => unknown }>()
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<template>
|
|
25
|
+
<div :class="[styles.root, interactive && styles.interactive, styles[padding], fill && styles.fill]">
|
|
26
|
+
<div v-if="slots.media" :class="styles.media"><slot name="media" /></div>
|
|
27
|
+
<div :class="styles.body">
|
|
28
|
+
<h3 v-if="title || slots.title" :class="styles.title"><slot name="title">{{ title }}</slot></h3>
|
|
29
|
+
<p v-if="description || slots.description" :class="styles.description"><slot name="description">{{ description }}</slot></p>
|
|
30
|
+
<slot />
|
|
31
|
+
</div>
|
|
32
|
+
<div v-if="slots.footer" :class="styles.footer"><slot name="footer" /></div>
|
|
33
|
+
</div>
|
|
34
|
+
</template>
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { space, type Space } from '../../internal/space'
|
|
3
|
+
import styles from './Center.module.css'
|
|
4
|
+
|
|
5
|
+
// The Vue Center: the same look (Center.module.css) as the React one. Other attributes go to the element.
|
|
6
|
+
withDefaults(
|
|
7
|
+
defineProps<{
|
|
8
|
+
/** "both" centres across and down; "inline" only across, and content starts at the top. */
|
|
9
|
+
axis?: 'both' | 'inline'
|
|
10
|
+
/** Least height of the box, from the --gooee-center-h-* tokens. "screen" fills the visible screen. Down centring needs a height to work in. */
|
|
11
|
+
minHeight?: 'sm' | 'md' | 'lg' | 'screen'
|
|
12
|
+
/** Centres each line of text too. Turn off for long text that reads better from the left. */
|
|
13
|
+
centerText?: boolean
|
|
14
|
+
/** Space between the items, as a step of the space scale. */
|
|
15
|
+
gap?: Space
|
|
16
|
+
/** Element to render, such as main for a full page error. */
|
|
17
|
+
as?: 'div' | 'section' | 'main' | 'article'
|
|
18
|
+
}>(),
|
|
19
|
+
{ axis: 'both', centerText: true, gap: 4, as: 'div' },
|
|
20
|
+
)
|
|
21
|
+
defineSlots<{ default?: () => unknown }>()
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<template>
|
|
25
|
+
<component
|
|
26
|
+
:is="as"
|
|
27
|
+
:class="[styles.root, axis === 'both' && styles.both, centerText && styles.text]"
|
|
28
|
+
:style="{ '--center-gap': space(gap), '--center-min-h': minHeight ? `var(--gooee-center-h-${minHeight})` : undefined }"
|
|
29
|
+
>
|
|
30
|
+
<slot />
|
|
31
|
+
</component>
|
|
32
|
+
</template>
|
|
@@ -4,7 +4,7 @@ import { useEffect, useId, useLayoutEffect, useRef, useState, type ComponentProp
|
|
|
4
4
|
import { cx } from '../../internal/cx'
|
|
5
5
|
import { FieldMessage } from '../../internal/FieldMessage'
|
|
6
6
|
import { mergeRefs } from '../../internal/mergeRefs'
|
|
7
|
-
import {
|
|
7
|
+
import { createCheckboxPour } from '../../internal/motion/checkbox'
|
|
8
8
|
import { useControllable } from '../../internal/useControllable'
|
|
9
9
|
import styles from './Checkbox.module.css'
|
|
10
10
|
|
|
@@ -62,36 +62,19 @@ export function Checkbox({
|
|
|
62
62
|
|
|
63
63
|
const state = mixed ? 'mixed' : checked ? 'on' : 'off'
|
|
64
64
|
|
|
65
|
-
// Ink pours in from the side that was pressed
|
|
66
|
-
// the mark draws. Clearing drains it the same way.
|
|
65
|
+
// Ink pours in from the side that was pressed, levels, and then the mark draws (motion/checkbox.ts).
|
|
67
66
|
const boxRef = useRef<HTMLSpanElement>(null)
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
useLayoutEffect(() => {
|
|
73
|
-
const prev = was.current
|
|
74
|
-
was.current = { filled, state }
|
|
75
|
-
if (prev.filled === filled) return
|
|
76
|
-
const box = boxRef.current
|
|
77
|
-
const at = pressedAt.current
|
|
78
|
-
pressedAt.current = null
|
|
79
|
-
if (!box) return
|
|
80
|
-
const r = box.getBoundingClientRect()
|
|
81
|
-
const side = at && performance.now() - at.t < 1000 ? Math.max(-1, Math.min(1, (at.x - r.left - r.width / 2) / (r.width / 2))) : 0
|
|
82
|
-
const mark = box.querySelector<SVGPathElement>(prev.state === 'mixed' || state === 'mixed' ? '[data-mark="dash"]' : '[data-mark="tick"]')
|
|
83
|
-
pouring.current = pour(box, mark, filled ? 1 : 0, side, styles.pour, pouring.current)
|
|
84
|
-
}, [filled, state])
|
|
85
|
-
useLayoutEffect(() => () => pouring.current?.stop(), [])
|
|
67
|
+
const cup = useRef<ReturnType<typeof createCheckboxPour>>(null)
|
|
68
|
+
cup.current ??= createCheckboxPour(() => boxRef.current, styles.pour, state)
|
|
69
|
+
useLayoutEffect(() => cup.current!.set(state), [state])
|
|
70
|
+
useLayoutEffect(() => () => cup.current!.destroy(), [])
|
|
86
71
|
|
|
87
72
|
return (
|
|
88
73
|
<div
|
|
89
74
|
className={cx(styles.root, className)}
|
|
90
75
|
data-disabled={disabled || undefined}
|
|
91
76
|
data-invalid={error ? true : undefined}
|
|
92
|
-
onPointerDown={(e) =>
|
|
93
|
-
pressedAt.current = { x: e.clientX, t: performance.now() }
|
|
94
|
-
}}
|
|
77
|
+
onPointerDown={(e) => cup.current!.press(e.clientX)}
|
|
95
78
|
>
|
|
96
79
|
<span className={styles.boxWrap}>
|
|
97
80
|
<input
|
|
@@ -110,7 +93,7 @@ export function Checkbox({
|
|
|
110
93
|
}}
|
|
111
94
|
onPointerDown={onPointerDown}
|
|
112
95
|
onKeyDown={(e) => {
|
|
113
|
-
|
|
96
|
+
cup.current!.key()
|
|
114
97
|
onKeyDown?.(e)
|
|
115
98
|
}}
|
|
116
99
|
{...rest}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onBeforeUnmount, ref, useAttrs, useId, watch } from 'vue'
|
|
3
|
+
import FieldMessage from '../../internal/FieldMessage.vue'
|
|
4
|
+
import { createCheckboxPour } from '../../internal/motion/checkbox'
|
|
5
|
+
import styles from './Checkbox.module.css'
|
|
6
|
+
|
|
7
|
+
// The Vue Checkbox: the same look (Checkbox.module.css) and cup fill as the React one.
|
|
8
|
+
// Other attributes, such as name or aria-required, go to the input. A class goes to the root, as in React.
|
|
9
|
+
defineOptions({ inheritAttrs: false })
|
|
10
|
+
|
|
11
|
+
const props = withDefaults(
|
|
12
|
+
defineProps<{
|
|
13
|
+
/** Visible text next to the box. Clicking it toggles the box. The label slot replaces it. */
|
|
14
|
+
label?: string
|
|
15
|
+
/** Muted text under the label, linked with aria-describedby. The description slot replaces it. */
|
|
16
|
+
description?: string
|
|
17
|
+
/** Starting checked state when there is no v-model. */
|
|
18
|
+
defaultChecked?: boolean
|
|
19
|
+
/** Shows a dash. A click clears the dash and checks the box. */
|
|
20
|
+
indeterminate?: boolean
|
|
21
|
+
/** Error message under the label. Marks the box invalid. */
|
|
22
|
+
error?: string
|
|
23
|
+
disabled?: boolean
|
|
24
|
+
id?: string
|
|
25
|
+
}>(),
|
|
26
|
+
{ defaultChecked: false, indeterminate: false },
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
/** Whether the box is ticked. Use v-model:checked, or leave it out and the box keeps its own state. */
|
|
30
|
+
const model = defineModel<boolean | undefined>('checked', { default: undefined })
|
|
31
|
+
const local = ref(props.defaultChecked)
|
|
32
|
+
const checked = computed({
|
|
33
|
+
get: () => model.value ?? local.value,
|
|
34
|
+
set: (next: boolean) => {
|
|
35
|
+
local.value = next
|
|
36
|
+
model.value = next
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
const slots = defineSlots<{ label?: () => unknown; description?: () => unknown }>()
|
|
40
|
+
const attrs = useAttrs()
|
|
41
|
+
const inputAttrs = computed(() => {
|
|
42
|
+
const { class: _, ...rest } = attrs
|
|
43
|
+
return rest
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const auto = useId()
|
|
47
|
+
const inputId = computed(() => props.id ?? auto)
|
|
48
|
+
const errorId = computed(() => `${inputId.value}-error`)
|
|
49
|
+
const hasDescription = computed(() => !!(props.description || slots.description))
|
|
50
|
+
const describedBy = computed(() => [hasDescription.value ? `${inputId.value}-desc` : null, props.error ? errorId.value : null, attrs['aria-describedby']].filter(Boolean).join(' ') || undefined)
|
|
51
|
+
|
|
52
|
+
// Like a native checkbox, a click clears the mixed look until the prop sets it again.
|
|
53
|
+
const mixed = ref(props.indeterminate)
|
|
54
|
+
watch(
|
|
55
|
+
() => props.indeterminate,
|
|
56
|
+
(now) => (mixed.value = now),
|
|
57
|
+
)
|
|
58
|
+
const state = computed(() => (mixed.value ? 'mixed' : checked.value ? 'on' : 'off'))
|
|
59
|
+
|
|
60
|
+
const onChange = (e: Event) => {
|
|
61
|
+
const input = e.target as HTMLInputElement
|
|
62
|
+
if (!mixed.value) checked.value = input.checked
|
|
63
|
+
else {
|
|
64
|
+
mixed.value = false
|
|
65
|
+
checked.value = true
|
|
66
|
+
}
|
|
67
|
+
// A v-model that holds its value keeps the box as it was.
|
|
68
|
+
input.checked = checked.value
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Ink pours in from the side that was pressed, levels, and then the mark draws (motion/checkbox.ts).
|
|
72
|
+
const box = ref<HTMLElement | null>(null)
|
|
73
|
+
const cup = createCheckboxPour(() => box.value, styles.pour, state.value)
|
|
74
|
+
watch(state, (s) => cup.set(s), { flush: 'post' })
|
|
75
|
+
onBeforeUnmount(cup.destroy)
|
|
76
|
+
</script>
|
|
77
|
+
|
|
78
|
+
<template>
|
|
79
|
+
<div :class="[styles.root, attrs.class]" :data-disabled="disabled || undefined" :data-invalid="error ? true : undefined" @pointerdown="cup.press($event.clientX)">
|
|
80
|
+
<span :class="styles.boxWrap">
|
|
81
|
+
<input
|
|
82
|
+
v-bind="inputAttrs"
|
|
83
|
+
:id="inputId"
|
|
84
|
+
type="checkbox"
|
|
85
|
+
:class="styles.input"
|
|
86
|
+
:checked="checked"
|
|
87
|
+
:indeterminate="mixed"
|
|
88
|
+
:disabled="disabled"
|
|
89
|
+
:aria-invalid="error ? true : (inputAttrs['aria-invalid'] as boolean | undefined)"
|
|
90
|
+
:aria-describedby="describedBy"
|
|
91
|
+
@change="onChange"
|
|
92
|
+
@keydown="cup.key()"
|
|
93
|
+
/>
|
|
94
|
+
<span ref="box" :class="styles.box" :data-state="state" aria-hidden="true">
|
|
95
|
+
<span :class="styles.fill" :data-pour-fill="true" />
|
|
96
|
+
<svg viewBox="0 0 16 16" :class="styles.mark">
|
|
97
|
+
<path :class="styles.tick" d="M3.5 8.5l3 3 6-7" pathLength="1" data-mark="tick" />
|
|
98
|
+
<path :class="styles.dash" d="M4 8h8" pathLength="1" data-mark="dash" />
|
|
99
|
+
</svg>
|
|
100
|
+
</span>
|
|
101
|
+
</span>
|
|
102
|
+
<span :class="styles.text">
|
|
103
|
+
<label :for="inputId" :class="styles.label"><slot name="label">{{ label }}</slot></label>
|
|
104
|
+
<span v-if="hasDescription" :id="`${inputId}-desc`" :class="styles.description"><slot name="description">{{ description }}</slot></span>
|
|
105
|
+
<FieldMessage :help-id="`${inputId}-help`" :error-id="errorId" :error="error" />
|
|
106
|
+
</span>
|
|
107
|
+
</div>
|
|
108
|
+
</template>
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, ref, useId } from 'vue'
|
|
3
|
+
import FieldMessage from '../../internal/FieldMessage.vue'
|
|
4
|
+
import Checkbox from '../Checkbox/Checkbox.vue'
|
|
5
|
+
import styles from './CheckboxGroup.module.css'
|
|
6
|
+
|
|
7
|
+
// The Vue CheckboxGroup: the same look (CheckboxGroup.module.css) and boxes as the React one.
|
|
8
|
+
|
|
9
|
+
export type CheckboxOption = { value: string; label: string; description?: string; disabled?: boolean }
|
|
10
|
+
|
|
11
|
+
const props = withDefaults(
|
|
12
|
+
defineProps<{
|
|
13
|
+
/** Visible name of the group, rendered as the legend. */
|
|
14
|
+
label: string
|
|
15
|
+
/** The boxes, in order. */
|
|
16
|
+
options: CheckboxOption[]
|
|
17
|
+
/** Starting value when there is no v-model. */
|
|
18
|
+
defaultValue?: string[]
|
|
19
|
+
/** Muted text under the label, read with the group. */
|
|
20
|
+
description?: string
|
|
21
|
+
/** Error under the boxes. Marks every box invalid and is read with the group. */
|
|
22
|
+
error?: string
|
|
23
|
+
/** Form field name shared by every box. Each ticked box sends its value. */
|
|
24
|
+
name?: string
|
|
25
|
+
/** Turns off every box in the group. */
|
|
26
|
+
disabled?: boolean
|
|
27
|
+
/** Stacks the boxes or puts them in a row that wraps. */
|
|
28
|
+
orientation?: 'vertical' | 'horizontal'
|
|
29
|
+
}>(),
|
|
30
|
+
{ orientation: 'vertical' },
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
/** Values of the ticked boxes, in option order. Use v-model, or leave it out and the group keeps its own state. */
|
|
34
|
+
const model = defineModel<string[]>()
|
|
35
|
+
const local = ref(props.defaultValue ?? [])
|
|
36
|
+
const value = computed({
|
|
37
|
+
get: () => model.value ?? local.value,
|
|
38
|
+
set: (next: string[]) => {
|
|
39
|
+
local.value = next
|
|
40
|
+
model.value = next
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const auto = useId()
|
|
45
|
+
const descId = `${auto}-desc`
|
|
46
|
+
const errorId = `${auto}-error`
|
|
47
|
+
const describedBy = computed(() => [props.description ? descId : null, props.error ? errorId : null].filter(Boolean).join(' ') || undefined)
|
|
48
|
+
const toggle = (v: string, on: boolean) => {
|
|
49
|
+
const next = new Set(value.value)
|
|
50
|
+
if (on) next.add(v)
|
|
51
|
+
else next.delete(v)
|
|
52
|
+
value.value = props.options.map((o) => o.value).filter((x) => next.has(x))
|
|
53
|
+
}
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<template>
|
|
57
|
+
<fieldset :class="styles.group" :disabled="disabled" :aria-describedby="describedBy" :data-disabled="disabled || undefined" :data-invalid="error ? true : undefined">
|
|
58
|
+
<legend :class="styles.legend">{{ label }}</legend>
|
|
59
|
+
<p v-if="description" :id="descId" :class="styles.description">{{ description }}</p>
|
|
60
|
+
<div :class="[styles.items, styles[orientation]]">
|
|
61
|
+
<Checkbox
|
|
62
|
+
v-for="o in options"
|
|
63
|
+
:key="o.value"
|
|
64
|
+
:label="o.label"
|
|
65
|
+
:description="o.description"
|
|
66
|
+
:name="name"
|
|
67
|
+
:value="o.value"
|
|
68
|
+
:disabled="o.disabled"
|
|
69
|
+
:checked="value.includes(o.value)"
|
|
70
|
+
:aria-invalid="error ? true : undefined"
|
|
71
|
+
@update:checked="(on?: boolean) => toggle(o.value, !!on)"
|
|
72
|
+
/>
|
|
73
|
+
</div>
|
|
74
|
+
<FieldMessage :help-id="`${auto}-help`" :error-id="errorId" :error="error" />
|
|
75
|
+
</fieldset>
|
|
76
|
+
</template>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { space, type Space } from '../../internal/space'
|
|
3
|
+
import styles from './Cluster.module.css'
|
|
4
|
+
|
|
5
|
+
// The Vue Cluster: the same look (Cluster.module.css) as the React one. Other attributes go to the element.
|
|
6
|
+
withDefaults(
|
|
7
|
+
defineProps<{
|
|
8
|
+
/** Space between items, as a step of the space scale (0 to 12, read as --gooee-space-N). */
|
|
9
|
+
gap?: Space
|
|
10
|
+
/** Space between wrapped rows. Defaults to the same step as gap. */
|
|
11
|
+
rowGap?: Space
|
|
12
|
+
/** Lines items up inside a row. "baseline" keeps mixed text sizes on one line. */
|
|
13
|
+
align?: 'start' | 'center' | 'end' | 'baseline' | 'stretch'
|
|
14
|
+
/** Places items along the row. "between" pushes the first and last item to the edges. */
|
|
15
|
+
justify?: 'start' | 'center' | 'end' | 'between'
|
|
16
|
+
/** Lets items flow onto the next row when they run out of room. */
|
|
17
|
+
wrap?: boolean
|
|
18
|
+
/** Element to render, so a row of links can be a nav or a list. */
|
|
19
|
+
as?: 'div' | 'section' | 'nav' | 'header' | 'footer' | 'ul' | 'ol'
|
|
20
|
+
}>(),
|
|
21
|
+
{ gap: 3, align: 'center', justify: 'start', wrap: true, as: 'div' },
|
|
22
|
+
)
|
|
23
|
+
defineSlots<{ default?: () => unknown }>()
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<template>
|
|
27
|
+
<component
|
|
28
|
+
:is="as"
|
|
29
|
+
:class="[styles.root, styles[`align-${align}`], styles[`justify-${justify}`], !wrap && styles.nowrap]"
|
|
30
|
+
:style="{ '--cluster-gap': space(gap), '--cluster-row-gap': space(rowGap ?? gap) }"
|
|
31
|
+
>
|
|
32
|
+
<slot />
|
|
33
|
+
</component>
|
|
34
|
+
</template>
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed } from 'vue'
|
|
3
|
+
import CopyButton from '../CopyButton/CopyButton.vue'
|
|
4
|
+
import { guessLanguage, tokenize, type CodeLanguage } from './highlight'
|
|
5
|
+
import styles from './CodeBlock.module.css'
|
|
6
|
+
|
|
7
|
+
// The Vue CodeBlock: the same look (CodeBlock.module.css) and colouring (highlight.ts) as the React one.
|
|
8
|
+
const props = withDefaults(
|
|
9
|
+
defineProps<{
|
|
10
|
+
/** The code to show and copy. */
|
|
11
|
+
code: string
|
|
12
|
+
/** Sets the colouring. Undefined guesses from the first line. */
|
|
13
|
+
language?: CodeLanguage
|
|
14
|
+
/** File name or short title in the header row. Undefined shows the language. */
|
|
15
|
+
title?: string
|
|
16
|
+
/** Shows a line number before each line. They are never copied or read out. */
|
|
17
|
+
lineNumbers?: boolean
|
|
18
|
+
/** Shows the copy button in the header row. */
|
|
19
|
+
copyable?: boolean
|
|
20
|
+
/** Screen reader name for the copy button. */
|
|
21
|
+
copyLabel?: string
|
|
22
|
+
/** Screen reader name for the scrollable code. Undefined uses the title. */
|
|
23
|
+
label?: string
|
|
24
|
+
}>(),
|
|
25
|
+
{ lineNumbers: false, copyable: true, copyLabel: 'Copy code' },
|
|
26
|
+
)
|
|
27
|
+
/** header: replaces the title in the header row, for example with tabs that switch the code. */
|
|
28
|
+
defineSlots<{ header?: () => unknown }>()
|
|
29
|
+
|
|
30
|
+
const NAMES: Record<CodeLanguage, string> = { tsx: 'TSX', html: 'HTML', css: 'CSS', shell: 'Terminal' }
|
|
31
|
+
const lang = computed(() => props.language ?? guessLanguage(props.code))
|
|
32
|
+
const numbers = computed(() => Array.from({ length: props.code.replace(/\n$/, '').split('\n').length }, (_, i) => i + 1).join('\n'))
|
|
33
|
+
const tokens = computed(() => tokenize(props.code, lang.value))
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
<template>
|
|
37
|
+
<div :class="styles.root">
|
|
38
|
+
<div :class="styles.head">
|
|
39
|
+
<slot name="header"><span :class="styles.title">{{ title ?? NAMES[lang] }}</span></slot>
|
|
40
|
+
<CopyButton v-if="copyable" :value="code" :label="copyLabel" size="sm" :class="styles.copy" />
|
|
41
|
+
</div>
|
|
42
|
+
<!-- Kept on one line: a pre keeps every space and line break in the template. -->
|
|
43
|
+
<pre :class="styles.pre" tabindex="0" :aria-label="label ?? (title ? `${title} code` : 'Code example')" :data-lines="lineNumbers || undefined"><span v-if="lineNumbers" :class="styles.gutter" aria-hidden="true">{{ numbers }}</span><code :class="styles.code"><template v-for="(t, i) in tokens" :key="i"><span v-if="t.kind" :class="styles[t.kind]">{{ t.text }}</span><template v-else>{{ t.text }}</template></template></code></pre>
|
|
44
|
+
</div>
|
|
45
|
+
</template>
|