UNPKG

@fruits-chain/react-native-xiaoshu

Version:
108 lines (107 loc) • 3.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _index = require("../helpers/index.js"); var _index2 = _interopRequireDefault(require("../theme/index.js")); var _style = require("./style.js"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } const PETAL_COUNT = 8; const PETALS = new Array(PETAL_COUNT).fill(0); const A_OPACITY = 1 / PETAL_COUNT; const A_ROTATE = 360 / PETAL_COUNT; const useLoop = (AnimatedValue, initValue, config) => { (0, _react.useEffect)(() => { const spinAnimation = _reactNative.Animated.timing(AnimatedValue, { toValue: config.toValue, duration: config.duration, easing: config.easing, useNativeDriver: true }); _reactNative.Animated.loop(spinAnimation).start(); }, [AnimatedValue, initValue, config.duration, config.toValue, config.easing]); }; const Spinner = ({ theme, size, color, ...restProps }) => { const [CV] = _index2.default.useStyle({ varCreator: _style.varCreator, theme }); const AnimatedSpinnerValue = (0, _react.useRef)(new _reactNative.Animated.Value(0)).current; size = (0, _index.getDefaultValue)(size, CV.loading_icon_size); color = (0, _index.getDefaultValue)(color, CV.loading_icon_color); useLoop(AnimatedSpinnerValue, 0, { toValue: 1, duration: CV.loading_icon_animation_duration, easing: _reactNative.Easing.linear }); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Animated.View, { ...restProps, style: [STYLES.icon, { width: size, height: size, transform: [{ rotateZ: AnimatedSpinnerValue.interpolate({ inputRange: [0, 1], outputRange: ['0deg', '360deg'] }) }] }, restProps.style], children: PETALS.map((_, i) => { return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, { style: _reactNative.StyleSheet.flatten([STYLES.petal, { opacity: A_OPACITY * (i + 1), transform: [{ rotate: `${A_ROTATE * i}deg` }] }]), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, { style: [STYLES.inner, { backgroundColor: color }] }) }, i); }) }); }; const STYLES = _reactNative.StyleSheet.create({ icon: { justifyContent: 'center', alignItems: 'center', position: 'relative' // backgroundColor: '#999', // to test ui // transform: [ // { // rotate: '-90deg', // }, // ], }, petal: { position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center' // backgroundColor: '#f30', // to test ui }, inner: { width: 2, height: '30%' // 高版本 React Native Android 端会出现奇怪的截断 // borderRadius: 1, // backgroundColor: '#000', } }); var _default = exports.default = /*#__PURE__*/(0, _react.memo)(Spinner); //# sourceMappingURL=loading-spinner.js.map