UNPKG

@kirz/react-native-toolkit

Version:

Toolkit to speed up React Native development

143 lines (142 loc) 8.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.View = View; var _maskedView = _interopRequireDefault(require("@react-native-masked-view/masked-view")); var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _BlurView = require("./BlurView"); var _ViewInsetShadow = require("./ViewInsetShadow"); var _ViewSkeleton = require("./ViewSkeleton"); var _renderGradient = require("../utils/renderGradient"); 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 View(_ref) { let { style, children, skeleton, skeletonStyle, ...props } = _ref; const { backgroundGradient: backgroundGradientProp, borderGradient: borderGradientProp, backgroundBlur, backgroundBlurProps, insetShadow, ...flattenStyle } = (0, _react.useMemo)(() => _reactNative.StyleSheet.flatten(style) ?? {}, [style]); const flattenSkeletonStyle = (0, _react.useMemo)(() => _reactNative.StyleSheet.flatten(skeletonStyle) ?? {}, [skeletonStyle]); const backgroundGradients = !backgroundGradientProp ? [] : typeof backgroundGradientProp === 'function' ? [backgroundGradientProp] : 'length' in backgroundGradientProp ? backgroundGradientProp : [backgroundGradientProp]; const borderGradients = !borderGradientProp ? [] : typeof borderGradientProp === 'function' ? [borderGradientProp] : 'length' in borderGradientProp ? borderGradientProp : [borderGradientProp]; const baseStyle = { position: 'absolute', left: 0, top: 0, right: 0, bottom: 0 }; const borderRadiusStyle = { borderBottomLeftRadius: flattenStyle.borderBottomLeftRadius ?? flattenStyle.borderRadius ?? 0, borderBottomRightRadius: flattenStyle.borderBottomRightRadius ?? flattenStyle.borderRadius ?? 0, borderTopLeftRadius: flattenStyle.borderTopLeftRadius ?? flattenStyle.borderRadius ?? 0, borderTopRightRadius: flattenStyle.borderTopRightRadius ?? flattenStyle.borderRadius ?? 0 }; const borderWidthStyle = { borderLeftWidth: flattenStyle.borderLeftWidth ?? flattenStyle.borderWidth ?? 0, borderRightWidth: flattenStyle.borderRightWidth ?? flattenStyle.borderWidth ?? 0, borderBottomWidth: flattenStyle.borderBottomWidth ?? flattenStyle.borderWidth ?? 0, borderTopWidth: flattenStyle.borderTopWidth ?? flattenStyle.borderWidth ?? 0 }; const innerBorderRadiusStyle = insetShadow && { borderBottomLeftRadius: borderRadiusStyle.borderBottomLeftRadius - (Math.max(borderWidthStyle.borderLeftWidth, borderWidthStyle.borderBottomWidth) ?? 0), borderBottomRightRadius: borderRadiusStyle.borderBottomRightRadius - (Math.max(borderWidthStyle.borderRightWidth, borderWidthStyle.borderBottomWidth) ?? 0), borderTopLeftRadius: borderRadiusStyle.borderTopLeftRadius - (Math.max(borderWidthStyle.borderLeftWidth, borderWidthStyle.borderTopWidth) ?? 0), borderTopRightRadius: borderRadiusStyle.borderTopRightRadius - (Math.max(borderWidthStyle.borderRightWidth, borderWidthStyle.borderTopWidth) ?? 0) }; const borderColorStyle = { borderLeftColor: flattenStyle.borderLeftColor ?? flattenStyle.borderColor, borderRightColor: flattenStyle.borderRightColor ?? flattenStyle.borderColor, borderTopColor: flattenStyle.borderTopColor ?? flattenStyle.borderColor, borderBottomColor: flattenStyle.borderBottomColor ?? flattenStyle.borderColor }; const borderWidthNegativeStyle = { marginLeft: -(flattenStyle.borderLeftWidth ?? flattenStyle.borderWidth ?? 0), marginRight: -(flattenStyle.borderRightWidth ?? flattenStyle.borderWidth ?? 0), marginTop: -(flattenStyle.borderTopWidth ?? flattenStyle.borderWidth ?? 0), marginBottom: -(flattenStyle.borderBottomWidth ?? flattenStyle.borderWidth ?? 0) }; const renderAbsolute = (content, borderAdjust, isSkeletonView) => { return /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: [borderAdjust ? { position: 'absolute', left: borderWidthStyle.borderLeftWidth > 0 ? 1 : 0, top: borderWidthStyle.borderTopWidth > 0 ? 1 : 0, right: borderWidthStyle.borderRightWidth > 0 ? 1 : 0, bottom: borderWidthStyle.borderBottomWidth > 0 ? 1 : 0 } : baseStyle, ...(!isSkeletonView && skeleton ? [{ opacity: 0 }] : [])], pointerEvents: "none" }, /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: [{ flex: 1, overflow: 'hidden' }, borderRadiusStyle, borderWidthNegativeStyle] }, content)); }; return /*#__PURE__*/_react.default.createElement(_reactNative.View, _extends({}, props, { style: [flattenStyle] }), backgroundGradients.length > 0 && renderAbsolute((0, _renderGradient.renderGradient)(backgroundGradients), true), backgroundBlur && renderAbsolute( /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: [baseStyle, borderColorStyle, borderRadiusStyle, borderWidthStyle, { borderColor: 'transparent' }] }, /*#__PURE__*/_react.default.createElement(_BlurView.BlurView, _extends({}, backgroundBlurProps, { style: [{ flex: 1 }, backgroundBlurProps === null || backgroundBlurProps === void 0 ? void 0 : backgroundBlurProps.style], blurType: (backgroundBlurProps === null || backgroundBlurProps === void 0 ? void 0 : backgroundBlurProps.blurType) ?? backgroundBlur })))), insetShadow && /*#__PURE__*/_react.default.createElement(_ViewInsetShadow.ViewInsetShadow, _extends({}, insetShadow, { wrapperProps: { style: [{ position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, overflow: 'hidden' }, innerBorderRadiusStyle] }, style: [baseStyle], innerBorderRadiusStyle: innerBorderRadiusStyle })), backgroundGradients.length > 0 && !borderGradients.length && renderAbsolute( /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: [baseStyle, borderColorStyle, borderRadiusStyle, borderWidthStyle] })), borderGradients.length > 0 && renderAbsolute( /*#__PURE__*/_react.default.createElement(_maskedView.default, { style: baseStyle, maskElement: /*#__PURE__*/_react.default.createElement(View, { style: [baseStyle, borderRadiusStyle, borderWidthStyle] }) }, (0, _renderGradient.renderGradient)(borderGradients))), skeleton ? /*#__PURE__*/_react.default.createElement(View, { style: { opacity: 0 } }, children) : children, skeleton && /*#__PURE__*/_react.default.createElement(_ViewSkeleton.ViewSkeleton, { style: [{ position: 'absolute', left: -1, top: -1, right: -1, bottom: -1 }, skeletonStyle, { borderBottomLeftRadius: flattenSkeletonStyle.borderBottomLeftRadius ?? flattenStyle.borderBottomLeftRadius ?? flattenSkeletonStyle.borderRadius ?? flattenStyle.borderRadius ?? 0, borderBottomRightRadius: flattenSkeletonStyle.borderBottomRightRadius ?? flattenStyle.borderBottomRightRadius ?? flattenSkeletonStyle.borderRadius ?? flattenStyle.borderRadius ?? 0, borderTopLeftRadius: flattenSkeletonStyle.borderTopLeftRadius ?? flattenStyle.borderTopLeftRadius ?? flattenSkeletonStyle.borderRadius ?? flattenStyle.borderRadius ?? 0, borderTopRightRadius: flattenSkeletonStyle.borderTopRightRadius ?? flattenStyle.borderTopRightRadius ?? flattenSkeletonStyle.borderRadius ?? flattenStyle.borderRadius ?? 0 }] })); } //# sourceMappingURL=View.js.map