@aleph-alpha/ui-library 5.8.0 → 5.10.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 (111) hide show
  1. package/README.md +8 -7
  2. package/config.js +157 -4
  3. package/dist/system/__tests__/test-utils.d.ts +24 -0
  4. package/dist/system/components/UiAutocomplete/UiAutocomplete.vue.d.ts +8 -4
  5. package/dist/system/components/UiAutocomplete/types.d.ts +11 -0
  6. package/dist/system/components/UiFadeMask/UiFadeMask.vue.d.ts +21 -0
  7. package/dist/system/components/UiFadeMask/index.d.ts +2 -0
  8. package/dist/system/components/UiFadeMask/types.d.ts +33 -0
  9. package/dist/system/components/UiFileItem/UiFileItem.vue.d.ts +3 -1
  10. package/dist/system/components/UiFileType/types.d.ts +2 -1
  11. package/dist/system/components/UiListbox/UiListboxVirtualizer.vue.d.ts +21 -0
  12. package/dist/system/components/UiListbox/index.d.ts +1 -0
  13. package/dist/system/components/UiListbox/types.d.ts +36 -0
  14. package/dist/system/components/UiSelect/UiSelectVirtualizer.vue.d.ts +21 -0
  15. package/dist/system/components/UiSelect/index.d.ts +1 -0
  16. package/dist/system/components/UiSelect/types.d.ts +36 -0
  17. package/dist/system/components/UiSwitch/UiSwitch.vue.d.ts +1 -0
  18. package/dist/system/components/UiSwitch/types.d.ts +12 -0
  19. package/dist/system/components/core/native-select/NativeSelectOptGroup.vue.d.ts +2 -2
  20. package/dist/system/components/core/native-select/NativeSelectOption.vue.d.ts +2 -2
  21. package/dist/system/components/core/switch/Switch.vue.d.ts +2 -0
  22. package/dist/system/components/core/switch/index.d.ts +8 -0
  23. package/dist/system/components/index.d.ts +1 -0
  24. package/dist/system/index.d.ts +1 -0
  25. package/dist/system/lib/scrollbar.d.ts +37 -0
  26. package/dist/system/lib/useAnchorFloat.d.ts +0 -2
  27. package/dist/system/lib/useIsTruncated.d.ts +15 -0
  28. package/dist/system/lib.js +22660 -21292
  29. package/dist/system/mobile-device-frame.png +0 -0
  30. package/dist/system/patterns/UiFloatActions/types.d.ts +2 -2
  31. package/dist/system/patterns/UiProfileCard/index.d.ts +7 -0
  32. package/dist/system/patterns/UiTopBar/UiTopBar.vue.d.ts +23 -0
  33. package/dist/system/patterns/UiTopBar/index.d.ts +2 -0
  34. package/dist/system/patterns/UiTopBar/types.d.ts +28 -0
  35. package/dist/system/patterns/index.d.ts +2 -0
  36. package/dist/system/story-sidebar-icons.json +7 -1
  37. package/docs/public-docs/foundations.md +17 -15
  38. package/package.json +4 -3
  39. package/src/__tests__/test-utils.ts +74 -0
  40. package/src/components/UiAutocomplete/UiAutocomplete.stories.ts +94 -0
  41. package/src/components/UiAutocomplete/UiAutocomplete.vue +164 -44
  42. package/src/components/UiAutocomplete/__tests__/UiAutocompleteVirtualized.test.ts +107 -0
  43. package/src/components/UiAutocomplete/types.ts +11 -0
  44. package/src/components/UiCodeBlock/UiCodeBlock.stories.ts +54 -0
  45. package/src/components/UiCodeBlock/UiCodeBlock.vue +84 -32
  46. package/src/components/UiCodeBlock/__tests__/UiCodeBlock.test.ts +65 -21
  47. package/src/components/UiDropdownMenu/UiDropdownMenu.stories.ts +760 -0
  48. package/src/components/UiFadeMask/UiFadeMask.stories.ts +563 -0
  49. package/src/components/UiFadeMask/UiFadeMask.vue +86 -0
  50. package/src/components/UiFadeMask/__tests__/UiFadeMask.test.ts +52 -0
  51. package/src/components/UiFadeMask/index.ts +6 -0
  52. package/src/components/UiFadeMask/types.ts +35 -0
  53. package/src/components/UiFileItem/UiFileItem.vue +17 -4
  54. package/src/components/UiFileItem/__tests__/UiFileItem.test.ts +49 -90
  55. package/src/components/UiFileType/UiFileType.stories.ts +13 -2
  56. package/src/components/UiFileType/UiFileType.vue +8 -0
  57. package/src/components/UiFileType/__tests__/UiFileType.test.ts +10 -0
  58. package/src/components/UiFileType/types.ts +3 -1
  59. package/src/components/UiListbox/UiListbox.stories.ts +121 -0
  60. package/src/components/UiListbox/UiListboxVirtualizer.vue +42 -0
  61. package/src/components/UiListbox/__tests__/UiListboxVirtualizer.test.ts +96 -0
  62. package/src/components/UiListbox/index.ts +1 -0
  63. package/src/components/UiListbox/types.ts +42 -0
  64. package/src/components/UiSelect/UiSelect.stories.ts +109 -1
  65. package/src/components/UiSelect/UiSelectVirtualizer.vue +373 -0
  66. package/src/components/UiSelect/__tests__/UiSelectVirtualizer.test.ts +185 -0
  67. package/src/components/UiSelect/index.ts +1 -0
  68. package/src/components/UiSelect/types.ts +38 -0
  69. package/src/components/UiSnackbar/UiSnackbar.stories.ts +4 -5
  70. package/src/components/UiSwitch/UiSwitch.stories.ts +658 -7
  71. package/src/components/UiSwitch/UiSwitch.vue +1 -0
  72. package/src/components/UiSwitch/__tests__/UiSwitch.test.ts +48 -0
  73. package/src/components/UiSwitch/types.ts +13 -0
  74. package/src/components/UiToggleGroup/UiToggleGroup.vue +1 -1
  75. package/src/components/core/dropdown-menu/DropdownMenuCheckboxItem.vue +8 -2
  76. package/src/components/core/dropdown-menu/DropdownMenuItem.vue +1 -1
  77. package/src/components/core/dropdown-menu/DropdownMenuRadioItem.vue +8 -2
  78. package/src/components/core/dropdown-menu/DropdownMenuSubTrigger.vue +1 -1
  79. package/src/components/core/field/FieldLabel.vue +1 -1
  80. package/src/components/core/label/Label.vue +1 -1
  81. package/src/components/core/listbox/ListboxItem.vue +1 -1
  82. package/src/components/core/switch/Switch.vue +23 -17
  83. package/src/components/core/switch/index.ts +36 -0
  84. package/src/components/index.ts +1 -0
  85. package/src/index.ts +3 -0
  86. package/src/lib/scrollbar.ts +295 -0
  87. package/src/lib/useAnchorFloat.ts +0 -2
  88. package/src/lib/useIsTruncated.test.ts +174 -0
  89. package/src/lib/useIsTruncated.ts +75 -0
  90. package/src/patterns/UiFileUpload/UiFileItem.stories.ts +1162 -0
  91. package/src/patterns/UiFileUpload/UiFileUpload.stories.ts +552 -1
  92. package/src/patterns/UiFileUpload/__tests__/UiFileUpload.test.ts +5 -21
  93. package/src/patterns/UiFloatActions/UiFloatActionItems.vue +0 -1
  94. package/src/patterns/UiFloatActions/UiFloatActions.stories.ts +4 -6
  95. package/src/patterns/UiFloatActions/UiFloatActions.vue +23 -12
  96. package/src/patterns/UiFloatActions/__tests__/UiFloatActions.test.ts +23 -0
  97. package/src/patterns/UiFloatActions/types.ts +2 -2
  98. package/src/patterns/UiProfileCard/UiProfileCard.stories.ts +1079 -0
  99. package/src/patterns/UiProfileCard/index.ts +7 -0
  100. package/src/patterns/UiTopBar/UiTopBar.stories.ts +1121 -0
  101. package/src/patterns/UiTopBar/UiTopBar.vue +166 -0
  102. package/src/patterns/UiTopBar/__tests__/UiTopBar.test.ts +245 -0
  103. package/src/patterns/UiTopBar/index.ts +2 -0
  104. package/src/patterns/UiTopBar/types.ts +29 -0
  105. package/src/patterns/index.ts +2 -0
  106. package/src/theme/Background.stories.ts +27 -1
  107. package/src/theme/BorderRadius.stories.ts +5 -0
  108. package/src/theme/Overlay.stories.ts +12 -1
  109. package/src/theme/Scrollbar.stories.ts +260 -0
  110. package/tokens.json +224 -2
  111. package/src/components/UiFileItem/UiFileItem.stories.ts +0 -442
@@ -0,0 +1,174 @@
1
+ import { render } from '@testing-library/vue';
2
+ import { afterAll, describe, expect, test, vi } from 'vitest';
3
+ import { defineComponent, h, nextTick, ref, useTemplateRef } from 'vue';
4
+ import { useIsTruncated } from './useIsTruncated';
5
+
6
+ // jsdom reports every element as 0x0, so geometry has to be faked per element.
7
+ const geometry = new WeakMap<Element, { scroll: number; client: number }>();
8
+
9
+ function setGeometry(el: Element, scroll: number, client: number): void {
10
+ geometry.set(el, { scroll, client });
11
+ }
12
+
13
+ Object.defineProperty(HTMLElement.prototype, 'scrollWidth', {
14
+ configurable: true,
15
+ get(this: HTMLElement) {
16
+ return geometry.get(this)?.scroll ?? 0;
17
+ },
18
+ });
19
+ Object.defineProperty(HTMLElement.prototype, 'clientWidth', {
20
+ configurable: true,
21
+ get(this: HTMLElement) {
22
+ return geometry.get(this)?.client ?? 0;
23
+ },
24
+ });
25
+
26
+ /**
27
+ * Stands in for the browser's ResizeObserver. Tracks its own observed targets
28
+ * so a test can both drive a resize and assert how many observers exist.
29
+ */
30
+ const observers: StubResizeObserver[] = [];
31
+
32
+ class StubResizeObserver {
33
+ readonly observed = new Set<Element>();
34
+ private readonly callback: (entries: { target: Element }[]) => void;
35
+
36
+ constructor(callback: (entries: { target: Element }[]) => void) {
37
+ this.callback = callback;
38
+ observers.push(this);
39
+ }
40
+ observe(el: Element): void {
41
+ this.observed.add(el);
42
+ }
43
+ unobserve(el: Element): void {
44
+ this.observed.delete(el);
45
+ }
46
+ disconnect(): void {
47
+ this.observed.clear();
48
+ }
49
+ /** Deliver a resize for every element currently observed. */
50
+ fire(): void {
51
+ this.callback([...this.observed].map((target) => ({ target })));
52
+ }
53
+ }
54
+
55
+ vi.stubGlobal('ResizeObserver', StubResizeObserver);
56
+
57
+ // Restore the real global so the stub cannot leak into other test files.
58
+ afterAll(() => {
59
+ vi.unstubAllGlobals();
60
+ });
61
+
62
+ /** Drives a resize through whichever observer the composable created. */
63
+ function resize(): void {
64
+ for (const o of observers) o.fire();
65
+ }
66
+
67
+ /**
68
+ * Mounts a span reporting the given geometry.
69
+ * @param scroll - Content width to report
70
+ * @param client - Visible width to report
71
+ */
72
+ function mountWithGeometry(scroll: number, client: number) {
73
+ const text = ref('some name');
74
+ let isTruncated!: ReturnType<typeof useIsTruncated>;
75
+
76
+ const Component = defineComponent({
77
+ setup() {
78
+ const el = useTemplateRef<HTMLElement>('el');
79
+ isTruncated = useIsTruncated(el, () => text.value);
80
+ return () => h('span', { ref: 'el' }, text.value);
81
+ },
82
+ });
83
+
84
+ const { container } = render(Component);
85
+ const span = container.querySelector('span')!;
86
+ setGeometry(span, scroll, client);
87
+
88
+ return { isTruncated: () => isTruncated, span, text };
89
+ }
90
+
91
+ // Deliberately never cleared: the composable keeps one module-level observer
92
+ // for the whole process, and that singleton is what the sharing test asserts.
93
+ describe('useIsTruncated', () => {
94
+ test('reports truncated when the content is wider than the box', async () => {
95
+ const { isTruncated, span } = mountWithGeometry(400, 200);
96
+
97
+ setGeometry(span, 400, 200);
98
+ resize();
99
+ await nextTick();
100
+
101
+ expect(isTruncated().value).toBe(true);
102
+ });
103
+
104
+ test('reports not truncated when the content fits', async () => {
105
+ const { isTruncated, span } = mountWithGeometry(120, 200);
106
+
107
+ setGeometry(span, 120, 200);
108
+ resize();
109
+ await nextTick();
110
+
111
+ expect(isTruncated().value).toBe(false);
112
+ });
113
+
114
+ test('ignores a sub-pixel overflow', async () => {
115
+ const { isTruncated, span } = mountWithGeometry(200.4, 200);
116
+
117
+ setGeometry(span, 200.4, 200);
118
+ resize();
119
+ await nextTick();
120
+
121
+ expect(isTruncated().value).toBe(false);
122
+ });
123
+
124
+ test('re-measures when the box is resized', async () => {
125
+ const { isTruncated, span } = mountWithGeometry(400, 200);
126
+
127
+ resize();
128
+ await nextTick();
129
+ expect(isTruncated().value).toBe(true);
130
+
131
+ setGeometry(span, 400, 400);
132
+ resize();
133
+ await nextTick();
134
+
135
+ expect(isTruncated().value).toBe(false);
136
+ });
137
+
138
+ test('re-measures when the text changes without a resize', async () => {
139
+ const { isTruncated, span, text } = mountWithGeometry(120, 200);
140
+
141
+ resize();
142
+ await nextTick();
143
+ expect(isTruncated().value).toBe(false);
144
+
145
+ // A longer name in a box of the same width fires no ResizeObserver entry.
146
+ setGeometry(span, 900, 200);
147
+ text.value = 'a considerably longer name';
148
+ await nextTick();
149
+
150
+ expect(isTruncated().value).toBe(true);
151
+ });
152
+
153
+ test('many callers share one observer, so a long list costs one callback', async () => {
154
+ const rows = Array.from({ length: 12 }, () => mountWithGeometry(400, 200));
155
+
156
+ resize();
157
+ await nextTick();
158
+
159
+ expect(observers).toHaveLength(1);
160
+ expect(observers[0]!.observed.size).toBe(12);
161
+ for (const row of rows) expect(row.isTruncated().value).toBe(true);
162
+ });
163
+
164
+ test('each element is measured with its own geometry', async () => {
165
+ const wide = mountWithGeometry(400, 200);
166
+ const fits = mountWithGeometry(100, 200);
167
+
168
+ resize();
169
+ await nextTick();
170
+
171
+ expect(wide.isTruncated().value).toBe(true);
172
+ expect(fits.isTruncated().value).toBe(false);
173
+ });
174
+ });
@@ -0,0 +1,75 @@
1
+ import { onScopeDispose, ref, watch, type Ref, type ShallowRef } from 'vue';
2
+
3
+ /** An element ref as produced by `useTemplateRef`. */
4
+ type ElementRef = Readonly<ShallowRef<HTMLElement | null>>;
5
+
6
+ /**
7
+ * One observer for every caller. `ResizeObserver` is built to watch many
8
+ * targets and delivers them in a single batched callback, so a list of rows
9
+ * costs one callback and one layout pass rather than one per row. Created
10
+ * lazily so importing this module never touches the DOM.
11
+ */
12
+ const measurers = new WeakMap<Element, () => void>();
13
+ let sharedObserver: ResizeObserver | undefined;
14
+
15
+ function getSharedObserver(): ResizeObserver | undefined {
16
+ if (typeof ResizeObserver === 'undefined') return undefined;
17
+ sharedObserver ??= new ResizeObserver((entries) => {
18
+ for (const entry of entries) measurers.get(entry.target)?.();
19
+ });
20
+ return sharedObserver;
21
+ }
22
+
23
+ function watchElement(el: Element, measure: () => void): void {
24
+ measurers.set(el, measure);
25
+ getSharedObserver()?.observe(el);
26
+ }
27
+
28
+ function unwatchElement(el: Element): void {
29
+ measurers.delete(el);
30
+ getSharedObserver()?.unobserve(el);
31
+ }
32
+
33
+ /**
34
+ * Whether an element's text is currently clipped by `text-overflow: ellipsis`.
35
+ *
36
+ * Re-measures when the element resizes and whenever `watchSource` changes, so
37
+ * it stays correct as the container narrows or the text is swapped out. Use it
38
+ * to show a tooltip only for names that are actually cut off.
39
+ * @param elementRef - The element whose text may be clipped
40
+ * @param watchSource - Value to re-measure on, e.g. the rendered text
41
+ * @returns Reactive truncation state
42
+ */
43
+ export function useIsTruncated(
44
+ elementRef: ElementRef,
45
+ watchSource?: () => unknown,
46
+ ): Ref<boolean> {
47
+ const isTruncated = ref(false);
48
+
49
+ function measure(): void {
50
+ const el = elementRef.value;
51
+ // Sub-pixel widths round differently per browser, so require a whole
52
+ // pixel of overflow before calling it truncated.
53
+ isTruncated.value = el ? el.scrollWidth - el.clientWidth >= 1 : false;
54
+ }
55
+
56
+ // A resize alone doesn't cover a text swap that keeps the box the same size.
57
+ if (watchSource) watch(watchSource, measure, { flush: 'post' });
58
+
59
+ // Runs on mount too, when the template ref goes null -> element.
60
+ watch(
61
+ elementRef,
62
+ (el, previous) => {
63
+ if (previous) unwatchElement(previous);
64
+ if (el) watchElement(el, measure);
65
+ measure();
66
+ },
67
+ { immediate: true, flush: 'post' },
68
+ );
69
+
70
+ onScopeDispose(() => {
71
+ if (elementRef.value) unwatchElement(elementRef.value);
72
+ });
73
+
74
+ return isTruncated;
75
+ }