sailfork / t3code-github / apps/ mobile/ modules/ t3-source-text/ src/ SourceText.tsx

import React, { type ComponentRef, type Ref } from "react";
import { Platform, StyleSheet, Text, type TextProps, type TextStyle } from "react-native";
import T3SourceTextRunNativeComponent from "./T3SourceTextRunNativeComponent";
import T3SourceTextNativeComponent from "./T3SourceTextNativeComponent";
import { flattenStyles } from "./util";

const TextAncestorContext = React.createContext<[boolean, TextStyle]>([false, {}]);

export type SourceTextProps = Omit<TextProps, "onTextLayout" | "onPress" | "onLongPress"> & {
  nativeTextRef?: Ref<ComponentRef<typeof Text>>;
  onSelectionChange?: (event: {
    nativeEvent: { target: number; start: number; end: number };
  }) => void;
};

export function SourceText({ style, children, nativeTextRef, ...props }: SourceTextProps) {
  const [isAncestor, rootStyle] = React.useContext(TextAncestorContext);
  const flattenedStyle = React.useMemo(() => flattenStyles(rootStyle, style), [rootStyle, style]);
  const contextValue = React.useMemo<[boolean, TextStyle]>(
    () => [true, StyleSheet.flatten([rootStyle, style]) ?? {}],
    [rootStyle, style],
  );
  if (Platform.OS !== "ios") {
    const { onSelectionChange: _onSelectionChange, ...textProps } = props;
    return (
      <Text ref={nativeTextRef} style={style} {...textProps}>
        {children}
      </Text>
    );
  }
  const runs = React.Children.toArray(children).map(
    (
      child,
      index,
    ) => /* oxlint-disable react/no-array-index-key -- Native text runs are stateless positions within the supplied text. */ {
        if (React.isValidElement(child)) return child;
        if (typeof child !== "string" && typeof child !== "number") return null;
        return (
          <T3SourceTextRunNativeComponent
            key={index}
            style={flattenedStyle}
            {...flattenedStyle}
            text={String(child)}
          />
        );
      } /* oxlint-enable react/no-array-index-key */,
  );
  if (isAncestor) {
    return <TextAncestorContext.Provider value={contextValue}>{runs}</TextAncestorContext.Provider>;
  }
  return (
    <TextAncestorContext.Provider value={contextValue}>
      <T3SourceTextNativeComponent allowFontScaling selectable {...props} style={flattenedStyle}>
        {runs}
      </T3SourceTextNativeComponent>
    </TextAncestorContext.Provider>
  );
}