From b98e7d7d0470e45b941eb4c90a1e5bfe16d4180a Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Thu, 3 Dec 2026 13:02:10 -0800 Subject: [PATCH 1/4] perf(keyboard): the keyboard height no longer loads the selection lock --- .../keyboard-height-caret-own-module.md | 7 +++ docs/architecture/react-component-runtime.md | 6 +++ packages/core/src/keyboard/keyboardHeight.ts | 2 +- packages/core/src/utils/caretAt.ts | 45 +++++++++++++++++++ packages/core/src/utils/selectionLock.ts | 37 +-------------- scripts/touch-support-bundle.test.mjs | 13 ++++++ 6 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 .changeset/keyboard-height-caret-own-module.md create mode 100644 packages/core/src/utils/caretAt.ts diff --git a/.changeset/keyboard-height-caret-own-module.md b/.changeset/keyboard-height-caret-own-module.md new file mode 100644 index 0000000000000..cfec1760c253c --- /dev/null +++ b/.changeset/keyboard-height-caret-own-module.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[perf] The keyboard height reads the caret position from a module of its own, not from the selection lock. A page that loads the keyboard height, as a DropdownMenu or a Table does, no longer loads the selection lock with it. First-load JavaScript drops by about 1.6 KB gzip on such a page, with esbuild and with Rolldown. A text-only page built with esbuild drops by about 1.7 KB. A page that loads the selection lock for another of its uses loads up to 80 bytes more. The keyboard height reads the same caret as before. + +@vjeux diff --git a/docs/architecture/react-component-runtime.md b/docs/architecture/react-component-runtime.md index 8fe0703ecacda..89a879c585c7a 100644 --- a/docs/architecture/react-component-runtime.md +++ b/docs/architecture/react-component-runtime.md @@ -596,6 +596,12 @@ contracts. correctness boundary without receiving stable public API status. - `packages/core/src/utils/sharedResizeObserver.ts` — current shared Core resize observation mechanism; tooling and utility tests own its concrete API. +- `packages/core/src/utils/selectionLock.ts` — the selection lock during a + gesture, one of the known public-API gaps above. + `packages/core/src/utils/caretAt.ts` — `caretAt(doc, x, y, shadowRoots)`, the + caret position the engine puts at a client point, as `{node, offset}` or + null. The selection lock and the keyboard height import it, so a page that + loads the keyboard height does not load the selection lock. - `packages/core/src/hooks/useCommittedRef.ts` — one internal hook with two call shapes, beside the internal `useIsomorphicLayoutEffect` (INV3, INV17, DEC-3). `useCommittedRef(value: T): { readonly current: T }` returns a read-only diff --git a/packages/core/src/keyboard/keyboardHeight.ts b/packages/core/src/keyboard/keyboardHeight.ts index bb6467da86237..820e68bcab1b8 100644 --- a/packages/core/src/keyboard/keyboardHeight.ts +++ b/packages/core/src/keyboard/keyboardHeight.ts @@ -23,7 +23,7 @@ import {getTouches, subscribeInput} from '../utils/inputStream'; import {isElement, isHTMLElement, isNode} from '../utils/nodeType'; import {layoutViewportHeight} from '../utils/layoutViewport'; import {PRESS_SLOP_PX} from '../utils/pressGesture'; -import {caretAt} from '../utils/selectionLock'; +import {caretAt} from '../utils/caretAt'; import {sharedWeakMap} from '../utils/sharedRealm'; // A shortfall smaller than this is rounding, not a keyboard. diff --git a/packages/core/src/utils/caretAt.ts b/packages/core/src/utils/caretAt.ts new file mode 100644 index 0000000000000..30b2b29313972 --- /dev/null +++ b/packages/core/src/utils/caretAt.ts @@ -0,0 +1,45 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +/** + * @file caretAt.ts + * @input A document, a client point and the shadow roots to look into + * @output caretAt: the caret position the engine puts at that point + * @position An internal utility. The keyboard height reads it apart from the + * selection lock, so a page that locks no selection does not load the lock. + */ + +/** The caret position the engine puts at a point, by either query. */ +export function caretAt( + doc: Document, + x: number, + y: number, + shadowRoots: ReadonlyArray, +): {node: Node; offset: number} | null { + const queries = doc as Document & { + caretPositionFromPoint?: ( + x: number, + y: number, + options?: {shadowRoots?: ShadowRoot[]}, + ) => {offsetNode: Node; offset: number} | null; + caretRangeFromPoint?: (x: number, y: number) => Range | null; + }; + if (typeof queries.caretPositionFromPoint === 'function') { + // Without the roots, a point over shadow text answers at its host. + const position = + shadowRoots.length > 0 + ? queries.caretPositionFromPoint(x, y, { + shadowRoots: [...shadowRoots], + }) + : queries.caretPositionFromPoint(x, y); + return position == null + ? null + : {node: position.offsetNode, offset: position.offset}; + } + if (typeof queries.caretRangeFromPoint === 'function') { + const range = queries.caretRangeFromPoint(x, y); + return range == null + ? null + : {node: range.startContainer, offset: range.startOffset}; + } + return null; +} diff --git a/packages/core/src/utils/selectionLock.ts b/packages/core/src/utils/selectionLock.ts index daab3ac2cc50f..ab8406f7f3b65 100644 --- a/packages/core/src/utils/selectionLock.ts +++ b/packages/core/src/utils/selectionLock.ts @@ -36,6 +36,7 @@ import { shadowRootsOutward, } from './flatTree'; import {subscribeInput} from './inputStream'; +import {caretAt} from './caretAt'; import {isIosWebKit} from './touchQueries'; import {isElement, isNode} from './nodeType'; import {sharedWeakMap} from './sharedRealm'; @@ -362,42 +363,6 @@ function isInButton(node: Node): boolean { return false; } -/** The caret position the engine puts at a point, by either query. */ -export function caretAt( - doc: Document, - x: number, - y: number, - shadowRoots: ReadonlyArray, -): {node: Node; offset: number} | null { - const queries = doc as Document & { - caretPositionFromPoint?: ( - x: number, - y: number, - options?: {shadowRoots?: ShadowRoot[]}, - ) => {offsetNode: Node; offset: number} | null; - caretRangeFromPoint?: (x: number, y: number) => Range | null; - }; - if (typeof queries.caretPositionFromPoint === 'function') { - // Without the roots, a point over shadow text answers at its host. - const position = - shadowRoots.length > 0 - ? queries.caretPositionFromPoint(x, y, { - shadowRoots: [...shadowRoots], - }) - : queries.caretPositionFromPoint(x, y); - return position == null - ? null - : {node: position.offsetNode, offset: position.offset}; - } - if (typeof queries.caretRangeFromPoint === 'function') { - const range = queries.caretRangeFromPoint(x, y); - return range == null - ? null - : {node: range.startContainer, offset: range.startOffset}; - } - return null; -} - /** Whether an element's text can be selected (`user-select` is not none). */ function allowsSelection(element: Element): boolean { const style = element.ownerDocument.defaultView?.getComputedStyle(element); diff --git a/scripts/touch-support-bundle.test.mjs b/scripts/touch-support-bundle.test.mjs index 5c23738d9c1af..c2ada1e9e511a 100644 --- a/scripts/touch-support-bundle.test.mjs +++ b/scripts/touch-support-bundle.test.mjs @@ -556,6 +556,19 @@ export const Holder = holdInput; } }); + it('leaves the selection lock out of what DropdownMenu loads at once: the keyboard height reads only the caret query', () => { + const menu = entryWith( + 'Menu.ts', + `export {DropdownMenu} from ${JSON.stringify( + path.join(CORE_SRC, 'DropdownMenu/DropdownMenu.tsx'), + )}; +`, + ); + expect(bytesOf(eagerBytesOf(menu), /utils\/selectionLock\.ts$/)).toEqual( + [], + ); + }); + it('is one copy that two components importing it share', () => { const component = name => entryWith( From e84b7a709022a0c7ea7f18335afd24a098614f28 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Thu, 3 Dec 2026 16:19:25 -0800 Subject: [PATCH 2/4] perf(keyboard): review round: the changeset scopes the saving, and caretAt's comments bound the fallback --- .changeset/keyboard-height-caret-own-module.md | 2 +- docs/architecture/react-component-runtime.md | 4 ++-- packages/core/src/utils/caretAt.ts | 9 ++++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.changeset/keyboard-height-caret-own-module.md b/.changeset/keyboard-height-caret-own-module.md index cfec1760c253c..581b1570fe367 100644 --- a/.changeset/keyboard-height-caret-own-module.md +++ b/.changeset/keyboard-height-caret-own-module.md @@ -2,6 +2,6 @@ '@astryxdesign/core': patch --- -[perf] The keyboard height reads the caret position from a module of its own, not from the selection lock. A page that loads the keyboard height, as a DropdownMenu or a Table does, no longer loads the selection lock with it. First-load JavaScript drops by about 1.6 KB gzip on such a page, with esbuild and with Rolldown. A text-only page built with esbuild drops by about 1.7 KB. A page that loads the selection lock for another of its uses loads up to 80 bytes more. The keyboard height reads the same caret as before. +[perf] The keyboard height reads the caret position from a module of its own, not from the selection lock. On a page whose lazily loaded code also uses the selection lock, as a page with a DropdownMenu or a Table does, the bundler no longer puts the whole lock in the page's first load: first-load JavaScript drops by about 1.6 to 1.8 KB gzip there, with esbuild and with Rolldown. Other pages that load the keyboard height, such as a TextInput, ChatLayout or BottomSheet page, change by under 50 bytes minified. A page that uses the selection lock itself loads up to 80 bytes more. The keyboard height reads the same caret as before. @vjeux diff --git a/docs/architecture/react-component-runtime.md b/docs/architecture/react-component-runtime.md index 89a879c585c7a..b889cd36d944f 100644 --- a/docs/architecture/react-component-runtime.md +++ b/docs/architecture/react-component-runtime.md @@ -600,8 +600,8 @@ contracts. gesture, one of the known public-API gaps above. `packages/core/src/utils/caretAt.ts` — `caretAt(doc, x, y, shadowRoots)`, the caret position the engine puts at a client point, as `{node, offset}` or - null. The selection lock and the keyboard height import it, so a page that - loads the keyboard height does not load the selection lock. + null. The selection lock and the keyboard height import it, so the keyboard + height does not bring the selection lock into a page. - `packages/core/src/hooks/useCommittedRef.ts` — one internal hook with two call shapes, beside the internal `useIsomorphicLayoutEffect` (INV3, INV17, DEC-3). `useCommittedRef(value: T): { readonly current: T }` returns a read-only diff --git a/packages/core/src/utils/caretAt.ts b/packages/core/src/utils/caretAt.ts index 30b2b29313972..6b8265e2810b0 100644 --- a/packages/core/src/utils/caretAt.ts +++ b/packages/core/src/utils/caretAt.ts @@ -2,10 +2,11 @@ /** * @file caretAt.ts - * @input A document, a client point and the shadow roots to look into + * @input A document, a client point and the shadow roots that + * `caretPositionFromPoint` looks into * @output caretAt: the caret position the engine puts at that point - * @position An internal utility. The keyboard height reads it apart from the - * selection lock, so a page that locks no selection does not load the lock. + * @position An internal utility. The keyboard height imports it from here, so + * it does not load the selection lock. */ /** The caret position the engine puts at a point, by either query. */ @@ -36,6 +37,8 @@ export function caretAt( : {node: position.offsetNode, offset: position.offset}; } if (typeof queries.caretRangeFromPoint === 'function') { + // This query takes no shadow roots: over shadow text it answers at the + // shadow host. const range = queries.caretRangeFromPoint(x, y); return range == null ? null From ed56d5e527f2c78268dd1950ece94cfe2520a9ae Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Thu, 3 Dec 2026 18:43:01 -0800 Subject: [PATCH 3/4] test(keyboard): the snap-paging bundle test counts caretAt with the selection lock The snap-paging hook brings the gesture host, which imports the selection lock. The selection lock reads the caret with caretAt. Now that caretAt has a module of its own, the test sees it as a module the hook adds. Its bytes are the ones the selection lock carried before. The test counts caretAt with the selection lock. --- scripts/snap-paging-bundle.test.mjs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/snap-paging-bundle.test.mjs b/scripts/snap-paging-bundle.test.mjs index 5f16fb39a28df..a77f19e16714b 100644 --- a/scripts/snap-paging-bundle.test.mjs +++ b/scripts/snap-paging-bundle.test.mjs @@ -37,7 +37,8 @@ const PAGER_CODE = new RegExp( 'motion/uikit/(gestures|paging|scroll)\\.ts', 'Layer/inputHold\\.ts', // primaryClick and isApplePlatform are the press controller's too. - 'utils/(clickGuard|selectionLock)\\.ts', + // The selection lock reads the caret with caretAt, a module of its own. + 'utils/(caretAt|clickGuard|selectionLock)\\.ts', // The reduced-motion mark's owner module is every script reader's rule // (spec:AST-108 FR6), so Carousel's own scrolls load it too. 'privateAttributes/hapticSwitch\\.stylex\\.ts', From 7ec779724322ea555fdeb2511dc8581d5b7ed89f Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Thu, 3 Dec 2026 19:55:10 -0800 Subject: [PATCH 4/4] perf(keyboard): review round: the changeset's saving needs no other control on the lock Beside a control that uses the selection lock at once, such as CheckboxInput, NumberInput or Slider, the lock stays in the first load. The changeset says so, and that such a page changes by under 200 bytes. --- .changeset/keyboard-height-caret-own-module.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/keyboard-height-caret-own-module.md b/.changeset/keyboard-height-caret-own-module.md index 581b1570fe367..c970bebb7b44c 100644 --- a/.changeset/keyboard-height-caret-own-module.md +++ b/.changeset/keyboard-height-caret-own-module.md @@ -2,6 +2,6 @@ '@astryxdesign/core': patch --- -[perf] The keyboard height reads the caret position from a module of its own, not from the selection lock. On a page whose lazily loaded code also uses the selection lock, as a page with a DropdownMenu or a Table does, the bundler no longer puts the whole lock in the page's first load: first-load JavaScript drops by about 1.6 to 1.8 KB gzip there, with esbuild and with Rolldown. Other pages that load the keyboard height, such as a TextInput, ChatLayout or BottomSheet page, change by under 50 bytes minified. A page that uses the selection lock itself loads up to 80 bytes more. The keyboard height reads the same caret as before. +[perf] The keyboard height reads the caret position from a module of its own, not from the selection lock. On a page whose lazily loaded code also uses the selection lock, as a page with a DropdownMenu or a Table does, the bundler then leaves the lock out of the page's first load, and first-load JavaScript drops by about 1.6 to 1.8 KB gzip, with esbuild and with Rolldown. This holds only where no other control on the page uses the selection lock at once. Beside a control that does, such as CheckboxInput, NumberInput or Slider, the lock stays in the first load, and the page changes by under 200 bytes. Other pages that load the keyboard height, such as a TextInput, ChatLayout or BottomSheet page, change by under 50 bytes minified. The keyboard height reads the same caret as before. @vjeux