@kirz/react-native-toolkit
Version:
Toolkit to speed up React Native development
105 lines (103 loc) • 3.71 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 MaskedView from '@react-native-masked-view/masked-view';
import React, { useMemo } from 'react';
import { StyleSheet, Text as TextBase } from 'react-native';
import { View, useTheme } from '../index';
import { getFontWeightName } from '../utils/getFontWeightName';
import { textStyleKeys } from '../utils/styles';
export function Text(_ref) {
let {
style,
color: initialColor,
fontSize: initialFontSize,
fontWeight: initialFontWeight,
fontFamily: initialFontFamily,
...props
} = _ref;
const theme = useTheme();
const {
textStyle,
flattenStyle: {
gradient: gradientProp,
...flattenStyle
}
} = useMemo(() => {
return Object.entries(StyleSheet.flatten(style) ?? {}).reduce((acc, x) => {
if (textStyleKeys.includes(x[0])) {
return {
flattenStyle: acc.flattenStyle,
textStyle: {
...acc.textStyle,
[x[0]]: x[1]
}
};
}
return {
flattenStyle: {
...acc.flattenStyle,
[x[0]]: x[1]
},
textStyle: acc.textStyle
};
}, {
flattenStyle: {},
textStyle: {}
});
}, [style]);
const gradients = !gradientProp ? [] : typeof gradientProp === 'function' ? [gradientProp] : 'length' in gradientProp ? gradientProp : [gradientProp];
const {
fontFamily: styleFontFamily,
fontWeight: styleFontWeight
} = textStyle;
// @ts-ignore
const variant = theme.typography[props.variant ?? 'default'];
const fontAssetName = useMemo(() => {
const fontFamily = styleFontFamily ?? initialFontFamily;
const fontWeight = styleFontWeight ?? initialFontWeight;
const font = fontFamily ?? (variant === null || variant === void 0 ? void 0 : variant.fontFamily);
if (!font) {
return null;
}
// @ts-ignore
const weights = theme.fonts[font];
const names = (fontWeight ?? (variant === null || variant === void 0 ? void 0 : variant.fontWeight)) && getFontWeightName(fontWeight ?? (variant === null || variant === void 0 ? void 0 : variant.fontWeight));
const name = names ? weights.find(x => names.includes(x.toLowerCase())) : weights[0];
if (!name) {
throw new Error(`Unknown font weight "${fontWeight}" for font family "${font}"`);
}
return `${font}-${name}`;
}, [styleFontWeight, styleFontWeight, theme.fonts, variant, initialFontFamily, initialFontSize, initialFontWeight]);
const renderTextNode = otherProps => {
return /*#__PURE__*/React.createElement(TextBase, _extends({}, props, otherProps, {
allowFontScaling: otherProps.allowFontScaling ?? props.allowFontScaling ?? false,
style: [{
color: initialColor ?? theme.colors.text,
...variant,
fontSize: initialFontSize,
fontWeight: initialFontWeight,
...textStyle,
...(fontAssetName && {
fontFamily: fontAssetName
})
}, otherProps.style]
}));
};
if (!gradients.length) {
return renderTextNode({
style: flattenStyle
});
}
return /*#__PURE__*/React.createElement(MaskedView, {
style: flattenStyle,
maskElement: renderTextNode({})
}, /*#__PURE__*/React.createElement(View, {
style: [{
backgroundGradient: gradients
}]
}, renderTextNode({
style: {
opacity: 0
}
})));
}
//# sourceMappingURL=Text.js.map