Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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/keyboard-height-caret-own-module.md
Original file line number Diff line number Diff line change
@@ -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.
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved

@vjeux
6 changes: 6 additions & 0 deletions docs/architecture/react-component-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
- `packages/core/src/hooks/useCommittedRef.ts` — one internal hook with two call
shapes, beside the internal `useIsomorphicLayoutEffect` (INV3, INV17, DEC-3).
`useCommittedRef<T>(value: T): { readonly current: T }` returns a read-only
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/keyboard/keyboardHeight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions packages/core/src/utils/caretAt.ts
Original file line number Diff line number Diff line change
@@ -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.
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
*/

/** The caret position the engine puts at a point, by either query. */
export function caretAt(
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
doc: Document,
x: number,
y: number,
shadowRoots: ReadonlyArray<ShadowRoot>,
): {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};
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
}
if (typeof queries.caretRangeFromPoint === 'function') {
const range = queries.caretRangeFromPoint(x, y);
return range == null
? null
: {node: range.startContainer, offset: range.startOffset};
}
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
return null;
}
37 changes: 1 addition & 36 deletions packages/core/src/utils/selectionLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<ShadowRoot>,
): {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);
Expand Down
13 changes: 13 additions & 0 deletions scripts/touch-support-bundle.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down