react-native-paper
Version:
Material design for React Native
101 lines (100 loc) • 3.72 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import * as React from 'react';
import { I18nManager, StyleSheet, Text as NativeText } from 'react-native';
import { useInternalTheme } from '../../core/theming';
import { forwardRef } from '../../utils/forwardRef';
// @component-group Typography
/**
* Typography component showing styles complied with passed `variant` prop and supported by the type system.
*
* <div class="screenshots">
* <img class="small" src="screenshots/typography.png" />
* </div>
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Text } from 'react-native-paper';
*
* const MyComponent = () => (
* <>
* <Text variant="displayLarge">Display Large</Text>
* <Text variant="displayMedium">Display Medium</Text>
* <Text variant="displaySmall">Display small</Text>
*
* <Text variant="headlineLarge">Headline Large</Text>
* <Text variant="headlineMedium">Headline Medium</Text>
* <Text variant="headlineSmall">Headline Small</Text>
*
* <Text variant="titleLarge">Title Large</Text>
* <Text variant="titleMedium">Title Medium</Text>
* <Text variant="titleSmall">Title Small</Text>
*
* <Text variant="bodyLarge">Body Large</Text>
* <Text variant="bodyMedium">Body Medium</Text>
* <Text variant="bodySmall">Body Small</Text>
*
* <Text variant="labelLarge">Label Large</Text>
* <Text variant="labelMedium">Label Medium</Text>
* <Text variant="labelSmall">Label Small</Text>
* </>
* );
*
* export default MyComponent;
* ```
*
* @extends Text props https://reactnative.dev/docs/text#props
*/
const Text = (_ref, ref) => {
let {
style,
variant,
theme: initialTheme,
...rest
} = _ref;
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 => {
var _root$current;
return (_root$current = root.current) === null || _root$current === void 0 ? void 0 : _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 /*#__PURE__*/React.createElement(NativeText, _extends({
ref: root,
style: [font, styles.text, {
writingDirection,
color: theme.colors.onSurface
}, style]
}, rest));
} else {
var _theme$fonts, _theme$colors;
const font = theme.isV3 ? theme.fonts.default : (_theme$fonts = theme.fonts) === null || _theme$fonts === void 0 ? void 0 : _theme$fonts.regular;
const textStyle = {
...font,
color: theme.isV3 ? (_theme$colors = theme.colors) === null || _theme$colors === void 0 ? void 0 : _theme$colors.onSurface : theme.colors.text
};
return /*#__PURE__*/React.createElement(NativeText, _extends({}, rest, {
ref: root,
style: [styles.text, textStyle, {
writingDirection
}, style]
}));
}
};
const styles = StyleSheet.create({
text: {
textAlign: 'left'
}
});
const Component = forwardRef(Text);
export const customText = () => Component;
export default Component;
//# sourceMappingURL=Text.js.map