Skip to content

Commit 0b31ddb

Browse files
committed
feat(DropdownMenu): on a phone the menu sits over its trigger, as UIMenu's does
Under the touch modes on a compact touch screen, the popover covers its trigger as UIMenu does (iOS 26.2): centered on it, top-aligned with it, or bottom-aligned when the trigger's center is in the lower half of the viewport, kept 8 px off the viewport's sides (or the safe area), and no taller than the room left. Larger screens, a mouse and presentation="popover" keep the menu below its trigger. The room is measured in the layout viewport, the space a fixed box's insets and getBoundingClientRect are in.
1 parent 4728dee commit 0b31ddb

8 files changed

Lines changed: 530 additions & 22 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[feature] On a compact touch screen (a phone, iOS or Android), the popover DropdownMenu and MoreMenu sit where UIMenu does: over their trigger, centered on it and top-aligned with it, or bottom-aligned when the trigger is in the lower half of the viewport, 8 px from the viewport's sides (or the safe area), and no taller than the room left, so they stay on screen and scroll. The finger that opened the menu rests on its first row and picks only once it moves, as on iOS. Larger screens, a mouse, and `presentation="popover"` keep the menu below its trigger.
6+
7+
@vjeux

‎packages/core/src/DropdownMenu/DropdownMenu.doc.mjs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,14 @@ export const docs = {
242242
name: 'placement',
243243
type: "'above' | 'below' | 'start' | 'end'",
244244
description:
245-
"Popover placement relative to the trigger. Ignored by the bottom-sheet presentation. Logical: start/end resolve against the menu's own inherited direction (RTL mirrors).",
245+
"Popover placement relative to the trigger. Ignored by the bottom-sheet presentation, and on a compact touch screen under the touch modes (unless presentation is 'popover'), where the menu sits over its trigger as UIMenu does. Logical: start/end resolve against the menu's own inherited direction (RTL mirrors).",
246246
default: "'below'",
247247
},
248248
{
249249
name: 'alignment',
250250
type: "'start' | 'center' | 'end'",
251251
description:
252-
"Popover alignment along the placement axis. Ignored by the bottom-sheet presentation. Logical: start/end follow the menu's own inherited direction (RTL mirrors).",
252+
"Popover alignment along the placement axis. Ignored by the bottom-sheet presentation, and on a compact touch screen under the touch modes (unless presentation is 'popover'), where the menu is centered on its trigger as UIMenu is. Logical: start/end follow the menu's own inherited direction (RTL mirrors).",
253253
default: "'start'",
254254
},
255255
{
@@ -276,7 +276,7 @@ export const docs = {
276276
usage: {
277277
anatomy,
278278
description:
279-
'A dropdown menu that displays a list of actionable items in a popup triggered by a button. Use to present action options as a next step in a process, or to offer contextual actions without cluttering the interface. Like the menus of macOS and iOS, the row under the pointer when it is released is the row that acts, the highlight follows a held mouse or finger, a mouse opens it on press and can drag straight into it, and a finger held on the trigger opens it with the finger down. Under a finger it behaves like UIMenu on iOS: a finger resting on the trigger for 150 ms opens the menu, the finger that opened it picks a row only once it has moved 10 px (so the row that appears under it is never picked by accident), the menu grows out of its trigger and collapses back into it on UIKit\u2019s springs, and a pan that starts outside it dismisses it as the page scrolls.',
279+
'A dropdown menu that displays a list of actionable items in a popup triggered by a button. Use to present action options as a next step in a process, or to offer contextual actions without cluttering the interface. Like the menus of macOS and iOS, the row under the pointer when it is released is the row that acts, the highlight follows a held mouse or finger, a mouse opens it on press and can drag straight into it, and a finger held on the trigger opens it with the finger down. Under a finger it behaves like UIMenu on iOS: a finger resting on the trigger for 150 ms opens the menu, the finger that opened it picks a row only once it has moved 10 px (so the row that appears under it is never picked by accident), the menu grows out of its trigger and collapses back into it on UIKit\u2019s springs, and a pan that starts outside it dismisses it as the page scrolls. On a phone the popover menu sits over its trigger as UIMenu does: centered on it, top-aligned with it (bottom-aligned when the trigger is in the lower half of the viewport), 8 px from the viewport\u2019s sides.',
280280
bestPractices: [
281281
{
282282
guidance: true,

‎packages/core/src/DropdownMenu/DropdownMenu.test.tsx‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,6 +2876,94 @@ describe('DropdownMenu touch presentation (UIMenu under a finger)', () => {
28762876
expect(trigger).toHaveAttribute('aria-expanded', 'false');
28772877
});
28782878

2879+
describe('on a phone (compact and touch): over its trigger, as UIMenu', () => {
2880+
beforeEach(() => {
2881+
vi.stubGlobal(
2882+
'matchMedia',
2883+
vi.fn().mockImplementation((query: string) => ({
2884+
// Coarse primary pointer, and the compact viewport.
2885+
matches: query === '(pointer: coarse)' || query === COMPACT_QUERY,
2886+
media: query,
2887+
onchange: null,
2888+
addEventListener: vi.fn(),
2889+
removeEventListener: vi.fn(),
2890+
addListener: vi.fn(),
2891+
removeListener: vi.fn(),
2892+
dispatchEvent: vi.fn(),
2893+
})),
2894+
);
2895+
});
2896+
2897+
it('is centered on its trigger, 8 px (or the safe area) off the viewport\u2019s sides, and top-aligned with a trigger in the upper half', async () => {
2898+
const user = userEvent.setup();
2899+
const trigger = renderMenu();
2900+
await user.click(trigger);
2901+
const {style} = popoverOf();
2902+
expect(style.getPropertyValue('justify-self')).toBe('anchor-center');
2903+
// UIKit's 8 pt margin, or the safe area where larger, plus the
2904+
// LayerProvider inset for that edge: the layer runtime's gutter terms.
2905+
expect(style.getPropertyValue('left')).toBe(
2906+
'calc(max(8px, env(safe-area-inset-left, 0px)) + var(--astryx-layer-inset-inline-start, 0px))',
2907+
);
2908+
expect(style.getPropertyValue('right')).toBe(
2909+
'calc(max(8px, env(safe-area-inset-right, 0px)) + var(--astryx-layer-inset-inline-end, 0px))',
2910+
);
2911+
// jsdom drops anchor() values; the bottom edge is released and the top
2912+
// edge is the one pinned (its value is checked in a real browser).
2913+
expect(style.getPropertyValue('bottom')).toBe('auto');
2914+
expect(style.getPropertyValue('top')).not.toBe('auto');
2915+
// Not the layer's below-the-trigger placement.
2916+
expect(style.getPropertyValue('position-area')).toBe('');
2917+
});
2918+
2919+
it('is bottom-aligned with a trigger whose center is in the lower half', async () => {
2920+
const lowRect = HTMLElement.prototype.getBoundingClientRect;
2921+
HTMLElement.prototype.getBoundingClientRect = function (
2922+
this: HTMLElement,
2923+
) {
2924+
const rect = lowRect.call(this);
2925+
if (this.hasAttribute('popover')) {
2926+
return rect;
2927+
}
2928+
const top = window.innerHeight - 100;
2929+
return {...rect, top, y: top, bottom: top + rect.height};
2930+
};
2931+
const user = userEvent.setup();
2932+
const trigger = renderMenu();
2933+
await user.click(trigger);
2934+
const {style} = popoverOf();
2935+
expect(style.getPropertyValue('top')).toBe('auto');
2936+
expect(style.getPropertyValue('bottom')).not.toBe('auto');
2937+
});
2938+
2939+
it('a mouse on a compact screen keeps the layer\u2019s placement below its trigger', async () => {
2940+
asMouse();
2941+
const user = userEvent.setup();
2942+
const trigger = renderMenu();
2943+
await user.click(trigger);
2944+
const {style} = popoverOf();
2945+
expect(style.getPropertyValue('justify-self')).toBe('');
2946+
expect(style.getPropertyValue('left')).toBe('');
2947+
expect(style.getPropertyValue('right')).toBe('');
2948+
});
2949+
2950+
it('presentation="popover" on a compact touch screen keeps the layer\u2019s placement below its trigger', async () => {
2951+
const user = userEvent.setup();
2952+
render(
2953+
<DropdownMenu button={{label: 'Pinned'}} presentation="popover">
2954+
<DropdownMenuItem label="Edit" onClick={() => {}} />
2955+
<DropdownMenuItem label="Delete" onClick={() => {}} />
2956+
</DropdownMenu>,
2957+
);
2958+
expect(window.matchMedia(TOUCH_QUERY).matches).toBe(true);
2959+
await user.click(screen.getByRole('button', {name: /Pinned/}));
2960+
const {style} = popoverOf();
2961+
expect(style.getPropertyValue('justify-self')).toBe('');
2962+
expect(style.getPropertyValue('top')).toBe('');
2963+
expect(style.getPropertyValue('bottom')).toBe('');
2964+
});
2965+
});
2966+
28792967
it('a pan that starts inside the menu, or a mouse, does not dismiss it', async () => {
28802968
const user = userEvent.setup();
28812969
const trigger = renderMenu();

‎packages/core/src/DropdownMenu/DropdownMenu.tsx‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
* touch model on every touch device) the popover opens and closes the way
2727
* UIMenu does: it grows out of its trigger on UIKit's open spring and
2828
* collapses back into it on the close spring (menuSurfaceMotion.ts), and a
29-
* pan that begins outside closes it. Under a mouse it keeps the layer's own
30-
* entrance and the theme's motion tokens (D-01).
29+
* pan that begins outside closes it. On a compact touch screen it also sits
30+
* where UIMenu does: over its trigger, centered on it, top-aligned
31+
* (bottom-aligned when the trigger is in the lower half of the viewport),
32+
* 8 px from the viewport's sides. Under a mouse it keeps the layer's own
33+
* entrance and placement and the theme's motion tokens (D-01).
3134
*
3235
* Initial focus on open follows the input modality: a keyboard open
3336
* (Enter / Space / ArrowDown on the trigger) focuses the first enabled item
@@ -89,8 +92,11 @@ import {resolveMenuWidth} from './menuWidth';
8992
import {layerViewportInset} from '../Layer/layerViewportInset.stylex';
9093
import type {AdaptivePresentation} from '../hooks/useAdaptivePresentation';
9194
import {usePresentation} from '../utils/presentation';
95+
import {useTouchPresentation} from '../utils/touchPresentation';
9296
import {
97+
COVER_TRIGGER_STYLE,
9398
createMenuSurfaceMotion,
99+
placeOverTrigger,
94100
type MenuSurfaceMotion,
95101
} from './DropdownMenuTouch';
96102
// Loaded on demand: only the bottom-sheet presentation renders the sheet.
@@ -436,15 +442,19 @@ interface DropdownMenuBaseProps extends BaseProps {
436442
hasChevron?: boolean;
437443
/**
438444
* Popover position relative to the trigger. Ignored by bottom-sheet
439-
* presentation.
445+
* presentation, and on a compact touch screen under the touch modes (unless
446+
* `presentation` is `'popover'`), where the menu sits over its trigger as
447+
* UIMenu does.
440448
* Uses the same placement values as other Astryx layer-based components.
441449
* @default 'below'
442450
*/
443451
placement?: LayerPlacement;
444452

445453
/**
446454
* Popover alignment along the placement axis. Ignored by bottom-sheet
447-
* presentation.
455+
* presentation, and on a compact touch screen under the touch modes (unless
456+
* `presentation` is `'popover'`), where the menu is centered on its trigger
457+
* as UIMenu is.
448458
* Uses the same alignment values as other Astryx layer-based components.
449459
* @default 'start'
450460
*/
@@ -985,6 +995,15 @@ function DropdownMenuPopover({
985995
// motion and the pan-outside dismissal.
986996
const isTouchPresentationRef = useRef(false);
987997
const isUikitMotionRef = useRef(false);
998+
// On a compact touch screen, under the touch modes, the popover covers its
999+
// trigger as UIMenu does (iOS 26.2): centered on it, kept off the
1000+
// viewport's sides by CSS, with its vertical edge chosen as it opens
1001+
// (placeOverTrigger). Elsewhere, and with a pinned 'popover', it opens
1002+
// below it.
1003+
const {isCompactTouch} = useTouchPresentation();
1004+
const coversTrigger = touchModes === true && isCompactTouch;
1005+
const coversTriggerRef = useRef(coversTrigger);
1006+
coversTriggerRef.current = coversTrigger;
9881007
const getSurfaceMotion = useCallback(
9891008
(element: HTMLElement): MenuSurfaceMotion => {
9901009
const current = surfaceMotionRef.current;
@@ -1136,6 +1155,7 @@ function DropdownMenuPopover({
11361155
isUikitMotionRef.current = isUikitMotion;
11371156
const trigger = buttonRef.current;
11381157
if (surface != null && trigger != null) {
1158+
placeOverTrigger(surface, trigger, coversTriggerRef.current);
11391159
const motion = getSurfaceMotion(surface);
11401160
if (isUikitMotion) {
11411161
motion.enter(() => {
@@ -1888,18 +1908,29 @@ function DropdownMenuPopover({
18881908
</MenuRegionsContext>
18891909
</div>,
18901910
),
1891-
{
1892-
placement,
1893-
alignment,
1894-
offset: spacingVars['--spacing-1'],
1895-
xstyle: [
1896-
styles.popoverViewport,
1897-
resolvedMaxHeight != null &&
1898-
dynamicStyles.popoverViewportMaxBlockSize(resolvedMaxHeight),
1899-
popoverXstyle,
1900-
layerAnimations[placement],
1901-
],
1902-
},
1911+
coversTrigger
1912+
? {
1913+
positioning: 'custom',
1914+
style: COVER_TRIGGER_STYLE,
1915+
xstyle: [
1916+
styles.popoverViewport,
1917+
resolvedMaxHeight != null &&
1918+
dynamicStyles.popoverViewportMaxBlockSize(resolvedMaxHeight),
1919+
popoverXstyle,
1920+
],
1921+
}
1922+
: {
1923+
placement,
1924+
alignment,
1925+
offset: spacingVars['--spacing-1'],
1926+
xstyle: [
1927+
styles.popoverViewport,
1928+
resolvedMaxHeight != null &&
1929+
dynamicStyles.popoverViewportMaxBlockSize(resolvedMaxHeight),
1930+
popoverXstyle,
1931+
layerAnimations[placement],
1932+
],
1933+
},
19031934
)}
19041935
</>
19051936
);

‎packages/core/src/DropdownMenu/DropdownMenuTouch.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
/**
66
* @file DropdownMenuTouch.ts
77
* @input menuSurfaceMotion.ts (UIMenu's grow and collapse on the animator),
8-
* the menu and springs tables' register functions
9-
* @output DropdownMenu's touch layer: createMenuSurfaceMotion and its type
8+
* menuTouchPlacement.ts (the menu over its trigger on a compact touch
9+
* screen), and the menu and springs tables' register functions
10+
* @output DropdownMenu's touch layer: createMenuSurfaceMotion and its type,
11+
* COVER_TRIGGER_STYLE and placeOverTrigger
1012
* @position The one module DropdownMenu imports touch-only code through, and
1113
* its only import site is DropdownMenu.tsx. The mouse paths stay outside
1214
* it, so a desktop build can swap it for a no-op (AST-080 FR8).
@@ -38,3 +40,5 @@ export function createMenuSurfaceMotion(
3840
registerMenuTable();
3941
return createSurfaceMotion(...args);
4042
}
43+
44+
export {COVER_TRIGGER_STYLE, placeOverTrigger} from './menuTouchPlacement';

0 commit comments

Comments
 (0)