UNPKG

@kirz/react-native-toolkit

Version:

Toolkit to speed up React Native development

81 lines (79 loc) 3.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppSplashScreen = AppSplashScreen; exports.SplashScreenContext = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _reactNativeBootsplash = require("react-native-bootsplash"); 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; } const SplashScreenContext = /*#__PURE__*/(0, _react.createContext)({}); exports.SplashScreenContext = SplashScreenContext; function AppSplashScreen(_ref) { let { visible, SplashScreen, children, hideManually, fadeDuration = 220 } = _ref; const [isSplashScreenVisible, setIsSplashScreenVisible] = (0, _react.useState)(true); const [isAnimationFinished, setIsAnimationFinished] = (0, _react.useState)(false); const childrenRenderAwaiter = (0, _react.useRef)(new _index.ControlledPromise()); const opacityAnim = (0, _react.useRef)(new _reactNative.Animated.Value(1)).current; const splashScreenContextData = (0, _react.useMemo)(() => ({ hide: () => setIsSplashScreenVisible(false) }), []); (0, _react.useEffect)(() => { if (visible || hideManually && isSplashScreenVisible) { return; } if (!SplashScreen) { (0, _reactNativeBootsplash.hide)({ fade: true, duration: fadeDuration }); return; } _reactNative.Animated.timing(opacityAnim, { toValue: 0, duration: fadeDuration, useNativeDriver: true }).start(() => { setIsAnimationFinished(true); }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [visible, isSplashScreenVisible, hideManually]); (0, _react.useEffect)(() => { (async () => { if (SplashScreen) { await childrenRenderAwaiter.current.wait(); (0, _reactNativeBootsplash.hide)({ fade: true }); } })(); }, [SplashScreen]); if (!SplashScreen) { return visible ? null : children; } return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, !visible && /*#__PURE__*/_react.default.createElement(SplashScreenContext.Provider, { value: splashScreenContextData }, children), !isAnimationFinished && /*#__PURE__*/_react.default.createElement(_reactNative.Animated.View, { onLayout: () => { childrenRenderAwaiter.current.resolve(); }, style: [{ position: 'absolute', top: 0, left: 0, width: '100%', height: '100%', opacity: opacityAnim }] }, SplashScreen)); } //# sourceMappingURL=AppSplashScreen.js.map