UNPKG

@kirz/react-native-toolkit

Version:

Toolkit to speed up React Native development

187 lines (186 loc) 7.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FadeView = FadeView; var _maskedView = _interopRequireDefault(require("@react-native-masked-view/masked-view")); var _react = _interopRequireWildcard(require("react")); var _reactNativeEasingGradient = require("react-native-easing-gradient"); var _reactNativeLinearGradient = _interopRequireDefault(require("react-native-linear-gradient")); var _View = require("./View"); var _index = require("../index"); 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); } const DEFAULT_FADE = (0, _index.scaleY)(10); function InnerFadeView(_ref) { let { style, sides, fadeDistance, fadeStart, fadeEnd, direction, fadeColor, colors, locations, ...props } = _ref; if (direction === 'vertical' && !(sides !== null && sides !== void 0 && sides.bottom) && !(sides !== null && sides !== void 0 && sides.top)) { return props.children; } if (direction === 'horizontal' && !(sides !== null && sides !== void 0 && sides.left) && !(sides !== null && sides !== void 0 && sides.right)) { return props.children; } const maskElement = direction === 'vertical' ? /*#__PURE__*/_react.default.createElement(_View.View, { style: [style, { position: 'absolute', left: 0, right: 0, bottom: 0, top: 0, pointerEvents: 'none' }] }, ((sides === null || sides === void 0 ? void 0 : sides.top) ?? false) && /*#__PURE__*/_react.default.createElement(_reactNativeLinearGradient.default, { start: { x: 0.5, y: (typeof fadeStart === 'number' ? 0 : fadeStart === null || fadeStart === void 0 ? void 0 : fadeStart.top) ?? 0 }, end: { x: 0.5, y: (typeof fadeEnd === 'number' ? 0 : fadeEnd === null || fadeEnd === void 0 ? void 0 : fadeEnd.top) ?? 1 }, style: { height: (typeof fadeDistance === 'number' ? fadeDistance : fadeDistance === null || fadeDistance === void 0 ? void 0 : fadeDistance.top) ?? DEFAULT_FADE }, colors: colors, locations: [...locations].reverse() }), /*#__PURE__*/_react.default.createElement(_View.View, { style: { flex: 1, ...(!fadeColor && { backgroundColor: 'white' }), marginVertical: -2 } }), ((sides === null || sides === void 0 ? void 0 : sides.bottom) ?? true) && /*#__PURE__*/_react.default.createElement(_reactNativeLinearGradient.default, { start: { x: 0.5, y: (typeof fadeStart === 'number' ? 0 : fadeStart === null || fadeStart === void 0 ? void 0 : fadeStart.bottom) ?? 0 }, end: { x: 0.5, y: (typeof fadeEnd === 'number' ? 0 : fadeEnd === null || fadeEnd === void 0 ? void 0 : fadeEnd.bottom) ?? 1 }, style: { height: (typeof fadeDistance === 'number' ? fadeDistance : fadeDistance === null || fadeDistance === void 0 ? void 0 : fadeDistance.bottom) ?? DEFAULT_FADE }, colors: colors, locations: locations })) : /*#__PURE__*/_react.default.createElement(_View.View, { style: [style, { flexDirection: 'row', position: 'absolute', left: 0, right: 0, bottom: 0, top: 0, pointerEvents: 'none' }] }, ((sides === null || sides === void 0 ? void 0 : sides.left) ?? false) && /*#__PURE__*/_react.default.createElement(_reactNativeLinearGradient.default, { start: { x: (typeof fadeStart === 'number' ? 0 : fadeStart === null || fadeStart === void 0 ? void 0 : fadeStart.left) ?? 0, y: 0.5 }, end: { x: (typeof fadeEnd === 'number' ? 0 : fadeEnd === null || fadeEnd === void 0 ? void 0 : fadeEnd.left) ?? 1, y: 0.5 }, style: { width: (typeof fadeDistance === 'number' ? fadeDistance : fadeDistance === null || fadeDistance === void 0 ? void 0 : fadeDistance.left) ?? DEFAULT_FADE }, colors: colors, locations: [...locations].reverse() }), /*#__PURE__*/_react.default.createElement(_View.View, { style: { flex: 1, backgroundColor: 'white', marginHorizontal: -2 } }), ((sides === null || sides === void 0 ? void 0 : sides.right) ?? false) && /*#__PURE__*/_react.default.createElement(_reactNativeLinearGradient.default, { start: { x: (typeof fadeStart === 'number' ? 0 : fadeStart === null || fadeStart === void 0 ? void 0 : fadeStart.right) ?? 0, y: 0.5 }, end: { x: (typeof fadeEnd === 'number' ? 0 : fadeEnd === null || fadeEnd === void 0 ? void 0 : fadeEnd.right) ?? 1, y: 0.5 }, style: { width: (typeof fadeDistance === 'number' ? fadeDistance : fadeDistance === null || fadeDistance === void 0 ? void 0 : fadeDistance.right) ?? DEFAULT_FADE }, colors: colors, locations: locations })); if (fadeColor) { return /*#__PURE__*/_react.default.createElement(_View.View, _extends({}, props, { style: style }), props.children, maskElement); } return /*#__PURE__*/_react.default.createElement(_maskedView.default, _extends({}, props, { style: style, maskElement: maskElement })); } function FadeView(_ref2) { let { sides = { bottom: true }, fadeDistance, fadeColor, ...props } = _ref2; const theme = (0, _index.useTheme)(); const { colors, locations } = (0, _react.useMemo)(() => { const { colors, locations } = (0, _reactNativeEasingGradient.easeGradient)({ colorStops: { 0: { color: fadeColor ? theme.parseColor(fadeColor).fade(1).hexa() : 'white' }, 1: { color: fadeColor ?? 'transparent' } } }); return { colors, locations }; }, [fadeColor]); return /*#__PURE__*/_react.default.createElement(InnerFadeView, _extends({ sides: sides, fadeDistance: fadeDistance }, props, { direction: "vertical", fadeColor: fadeColor, colors: colors, locations: locations }), /*#__PURE__*/_react.default.createElement(InnerFadeView, _extends({ sides: sides, fadeDistance: fadeDistance }, props, { direction: "horizontal", fadeColor: fadeColor, colors: colors, locations: locations }))); } //# sourceMappingURL=FadeView.js.map