Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
4076bf3
spec(AST-094): touch presentations follow UIKit
vjeux Oct 24, 2026
4dc2aae
feat(core): one touch presentation resolver
vjeux Oct 24, 2026
9d2d720
feat(core): the touch presentation resolver knows the iOS idiom
vjeux Oct 24, 2026
7065daf
fix(core): one touch model on every device; the adaptive change is a …
cixzhang Oct 24, 2026
92ef66c
Merge main into wip/uikit-pr-touch-presentation
cixzhang Oct 24, 2026
398c723
fix(core): the touch resolver reads the theme's compact definition
cixzhang Oct 24, 2026
33ee284
docs(MoreMenu): keep the doc's own formatting; change only the compac…
cixzhang Oct 24, 2026
1264c5b
fix(core): compact follows the active theme's md; the branch drops it…
cixzhang Oct 24, 2026
ec08340
docs(spec): drop this branch's stacked copy of AST-094
cixzhang Oct 24, 2026
ced7766
docs(ContextMenu, MoreMenu): the compact edge is the theme md breakpoint
cixzhang Oct 25, 2026
4d728a7
refactor(core): drop the unused isCompactTouch from the touch present…
vjeux Oct 25, 2026
d34d800
test(core): the adaptive menu's compact edges in Chromium and WebKit
vjeux Oct 25, 2026
c5efd27
Merge main into #8062 (lab typecheck fix aa3b92f320 and current main)
vjeux Oct 25, 2026
b8642ba
test(core): the touch presentation's browser proofs leave for the des…
vjeux Oct 25, 2026
f809dae
test(DropdownMenu): a sub-menu's drill-in follows a theme that moves md
vjeux Oct 25, 2026
4b5e872
refactor(core): readTouchPresentation takes the compact definition it…
vjeux Oct 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/touch-presentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[feat] `presentation="adaptive"` on DropdownMenu, ContextMenu, MoreMenu, Selector and MultiSelector chooses the bottom sheet on a compact touch screen: a coarse primary pointer and a viewport narrower than 744px or shorter than 500px (it was 768px wide and below). An iPad mini in portrait keeps the anchored popover; a phone in landscape now gets the sheet.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note covers the root presentation="adaptive" only. DropdownMenuSubMenu defaults to presentation="adaptive" and drills in from the same COMPACT_TOUCH_PRESENTATION_QUERY, so every default sub-menu changes too: on a phone in landscape it now drills in, and on an iPad mini in portrait it flies out. Please add a sentence for that.


@vjeux
2 changes: 1 addition & 1 deletion apps/storybook/stories/ContextMenu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ export const AdaptivePresentation: Story = {
docs: {
description: {
story:
'Uses the built-in adaptive policy: BottomSheet at 768px and below with a coarse primary pointer, cursor-positioned popover otherwise. Important actions still need a visible MoreMenu or equivalent entry point.',
'Uses the built-in adaptive policy: BottomSheet on a compact touch screen (a coarse primary pointer, and a viewport narrower than 744px or shorter than 500px), cursor-positioned popover otherwise. Important actions still need a visible MoreMenu or equivalent entry point.',
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion apps/storybook/stories/MoreMenu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export const AdaptivePresentation: Story = {
docs: {
description: {
story:
'Uses the built-in adaptive policy: BottomSheet at 768px and below with a coarse primary pointer, anchored popover otherwise. Use device emulation to exercise the touch branch.',
'Uses the built-in adaptive policy: BottomSheet on a compact touch screen (a coarse primary pointer, and a viewport narrower than 744px or shorter than 500px), anchored popover otherwise. Use device emulation to exercise the touch branch.',
},
},
},
Expand Down
293 changes: 293 additions & 0 deletions docs/specs/AST-094-touch-presentations/spec.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/core/src/ContextMenu/ContextMenu.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const docs = {
name: 'presentation',
type: "'popover' | 'bottom-sheet' | 'adaptive'",
description:
'Presentation policy. `popover` opens at the pointer position, `bottom-sheet` always uses an action sheet, and `adaptive` uses the BottomSheet at 768px and below when the primary pointer is coarse.',
'Presentation policy. `popover` opens at the pointer position, `bottom-sheet` always uses an action sheet, and `adaptive` uses the BottomSheet on a compact touch screen: a coarse primary pointer, and a viewport narrower than 744px or shorter than 500px.',
default: "'popover'",
},
{
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/ContextMenu/ContextMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {DropdownMenuSubMenu} from '../DropdownMenu/DropdownMenuSubMenu';
import {Divider} from '../Divider';
import {useLayerDismissal} from '../Layer/useLayerDismissal';
import {warmMenuBottomSheet} from '../DropdownMenu/__tests__/warmMenuBottomSheet';
import {COMPACT_TOUCH_PRESENTATION_QUERY} from '../hooks/useAdaptivePresentation';

// The touch sheet is loaded lazily; resolve its modules once up front, so
// ContextMenu's own lazy sheet renders within the cases' waitFor.
Expand Down Expand Up @@ -309,7 +310,7 @@ describe('ContextMenu', () => {
vi.stubGlobal(
'matchMedia',
vi.fn().mockImplementation((query: string) => ({
matches: query === '(max-width: 768px) and (pointer: coarse)',
matches: query === COMPACT_TOUCH_PRESENTATION_QUERY,
media: query,
onchange: null,
addEventListener: vi.fn(),
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/DropdownMenu/DropdownMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {rtlStyles} from '../utils';
import {__resetInteractionModalityForTest} from '../utils/interactionModality';
import {focusOutlineStyles} from '../utils/focusOutline.stylex';
import {spacingVars} from '../theme/tokens.stylex';
import {COMPACT_TOUCH_PRESENTATION_QUERY} from '../hooks/useAdaptivePresentation';

// Mock showPopover and hidePopover methods since they're not implemented in jsdom
beforeEach(() => {
Expand Down Expand Up @@ -341,7 +342,7 @@ describe('DropdownMenu', () => {
vi.stubGlobal(
'matchMedia',
vi.fn().mockImplementation((query: string) => ({
matches: query === '(max-width: 768px) and (pointer: coarse)',
matches: query === COMPACT_TOUCH_PRESENTATION_QUERY,
media: query,
onchange: null,
addEventListener: vi.fn(),
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/MoreMenu/MoreMenu.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const docs = {
{
name: 'presentation',
type: "'popover' | 'bottom-sheet' | 'adaptive'",
description: 'Presentation policy forwarded to DropdownMenu. `adaptive` keeps the anchored popover on pointer-based layouts and uses a BottomSheet at 768px and below when the primary pointer is coarse.',
description: 'Presentation policy forwarded to DropdownMenu. `adaptive` keeps the anchored popover on pointer-based layouts and uses a BottomSheet on a compact touch screen: a coarse primary pointer, and a viewport narrower than 744px or shorter than 500px.',
default: "'popover'",
},
{
Expand Down Expand Up @@ -204,7 +204,7 @@ export const docsZh = {
{
name: 'presentation',
type: "'popover' | 'bottom-sheet' | 'adaptive'",
description: '菜单呈现策略。`adaptive` 在指针布局中使用锚定浮层,在主指针为粗略指针且宽度不超过 768px 时使用 BottomSheet。',
description: '菜单呈现策略。`adaptive` 在指针布局中使用锚定浮层,在主指针为粗略指针且视口宽度小于 744px 或高度小于 500px 时使用 BottomSheet。',
default: "'popover'",
},
{
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/MultiSelector/MultiSelector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {selectorPresentationStyles} from '../Selector/selectorPresentation.style
import {groupStyles} from '../InputGroup/groupStyles';
import {InputGroup, InputGroupText} from '../InputGroup';
import {colorVars, focusVars} from '../theme/tokens.stylex';
import {COMPACT_TOUCH_PRESENTATION_QUERY} from '../hooks/useAdaptivePresentation';

function generateThemeTestCSS(theme: Parameters<typeof generateThemeCSS>[0]) {
const {prose, component} = generateThemeCSS(theme);
Expand Down Expand Up @@ -146,7 +147,7 @@ describe('MultiSelector', () => {
vi.stubGlobal(
'matchMedia',
vi.fn().mockImplementation((query: string) => ({
matches: query === '(max-width: 768px) and (pointer: coarse)',
matches: query === COMPACT_TOUCH_PRESENTATION_QUERY,
media: query,
onchange: null,
addEventListener: vi.fn(),
Expand Down Expand Up @@ -2183,7 +2184,7 @@ describe('MultiSelector', () => {
vi.mocked(matchMedia).mockImplementation(
(query: string) =>
({
matches: query === '(max-width: 768px) and (pointer: coarse)',
matches: query === COMPACT_TOUCH_PRESENTATION_QUERY,
media: query,
onchange: null,
addEventListener: vi.fn(),
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/Selector/Selector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {generateThemeCSS} from '../theme/generateThemeRules';
import {colorVars, spacingVars} from '../theme/tokens.stylex';
import {selectorPresentationStyles} from './selectorPresentation.stylex';
import {groupStyles} from '../InputGroup/groupStyles';
import {COMPACT_TOUCH_PRESENTATION_QUERY} from '../hooks/useAdaptivePresentation';

function generateThemeTestCSS(theme: Parameters<typeof generateThemeCSS>[0]) {
const {prose, component} = generateThemeCSS(theme);
Expand Down Expand Up @@ -378,7 +379,7 @@ describe('Selector', () => {
vi.stubGlobal(
'matchMedia',
vi.fn().mockImplementation((query: string) => ({
matches: query === '(max-width: 768px) and (pointer: coarse)',
matches: query === COMPACT_TOUCH_PRESENTATION_QUERY,
media: query,
onchange: null,
addEventListener: vi.fn(),
Expand Down Expand Up @@ -2817,7 +2818,7 @@ describe('Selector', () => {
vi.mocked(matchMedia).mockImplementation(
(query: string) =>
({
matches: query === '(max-width: 768px) and (pointer: coarse)',
matches: query === COMPACT_TOUCH_PRESENTATION_QUERY,
media: query,
onchange: null,
addEventListener: vi.fn(),
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/hooks/useAdaptivePresentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

/**
* @file useAdaptivePresentation.ts
* @input Uses the shared SSR-safe media-query hook
* @input Uses the shared SSR-safe media-query hook and the touch
* presentation's compact-touch query
* @output Resolves popover/bottom-sheet presentation policy
* @position Internal overlay policy shared across component families
*/

import {COMPACT_TOUCH_QUERY} from '../utils/touchPresentation';
import {useMediaQuery} from './useMediaQuery';

export type AdaptivePresentation = 'popover' | 'bottom-sheet' | 'adaptive';
Expand All @@ -17,11 +19,11 @@ export type ResolvedAdaptivePresentation = Exclude<
'adaptive'
>;

// Width keeps desktop-class tablets and large touch displays anchored by
// default. Pointer capability, rather than `hover: none`, avoids excluding
// touch browsers that also report hover capability.
export const COMPACT_TOUCH_PRESENTATION_QUERY =
'(max-width: 768px) and (pointer: coarse)';
// A sheet needs both a compact viewport and a coarse primary pointer: tablets
// and large touch displays stay anchored, phones in either orientation get
// the sheet. The query is the shared touch presentation's
// (utils/touchPresentation.ts).
export const COMPACT_TOUCH_PRESENTATION_QUERY = COMPACT_TOUCH_QUERY;

export function useAdaptivePresentation(
presentation: AdaptivePresentation,
Expand Down
228 changes: 228 additions & 0 deletions packages/core/src/utils/touchPresentation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
* @file touchPresentation.test.ts
* @input Uses vitest, React Testing Library, react-dom/server and a
* query-aware matchMedia stub
* @output The touch presentation's answers: touch from the primary pointer,
* compact from the width, both for presentations; the server default and
* hints; live updates; the one-moment read; the motion choice; core's
* compact-touch media keys spelled as the shared query; the adaptive
* presentation reading it
* @position Unit tests of packages/core/src/utils/touchPresentation.ts
*/

import fs from 'node:fs';
import path from 'node:path';
import {act, renderHook} from '@testing-library/react';
import {createElement} from 'react';
import {renderToString} from 'react-dom/server';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {
COMPACT_TOUCH_PRESENTATION_QUERY,
useAdaptivePresentation,
} from '../hooks/useAdaptivePresentation';
import {
COMPACT_QUERY,
COMPACT_TOUCH_QUERY,
TOUCH_QUERY,
readTouchPresentation,
useMotionPresentation,
useTouchPresentation,
} from './touchPresentation';

/** A device: what its primary pointer is and how big its viewport is. */
const device = {coarse: false, width: 1280, height: 800};
const listeners = new Set<() => void>();

/** Answers a media query list: `,` is or, `and` is and. */
function answer(query: string): boolean {
return query.split(', ').some(alternative =>
alternative.split(' and ').every(part => {
if (part === '(pointer: coarse)') {
return device.coarse;
}
const range = /^\((width|height) < (\d+)px\)$/.exec(part);
if (range != null) {
return device[range[1] as 'width' | 'height'] < Number(range[2]);
}
throw new Error(`unexpected query ${query}`);
}),
);
}

function setDevice(next: Partial<typeof device>): void {
Object.assign(device, next);
for (const listener of [...listeners]) {
listener();
}
}

beforeEach(() => {
setDevice({coarse: false, width: 1280, height: 800});
vi.stubGlobal(
'matchMedia',
vi.fn((query: string) => ({
get matches() {
return answer(query);
},
media: query,
addEventListener: (_type: string, listener: () => void) =>
listeners.add(listener),
removeEventListener: (_type: string, listener: () => void) =>
listeners.delete(listener),
})),
);
});

afterEach(() => {
listeners.clear();
vi.unstubAllGlobals();
});

describe('touch presentation', () => {
it('keys touch on the primary pointer and compact on the width', () => {
const {result} = renderHook(() => useTouchPresentation());
expect(result.current).toEqual({
isTouch: false,
isCompact: false,
isCompactTouch: false,
});
// A tablet: a finger at any width.
act(() => setDevice({coarse: true, width: 1024, height: 1366}));
expect(result.current).toEqual({
isTouch: true,
isCompact: false,
isCompactTouch: false,
});
// A narrowed desktop window: compact, still a mouse.
act(() => setDevice({coarse: false, width: 600, height: 900}));
expect(result.current).toEqual({
isTouch: false,
isCompact: true,
isCompactTouch: false,
});
// A phone: both.
act(() => setDevice({coarse: true, width: 390, height: 844}));
expect(result.current).toEqual({
isTouch: true,
isCompact: true,
isCompactTouch: true,
});
});

it('keeps one answer object while nothing changes', () => {
const {result, rerender} = renderHook(() => useTouchPresentation());
const first = result.current;
rerender();
expect(result.current).toBe(first);
});

it('answers the server default, or the server hint, before hydration', () => {
const Probe = (props: {hint?: boolean}) => {
const presentation = useTouchPresentation(
props.hint === true ? {isTouch: true, isCompact: true} : {},
);
return createElement(
'span',
null,
`${presentation.isTouch}/${presentation.isCompactTouch}/${useMotionPresentation(props.hint)}`,
);
};
setDevice({coarse: true, width: 390, height: 844});
expect(renderToString(createElement(Probe))).toContain('false/false/theme');
expect(renderToString(createElement(Probe, {hint: true}))).toContain(
'true/true/uikit',
);
});

it('approximates UIKit size classes with a width and a height', () => {
const {result} = renderHook(() => useTouchPresentation());
// iPad mini in portrait: regular in UIKit, not compact here.
act(() => setDevice({coarse: true, width: 744, height: 1133}));
expect(result.current.isCompact).toBe(false);
// iPhone in landscape: compact in UIKit and here.
act(() => setDevice({coarse: true, width: 852, height: 393}));
expect(result.current.isCompact).toBe(true);
// iPad in Slide Over: compact in UIKit and here.
act(() => setDevice({coarse: true, width: 375, height: 1000}));
expect(result.current.isCompact).toBe(true);
// The known miss: a Max-size iPhone in landscape is regular in UIKit.
act(() => setDevice({coarse: true, width: 932, height: 430}));
expect(result.current.isCompact).toBe(true);
});

it('reads the conditions at one moment, and without a window', () => {
setDevice({coarse: true, width: 390, height: 844});
expect(readTouchPresentation()).toEqual({
isTouch: true,
isCompact: true,
isCompactTouch: true,
});
expect(readTouchPresentation(null)).toEqual({
isTouch: false,
isCompact: false,
isCompactTouch: false,
});
});

it('chooses UIKit motion under a finger and the theme under a mouse', () => {
const {result} = renderHook(() => useMotionPresentation());
expect(result.current).toBe('theme');
act(() => setDevice({coarse: true}));
expect(result.current).toBe('uikit');
});

it('keeps every compact-touch media key in core spelled as the resolver', () => {
// A style key that joins a coarse pointer with a width or height is a
// presentation decision: it must be the shared query, not its own.
const root = path.resolve(__dirname, '..');
const found: string[] = [];
const walk = (dir: string): void => {
for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (entry.name !== '__tests__' && entry.name !== 'node_modules') {
walk(full);
}
} else if (
/\.(ts|tsx)$/.test(entry.name) &&
!/\.test\.|\.spec\./.test(entry.name)
) {
const source = fs.readFileSync(full, 'utf8');
for (const match of source.matchAll(/'@media ([^']*)'/g)) {
const query = match[1];
if (
query.includes('pointer: coarse') &&
/\b(width|height)\b/.test(query)
) {
found.push(`${path.relative(root, full)}: ${query}`);
}
}
}
}
};
walk(root);
for (const entry of found) {
expect(entry.slice(entry.indexOf(': ') + 2), entry).toBe(
COMPACT_TOUCH_QUERY,
);
}
expect(TOUCH_QUERY).toBe('(pointer: coarse)');
expect(COMPACT_QUERY).toBe('(width < 744px), (height < 500px)');
});

it('is the query the adaptive popover-or-sheet presentation reads', () => {
expect(COMPACT_TOUCH_PRESENTATION_QUERY).toBe(COMPACT_TOUCH_QUERY);
const {result} = renderHook(() => useAdaptivePresentation('adaptive'));
expect(result.current).toBe('popover');
act(() => setDevice({coarse: true, width: 390, height: 844}));
expect(result.current).toBe('bottom-sheet');
// A phone in landscape too.
act(() => setDevice({coarse: true, width: 852, height: 393}));
expect(result.current).toBe('bottom-sheet');
// A tablet, even an iPad mini in portrait, keeps the anchored popover.
act(() => setDevice({coarse: true, width: 744, height: 1133}));
expect(result.current).toBe('popover');
});
});
Loading
Loading