Skip to content

Commit 17b9b65

Browse files
committed
Merge the edit mode change (the touch target in rem or em, read again on a pointer or theme change) into the selection control change
2 parents aa992d8 + aa928ba commit 17b9b65

2 files changed

Lines changed: 109 additions & 11 deletions

File tree

‎packages/core/src/utils/touchHitArea.test.tsx‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* List/__tests__/ListTouchHitArea.interaction.browser.spec.ts
1717
*/
1818

19-
import {useRef} from 'react';
19+
import {act, useRef, type ReactNode} from 'react';
2020
import * as stylex from '@stylexjs/stylex';
2121
import {afterEach, describe, expect, it, vi} from 'vitest';
2222
import {render, screen} from '@testing-library/react';
@@ -49,10 +49,19 @@ vi.mock('./hitArea', async importOriginal => {
4949
};
5050
});
5151

52+
import {ThemeContext, type ThemeContextValue} from '../theme/useTheme';
5253
import {useTouchHitArea} from './touchHitArea';
5354
import {touchHitAreaStyles} from './touchHitArea.stylex';
5455

55-
function Host({enabled, target}: {enabled: boolean; target?: string}) {
56+
function Host({
57+
enabled,
58+
target,
59+
fontSize,
60+
}: {
61+
enabled: boolean;
62+
target?: string;
63+
fontSize?: string;
64+
}) {
5665
const ref = useRef<HTMLSpanElement | null>(null);
5766
useTouchHitArea(ref, enabled);
5867
return (
@@ -64,6 +73,9 @@ function Host({enabled, target}: {enabled: boolean; target?: string}) {
6473
if (node != null && target != null) {
6574
node.style.setProperty('--size-touch-target', target);
6675
}
76+
if (node != null && fontSize != null) {
77+
node.style.setProperty('font-size', fontSize);
78+
}
6779
ref.current = node;
6880
}}
6981
{...stylex.props(touchHitAreaStyles.layer)}
@@ -72,6 +84,32 @@ function Host({enabled, target}: {enabled: boolean; target?: string}) {
7284
);
7385
}
7486

87+
/** A matchMedia whose primary pointer can change, firing its listeners. */
88+
function stubPointer(coarse: boolean): (coarse: boolean) => void {
89+
let isCoarse = coarse;
90+
const listeners = new Set<() => void>();
91+
vi.stubGlobal('matchMedia', (query: string) => ({
92+
get matches() {
93+
return query === '(pointer: coarse)' && isCoarse;
94+
},
95+
media: query,
96+
addEventListener: (_type: string, listener: () => void) =>
97+
listeners.add(listener),
98+
removeEventListener: (_type: string, listener: () => void) =>
99+
listeners.delete(listener),
100+
}));
101+
return next => {
102+
isCoarse = next;
103+
for (const listener of [...listeners]) {
104+
listener();
105+
}
106+
};
107+
}
108+
109+
function themed(value: ThemeContextValue, children: ReactNode): ReactNode {
110+
return <ThemeContext value={value}>{children}</ThemeContext>;
111+
}
112+
75113
/** The element's injected `::after` rules, split by the touch gate. */
76114
function afterRules(el: Element): {touch: string[]; other: string[]} {
77115
const classes = el.className
@@ -152,6 +190,39 @@ describe('useTouchHitArea', () => {
152190
expect(watches[1].minSize).toBe(44);
153191
});
154192

193+
it('reads the page’s touch target in rem through the root’s font size and in em through the element’s', () => {
194+
stubCoarse();
195+
document.documentElement.style.setProperty('font-size', '16px');
196+
try {
197+
const rem = render(<Host enabled target="3.75rem" />);
198+
expect(watches[0].minSize).toBe(60);
199+
rem.unmount();
200+
render(<Host enabled target="3em" fontSize="20px" />);
201+
expect(watches[1].minSize).toBe(60);
202+
} finally {
203+
document.documentElement.style.removeProperty('font-size');
204+
}
205+
});
206+
207+
it('reads the touch target again when the primary pointer turns coarse', () => {
208+
const setCoarse = stubPointer(false);
209+
render(<Host enabled target="60px" />);
210+
expect(watches.at(-1)?.minSize).toBe(44);
211+
act(() => setCoarse(true));
212+
expect(watches.at(-1)?.minSize).toBe(60);
213+
expect(watches.at(-2)?.stop).toHaveBeenCalledTimes(1);
214+
});
215+
216+
it('reads the touch target again when the theme changes', () => {
217+
stubCoarse();
218+
const first = {mode: 'light'} as unknown as ThemeContextValue;
219+
const second = {mode: 'dark'} as unknown as ThemeContextValue;
220+
const {rerender} = render(themed(first, <Host enabled target="48px" />));
221+
expect(watches.at(-1)?.minSize).toBe(48);
222+
rerender(themed(second, <Host enabled target="60px" />));
223+
expect(watches.at(-1)?.minSize).toBe(60);
224+
});
225+
155226
it('writes the insets it hears on the element', () => {
156227
render(<Host enabled />);
157228
watches[0].onInsets({top: 3, right: 2, bottom: 1, left: 0});

‎packages/core/src/utils/touchHitArea.ts‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
/**
66
* @file touchHitArea.ts
77
* @input An element ref and whether the element takes taps, the shared touch
8-
* hit area (utils/hitArea.ts), the theme's touch target size
9-
* (`--size-touch-target`), and the Web Animations running around the
10-
* element
8+
* hit area (utils/hitArea.ts), the page's touch target size
9+
* (`--size-touch-target`, in px, rem or em), read again when the primary
10+
* pointer (touchQueries.ts) or the theme (ThemeContext) changes, and the
11+
* Web Animations running around the element
1112
* @output Exports `useTouchHitArea`, which keeps an element's hit insets live
1213
* for its invisible layer (`touchHitAreaStyles.layer`,
1314
* utils/touchHitArea.stylex.ts), and `touchTargetSize`
@@ -33,24 +34,46 @@
3334
* touchHitArea.test.tsx.
3435
*/
3536

36-
import {useEffect, type RefObject} from 'react';
37+
import {use, useEffect, type RefObject} from 'react';
38+
import {useMediaQuery} from '../hooks/useMediaQuery';
39+
import {ThemeContext} from '../theme/useTheme';
3740
import {MIN_TOUCH_TARGET, watchHitArea, writeHitInsets} from './hitArea';
41+
import {TOUCH_QUERY} from './touchQueries';
42+
43+
/**
44+
* A length in px, in the units a theme writes a size in: px, rem (the root's
45+
* font size) or em (the font size of the element the size applies to).
46+
* Anything else is not a length here (NaN).
47+
*/
48+
function lengthInPx(value: string, element: Element, view: Window): number {
49+
const match = /^(\d*\.?\d+)(px|rem|em)$/.exec(value);
50+
if (match == null) {
51+
return Number.NaN;
52+
}
53+
const amount = Number(match[1]);
54+
if (match[2] === 'px') {
55+
return amount;
56+
}
57+
const reference =
58+
match[2] === 'rem' ? element.ownerDocument.documentElement : element;
59+
return amount * Number.parseFloat(view.getComputedStyle(reference).fontSize);
60+
}
3861

3962
/**
4063
* The touch target size in px: 44 (`spec:AST-094` FR42), or more where the
41-
* page sets `--size-touch-target` larger, read under a finger only. A smaller
42-
* value never lowers the target below 44.
64+
* page sets `--size-touch-target` larger (in px, rem or em), read under a
65+
* finger only. A smaller value never lowers the target below 44.
4366
*/
4467
export function touchTargetSize(element: Element): number {
4568
const view = element.ownerDocument.defaultView;
46-
if (view?.matchMedia?.('(pointer: coarse)').matches !== true) {
69+
if (view?.matchMedia?.(TOUCH_QUERY).matches !== true) {
4770
return MIN_TOUCH_TARGET;
4871
}
4972
const value = view
5073
.getComputedStyle(element)
5174
.getPropertyValue('--size-touch-target')
5275
.trim();
53-
const size = value.endsWith('px') ? Number.parseFloat(value) : Number.NaN;
76+
const size = lengthInPx(value, element, view);
5477
return Number.isFinite(size)
5578
? Math.max(MIN_TOUCH_TARGET, size)
5679
: MIN_TOUCH_TARGET;
@@ -129,6 +152,10 @@ export function useTouchHitArea(
129152
ref: RefObject<HTMLElement | null>,
130153
enabled: boolean,
131154
): void {
155+
// The size is read again when the primary pointer or the theme changes: a
156+
// theme sets it, possibly under a coarse pointer only.
157+
const isCoarse = useMediaQuery(TOUCH_QUERY);
158+
const theme = use(ThemeContext);
132159
// External system (`architecture:react-component-runtime` INV3): the shared
133160
// touch hit area (utils/hitArea.ts), which measures the element against its
134161
// neighbors and writes its insets on it; the cleanup stops the watch.
@@ -138,5 +165,5 @@ export function useTouchHitArea(
138165
return undefined;
139166
}
140167
return watchAtRest(element, touchTargetSize(element));
141-
}, [enabled, ref]);
168+
}, [enabled, ref, isCoarse, theme]);
142169
}

0 commit comments

Comments
 (0)