@thekestongroup/ui 0.6.0 → 0.7.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
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from "@storybook/vue3-vite";
|
|
2
|
-
import { Building2, CalendarCog, KeyRound, Layers, LockOpen, LogOut, Pencil, Trash2, UserPlus, Users } from "@lucide/vue";
|
|
2
|
+
import { Ban, Building2, CalendarCog, KeyRound, Layers, LockOpen, LogOut, Pencil, Trash2, UserPlus, Users, UsersRound, X } from "@lucide/vue";
|
|
3
3
|
import AvatarGroup from "../AvatarGroup/AvatarGroup.vue";
|
|
4
4
|
import Badge from "../Badge/Badge.vue";
|
|
5
5
|
import Button from "../Button/Button.vue";
|
|
@@ -109,3 +109,31 @@ export const WithToolbar: Story = {
|
|
|
109
109
|
</PageHeader>`,
|
|
110
110
|
}),
|
|
111
111
|
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Something before the buttons (`toolbar-start`), e.g. how many rows are
|
|
115
|
+
* ticked. Narrow the preview: actions that don't fit move into the "⋯" menu
|
|
116
|
+
* at the end, and come back as it widens.
|
|
117
|
+
*/
|
|
118
|
+
export const ToolbarWithSelection: Story = {
|
|
119
|
+
args: {
|
|
120
|
+
title: "Users",
|
|
121
|
+
description: undefined,
|
|
122
|
+
breadcrumb: undefined,
|
|
123
|
+
toolbar: [
|
|
124
|
+
{ label: "Reset password", icon: KeyRound, onSelect: () => {} },
|
|
125
|
+
{ label: "Change sign-in status", icon: Ban, onSelect: () => {} },
|
|
126
|
+
{ label: "Manage group memberships", icon: UsersRound, onSelect: () => {} },
|
|
127
|
+
{ label: "Delete users", icon: Trash2, onSelect: () => {} },
|
|
128
|
+
{ label: "Clear selection", icon: X, onSelect: () => {} },
|
|
129
|
+
],
|
|
130
|
+
},
|
|
131
|
+
render: (args) => ({
|
|
132
|
+
components: { PageHeader },
|
|
133
|
+
setup: () => ({ args }),
|
|
134
|
+
template: `
|
|
135
|
+
<PageHeader v-bind="args">
|
|
136
|
+
<template #toolbar-start>2 users selected</template>
|
|
137
|
+
</PageHeader>`,
|
|
138
|
+
}),
|
|
139
|
+
};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import type
|
|
2
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch, type Component, type HTMLAttributes } from 'vue'
|
|
3
3
|
import { cn } from '../../lib/cn'
|
|
4
4
|
import Breadcrumb, { type BreadcrumbItem } from '../Breadcrumb/Breadcrumb.vue'
|
|
5
5
|
import Button from '../Button/Button.vue'
|
|
6
|
+
import Menu, { type MenuItem } from '../Menu/Menu.vue'
|
|
6
7
|
|
|
7
8
|
/** One of the page's everyday actions, in the toolbar under the title. */
|
|
8
9
|
export interface PageHeaderAction {
|
|
@@ -24,12 +25,15 @@ export interface PageHeaderAction {
|
|
|
24
25
|
*
|
|
25
26
|
* `toolbar`: the everyday things people do to what the page is about (e.g.
|
|
26
27
|
* on a user: Reset password, Unblock sign-in, Delete user), as a row of quiet
|
|
27
|
-
* buttons under the title.
|
|
28
|
+
* buttons under the title. Those that don't fit go in a "⋯" Menu at its
|
|
29
|
+
* right-hand end (and come back as it widens). `toolbar-start` and `toolbar-end` slots put
|
|
30
|
+
* something before or after them, e.g. "2 users selected". Keep the one main action (Save, Add…) in the
|
|
28
31
|
* actions slot on the right; the toolbar is for the rest.
|
|
29
32
|
*
|
|
30
33
|
* Slots: badge (after the title, e.g. a Badge), description (instead of the
|
|
31
|
-
* prop, for links or formatting), actions (e.g. Buttons),
|
|
32
|
-
*
|
|
34
|
+
* prop, for links or formatting), actions (e.g. Buttons), toolbar-start and
|
|
35
|
+
* toolbar-end (in the toolbar, before and after its buttons), default
|
|
36
|
+
* (anything under, e.g. an alert or tabs).
|
|
33
37
|
*/
|
|
34
38
|
export interface PageHeaderProps {
|
|
35
39
|
/** The page's name: its h1. */
|
|
@@ -61,12 +65,59 @@ function as(item: PageHeaderAction) {
|
|
|
61
65
|
return 'button'
|
|
62
66
|
}
|
|
63
67
|
|
|
64
|
-
defineSlots<{
|
|
68
|
+
const slots = defineSlots<{
|
|
65
69
|
badge?(): unknown
|
|
66
70
|
description?(): unknown
|
|
67
71
|
actions?(): unknown
|
|
72
|
+
'toolbar-start'?(): unknown
|
|
73
|
+
'toolbar-end'?(): unknown
|
|
68
74
|
default?(): unknown
|
|
69
75
|
}>()
|
|
76
|
+
|
|
77
|
+
const hasToolbar = computed(() => !!props.toolbar?.length || !!slots['toolbar-start'] || !!slots['toolbar-end'])
|
|
78
|
+
|
|
79
|
+
// ── Toolbar overflow ────────────────────────────────────────────────────────
|
|
80
|
+
// A hidden copy of every button is measured; as many as fit are shown, and
|
|
81
|
+
// the rest go in the "⋯" Menu (whose width is kept free for it).
|
|
82
|
+
const fitArea = ref<HTMLElement>()
|
|
83
|
+
const measure = ref<HTMLElement>()
|
|
84
|
+
const shown = ref(Infinity)
|
|
85
|
+
|
|
86
|
+
function fit() {
|
|
87
|
+
const area = fitArea.value
|
|
88
|
+
const copy = measure.value
|
|
89
|
+
const items = props.toolbar ?? []
|
|
90
|
+
if (!area || !copy || !items.length) return
|
|
91
|
+
const buttons = Array.from(copy.children) as HTMLElement[]
|
|
92
|
+
const more = buttons.pop()! // the ⋯ button, last in the copy
|
|
93
|
+
const gap = parseFloat(getComputedStyle(copy).columnGap) || 0
|
|
94
|
+
const widths = buttons.map((b) => b.getBoundingClientRect().width)
|
|
95
|
+
const available = area.clientWidth
|
|
96
|
+
const all = widths.reduce((sum, w) => sum + w, 0) + gap * Math.max(widths.length - 1, 0)
|
|
97
|
+
if (all <= available) {
|
|
98
|
+
shown.value = items.length
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
let used = more.getBoundingClientRect().width
|
|
102
|
+
let n = 0
|
|
103
|
+
while (n < widths.length && used + gap + widths[n]! <= available) used += gap + widths[n++]!
|
|
104
|
+
shown.value = n
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let observer: ResizeObserver | undefined
|
|
108
|
+
onMounted(() => {
|
|
109
|
+
observer = new ResizeObserver(() => fit())
|
|
110
|
+
if (fitArea.value) observer.observe(fitArea.value)
|
|
111
|
+
fit()
|
|
112
|
+
})
|
|
113
|
+
onBeforeUnmount(() => observer?.disconnect())
|
|
114
|
+
watch(
|
|
115
|
+
() => props.toolbar?.map((i) => `${i.label}${i.disabled ? '-' : ''}`).join('|'),
|
|
116
|
+
() => nextTick(fit),
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
const visible = computed(() => (props.toolbar ?? []).slice(0, shown.value))
|
|
120
|
+
const overflow = computed<MenuItem[]>(() => (props.toolbar ?? []).slice(shown.value))
|
|
70
121
|
</script>
|
|
71
122
|
|
|
72
123
|
<template>
|
|
@@ -90,26 +141,50 @@ defineSlots<{
|
|
|
90
141
|
</div>
|
|
91
142
|
</div>
|
|
92
143
|
|
|
93
|
-
<!-- Ghost buttons in a white bar, like a Card
|
|
144
|
+
<!-- Ghost buttons in a white bar, like a Card; those that don't fit go
|
|
145
|
+
in the ⋯ Menu at the end. -->
|
|
94
146
|
<div
|
|
95
|
-
v-if="
|
|
147
|
+
v-if="hasToolbar"
|
|
96
148
|
role="group"
|
|
97
149
|
:aria-label="`${title} actions`"
|
|
98
|
-
:class="cn('flex
|
|
150
|
+
:class="cn('flex items-center gap-1 rounded-lg border border-black/15 bg-white p-1', ui?.toolbar)"
|
|
99
151
|
>
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
152
|
+
<!-- Text here reads as the toolbar's own: bold, spaced like a button. -->
|
|
153
|
+
<div v-if="$slots['toolbar-start']" class="flex shrink-0 items-center gap-2 px-3 text-sm font-bold" aria-live="polite">
|
|
154
|
+
<slot name="toolbar-start" />
|
|
155
|
+
</div>
|
|
156
|
+
<div ref="fitArea" class="relative flex min-w-0 flex-1 items-center gap-1">
|
|
157
|
+
<Button
|
|
158
|
+
v-for="item in visible"
|
|
159
|
+
:key="item.label"
|
|
160
|
+
:as="as(item)"
|
|
161
|
+
:to="item.to"
|
|
162
|
+
:href="item.href"
|
|
163
|
+
variant="ghost"
|
|
164
|
+
:disabled="item.disabled"
|
|
165
|
+
class="shrink-0"
|
|
166
|
+
@click="item.onSelect?.()"
|
|
167
|
+
>
|
|
168
|
+
<template v-if="item.icon" #leading><component :is="item.icon" aria-hidden="true" /></template>
|
|
169
|
+
{{ item.label }}
|
|
170
|
+
</Button>
|
|
171
|
+
<!-- Always at the right-hand end. -->
|
|
172
|
+
<Menu v-if="overflow.length" :items="overflow" label="More actions" :link-component="linkComponent" class="ml-auto shrink-0" />
|
|
173
|
+
|
|
174
|
+
<!-- For measuring only: every button, and the ⋯ one, at full width. -->
|
|
175
|
+
<div aria-hidden="true" inert class="invisible absolute top-0 left-0 size-0 overflow-hidden">
|
|
176
|
+
<div ref="measure" class="flex w-max gap-1">
|
|
177
|
+
<Button v-for="item in toolbar" :key="item.label" variant="ghost" tabindex="-1" class="shrink-0">
|
|
178
|
+
<template v-if="item.icon" #leading><component :is="item.icon" /></template>
|
|
179
|
+
{{ item.label }}
|
|
180
|
+
</Button>
|
|
181
|
+
<Button variant="ghost" square tabindex="-1" class="shrink-0"><span /></Button>
|
|
182
|
+
</div>
|
|
183
|
+
</div>
|
|
184
|
+
</div>
|
|
185
|
+
<div v-if="$slots['toolbar-end']" class="flex shrink-0 items-center gap-2 px-3 text-sm">
|
|
186
|
+
<slot name="toolbar-end" />
|
|
187
|
+
</div>
|
|
113
188
|
</div>
|
|
114
189
|
|
|
115
190
|
<slot />
|