diff --git a/.changeset/keyboard-height-caret-own-module.md b/.changeset/keyboard-height-caret-own-module.md new file mode 100644 index 0000000000000..c970bebb7b44c --- /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. 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 diff --git a/docs/architecture/react-component-runtime.md b/docs/architecture/react-component-runtime.md index 8fe0703ecacda..b889cd36d944f 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 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/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..6b8265e2810b0 --- /dev/null +++ b/packages/core/src/utils/caretAt.ts @@ -0,0 +1,48 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +/** + * @file caretAt.ts + * @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 imports it from here, so + * it does not load the selection 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') { + // 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 + : {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/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', 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(