Skip to content

Commit 5e61d9a

Browse files
Mark Verlingierimeta-codesync[bot]
authored andcommitted
Add getTextLayout() to Text refs
Summary: `onTextLayout` reaches JS after the first frame with the text is drawn, so output that depends on a text's lines shows unadjusted for a few frames. This adds `getTextLayout()` to refs of a `<Text>` not nested in another `<Text>`. It synchronously returns `{lines}`, the same shape and lines as the `onTextLayout` event, so a layout effect can use them in the same commit. - `ParagraphShadowNode` keeps the lines it measures for `onTextLayout` and exposes them through `TextLayoutLinesProvider`, a header-only interface. - `dom::getTextLayoutLines` reads them through that interface; `NativeDOM.getTextLayout` resolves the revision and builds the `{lines}` object. - `Text` adds the method before the ref receives the instance, only when a ref is passed; nested texts don't get it. Changelog: [General][Added] - Add `getTextLayout()` to `<Text>` refs Reviewed By: rubennorte Differential Revision: D134142257
1 parent 3ea6c42 commit 5e61d9a

16 files changed

Lines changed: 747 additions & 21 deletions

File tree

‎packages/react-native/Libraries/Text/Text.js‎

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
import type {HostInstance} from '../../src/private/types/HostInstance';
1212
import type {TextStyleProp} from '../StyleSheet/StyleSheet';
1313
import type {TextStyle} from '../StyleSheet/StyleSheetTypes';
14-
import type {GestureResponderEvent} from '../Types/CoreEventTypes';
14+
import type {
15+
GestureResponderEvent,
16+
TextLayoutLine,
17+
} from '../Types/CoreEventTypes';
1518
import type {NativeTextProps} from './TextNativeComponent';
1619
import type {PressRetentionOffset, TextProps} from './TextProps';
1720

1821
import * as ReactNativeFeatureFlags from '../../src/private/featureflags/ReactNativeFeatureFlags';
22+
import {getNativeElementReference} from '../../src/private/webapis/dom/nodes/internals/NodeInternals';
23+
import NativeDOM from '../../src/private/webapis/dom/nodes/specs/NativeDOM';
1924
import * as PressabilityDebug from '../Pressability/PressabilityDebug';
2025
import usePressability from '../Pressability/usePressability';
2126
import flattenStyle from '../StyleSheet/flattenStyle';
@@ -31,7 +36,30 @@ import {
3136
import * as React from 'react';
3237
import {useContext, useMemo, useState} from 'react';
3338

34-
export type TextInstance = HostInstance;
39+
/**
40+
* The methods `Text` adds to the instance of a `<Text>` that is not nested in
41+
* another `<Text>`.
42+
*/
43+
interface TextInstanceMethods {
44+
/**
45+
* The text's last layout, read synchronously (for example in a layout
46+
* effect), so it is available before the `onTextLayout` event. It has the
47+
* shape of that event's `nativeEvent`: `lines` holds the lines
48+
* `onTextLayout` reports for that layout, with the same platform-specific
49+
* `text`: it need not equal the painted text, and a truncated line does not
50+
* end with an ellipsis on every platform. The text needs an `onTextLayout`
51+
* handler; otherwise, and while new content has not been laid out (for
52+
* example while the text is hidden), `lines` is empty.
53+
*
54+
* Only the instance of a `<Text>` that is not nested in another `<Text>`,
55+
* and is passed a `ref`, has this method.
56+
*/
57+
getTextLayout?: () => TextLayout;
58+
}
59+
60+
type TextLayout = Readonly<{lines: ReadonlyArray<TextLayoutLine>}>;
61+
62+
export type TextInstance = HostInstance & TextInstanceMethods;
3563

3664
export type {TextProps} from './TextProps';
3765

@@ -292,7 +320,7 @@ const TextImpl: component(
292320
if (isPressable) {
293321
nativeText = (
294322
<PressableText
295-
ref={forwardedRef}
323+
ref={getTextRef(forwardedRef)}
296324
selectable={_selectable}
297325
textProps={processedProps}
298326
textPressabilityProps={textPressabilityProps ?? {}}
@@ -301,9 +329,12 @@ const TextImpl: component(
301329
} else {
302330
nativeText =
303331
_selectable === true ? (
304-
<NativeSelectableText {...processedProps} ref={forwardedRef} />
332+
<NativeSelectableText
333+
{...processedProps}
334+
ref={getTextRef(forwardedRef)}
335+
/>
305336
) : (
306-
<NativeText {...processedProps} ref={forwardedRef} />
337+
<NativeText {...processedProps} ref={getTextRef(forwardedRef)} />
307338
);
308339
}
309340

@@ -337,6 +368,66 @@ const TextImpl: component(
337368

338369
TextImpl.displayName = 'Text';
339370

371+
function getTextLayout(this: HostInstance): TextLayout {
372+
const node = getNativeElementReference(this);
373+
if (node == null || NativeDOM == null || NativeDOM.getTextLayout == null) {
374+
return {lines: []};
375+
}
376+
return NativeDOM.getTextLayout(node);
377+
}
378+
379+
function addTextInstanceMethods(instance: TextInstance): void {
380+
// $FlowFixMe[incompatible-use] - Text adds this method to its host instance.
381+
instance.getTextLayout = getTextLayout;
382+
}
383+
384+
// One ref per forwarded ref, kept across renders, so a `<Text>` without a ref
385+
// does no work for it.
386+
const textRefs: WeakMap<
387+
NonNullable<React.RefSetter<TextInstance>>,
388+
(instance: TextInstance | null) => () => void,
389+
> = new WeakMap();
390+
391+
/**
392+
* The ref for a `<Text>` that is not nested in another `<Text>`: it adds the
393+
* instance methods before the forwarded ref gets the instance.
394+
*/
395+
function getTextRef(
396+
forwardedRef: ?React.RefSetter<TextInstance>,
397+
): ?React.RefSetter<TextInstance> {
398+
if (forwardedRef == null) {
399+
return undefined;
400+
}
401+
let textRef = textRefs.get(forwardedRef);
402+
if (textRef == null) {
403+
textRef = createTextRef(forwardedRef);
404+
textRefs.set(forwardedRef, textRef);
405+
}
406+
return textRef;
407+
}
408+
409+
function createTextRef(
410+
forwardedRef: NonNullable<React.RefSetter<TextInstance>>,
411+
): (instance: TextInstance | null) => () => void {
412+
return instance => {
413+
if (instance != null) {
414+
addTextInstanceMethods(instance);
415+
}
416+
if (typeof forwardedRef === 'function') {
417+
const cleanup = forwardedRef(instance);
418+
return typeof cleanup === 'function'
419+
? cleanup
420+
: () => {
421+
forwardedRef(null);
422+
};
423+
}
424+
forwardedRef.current = instance;
425+
return () => {
426+
forwardedRef.current = null;
427+
};
428+
};
429+
}
430+
340431
type TextPressabilityProps = Readonly<{
341432
onLongPress?: ?(event: GestureResponderEvent) => unknown,
342433
onPress?: ?(event: GestureResponderEvent) => unknown,

0 commit comments

Comments
 (0)