@kirz/react-native-toolkit
Version:
Toolkit to speed up React Native development
114 lines (111 loc) • 5.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Text = Text;
var _maskedView = _interopRequireDefault(require("@react-native-masked-view/masked-view"));
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _index = require("../index");
var _getFontWeightName = require("../utils/getFontWeightName");
var _styles = require("../utils/styles");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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); }
function Text(_ref) {
let {
style,
color: initialColor,
fontSize: initialFontSize,
fontWeight: initialFontWeight,
fontFamily: initialFontFamily,
...props
} = _ref;
const theme = (0, _index.useTheme)();
const {
textStyle,
flattenStyle: {
gradient: gradientProp,
...flattenStyle
}
} = (0, _react.useMemo)(() => {
return Object.entries(_reactNative.StyleSheet.flatten(style) ?? {}).reduce((acc, x) => {
if (_styles.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 = (0, _react.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)) && (0, _getFontWeightName.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.default.createElement(_reactNative.Text, _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.default.createElement(_maskedView.default, {
style: flattenStyle,
maskElement: renderTextNode({})
}, /*#__PURE__*/_react.default.createElement(_index.View, {
style: [{
backgroundGradient: gradients
}]
}, renderTextNode({
style: {
opacity: 0
}
})));
}
//# sourceMappingURL=Text.js.map