@aleph-alpha/ui-library 7.0.0 → 7.0.1
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/UiDropdownMenu/useMobileSubmenuPlacement.d.ts +1 -1
- package/dist/system/lib.js +1159 -1157
- package/package.json +1 -1
- package/src/components/UiDropdownMenu/UiDropdownMenu.stories.ts +206 -0
- package/src/components/UiDropdownMenu/__tests__/UiDropdownMenu.test.ts +134 -0
- package/src/components/UiDropdownMenu/useMobileSubmenuPlacement.ts +23 -2
package/package.json
CHANGED
|
@@ -2715,3 +2715,209 @@ export const MobileSubmenu: Story = {
|
|
|
2715
2715
|
);
|
|
2716
2716
|
},
|
|
2717
2717
|
};
|
|
2718
|
+
|
|
2719
|
+
/**
|
|
2720
|
+
* Makes the menu placement see a window `height` pixels tall, in the same
|
|
2721
|
+
* pixels as `getBoundingClientRect`. The test-runner opens mobile stories in a
|
|
2722
|
+
* 320 × 900 viewport, where a five-row submenu always fits beside its row, so
|
|
2723
|
+
* the short window has to be reported rather than resized.
|
|
2724
|
+
* @param height The window height to report.
|
|
2725
|
+
* @returns Restores the real window height.
|
|
2726
|
+
*/
|
|
2727
|
+
function pretendWindowHeight(height: number) {
|
|
2728
|
+
const own = Object.getOwnPropertyDescriptor(window, 'innerHeight');
|
|
2729
|
+
Object.defineProperty(window, 'innerHeight', {
|
|
2730
|
+
configurable: true,
|
|
2731
|
+
get: () => height,
|
|
2732
|
+
});
|
|
2733
|
+
return () => {
|
|
2734
|
+
if (own) {
|
|
2735
|
+
Object.defineProperty(window, 'innerHeight', own);
|
|
2736
|
+
} else {
|
|
2737
|
+
Reflect.deleteProperty(window, 'innerHeight');
|
|
2738
|
+
}
|
|
2739
|
+
};
|
|
2740
|
+
}
|
|
2741
|
+
|
|
2742
|
+
/** Resolves after two frames, once scroll events and placement have run. */
|
|
2743
|
+
function settle() {
|
|
2744
|
+
return new Promise((resolve) => {
|
|
2745
|
+
requestAnimationFrame(() => requestAnimationFrame(resolve));
|
|
2746
|
+
});
|
|
2747
|
+
}
|
|
2748
|
+
|
|
2749
|
+
/**
|
|
2750
|
+
* Waits until `row` sits fully inside the visible part of `scroll`.
|
|
2751
|
+
* @param row A menu row.
|
|
2752
|
+
* @param scroll The scroll box that holds it.
|
|
2753
|
+
*/
|
|
2754
|
+
async function expectRowInList(row: HTMLElement, scroll: HTMLElement) {
|
|
2755
|
+
await waitFor(async () => {
|
|
2756
|
+
const rowRect = row.getBoundingClientRect();
|
|
2757
|
+
const listRect = scroll.getBoundingClientRect();
|
|
2758
|
+
await expect(rowRect.top).toBeGreaterThanOrEqual(listRect.top - 1);
|
|
2759
|
+
await expect(rowRect.bottom).toBeLessThanOrEqual(listRect.bottom + 1);
|
|
2760
|
+
});
|
|
2761
|
+
}
|
|
2762
|
+
|
|
2763
|
+
/**
|
|
2764
|
+
* A capped submenu on a short phone screen, such as 400% zoom: neither side of
|
|
2765
|
+
* its row can hold five rows, so its list is cut to the room left and every
|
|
2766
|
+
* row stays reachable by scrolling, by keyboard, and after the submenu is
|
|
2767
|
+
* placed again. Hidden test-only story.
|
|
2768
|
+
*/
|
|
2769
|
+
export const MobileCappedSubmenu: Story = {
|
|
2770
|
+
name: 'test - Mobile capped submenu',
|
|
2771
|
+
tags: ['!dev', '!autodocs'],
|
|
2772
|
+
globals: { mediaBreakpoint: 'reflow' },
|
|
2773
|
+
parameters: {
|
|
2774
|
+
// The play moves focus into the submenu, and the reflow Chromatic mode
|
|
2775
|
+
// already snapshots the mobile layout.
|
|
2776
|
+
chromatic: { disableSnapshot: true },
|
|
2777
|
+
},
|
|
2778
|
+
render: () => ({
|
|
2779
|
+
components: {
|
|
2780
|
+
UiDropdownMenu,
|
|
2781
|
+
UiDropdownMenuTrigger,
|
|
2782
|
+
UiDropdownMenuContent,
|
|
2783
|
+
UiDropdownMenuItem,
|
|
2784
|
+
UiDropdownMenuSub,
|
|
2785
|
+
UiDropdownMenuSubTrigger,
|
|
2786
|
+
UiDropdownMenuSubContent,
|
|
2787
|
+
UiButton,
|
|
2788
|
+
},
|
|
2789
|
+
setup() {
|
|
2790
|
+
const folders = Array.from({ length: 12 }, (_, i) => `Folder ${i + 1}`);
|
|
2791
|
+
return { folders };
|
|
2792
|
+
},
|
|
2793
|
+
template: `<div class="flex min-h-100 items-start p-6">
|
|
2794
|
+
<UiDropdownMenu :modal="false">
|
|
2795
|
+
<UiDropdownMenuTrigger as-child>
|
|
2796
|
+
<UiButton variant="outline">Open Menu</UiButton>
|
|
2797
|
+
</UiDropdownMenuTrigger>
|
|
2798
|
+
<UiDropdownMenuContent class="w-56" align="start">
|
|
2799
|
+
<UiDropdownMenuSub>
|
|
2800
|
+
<UiDropdownMenuSubTrigger>Move to</UiDropdownMenuSubTrigger>
|
|
2801
|
+
<UiDropdownMenuSubContent>
|
|
2802
|
+
<UiDropdownMenuItem v-for="folder in folders" :key="folder">
|
|
2803
|
+
{{ folder }}
|
|
2804
|
+
</UiDropdownMenuItem>
|
|
2805
|
+
</UiDropdownMenuSubContent>
|
|
2806
|
+
</UiDropdownMenuSub>
|
|
2807
|
+
<UiDropdownMenuItem>Rename</UiDropdownMenuItem>
|
|
2808
|
+
<UiDropdownMenuItem>Delete</UiDropdownMenuItem>
|
|
2809
|
+
</UiDropdownMenuContent>
|
|
2810
|
+
</UiDropdownMenu>
|
|
2811
|
+
</div>`,
|
|
2812
|
+
}),
|
|
2813
|
+
play: async ({ canvasElement, step }) => {
|
|
2814
|
+
// Scenario (UiDropdownMenu): on a short phone screen a keyboard user opens
|
|
2815
|
+
// "Move to", gets a submenu capped to the room below its row, scrolls it
|
|
2816
|
+
// to "Folder 12", jumps between its ends with Home and End, and still
|
|
2817
|
+
// reaches the last folder after the submenu is placed again.
|
|
2818
|
+
const canvas = within(canvasElement);
|
|
2819
|
+
const body = within(document.body);
|
|
2820
|
+
let restoreHeight: (() => void) | undefined;
|
|
2821
|
+
|
|
2822
|
+
try {
|
|
2823
|
+
await step(
|
|
2824
|
+
'mobile: in a short window the submenu caps its list to the room left (WCAG 1.4.10 Reflow)',
|
|
2825
|
+
async () => {
|
|
2826
|
+
await userEvent.click(
|
|
2827
|
+
canvas.getByRole('button', { name: 'Open Menu' }),
|
|
2828
|
+
);
|
|
2829
|
+
await expectOpenMenu(body);
|
|
2830
|
+
const row = body.getByRole('menuitem', { name: 'Move to' });
|
|
2831
|
+
// 120px under the row and less above it: neither side holds five rows.
|
|
2832
|
+
const height = Math.round(row.getBoundingClientRect().bottom + 120);
|
|
2833
|
+
restoreHeight = pretendWindowHeight(height);
|
|
2834
|
+
|
|
2835
|
+
row.focus();
|
|
2836
|
+
await userEvent.keyboard('{ArrowRight}');
|
|
2837
|
+
const submenu = await waitFor(() => {
|
|
2838
|
+
const found = body
|
|
2839
|
+
.getAllByRole('menu')
|
|
2840
|
+
.find((menu) => menu.getAttribute('aria-labelledby') === row.id);
|
|
2841
|
+
if (!found) throw new Error('The submenu is not open.');
|
|
2842
|
+
return found;
|
|
2843
|
+
});
|
|
2844
|
+
await waitFor(async () => {
|
|
2845
|
+
await expect(submenu).toHaveAttribute('data-mobile-clamped');
|
|
2846
|
+
const rect = submenu.getBoundingClientRect();
|
|
2847
|
+
await expect(rect.top).toBeGreaterThanOrEqual(0);
|
|
2848
|
+
await expect(rect.bottom).toBeLessThanOrEqual(height + 1);
|
|
2849
|
+
});
|
|
2850
|
+
},
|
|
2851
|
+
);
|
|
2852
|
+
|
|
2853
|
+
await step(
|
|
2854
|
+
'interaction: the capped list scrolls all the way to its last row',
|
|
2855
|
+
async () => {
|
|
2856
|
+
const submenu = body.getByRole('menu', { name: 'Move to' });
|
|
2857
|
+
const { scroll } = getMobileViewport(submenu);
|
|
2858
|
+
await expect(scroll.scrollHeight).toBeGreaterThan(
|
|
2859
|
+
scroll.clientHeight,
|
|
2860
|
+
);
|
|
2861
|
+
scroll.scrollTop = scroll.scrollHeight;
|
|
2862
|
+
await settle();
|
|
2863
|
+
await expectRowInList(
|
|
2864
|
+
body.getByRole('menuitem', { name: 'Folder 12' }),
|
|
2865
|
+
scroll,
|
|
2866
|
+
);
|
|
2867
|
+
},
|
|
2868
|
+
);
|
|
2869
|
+
|
|
2870
|
+
await step(
|
|
2871
|
+
'keyboard: End and Home scroll the focused row into the capped list (WCAG 2.4.11)',
|
|
2872
|
+
async () => {
|
|
2873
|
+
const submenu = body.getByRole('menu', { name: 'Move to' });
|
|
2874
|
+
const { scroll } = getMobileViewport(submenu);
|
|
2875
|
+
// Back at the top, with focus still on "Folder 1" from opening it.
|
|
2876
|
+
scroll.scrollTop = 0;
|
|
2877
|
+
await settle();
|
|
2878
|
+
|
|
2879
|
+
await userEvent.keyboard('{End}');
|
|
2880
|
+
const last = body.getByRole('menuitem', { name: 'Folder 12' });
|
|
2881
|
+
await waitFor(async () => {
|
|
2882
|
+
await expect(last).toHaveFocus();
|
|
2883
|
+
});
|
|
2884
|
+
await settle();
|
|
2885
|
+
await expectRowInList(last, scroll);
|
|
2886
|
+
|
|
2887
|
+
await userEvent.keyboard('{Home}');
|
|
2888
|
+
const first = body.getByRole('menuitem', { name: 'Folder 1' });
|
|
2889
|
+
await waitFor(async () => {
|
|
2890
|
+
await expect(first).toHaveFocus();
|
|
2891
|
+
});
|
|
2892
|
+
await settle();
|
|
2893
|
+
await expectRowInList(first, scroll);
|
|
2894
|
+
},
|
|
2895
|
+
);
|
|
2896
|
+
|
|
2897
|
+
await step(
|
|
2898
|
+
'mobile: placing the submenu again on resize or a parent scroll keeps the last row in view',
|
|
2899
|
+
async () => {
|
|
2900
|
+
const submenu = body.getByRole('menu', { name: 'Move to' });
|
|
2901
|
+
const parent = body
|
|
2902
|
+
.getAllByRole('menu')
|
|
2903
|
+
.find((menu) => menu !== submenu) as HTMLElement;
|
|
2904
|
+
const { scroll } = getMobileViewport(submenu);
|
|
2905
|
+
const last = body.getByRole('menuitem', { name: 'Folder 12' });
|
|
2906
|
+
scroll.scrollTop = scroll.scrollHeight;
|
|
2907
|
+
await settle();
|
|
2908
|
+
await expectRowInList(last, scroll);
|
|
2909
|
+
|
|
2910
|
+
window.dispatchEvent(new Event('resize'));
|
|
2911
|
+
await settle();
|
|
2912
|
+
await expectRowInList(last, scroll);
|
|
2913
|
+
|
|
2914
|
+
getMobileViewport(parent).scroll.dispatchEvent(new Event('scroll'));
|
|
2915
|
+
await settle();
|
|
2916
|
+
await expectRowInList(last, scroll);
|
|
2917
|
+
},
|
|
2918
|
+
);
|
|
2919
|
+
} finally {
|
|
2920
|
+
restoreHeight?.();
|
|
2921
|
+
}
|
|
2922
|
+
},
|
|
2923
|
+
};
|
|
@@ -499,6 +499,140 @@ describe('UiDropdownMenuSub', () => {
|
|
|
499
499
|
restore();
|
|
500
500
|
}
|
|
501
501
|
});
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Gives the submenu's list a browser's scroll range, which jsdom lacks: 480px
|
|
505
|
+
* of rows in a box that is five 36px rows tall, or the capped height while
|
|
506
|
+
* the cap is set. Reading the panel's layout height clamps the scroll
|
|
507
|
+
* position to the range at that moment, as a browser's layout pass does.
|
|
508
|
+
* @param submenu The open submenu panel.
|
|
509
|
+
* @returns The submenu's scroll box.
|
|
510
|
+
*/
|
|
511
|
+
function stubScrollRange(submenu: HTMLElement) {
|
|
512
|
+
const scroll = submenu.querySelector<HTMLElement>(
|
|
513
|
+
'[data-slot="dropdown-menu-scroll"]',
|
|
514
|
+
) as HTMLElement;
|
|
515
|
+
const scrollHeight = 480;
|
|
516
|
+
const clientHeight = () =>
|
|
517
|
+
Number.parseFloat(
|
|
518
|
+
submenu.style.getPropertyValue(
|
|
519
|
+
'--ui-dropdown-submenu-scroll-max-height',
|
|
520
|
+
),
|
|
521
|
+
) || 36 * 5;
|
|
522
|
+
let position = 0;
|
|
523
|
+
const clamp = (value: number) =>
|
|
524
|
+
Math.min(Math.max(value, 0), scrollHeight - clientHeight());
|
|
525
|
+
Object.defineProperty(scroll, 'scrollTop', {
|
|
526
|
+
configurable: true,
|
|
527
|
+
get: () => position,
|
|
528
|
+
set: (value: number) => {
|
|
529
|
+
position = clamp(value);
|
|
530
|
+
},
|
|
531
|
+
});
|
|
532
|
+
Object.defineProperty(submenu, 'offsetHeight', {
|
|
533
|
+
configurable: true,
|
|
534
|
+
get: () => {
|
|
535
|
+
position = clamp(position);
|
|
536
|
+
return 190;
|
|
537
|
+
},
|
|
538
|
+
});
|
|
539
|
+
return scroll;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
test('keeps a capped submenu scrolled to its last row while its list scrolls', async () => {
|
|
543
|
+
const geometry = {
|
|
544
|
+
content: new DOMRect(0, 40, 320, 240),
|
|
545
|
+
subTrigger: new DOMRect(8, 142, 304, 36),
|
|
546
|
+
subContent: new DOMRect(0, 0, 320, 190),
|
|
547
|
+
innerHeight: 320,
|
|
548
|
+
};
|
|
549
|
+
const restore = stubMobileGeometry(geometry);
|
|
550
|
+
|
|
551
|
+
try {
|
|
552
|
+
const { submenu } = await openStackedSubmenu();
|
|
553
|
+
const scroll = stubScrollRange(submenu);
|
|
554
|
+
|
|
555
|
+
// The list is capped at 134px, so its end is at 480 - 134 = 346. With
|
|
556
|
+
// the cap lifted it would be 480 - 180 = 300.
|
|
557
|
+
scroll.scrollTop = 346;
|
|
558
|
+
scroll.dispatchEvent(new Event('scroll'));
|
|
559
|
+
expect(scroll.scrollTop).toBe(346);
|
|
560
|
+
|
|
561
|
+
// Its own scroll leaves the row where it was, so the placement stays.
|
|
562
|
+
geometry.subTrigger = new DOMRect(8, 120, 304, 36);
|
|
563
|
+
scroll.dispatchEvent(new Event('scroll'));
|
|
564
|
+
expect(submenu.style.getPropertyValue('--ui-dropdown-submenu-top')).toBe(
|
|
565
|
+
'0px',
|
|
566
|
+
);
|
|
567
|
+
} finally {
|
|
568
|
+
restore();
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
test('ignores scrolls in panels that do not hold its row', async () => {
|
|
573
|
+
const geometry = {
|
|
574
|
+
content: new DOMRect(0, 40, 320, 240),
|
|
575
|
+
subTrigger: new DOMRect(8, 142, 304, 36),
|
|
576
|
+
subContent: new DOMRect(0, 0, 320, 190),
|
|
577
|
+
innerHeight: 320,
|
|
578
|
+
};
|
|
579
|
+
const restore = stubMobileGeometry(geometry);
|
|
580
|
+
// Another portalled panel, such as a sibling submenu.
|
|
581
|
+
const other = document.createElement('div');
|
|
582
|
+
document.body.append(other);
|
|
583
|
+
|
|
584
|
+
try {
|
|
585
|
+
const { submenu } = await openStackedSubmenu();
|
|
586
|
+
geometry.subTrigger = new DOMRect(8, 120, 304, 36);
|
|
587
|
+
other.dispatchEvent(new Event('scroll'));
|
|
588
|
+
expect(submenu.style.getPropertyValue('--ui-dropdown-submenu-top')).toBe(
|
|
589
|
+
'0px',
|
|
590
|
+
);
|
|
591
|
+
|
|
592
|
+
// The page scrolling does move the row, so the submenu follows it.
|
|
593
|
+
document.dispatchEvent(new Event('scroll'));
|
|
594
|
+
expect(submenu.style.getPropertyValue('--ui-dropdown-submenu-top')).toBe(
|
|
595
|
+
'164px',
|
|
596
|
+
);
|
|
597
|
+
} finally {
|
|
598
|
+
other.remove();
|
|
599
|
+
restore();
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
test('keeps the submenu list where it was when the parent menu scrolls', async () => {
|
|
604
|
+
const geometry = {
|
|
605
|
+
content: new DOMRect(0, 40, 320, 240),
|
|
606
|
+
subTrigger: new DOMRect(8, 142, 304, 36),
|
|
607
|
+
subContent: new DOMRect(0, 0, 320, 190),
|
|
608
|
+
innerHeight: 320,
|
|
609
|
+
};
|
|
610
|
+
const restore = stubMobileGeometry(geometry);
|
|
611
|
+
|
|
612
|
+
try {
|
|
613
|
+
const { submenu } = await openStackedSubmenu();
|
|
614
|
+
const scroll = stubScrollRange(submenu);
|
|
615
|
+
// Past 300, where the list could not be with the cap lifted.
|
|
616
|
+
scroll.scrollTop = 320;
|
|
617
|
+
|
|
618
|
+
// The parent list scrolls the row 10px down: the submenu follows it with
|
|
619
|
+
// 10px more room above, and its list keeps its place.
|
|
620
|
+
geometry.subTrigger = new DOMRect(8, 152, 304, 36);
|
|
621
|
+
const parent = document.querySelector<HTMLElement>(
|
|
622
|
+
'[data-slot="dropdown-menu-content"]',
|
|
623
|
+
) as HTMLElement;
|
|
624
|
+
parent.dispatchEvent(new Event('scroll'));
|
|
625
|
+
|
|
626
|
+
expect(
|
|
627
|
+
submenu.style.getPropertyValue(
|
|
628
|
+
'--ui-dropdown-submenu-scroll-max-height',
|
|
629
|
+
),
|
|
630
|
+
).toBe('144px');
|
|
631
|
+
expect(scroll.scrollTop).toBe(320);
|
|
632
|
+
} finally {
|
|
633
|
+
restore();
|
|
634
|
+
}
|
|
635
|
+
});
|
|
502
636
|
});
|
|
503
637
|
|
|
504
638
|
describe('UiDropdownMenuContent', () => {
|
|
@@ -65,8 +65,9 @@ export function useMobileSubmenuPlacement(
|
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Measures the trigger row and parent panel, and writes the placement.
|
|
68
|
+
* @param event The event that asked for a new placement, if any.
|
|
68
69
|
*/
|
|
69
|
-
function apply() {
|
|
70
|
+
function apply(event?: Event) {
|
|
70
71
|
const content = getContent();
|
|
71
72
|
if (!content?.isConnected) {
|
|
72
73
|
return;
|
|
@@ -80,12 +81,29 @@ export function useMobileSubmenuPlacement(
|
|
|
80
81
|
if (!trigger || !parent) {
|
|
81
82
|
return;
|
|
82
83
|
}
|
|
84
|
+
// Only a scroll around the row moves it. The submenu's own list and the
|
|
85
|
+
// other menu panels, portalled beside it, leave it where it is; placing
|
|
86
|
+
// again for those would also let two capped panels keep re-placing each
|
|
87
|
+
// other.
|
|
88
|
+
if (
|
|
89
|
+
event?.type === 'scroll' &&
|
|
90
|
+
event.target instanceof Node &&
|
|
91
|
+
!event.target.contains(trigger)
|
|
92
|
+
) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
83
95
|
|
|
84
96
|
const triggerRect = trigger.getBoundingClientRect();
|
|
85
97
|
const parentRect = parent.getBoundingClientRect();
|
|
86
98
|
// Measure the panel at its natural height, so a cap written on an earlier
|
|
87
99
|
// pass does not stick once there is room again. Layout height, not the
|
|
88
|
-
// bounding box: the panel is scaled while it animates in.
|
|
100
|
+
// bounding box: the panel is scaled while it animates in. Uncapped, the
|
|
101
|
+
// list is taller and can scroll less far, so the browser pulls its
|
|
102
|
+
// position back; it is put back once the cap is written again.
|
|
103
|
+
const scroll = content.querySelector<HTMLElement>(
|
|
104
|
+
'[data-slot="dropdown-menu-scroll"]',
|
|
105
|
+
);
|
|
106
|
+
const scrollTop = scroll?.scrollTop ?? 0;
|
|
89
107
|
content.style.removeProperty(SCROLL_MAX_HEIGHT_VAR);
|
|
90
108
|
delete content.dataset.mobileClamped;
|
|
91
109
|
const submenuHeight =
|
|
@@ -109,6 +127,9 @@ export function useMobileSubmenuPlacement(
|
|
|
109
127
|
);
|
|
110
128
|
content.dataset.mobileClamped = '';
|
|
111
129
|
}
|
|
130
|
+
if (scroll && scroll.scrollTop !== scrollTop) {
|
|
131
|
+
scroll.scrollTop = scrollTop;
|
|
132
|
+
}
|
|
112
133
|
content.style.setProperty(
|
|
113
134
|
'--ui-dropdown-submenu-top',
|
|
114
135
|
`${placement.top}px`,
|