Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
101 changes: 96 additions & 5 deletions packages/react-native/Libraries/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
import type {HostInstance} from '../../src/private/types/HostInstance';
import type {TextStyleProp} from '../StyleSheet/StyleSheet';
import type {TextStyle} from '../StyleSheet/StyleSheetTypes';
import type {GestureResponderEvent} from '../Types/CoreEventTypes';
import type {
GestureResponderEvent,
TextLayoutLine,
} from '../Types/CoreEventTypes';
import type {NativeTextProps} from './TextNativeComponent';
import type {PressRetentionOffset, TextProps} from './TextProps';

import * as ReactNativeFeatureFlags from '../../src/private/featureflags/ReactNativeFeatureFlags';
import {getNativeElementReference} from '../../src/private/webapis/dom/nodes/internals/NodeInternals';
import NativeDOM from '../../src/private/webapis/dom/nodes/specs/NativeDOM';
import * as PressabilityDebug from '../Pressability/PressabilityDebug';
import usePressability from '../Pressability/usePressability';
import flattenStyle from '../StyleSheet/flattenStyle';
Expand All @@ -31,7 +36,30 @@ import {
import * as React from 'react';
import {useContext, useMemo, useState} from 'react';

export type TextInstance = HostInstance;
/**
* The methods `Text` adds to the instance of a `<Text>` that is not nested in
* another `<Text>`.
*/
interface TextInstanceMethods {
/**
* The text's last layout, read synchronously (for example in a layout
* effect), so it is available before the `onTextLayout` event. It has the
* shape of that event's `nativeEvent`: `lines` holds the lines
* `onTextLayout` reports for that layout, with the same platform-specific
* `text`: it need not equal the painted text, and a truncated line does not
* end with an ellipsis on every platform. The text needs an `onTextLayout`
* handler; otherwise, and while new content has not been laid out (for
* example while the text is hidden), `lines` is empty.
*
* Only the instance of a `<Text>` that is not nested in another `<Text>`,
* and is passed a `ref`, has this method.
*/
getTextLayout?: () => TextLayout;
}

type TextLayout = Readonly<{lines: ReadonlyArray<TextLayoutLine>}>;

export type TextInstance = HostInstance & TextInstanceMethods;

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

Expand Down Expand Up @@ -292,7 +320,7 @@ const TextImpl: component(
if (isPressable) {
nativeText = (
<PressableText
ref={forwardedRef}
ref={getTextRef(forwardedRef)}
selectable={_selectable}
textProps={processedProps}
textPressabilityProps={textPressabilityProps ?? {}}
Expand All @@ -301,9 +329,12 @@ const TextImpl: component(
} else {
nativeText =
_selectable === true ? (
<NativeSelectableText {...processedProps} ref={forwardedRef} />
<NativeSelectableText
{...processedProps}
ref={getTextRef(forwardedRef)}
/>
) : (
<NativeText {...processedProps} ref={forwardedRef} />
<NativeText {...processedProps} ref={getTextRef(forwardedRef)} />
);
}

Expand Down Expand Up @@ -337,6 +368,66 @@ const TextImpl: component(

TextImpl.displayName = 'Text';

function getTextLayout(this: HostInstance): TextLayout {
const node = getNativeElementReference(this);
if (node == null || NativeDOM == null || NativeDOM.getTextLayout == null) {
return {lines: []};
}
return NativeDOM.getTextLayout(node);
}

function addTextInstanceMethods(instance: TextInstance): void {
// $FlowFixMe[incompatible-use] - Text adds this method to its host instance.
instance.getTextLayout = getTextLayout;
}

// One ref per forwarded ref, kept across renders, so a `<Text>` without a ref
// does no work for it.
const textRefs: WeakMap<
NonNullable<React.RefSetter<TextInstance>>,
(instance: TextInstance | null) => () => void,
> = new WeakMap();

/**
* The ref for a `<Text>` that is not nested in another `<Text>`: it adds the
* instance methods before the forwarded ref gets the instance.
*/
function getTextRef(
forwardedRef: ?React.RefSetter<TextInstance>,
): ?React.RefSetter<TextInstance> {
if (forwardedRef == null) {
return undefined;
}
let textRef = textRefs.get(forwardedRef);
if (textRef == null) {
textRef = createTextRef(forwardedRef);
textRefs.set(forwardedRef, textRef);
}
return textRef;
}

function createTextRef(
forwardedRef: NonNullable<React.RefSetter<TextInstance>>,
): (instance: TextInstance | null) => () => void {
return instance => {
if (instance != null) {
addTextInstanceMethods(instance);
}
if (typeof forwardedRef === 'function') {
const cleanup = forwardedRef(instance);
return typeof cleanup === 'function'
? cleanup
: () => {
forwardedRef(null);
};
}
forwardedRef.current = instance;
return () => {
forwardedRef.current = null;
};
};
}

type TextPressabilityProps = Readonly<{
onLongPress?: ?(event: GestureResponderEvent) => unknown,
onPress?: ?(event: GestureResponderEvent) => unknown,
Expand Down
Loading
Loading