Repository navigation
feat(core): one touch presentation resolver #8062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 4dc2aae
feat(core): one touch presentation resolver
vjeux 9d2d720
feat(core): the touch presentation resolver knows the iOS idiom
vjeux 7065daf
fix(core): one touch model on every device; the adaptive change is a …
cixzhang 92ef66c
Merge main into wip/uikit-pr-touch-presentation
cixzhang 398c723
fix(core): the touch resolver reads the theme's compact definition
cixzhang 33ee284
docs(MoreMenu): keep the doc's own formatting; change only the compac…
cixzhang 1264c5b
fix(core): compact follows the active theme's md; the branch drops it…
cixzhang ec08340
docs(spec): drop this branch's stacked copy of AST-094
cixzhang ced7766
docs(ContextMenu, MoreMenu): the compact edge is the theme md breakpoint
cixzhang 4d728a7
refactor(core): drop the unused isCompactTouch from the touch present…
vjeux d34d800
test(core): the adaptive menu's compact edges in Chromium and WebKit
vjeux c5efd27
Merge main into #8062 (lab typecheck fix aa3b92f320 and current main)
vjeux b8642ba
test(core): the touch presentation's browser proofs leave for the des…
vjeux f809dae
test(DropdownMenu): a sub-menu's drill-in follows a theme that moves md
vjeux 4b5e872
refactor(core): readTouchPresentation takes the compact definition it…
vjeux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
| @vjeux | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.DropdownMenuSubMenudefaults topresentation="adaptive"and drills in from the sameCOMPACT_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.