@aleph-alpha/ui-library 3.0.0 → 3.1.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/dist/system/components/core/sidebar/SidebarProvider.vue.d.ts +2 -0
- package/dist/system/components/core/sidebar/utils.d.ts +1 -1
- package/dist/system/index.d.ts +1 -0
- package/dist/system/lib/useDevice.d.ts +59 -0
- package/dist/system/lib.js +13579 -13483
- package/dist/system/patterns/UiSidebar/UiSidebarProvider.vue.d.ts +10 -1
- package/dist/system/patterns/UiSidebar/__tests__/sidebar-test-helpers.d.ts +55 -6
- package/dist/system/patterns/UiSidebar/index.d.ts +1 -1
- package/dist/system/patterns/UiSidebar/types.d.ts +26 -2
- package/package.json +1 -1
- package/src/components/UiButton/UiButton.stories.ts +2 -2
- package/src/components/UiCheckbox/UiCheckbox.stories.ts +2 -2
- package/src/components/UiField/UiField.stories.ts +3 -2
- package/src/components/UiItem/UiItem.stories.ts +2 -2
- package/src/components/core/sidebar/Sidebar.vue +25 -27
- package/src/components/core/sidebar/SidebarInset.vue +13 -1
- package/src/components/core/sidebar/SidebarProvider.vue +22 -2
- package/src/components/core/sidebar/SidebarTrigger.vue +2 -2
- package/src/components/core/sidebar/utils.ts +1 -1
- package/src/directives/vHover.stories.ts +3 -0
- package/src/index.ts +1 -0
- package/src/lib/useDevice.stories.ts +407 -0
- package/src/lib/useDevice.test.ts +129 -0
- package/src/lib/useDevice.ts +199 -0
- package/src/lib/useDrag.stories.ts +3 -0
- package/src/patterns/UiSidebar/UiSidebar.stories.ts +14 -1
- package/src/patterns/UiSidebar/UiSidebarProvider.vue +21 -1
- package/src/patterns/UiSidebar/UiSidebarTrigger.vue +39 -5
- package/src/patterns/UiSidebar/__tests__/UiSidebar.test.ts +66 -1
- package/src/patterns/UiSidebar/__tests__/UiSidebarHeader.test.ts +1 -1
- package/src/patterns/UiSidebar/__tests__/UiSidebarTrigger.test.ts +1 -1
- package/src/patterns/UiSidebar/__tests__/sidebar-test-helpers.ts +21 -0
- package/src/patterns/UiSidebar/index.ts +1 -0
- package/src/patterns/UiSidebar/types.ts +29 -2
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/vue3-vite';
|
|
2
|
+
import { computed, ref } from 'vue';
|
|
3
|
+
import {
|
|
4
|
+
UiAccordion,
|
|
5
|
+
UiAccordionContent,
|
|
6
|
+
UiAccordionItem,
|
|
7
|
+
UiAccordionTrigger,
|
|
8
|
+
} from '../components/UiAccordion';
|
|
9
|
+
import {
|
|
10
|
+
UiResizableHandle,
|
|
11
|
+
UiResizablePanel,
|
|
12
|
+
UiResizablePanelGroup,
|
|
13
|
+
} from '../components/UiResizable';
|
|
14
|
+
import { getDeviceData, useDevice, type DeviceData } from './useDevice';
|
|
15
|
+
|
|
16
|
+
type UserAgentExample = {
|
|
17
|
+
label: string;
|
|
18
|
+
userAgent: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type MutableDetectorWindow = Window & {
|
|
22
|
+
innerHeight: number;
|
|
23
|
+
innerWidth: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const DETECTOR_TOTAL_WIDTH = 960;
|
|
27
|
+
const DETECTOR_HEIGHT = 420;
|
|
28
|
+
|
|
29
|
+
const userAgentExamples: UserAgentExample[] = [
|
|
30
|
+
{
|
|
31
|
+
label: 'iPhone Safari',
|
|
32
|
+
userAgent:
|
|
33
|
+
'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
label: 'Desktop Chrome',
|
|
37
|
+
userAgent:
|
|
38
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
label: 'Googlebot',
|
|
42
|
+
userAgent: 'Googlebot/2.1',
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
const meta = {
|
|
47
|
+
title: 'Utilities/useDevice',
|
|
48
|
+
tags: ['autodocs'],
|
|
49
|
+
parameters: {
|
|
50
|
+
chromatic: {
|
|
51
|
+
disableSnapshot: true,
|
|
52
|
+
},
|
|
53
|
+
docs: {
|
|
54
|
+
description: {
|
|
55
|
+
component:
|
|
56
|
+
'`useDevice()` returns SSR-safe user-agent device data, reactive screen size, and mobile/desktop breakpoint booleans that update on resize.',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
} satisfies Meta;
|
|
61
|
+
|
|
62
|
+
export default meta;
|
|
63
|
+
type Story = StoryObj<typeof meta>;
|
|
64
|
+
|
|
65
|
+
function toRows(device: DeviceData) {
|
|
66
|
+
return [
|
|
67
|
+
['type', device.type],
|
|
68
|
+
['os', device.os],
|
|
69
|
+
['browser', device.browser],
|
|
70
|
+
['isMobile', String(device.isMobile)],
|
|
71
|
+
['isDesktop', String(device.isDesktop)],
|
|
72
|
+
['isBot', String(device.isBot)],
|
|
73
|
+
];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function createDetectorWindow(
|
|
77
|
+
width: number,
|
|
78
|
+
height: number,
|
|
79
|
+
userAgent: string,
|
|
80
|
+
): MutableDetectorWindow {
|
|
81
|
+
const listeners = new Set<EventListenerOrEventListenerObject>();
|
|
82
|
+
const detectorWindow = {
|
|
83
|
+
innerHeight: height,
|
|
84
|
+
innerWidth: width,
|
|
85
|
+
navigator: { userAgent } as Navigator,
|
|
86
|
+
addEventListener(
|
|
87
|
+
type: string,
|
|
88
|
+
listener: EventListenerOrEventListenerObject,
|
|
89
|
+
) {
|
|
90
|
+
if (type === 'resize') listeners.add(listener);
|
|
91
|
+
},
|
|
92
|
+
removeEventListener(
|
|
93
|
+
type: string,
|
|
94
|
+
listener: EventListenerOrEventListenerObject,
|
|
95
|
+
) {
|
|
96
|
+
if (type === 'resize') listeners.delete(listener);
|
|
97
|
+
},
|
|
98
|
+
dispatchEvent(event: Event) {
|
|
99
|
+
if (event.type === 'resize') {
|
|
100
|
+
listeners.forEach((listener) => {
|
|
101
|
+
if (typeof listener === 'function') {
|
|
102
|
+
listener.call(detectorWindow, event);
|
|
103
|
+
} else {
|
|
104
|
+
listener.handleEvent(event);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return true;
|
|
110
|
+
},
|
|
111
|
+
} as unknown as MutableDetectorWindow;
|
|
112
|
+
|
|
113
|
+
return detectorWindow;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export const UseDevice: Story = {
|
|
117
|
+
parameters: {
|
|
118
|
+
docs: {
|
|
119
|
+
source: {
|
|
120
|
+
code: `<script setup lang="ts">
|
|
121
|
+
import { useDevice } from '@aleph-alpha/ui-library';
|
|
122
|
+
|
|
123
|
+
const { device, screenSize, breakpoint, isMobile, isDesktop } = useDevice();
|
|
124
|
+
</script>
|
|
125
|
+
|
|
126
|
+
<template>
|
|
127
|
+
<div>
|
|
128
|
+
<p>Viewport: {{ screenSize.width }} x {{ screenSize.height }}</p>
|
|
129
|
+
<p>Breakpoint: {{ breakpoint }}</p>
|
|
130
|
+
<p>Mobile: {{ isMobile }}</p>
|
|
131
|
+
<p>Desktop: {{ isDesktop }}</p>
|
|
132
|
+
<p>Device type: {{ device.type }}</p>
|
|
133
|
+
</div>
|
|
134
|
+
</template>`,
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
render: () => ({
|
|
139
|
+
setup() {
|
|
140
|
+
const { device, screenSize, breakpoint, isMobile, isDesktop } =
|
|
141
|
+
useDevice();
|
|
142
|
+
const currentRows = computed(() => toRows(device.value));
|
|
143
|
+
const exampleRows = computed(() =>
|
|
144
|
+
userAgentExamples.map((example) => ({
|
|
145
|
+
...example,
|
|
146
|
+
rows: toRows(getDeviceData(example.userAgent)),
|
|
147
|
+
})),
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
breakpoint,
|
|
152
|
+
currentRows,
|
|
153
|
+
device,
|
|
154
|
+
exampleRows,
|
|
155
|
+
isDesktop,
|
|
156
|
+
isMobile,
|
|
157
|
+
screenSize,
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
template: `
|
|
161
|
+
<div class="grid gap-6 lg:grid-cols-[minmax(0,1fr)_minmax(0,1.4fr)]">
|
|
162
|
+
<section class="rounded-lg border border-border-surface-default bg-background-surface-primary p-4 shadow-xs">
|
|
163
|
+
<h2 class="label-14-medium text-content-on-surface-primary">Live viewport</h2>
|
|
164
|
+
<p class="body-14-regular text-content-on-surface-muted mt-1">
|
|
165
|
+
Resize the preview iframe to see screen size and breakpoint data update.
|
|
166
|
+
</p>
|
|
167
|
+
|
|
168
|
+
<dl class="mt-4 grid grid-cols-2 gap-3">
|
|
169
|
+
<div class="rounded-md bg-background-surface-muted p-3">
|
|
170
|
+
<dt class="label-12-medium text-content-on-surface-muted">screenSize</dt>
|
|
171
|
+
<dd class="body-14-medium text-content-on-surface-primary">
|
|
172
|
+
{{ screenSize.width ?? 'unknown' }} x {{ screenSize.height ?? 'unknown' }}
|
|
173
|
+
</dd>
|
|
174
|
+
</div>
|
|
175
|
+
<div class="rounded-md bg-background-surface-muted p-3">
|
|
176
|
+
<dt class="label-12-medium text-content-on-surface-muted">breakpoint</dt>
|
|
177
|
+
<dd class="body-14-medium text-content-on-surface-primary">{{ breakpoint }}</dd>
|
|
178
|
+
</div>
|
|
179
|
+
<div class="rounded-md bg-background-surface-muted p-3">
|
|
180
|
+
<dt class="label-12-medium text-content-on-surface-muted">isMobile</dt>
|
|
181
|
+
<dd class="body-14-medium text-content-on-surface-primary">{{ isMobile }}</dd>
|
|
182
|
+
</div>
|
|
183
|
+
<div class="rounded-md bg-background-surface-muted p-3">
|
|
184
|
+
<dt class="label-12-medium text-content-on-surface-muted">isDesktop</dt>
|
|
185
|
+
<dd class="body-14-medium text-content-on-surface-primary">{{ isDesktop }}</dd>
|
|
186
|
+
</div>
|
|
187
|
+
</dl>
|
|
188
|
+
|
|
189
|
+
<div class="mt-4 overflow-hidden rounded-md border border-border-surface-default">
|
|
190
|
+
<div
|
|
191
|
+
v-for="[label, value] in currentRows"
|
|
192
|
+
:key="label"
|
|
193
|
+
class="grid grid-cols-[8rem_1fr] border-b border-border-surface-default last:border-b-0"
|
|
194
|
+
>
|
|
195
|
+
<span class="label-12-medium bg-background-surface-muted px-3 py-2 text-content-on-surface-muted">
|
|
196
|
+
{{ label }}
|
|
197
|
+
</span>
|
|
198
|
+
<span class="body-14-regular px-3 py-2 text-content-on-surface-primary">
|
|
199
|
+
{{ value }}
|
|
200
|
+
</span>
|
|
201
|
+
</div>
|
|
202
|
+
</div>
|
|
203
|
+
</section>
|
|
204
|
+
|
|
205
|
+
<section class="rounded-lg border border-border-surface-default bg-background-surface-primary p-4 shadow-xs">
|
|
206
|
+
<h2 class="label-14-medium text-content-on-surface-primary">User-agent examples</h2>
|
|
207
|
+
<p class="body-14-regular text-content-on-surface-muted mt-1">
|
|
208
|
+
Pass <code>userAgent</code> during SSR to derive device data without touching browser APIs.
|
|
209
|
+
</p>
|
|
210
|
+
|
|
211
|
+
<div class="mt-4 grid gap-3 md:grid-cols-2">
|
|
212
|
+
<article
|
|
213
|
+
v-for="example in exampleRows"
|
|
214
|
+
:key="example.label"
|
|
215
|
+
class="rounded-md border border-border-surface-default p-3"
|
|
216
|
+
>
|
|
217
|
+
<h3 class="label-14-medium text-content-on-surface-primary">{{ example.label }}</h3>
|
|
218
|
+
<dl class="mt-3 grid gap-1">
|
|
219
|
+
<div
|
|
220
|
+
v-for="[label, value] in example.rows"
|
|
221
|
+
:key="label"
|
|
222
|
+
class="flex justify-between gap-4"
|
|
223
|
+
>
|
|
224
|
+
<dt class="label-12-medium text-content-on-surface-muted">{{ label }}</dt>
|
|
225
|
+
<dd class="body-14-regular text-content-on-surface-primary">{{ value }}</dd>
|
|
226
|
+
</div>
|
|
227
|
+
</dl>
|
|
228
|
+
</article>
|
|
229
|
+
</div>
|
|
230
|
+
</section>
|
|
231
|
+
</div>
|
|
232
|
+
`,
|
|
233
|
+
}),
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
export const LiveDetector: Story = {
|
|
237
|
+
parameters: {
|
|
238
|
+
docs: {
|
|
239
|
+
source: {
|
|
240
|
+
code: `<script setup lang="ts">
|
|
241
|
+
import {
|
|
242
|
+
UiResizableHandle,
|
|
243
|
+
UiResizablePanel,
|
|
244
|
+
UiResizablePanelGroup,
|
|
245
|
+
useDevice,
|
|
246
|
+
} from '@aleph-alpha/ui-library';
|
|
247
|
+
|
|
248
|
+
// In application code, useDevice() listens to the real browser window.
|
|
249
|
+
const { screenSize, breakpoint, isMobile, isDesktop } = useDevice();
|
|
250
|
+
</script>
|
|
251
|
+
|
|
252
|
+
<template>
|
|
253
|
+
<UiResizablePanelGroup direction="horizontal">
|
|
254
|
+
<UiResizablePanel :default-size="50">
|
|
255
|
+
{{ screenSize.width }}px - {{ breakpoint }}
|
|
256
|
+
</UiResizablePanel>
|
|
257
|
+
<UiResizableHandle with-handle />
|
|
258
|
+
<UiResizablePanel :default-size="50">
|
|
259
|
+
mobile: {{ isMobile }}, desktop: {{ isDesktop }}
|
|
260
|
+
</UiResizablePanel>
|
|
261
|
+
</UiResizablePanelGroup>
|
|
262
|
+
</template>`,
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
render: () => ({
|
|
267
|
+
components: {
|
|
268
|
+
UiAccordion,
|
|
269
|
+
UiAccordionContent,
|
|
270
|
+
UiAccordionItem,
|
|
271
|
+
UiAccordionTrigger,
|
|
272
|
+
UiResizableHandle,
|
|
273
|
+
UiResizablePanel,
|
|
274
|
+
UiResizablePanelGroup,
|
|
275
|
+
},
|
|
276
|
+
setup() {
|
|
277
|
+
const detectorWidth = ref(DETECTOR_TOTAL_WIDTH / 2);
|
|
278
|
+
const detectorPanelSize = ref(50);
|
|
279
|
+
const detectorWindow = createDetectorWindow(
|
|
280
|
+
detectorWidth.value,
|
|
281
|
+
DETECTOR_HEIGHT,
|
|
282
|
+
globalThis.navigator?.userAgent ?? '',
|
|
283
|
+
);
|
|
284
|
+
const { userAgent, device, screenSize, breakpoint, isMobile, isDesktop } =
|
|
285
|
+
useDevice({ window: detectorWindow });
|
|
286
|
+
const detectorRows = computed(() => [
|
|
287
|
+
['breakpoint', breakpoint.value],
|
|
288
|
+
['isMobile', String(isMobile.value)],
|
|
289
|
+
['isDesktop', String(isDesktop.value)],
|
|
290
|
+
]);
|
|
291
|
+
const deviceRows = computed(() => toRows(device.value));
|
|
292
|
+
|
|
293
|
+
function updateDetectorPanel(size: number) {
|
|
294
|
+
detectorPanelSize.value = Math.round(size);
|
|
295
|
+
detectorWidth.value = Math.round((DETECTOR_TOTAL_WIDTH * size) / 100);
|
|
296
|
+
detectorWindow.innerWidth = detectorWidth.value;
|
|
297
|
+
detectorWindow.dispatchEvent(new Event('resize'));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return {
|
|
301
|
+
detectorPanelSize,
|
|
302
|
+
detectorRows,
|
|
303
|
+
detectorWidth,
|
|
304
|
+
deviceRows,
|
|
305
|
+
screenSize,
|
|
306
|
+
updateDetectorPanel,
|
|
307
|
+
userAgent,
|
|
308
|
+
};
|
|
309
|
+
},
|
|
310
|
+
template: `
|
|
311
|
+
<div class="rounded-lg border border-border-surface-default bg-background-surface-primary p-4 shadow-xs">
|
|
312
|
+
<div class="mb-4">
|
|
313
|
+
<h2 class="label-14-medium text-content-on-surface-primary">Live device detector</h2>
|
|
314
|
+
<p class="body-14-regular text-content-on-surface-muted mt-1">
|
|
315
|
+
Drag the resize handle. The left panel acts as a local viewport and dispatches
|
|
316
|
+
resize updates to <code>useDevice()</code>.
|
|
317
|
+
</p>
|
|
318
|
+
</div>
|
|
319
|
+
|
|
320
|
+
<UiResizablePanelGroup
|
|
321
|
+
direction="horizontal"
|
|
322
|
+
class="min-h-96 rounded-lg border border-border-surface-default"
|
|
323
|
+
>
|
|
324
|
+
<UiResizablePanel
|
|
325
|
+
:default-size="50"
|
|
326
|
+
:min-size="25"
|
|
327
|
+
@resize="updateDetectorPanel"
|
|
328
|
+
>
|
|
329
|
+
<div class="flex h-full min-w-0 flex-col gap-4 overflow-auto p-4">
|
|
330
|
+
<div class="rounded-md bg-background-surface-muted p-3">
|
|
331
|
+
<span class="label-12-medium text-content-on-surface-muted">Resizable viewport</span>
|
|
332
|
+
<p class="heading-s text-content-on-surface-primary">
|
|
333
|
+
{{ screenSize.width ?? 'unknown' }}px
|
|
334
|
+
</p>
|
|
335
|
+
<p class="body-14-regular text-content-on-surface-muted">
|
|
336
|
+
Panel size: {{ detectorPanelSize }}%
|
|
337
|
+
</p>
|
|
338
|
+
</div>
|
|
339
|
+
|
|
340
|
+
<UiAccordion
|
|
341
|
+
type="multiple"
|
|
342
|
+
:default-value="['breakpoint', 'device']"
|
|
343
|
+
class="w-full"
|
|
344
|
+
>
|
|
345
|
+
<UiAccordionItem value="breakpoint">
|
|
346
|
+
<UiAccordionTrigger>Breakpoint flags</UiAccordionTrigger>
|
|
347
|
+
<UiAccordionContent>
|
|
348
|
+
<dl class="grid gap-2">
|
|
349
|
+
<div
|
|
350
|
+
v-for="[label, value] in detectorRows"
|
|
351
|
+
:key="label"
|
|
352
|
+
class="flex justify-between gap-4 rounded-md bg-background-surface-muted px-3 py-2"
|
|
353
|
+
>
|
|
354
|
+
<dt class="label-12-medium text-content-on-surface-muted">{{ label }}</dt>
|
|
355
|
+
<dd class="body-14-regular text-content-on-surface-primary">{{ value }}</dd>
|
|
356
|
+
</div>
|
|
357
|
+
</dl>
|
|
358
|
+
</UiAccordionContent>
|
|
359
|
+
</UiAccordionItem>
|
|
360
|
+
|
|
361
|
+
<UiAccordionItem value="device">
|
|
362
|
+
<UiAccordionTrigger>User-agent device data</UiAccordionTrigger>
|
|
363
|
+
<UiAccordionContent>
|
|
364
|
+
<dl class="grid gap-2">
|
|
365
|
+
<div
|
|
366
|
+
v-for="[label, value] in deviceRows"
|
|
367
|
+
:key="label"
|
|
368
|
+
class="flex justify-between gap-4 rounded-md bg-background-surface-muted px-3 py-2"
|
|
369
|
+
>
|
|
370
|
+
<dt class="label-12-medium text-content-on-surface-muted">{{ label }}</dt>
|
|
371
|
+
<dd class="body-14-regular text-content-on-surface-primary">{{ value }}</dd>
|
|
372
|
+
</div>
|
|
373
|
+
</dl>
|
|
374
|
+
</UiAccordionContent>
|
|
375
|
+
</UiAccordionItem>
|
|
376
|
+
|
|
377
|
+
<UiAccordionItem value="user-agent">
|
|
378
|
+
<UiAccordionTrigger>Raw user agent</UiAccordionTrigger>
|
|
379
|
+
<UiAccordionContent>
|
|
380
|
+
<p class="body-14-regular break-all rounded-md bg-background-surface-muted p-3 text-content-on-surface-primary">
|
|
381
|
+
{{ userAgent || 'unknown' }}
|
|
382
|
+
</p>
|
|
383
|
+
</UiAccordionContent>
|
|
384
|
+
</UiAccordionItem>
|
|
385
|
+
</UiAccordion>
|
|
386
|
+
</div>
|
|
387
|
+
</UiResizablePanel>
|
|
388
|
+
|
|
389
|
+
<UiResizableHandle with-handle />
|
|
390
|
+
|
|
391
|
+
<UiResizablePanel :default-size="50" :min-size="25">
|
|
392
|
+
<div class="flex h-full flex-col justify-center gap-3 p-4">
|
|
393
|
+
<h3 class="label-14-medium text-content-on-surface-primary">Resize detector</h3>
|
|
394
|
+
<p class="body-14-regular text-content-on-surface-muted">
|
|
395
|
+
This panel is only here to give the detector panel room to grow and shrink.
|
|
396
|
+
Crossing 640px flips the detector between mobile and desktop.
|
|
397
|
+
</p>
|
|
398
|
+
<p class="body-14-regular rounded-md bg-background-surface-muted p-3 text-content-on-surface-primary">
|
|
399
|
+
Current detector width: {{ detectorWidth }}px
|
|
400
|
+
</p>
|
|
401
|
+
</div>
|
|
402
|
+
</UiResizablePanel>
|
|
403
|
+
</UiResizablePanelGroup>
|
|
404
|
+
</div>
|
|
405
|
+
`,
|
|
406
|
+
}),
|
|
407
|
+
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { render } from '@testing-library/vue';
|
|
2
|
+
import { afterEach, describe, expect, test, vi } from 'vitest';
|
|
3
|
+
import { defineComponent, nextTick } from 'vue';
|
|
4
|
+
import { getDeviceData, useDevice, type UseDeviceOptions } from './useDevice';
|
|
5
|
+
|
|
6
|
+
const IPHONE_USER_AGENT =
|
|
7
|
+
'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1';
|
|
8
|
+
const IPAD_USER_AGENT =
|
|
9
|
+
'Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1';
|
|
10
|
+
const DESKTOP_USER_AGENT =
|
|
11
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
|
|
12
|
+
let testOptions: UseDeviceOptions | undefined;
|
|
13
|
+
let device!: ReturnType<typeof useDevice>;
|
|
14
|
+
|
|
15
|
+
const TestComponent = defineComponent({
|
|
16
|
+
setup() {
|
|
17
|
+
device = useDevice(testOptions);
|
|
18
|
+
return () => null;
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
function setWindowSize(width: number, height: number) {
|
|
23
|
+
Object.defineProperty(window, 'innerWidth', {
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: width,
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(window, 'innerHeight', {
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: height,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
testOptions = undefined;
|
|
37
|
+
vi.unstubAllGlobals();
|
|
38
|
+
vi.restoreAllMocks();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('getDeviceData', () => {
|
|
42
|
+
test('detects mobile user agents', () => {
|
|
43
|
+
expect(getDeviceData(IPHONE_USER_AGENT)).toMatchObject({
|
|
44
|
+
type: 'mobile',
|
|
45
|
+
os: 'ios',
|
|
46
|
+
browser: 'safari',
|
|
47
|
+
isMobile: true,
|
|
48
|
+
isDesktop: false,
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('treats tablet user agents as mobile until tablet breakpoints exist', () => {
|
|
53
|
+
expect(getDeviceData(IPAD_USER_AGENT)).toMatchObject({
|
|
54
|
+
type: 'mobile',
|
|
55
|
+
os: 'ios',
|
|
56
|
+
isMobile: true,
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('detects desktop user agents', () => {
|
|
61
|
+
expect(getDeviceData(DESKTOP_USER_AGENT)).toMatchObject({
|
|
62
|
+
type: 'desktop',
|
|
63
|
+
os: 'macos',
|
|
64
|
+
browser: 'chrome',
|
|
65
|
+
isDesktop: true,
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('detects bot user agents', () => {
|
|
70
|
+
expect(getDeviceData('Googlebot/2.1')).toMatchObject({
|
|
71
|
+
type: 'bot',
|
|
72
|
+
isBot: true,
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('useDevice', () => {
|
|
78
|
+
test('uses user agent data without a browser window for SSR', () => {
|
|
79
|
+
vi.stubGlobal('window', undefined);
|
|
80
|
+
|
|
81
|
+
const device = useDevice({ userAgent: IPHONE_USER_AGENT });
|
|
82
|
+
|
|
83
|
+
expect(device.screenSize.value).toEqual({});
|
|
84
|
+
expect(device.breakpoint.value).toBe('mobile');
|
|
85
|
+
expect(device.isMobile.value).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('measures on mount and updates breakpoint on resize', async () => {
|
|
89
|
+
setWindowSize(1200, 800);
|
|
90
|
+
|
|
91
|
+
render(TestComponent);
|
|
92
|
+
await nextTick();
|
|
93
|
+
|
|
94
|
+
expect(device.screenSize.value).toEqual({ width: 1200, height: 800 });
|
|
95
|
+
expect(device.breakpoint.value).toBe('desktop');
|
|
96
|
+
expect(device.isDesktop.value).toBe(true);
|
|
97
|
+
|
|
98
|
+
setWindowSize(500, 700);
|
|
99
|
+
window.dispatchEvent(new Event('resize'));
|
|
100
|
+
await nextTick();
|
|
101
|
+
|
|
102
|
+
expect(device.screenSize.value).toEqual({ width: 500, height: 700 });
|
|
103
|
+
expect(device.breakpoint.value).toBe('mobile');
|
|
104
|
+
expect(device.isMobile.value).toBe(true);
|
|
105
|
+
|
|
106
|
+
setWindowSize(720, 700);
|
|
107
|
+
window.dispatchEvent(new Event('resize'));
|
|
108
|
+
await nextTick();
|
|
109
|
+
|
|
110
|
+
expect(device.screenSize.value).toEqual({ width: 720, height: 700 });
|
|
111
|
+
expect(device.breakpoint.value).toBe('desktop');
|
|
112
|
+
expect(device.isDesktop.value).toBe(true);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('can opt out of resize listeners', async () => {
|
|
116
|
+
setWindowSize(1200, 800);
|
|
117
|
+
testOptions = { listenToResize: false };
|
|
118
|
+
|
|
119
|
+
render(TestComponent);
|
|
120
|
+
await nextTick();
|
|
121
|
+
|
|
122
|
+
setWindowSize(500, 700);
|
|
123
|
+
window.dispatchEvent(new Event('resize'));
|
|
124
|
+
await nextTick();
|
|
125
|
+
|
|
126
|
+
expect(device.screenSize.value).toEqual({ width: 1200, height: 800 });
|
|
127
|
+
expect(device.breakpoint.value).toBe('desktop');
|
|
128
|
+
});
|
|
129
|
+
});
|