@kirz/react-native-toolkit
Version:
Toolkit to speed up React Native development
106 lines (105 loc) • 4.98 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ViewInsetShadow = ViewInsetShadow;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _reactNativeViewShot = _interopRequireDefault(require("react-native-view-shot"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 _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 cachedShadows = {};
function ViewInsetShadow(_ref) {
let {
offsetX,
offsetY,
color = 'black',
blurRadius = 10,
wrapperProps,
innerBorderRadiusStyle,
...props
} = _ref;
const viewShotRef = (0, _react.useRef)();
const onLayoutCalled = (0, _react.useRef)(false);
const invisibleBorderWidth = 20;
const borderWidth = {
x: Math.max(invisibleBorderWidth, Math.abs(offsetX ?? 0)) + 5,
y: Math.max(invisibleBorderWidth, Math.abs(offsetY ?? 0)) + 5
};
const margin = {
x: borderWidth.x,
y: borderWidth.x
};
const cacheKey = `${color}_${blurRadius}_${offsetX}_${offsetY}_${JSON.stringify((wrapperProps === null || wrapperProps === void 0 ? void 0 : wrapperProps.style) ?? {})}_${JSON.stringify(props.style)}_${JSON.stringify(margin)}`;
const [imageUri, setImageUri] = (0, _react.useState)(cachedShadows[cacheKey] ?? null);
const redrawShadow = async () => {
var _viewShotRef$current;
if (!((_viewShotRef$current = viewShotRef.current) !== null && _viewShotRef$current !== void 0 && _viewShotRef$current.capture) || cachedShadows[cacheKey]) {
return;
}
onLayoutCalled.current = true;
const result = await viewShotRef.current.capture();
cachedShadows[cacheKey] = result;
setImageUri(result);
};
(0, _react.useEffect)(() => {
if (!onLayoutCalled.current || cachedShadows[cacheKey]) {
return;
}
redrawShadow();
}, [cacheKey]);
return /*#__PURE__*/_react.default.createElement(_reactNative.View, wrapperProps, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
style: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0
}
}, /*#__PURE__*/_react.default.createElement(_reactNativeViewShot.default, {
ref: viewShotRef,
options: {
result: 'tmpfile'
},
style: {
position: 'absolute',
left: -margin.x,
right: -margin.x,
bottom: -margin.y,
top: -margin.y
}
}, /*#__PURE__*/_react.default.createElement(_reactNative.View, _extends({}, props, {
style: [props.style, {
borderColor: color,
borderLeftWidth: borderWidth.x,
borderRightWidth: borderWidth.x,
borderTopWidth: borderWidth.y,
borderBottomWidth: borderWidth.y,
borderBottomLeftRadius: innerBorderRadiusStyle.borderBottomLeftRadius + borderWidth.x,
borderBottomRightRadius: innerBorderRadiusStyle.borderBottomRightRadius + borderWidth.x,
borderTopLeftRadius: innerBorderRadiusStyle.borderTopLeftRadius + borderWidth.x,
borderTopRightRadius: innerBorderRadiusStyle.borderTopRightRadius + borderWidth.x
}, {
left: offsetX,
top: offsetY,
height: '100%',
width: '100%'
}],
onLayout: redrawShadow
})))), imageUri && /*#__PURE__*/_react.default.createElement(_reactNative.Image, {
source: {
uri: imageUri
},
blurRadius: blurRadius,
style: {
position: 'absolute',
left: -margin.x,
right: -margin.x,
bottom: -margin.y,
top: -margin.y
}
}));
}
//# sourceMappingURL=ViewInsetShadow.js.map