@aleph-alpha/ui-library 7.2.1 → 7.3.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.
Files changed (32) hide show
  1. package/config.js +6 -0
  2. package/dist/system/components/UiFileUploadBox/UiFileUploadBox.vue.d.ts +2 -2
  3. package/dist/system/components/UiSpinner/UiSpinner.vue.d.ts +1 -3
  4. package/dist/system/components/UiSpinner/index.d.ts +1 -1
  5. package/dist/system/components/UiSpinner/types.d.ts +19 -6
  6. package/dist/system/components/core/native-select/NativeSelectOptGroup.vue.d.ts +2 -2
  7. package/dist/system/components/core/native-select/NativeSelectOption.vue.d.ts +2 -2
  8. package/dist/system/icons/story-sidebar-icons.json +2 -0
  9. package/dist/system/lib.js +7081 -6851
  10. package/dist/system/patterns/UiFileUpload/UiFileUpload.vue.d.ts +2 -2
  11. package/dist/system/patterns/UiHeadline/UiHeadline.vue.d.ts +204 -0
  12. package/dist/system/patterns/UiHeadline/index.d.ts +2 -0
  13. package/dist/system/patterns/UiHeadline/types.d.ts +64 -0
  14. package/dist/system/patterns/index.d.ts +1 -0
  15. package/dist/system/story-sidebar-icons.json +2 -0
  16. package/package.json +1 -1
  17. package/src/components/UiField/UiField.stories.ts +302 -0
  18. package/src/components/UiSpinner/UiSpinner.stories.ts +579 -6
  19. package/src/components/UiSpinner/UiSpinner.vue +76 -4
  20. package/src/components/UiSpinner/__tests__/UiSpinner.test.ts +79 -0
  21. package/src/components/UiSpinner/index.ts +1 -1
  22. package/src/components/UiSpinner/types.ts +21 -6
  23. package/src/components/core/input/Input.vue +1 -1
  24. package/src/components/core/input-group/InputGroupInput.vue +1 -1
  25. package/src/components/core/native-select/NativeSelect.vue +1 -1
  26. package/src/patterns/UiHeadline/UiHeadline.guidance.md +53 -0
  27. package/src/patterns/UiHeadline/UiHeadline.stories.ts +915 -0
  28. package/src/patterns/UiHeadline/UiHeadline.vue +203 -0
  29. package/src/patterns/UiHeadline/__tests__/UiHeadline.test.ts +204 -0
  30. package/src/patterns/UiHeadline/index.ts +2 -0
  31. package/src/patterns/UiHeadline/types.ts +73 -0
  32. package/src/patterns/index.ts +1 -0
@@ -0,0 +1,203 @@
1
+ <script setup lang="ts">
2
+ import { computed, nextTick, ref, useId } from 'vue';
3
+ import { UiButton } from '@/components/UiButton';
4
+ import {
5
+ UiDropdownMenu,
6
+ UiDropdownMenuContent,
7
+ UiDropdownMenuItem,
8
+ UiDropdownMenuTrigger,
9
+ } from '@/components/UiDropdownMenu';
10
+ import { UiIcon } from '@/components/UiIcon';
11
+ import {
12
+ CheckIcon,
13
+ EllipsisVerticalIcon,
14
+ PenIcon,
15
+ XIcon,
16
+ } from '@/icons/lucide';
17
+ import { cn } from '@/lib/utils';
18
+ import type { UiHeadlineEmits, UiHeadlineProps } from './types';
19
+
20
+ defineOptions({
21
+ name: 'UiHeadline',
22
+ inheritAttrs: false,
23
+ });
24
+
25
+ const props = withDefaults(defineProps<UiHeadlineProps>(), {
26
+ description: undefined,
27
+ as: 'h2',
28
+ editable: true,
29
+ editLabel: 'Edit name',
30
+ menuLabel: 'More actions',
31
+ inputLabel: 'Name',
32
+ saveLabel: 'Save',
33
+ cancelLabel: 'Cancel',
34
+ });
35
+
36
+ const emit = defineEmits<UiHeadlineEmits>();
37
+
38
+ defineSlots<{
39
+ /** Extra menu items, rendered after "Edit name". */
40
+ menu?: () => unknown;
41
+ /** Replaces the description text. */
42
+ description?: () => unknown;
43
+ }>();
44
+
45
+ /** The title. Updated with the trimmed value when a changed name is saved. */
46
+ const title = defineModel<string>({ default: '' });
47
+
48
+ const descriptionId = useId();
49
+ const isEditing = ref(false);
50
+ const menuOpen = ref(false);
51
+ const draft = ref('');
52
+ const inputRef = ref<HTMLInputElement | null>(null);
53
+ const menuButtonRef = ref<InstanceType<typeof UiButton> | null>(null);
54
+
55
+ const canSave = computed(() => draft.value.trim().length > 0);
56
+
57
+ function focusMenuButton() {
58
+ const el = menuButtonRef.value?.$el as HTMLElement | undefined;
59
+ el?.focus();
60
+ }
61
+
62
+ /** Switches to edit mode with the current title selected. */
63
+ async function edit() {
64
+ if (!props.editable) return;
65
+ draft.value = title.value;
66
+ menuOpen.value = false;
67
+ isEditing.value = true;
68
+ await nextTick();
69
+ // The closing menu restores focus to its trigger; take it after that.
70
+ requestAnimationFrame(() => {
71
+ inputRef.value?.focus();
72
+ inputRef.value?.select();
73
+ });
74
+ }
75
+
76
+ async function finish() {
77
+ isEditing.value = false;
78
+ await nextTick();
79
+ focusMenuButton();
80
+ }
81
+
82
+ function save() {
83
+ if (!canSave.value) return;
84
+ const next = draft.value.trim();
85
+ if (next !== title.value) {
86
+ title.value = next;
87
+ emit('save', next);
88
+ }
89
+ void finish();
90
+ }
91
+
92
+ function cancel() {
93
+ emit('cancel');
94
+ void finish();
95
+ }
96
+
97
+ defineExpose({ edit });
98
+ </script>
99
+
100
+ <template>
101
+ <div
102
+ v-bind="$attrs"
103
+ data-slot="headline"
104
+ :data-editing="isEditing ? 'true' : undefined"
105
+ :class="
106
+ cn(
107
+ 'flex min-w-0 flex-col items-start',
108
+ isEditing ? 'gap-2' : 'gap-1',
109
+ $attrs.class as string,
110
+ )
111
+ "
112
+ >
113
+ <form
114
+ v-if="isEditing"
115
+ class="flex max-w-full min-w-0 items-center gap-1.5"
116
+ @submit.prevent="save"
117
+ >
118
+ <!-- The hidden copy of the draft sizes the field to its text. -->
119
+ <div
120
+ class="inline-grid max-w-full min-w-0 grid-cols-[minmax(0,max-content)] overflow-hidden rounded-md border border-input bg-background-input-default px-2 py-0.5 ui-focus-ring-within"
121
+ >
122
+ <span
123
+ aria-hidden="true"
124
+ class="invisible col-start-1 row-start-1 whitespace-pre pr-px heading-m !font-medium"
125
+ >{{ draft || ' ' }}</span
126
+ >
127
+ <input
128
+ ref="inputRef"
129
+ v-model="draft"
130
+ type="text"
131
+ size="1"
132
+ :aria-label="props.inputLabel"
133
+ :aria-describedby="
134
+ props.description || $slots.description ? descriptionId : undefined
135
+ "
136
+ class="col-start-1 row-start-1 w-full min-w-0 bg-transparent p-0 text-content-on-surface-primary outline-none heading-m !font-medium"
137
+ @keydown.esc.prevent="cancel"
138
+ />
139
+ </div>
140
+ <div class="flex shrink-0 items-center gap-1">
141
+ <UiButton
142
+ type="submit"
143
+ variant="ghost"
144
+ size="icon-sm"
145
+ :aria-label="props.saveLabel"
146
+ :disabled="!canSave"
147
+ >
148
+ <UiIcon :name="CheckIcon" size="s" />
149
+ </UiButton>
150
+ <UiButton
151
+ variant="ghost"
152
+ size="icon-sm"
153
+ :aria-label="props.cancelLabel"
154
+ @click="cancel"
155
+ >
156
+ <UiIcon :name="XIcon" size="s" />
157
+ </UiButton>
158
+ </div>
159
+ </form>
160
+
161
+ <div v-else class="flex max-w-full min-w-0 items-center gap-1">
162
+ <component
163
+ :is="props.as"
164
+ class="min-w-0 rounded-md border border-transparent px-2 py-0.5 break-words text-content-on-surface-primary heading-m !font-medium"
165
+ >
166
+ {{ title }}
167
+ </component>
168
+ <UiDropdownMenu v-if="props.editable" v-model:open="menuOpen">
169
+ <UiDropdownMenuTrigger as-child>
170
+ <UiButton
171
+ ref="menuButtonRef"
172
+ variant="ghost"
173
+ size="icon-sm"
174
+ class="shrink-0"
175
+ :aria-label="props.menuLabel"
176
+ >
177
+ <UiIcon :name="EllipsisVerticalIcon" size="s" />
178
+ </UiButton>
179
+ </UiDropdownMenuTrigger>
180
+ <UiDropdownMenuContent
181
+ side="right"
182
+ align="start"
183
+ :side-offset="8"
184
+ class="w-56"
185
+ >
186
+ <UiDropdownMenuItem @select="edit">
187
+ <UiIcon :name="PenIcon" size="s" />
188
+ {{ props.editLabel }}
189
+ </UiDropdownMenuItem>
190
+ <slot name="menu" />
191
+ </UiDropdownMenuContent>
192
+ </UiDropdownMenu>
193
+ </div>
194
+
195
+ <p
196
+ v-if="props.description || $slots.description"
197
+ :id="descriptionId"
198
+ class="pl-2 text-content-on-surface-muted label-xs-regular"
199
+ >
200
+ <slot name="description">{{ props.description }}</slot>
201
+ </p>
202
+ </div>
203
+ </template>
@@ -0,0 +1,204 @@
1
+ import { render, screen, waitFor } from '@testing-library/vue';
2
+ import userEvent from '@testing-library/user-event';
3
+ import { describe, expect, test } from 'vitest';
4
+ import { defineComponent, h, ref } from 'vue';
5
+ import UiHeadline from '../UiHeadline.vue';
6
+
7
+ type HeadlineInstance = { edit: () => Promise<void> };
8
+
9
+ function renderHeadline(props: Record<string, unknown> = {}) {
10
+ const title = ref('This is a headline');
11
+ const instance = ref<HeadlineInstance | null>(null);
12
+ const saved: string[] = [];
13
+ let cancelled = 0;
14
+
15
+ const Host = defineComponent({
16
+ setup() {
17
+ return () =>
18
+ h(UiHeadline, {
19
+ ref: instance,
20
+ modelValue: title.value,
21
+ 'onUpdate:modelValue': (value: string) => {
22
+ title.value = value;
23
+ },
24
+ onSave: (value: string) => saved.push(value),
25
+ onCancel: () => {
26
+ cancelled += 1;
27
+ },
28
+ ...props,
29
+ });
30
+ },
31
+ });
32
+
33
+ const utils = render(Host);
34
+ return {
35
+ ...utils,
36
+ title,
37
+ saved,
38
+ cancelled: () => cancelled,
39
+ edit: async () => {
40
+ await instance.value?.edit();
41
+ return waitFor(() =>
42
+ expect(screen.getByRole('textbox', { name: 'Name' })).toHaveFocus(),
43
+ );
44
+ },
45
+ };
46
+ }
47
+
48
+ describe('UiHeadline', () => {
49
+ test('renders the title as an h2 with the description', () => {
50
+ renderHeadline({ description: 'Lorem ipsum dolor sit amet.' });
51
+
52
+ expect(
53
+ screen.getByRole('heading', { level: 2, name: 'This is a headline' }),
54
+ ).toBeInTheDocument();
55
+ expect(screen.getByText('Lorem ipsum dolor sit amet.')).toBeInTheDocument();
56
+ expect(
57
+ screen.getByRole('button', { name: 'More actions' }),
58
+ ).toBeInTheDocument();
59
+ });
60
+
61
+ test('renders the heading level from as', () => {
62
+ renderHeadline({ as: 'h1' });
63
+ expect(screen.getByRole('heading', { level: 1 })).toBeInTheDocument();
64
+ });
65
+
66
+ test('hides the menu when not editable', () => {
67
+ renderHeadline({ editable: false });
68
+ expect(
69
+ screen.queryByRole('button', { name: 'More actions' }),
70
+ ).not.toBeInTheDocument();
71
+ });
72
+
73
+ test('"Edit name" in the menu opens the field with the title selected', async () => {
74
+ const user = userEvent.setup();
75
+ renderHeadline();
76
+
77
+ await user.click(screen.getByRole('button', { name: 'More actions' }));
78
+ await user.click(
79
+ await screen.findByRole('menuitem', { name: 'Edit name' }),
80
+ );
81
+
82
+ const input = await screen.findByRole('textbox', { name: 'Name' });
83
+ await waitFor(() => expect(input).toHaveFocus());
84
+ expect(input).toHaveValue('This is a headline');
85
+ expect(input.selectionStart).toBe(0);
86
+ expect(input.selectionEnd).toBe('This is a headline'.length);
87
+ expect(screen.queryByRole('heading')).not.toBeInTheDocument();
88
+ });
89
+
90
+ test('Enter saves the trimmed title and returns focus to the menu button', async () => {
91
+ const user = userEvent.setup();
92
+ const { edit, title, saved } = renderHeadline();
93
+ await edit();
94
+
95
+ await user.clear(screen.getByRole('textbox', { name: 'Name' }));
96
+ await user.type(
97
+ screen.getByRole('textbox', { name: 'Name' }),
98
+ ' Renamed {Enter}',
99
+ );
100
+
101
+ expect(title.value).toBe('Renamed');
102
+ expect(saved).toEqual(['Renamed']);
103
+ expect(
104
+ screen.getByRole('heading', { name: 'Renamed' }),
105
+ ).toBeInTheDocument();
106
+ await waitFor(() =>
107
+ expect(
108
+ screen.getByRole('button', { name: 'More actions' }),
109
+ ).toHaveFocus(),
110
+ );
111
+ });
112
+
113
+ test('typing replaces the selected title and the save button saves it', async () => {
114
+ const user = userEvent.setup();
115
+ const { edit, title } = renderHeadline();
116
+ await edit();
117
+
118
+ await user.keyboard('Q3 plan');
119
+ await user.click(screen.getByRole('button', { name: 'Save' }));
120
+
121
+ expect(title.value).toBe('Q3 plan');
122
+ });
123
+
124
+ test('Escape cancels and keeps the original title', async () => {
125
+ const user = userEvent.setup();
126
+ const { edit, title, saved, cancelled } = renderHeadline();
127
+ await edit();
128
+
129
+ await user.type(screen.getByRole('textbox', { name: 'Name' }), 'Draft');
130
+ await user.keyboard('{Escape}');
131
+
132
+ expect(title.value).toBe('This is a headline');
133
+ expect(saved).toEqual([]);
134
+ expect(cancelled()).toBe(1);
135
+ expect(
136
+ screen.getByRole('heading', { name: 'This is a headline' }),
137
+ ).toBeInTheDocument();
138
+ });
139
+
140
+ test('the cancel button leaves edit mode without saving', async () => {
141
+ const user = userEvent.setup();
142
+ const { edit, title } = renderHeadline();
143
+ await edit();
144
+
145
+ await user.type(screen.getByRole('textbox', { name: 'Name' }), 'Draft');
146
+ await user.click(screen.getByRole('button', { name: 'Cancel' }));
147
+
148
+ expect(title.value).toBe('This is a headline');
149
+ expect(
150
+ screen.queryByRole('textbox', { name: 'Name' }),
151
+ ).not.toBeInTheDocument();
152
+ });
153
+
154
+ test('an empty title cannot be saved', async () => {
155
+ const user = userEvent.setup();
156
+ const { edit, title, saved } = renderHeadline();
157
+ await edit();
158
+
159
+ await user.clear(screen.getByRole('textbox', { name: 'Name' }));
160
+ await user.type(screen.getByRole('textbox', { name: 'Name' }), ' ');
161
+
162
+ expect(screen.getByRole('button', { name: 'Save' })).toBeDisabled();
163
+ await user.keyboard('{Enter}');
164
+ expect(title.value).toBe('This is a headline');
165
+ expect(saved).toEqual([]);
166
+ expect(screen.getByRole('textbox', { name: 'Name' })).toBeInTheDocument();
167
+ });
168
+
169
+ test('saving an unchanged title does not emit save', async () => {
170
+ const user = userEvent.setup();
171
+ const { edit, saved } = renderHeadline();
172
+ await edit();
173
+
174
+ await user.keyboard('{Enter}');
175
+ expect(saved).toEqual([]);
176
+ expect(screen.getByRole('heading')).toBeInTheDocument();
177
+ });
178
+
179
+ test('the field is described by the description', async () => {
180
+ const { edit } = renderHeadline({ description: 'Shown to your team.' });
181
+ await edit();
182
+
183
+ expect(
184
+ screen.getByRole('textbox', { name: 'Name' }),
185
+ ).toHaveAccessibleDescription('Shown to your team.');
186
+ });
187
+
188
+ test('uses custom labels', async () => {
189
+ const { edit } = renderHeadline({
190
+ menuLabel: 'Workspace actions',
191
+ inputLabel: 'Name',
192
+ saveLabel: 'Save name',
193
+ cancelLabel: 'Discard',
194
+ });
195
+ expect(
196
+ screen.getByRole('button', { name: 'Workspace actions' }),
197
+ ).toBeInTheDocument();
198
+ await edit();
199
+ expect(
200
+ screen.getByRole('button', { name: 'Save name' }),
201
+ ).toBeInTheDocument();
202
+ expect(screen.getByRole('button', { name: 'Discard' })).toBeInTheDocument();
203
+ });
204
+ });
@@ -0,0 +1,2 @@
1
+ export { default as UiHeadline } from './UiHeadline.vue';
2
+ export type * from './types';
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Heading levels the headline can render as.
3
+ */
4
+ export type UiHeadlineLevel = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
5
+
6
+ /**
7
+ * A page or section headline with an optional description that the user can
8
+ * rename in place: "Edit name" in its menu turns the title into a field with
9
+ * save and cancel actions.
10
+ * @category Layout
11
+ * @useCases page title, section title, rename in place, editable name, workspace name
12
+ * @keywords headline, heading, title, rename, inline edit, editable, description
13
+ * @related UiDropdownMenu, UiInput, UiButton
14
+ */
15
+ export interface UiHeadlineProps {
16
+ /**
17
+ * Supporting text shown below the title.
18
+ */
19
+ description?: string;
20
+
21
+ /**
22
+ * Heading element the title renders as. Pick the level that fits the page
23
+ * outline; the visual size stays the same.
24
+ * @default 'h2'
25
+ */
26
+ as?: UiHeadlineLevel;
27
+
28
+ /**
29
+ * Shows the menu with "Edit name". Set to `false` for a read-only headline.
30
+ * @default true
31
+ */
32
+ editable?: boolean;
33
+
34
+ /**
35
+ * Label of the menu item that starts editing.
36
+ * @default 'Edit name'
37
+ */
38
+ editLabel?: string;
39
+
40
+ /**
41
+ * Accessible name of the menu button.
42
+ * @default 'More actions'
43
+ */
44
+ menuLabel?: string;
45
+
46
+ /**
47
+ * Accessible name of the title field while editing.
48
+ * @default 'Name'
49
+ */
50
+ inputLabel?: string;
51
+
52
+ /**
53
+ * Accessible name of the save button.
54
+ * @default 'Save'
55
+ */
56
+ saveLabel?: string;
57
+
58
+ /**
59
+ * Accessible name of the cancel button.
60
+ * @default 'Cancel'
61
+ */
62
+ cancelLabel?: string;
63
+ }
64
+
65
+ /**
66
+ * Emits for the UiHeadline component.
67
+ */
68
+ export interface UiHeadlineEmits {
69
+ /** Emitted with the trimmed title when a changed name is saved. */
70
+ save: [value: string];
71
+ /** Emitted when editing ends without saving. */
72
+ cancel: [];
73
+ }
@@ -7,6 +7,7 @@ export * from './UiDataTable';
7
7
  export * from './UiDatePicker';
8
8
  export * from './UiFileUpload';
9
9
  export * from './UiFloatActions';
10
+ export * from './UiHeadline';
10
11
  export * from './UiLayout';
11
12
  export * from './UiPlaceholder';
12
13
  export * from './UiProfileCard';