UNPKG

@kirz/react-native-toolkit

Version:

Toolkit to speed up React Native development

71 lines (70 loc) 2.2 kB
import React, { createContext, useEffect, useMemo, useRef, useState } from 'react'; import { Animated } from 'react-native'; import { hide as hideNativeSplash } from 'react-native-bootsplash'; import { ControlledPromise } from '../index'; export const SplashScreenContext = /*#__PURE__*/createContext({}); export function AppSplashScreen(_ref) { let { visible, SplashScreen, children, hideManually, fadeDuration = 220 } = _ref; const [isSplashScreenVisible, setIsSplashScreenVisible] = useState(true); const [isAnimationFinished, setIsAnimationFinished] = useState(false); const childrenRenderAwaiter = useRef(new ControlledPromise()); const opacityAnim = useRef(new Animated.Value(1)).current; const splashScreenContextData = useMemo(() => ({ hide: () => setIsSplashScreenVisible(false) }), []); useEffect(() => { if (visible || hideManually && isSplashScreenVisible) { return; } if (!SplashScreen) { hideNativeSplash({ fade: true, duration: fadeDuration }); return; } Animated.timing(opacityAnim, { toValue: 0, duration: fadeDuration, useNativeDriver: true }).start(() => { setIsAnimationFinished(true); }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [visible, isSplashScreenVisible, hideManually]); useEffect(() => { (async () => { if (SplashScreen) { await childrenRenderAwaiter.current.wait(); hideNativeSplash({ fade: true }); } })(); }, [SplashScreen]); if (!SplashScreen) { return visible ? null : children; } return /*#__PURE__*/React.createElement(React.Fragment, null, !visible && /*#__PURE__*/React.createElement(SplashScreenContext.Provider, { value: splashScreenContextData }, children), !isAnimationFinished && /*#__PURE__*/React.createElement(Animated.View, { onLayout: () => { childrenRenderAwaiter.current.resolve(); }, style: [{ position: 'absolute', top: 0, left: 0, width: '100%', height: '100%', opacity: opacityAnim }] }, SplashScreen)); } //# sourceMappingURL=AppSplashScreen.js.map