import * as React from 'react'; import { I18nManager, StyleProp, StyleSheet, Text as NativeText, TextStyle, } from 'react-native'; import { useInternalTheme } from '../../core/theming'; import type { ThemeProp } from '../../types'; import { forwardRef } from '../../utils/forwardRef'; import type { VariantProp } from './types'; export type Props = React.ComponentProps & { /** * @supported Available in v5.x with theme version 3 * * Variant defines appropriate text styles for type role and its size. * Available variants: * * Display: `displayLarge`, `displayMedium`, `displaySmall` * * Headline: `headlineLarge`, `headlineMedium`, `headlineSmall` * * Title: `titleLarge`, `titleMedium`, `titleSmall` * * Label: `labelLarge`, `labelMedium`, `labelSmall` * * Body: `bodyLarge`, `bodyMedium`, `bodySmall` */ variant?: VariantProp; children: React.ReactNode; theme?: ThemeProp; style?: StyleProp; }; export type TextRef = React.ForwardedRef<{ setNativeProps(args: Object): void; }>; // @component-group Typography /** * Typography component showing styles complied with passed `variant` prop and supported by the type system. * *
* *
* * ## Usage * ```js * import * as React from 'react'; * import { Text } from 'react-native-paper'; * * const MyComponent = () => ( * <> * Display Large * Display Medium * Display small * * Headline Large * Headline Medium * Headline Small * * Title Large * Title Medium * Title Small * * Body Large * Body Medium * Body Small * * Label Large * Label Medium * Label Small * * ); * * export default MyComponent; * ``` * * @extends Text props https://reactnative.dev/docs/text#props */ const Text = ( { style, variant, theme: initialTheme, ...rest }: Props, ref: TextRef ) => { const root = React.useRef(null); // FIXME: destructure it in TS 4.6+ const theme = useInternalTheme(initialTheme); const writingDirection = I18nManager.getConstants().isRTL ? 'rtl' : 'ltr'; React.useImperativeHandle(ref, () => ({ setNativeProps: (args: Object) => root.current?.setNativeProps(args), })); if (theme.isV3 && variant) { const font = theme.fonts[variant]; if (typeof font !== 'object') { throw new Error( `Variant ${variant} was not provided properly. Valid variants are ${Object.keys( theme.fonts ).join(', ')}.` ); } return ( ); } else { const font = theme.isV3 ? theme.fonts.default : theme.fonts?.regular; const textStyle = { ...font, color: theme.isV3 ? theme.colors?.onSurface : theme.colors.text, }; return ( ); } }; const styles = StyleSheet.create({ text: { textAlign: 'left', }, }); type TextComponent = ( props: Props & { ref?: React.RefObject } ) => JSX.Element; const Component = forwardRef(Text) as TextComponent; export const customText = () => Component as unknown as TextComponent; export default Component;