@nghinv/react-native-app-tour
Version:
React Native app tour Library
155 lines (128 loc) • 5.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactFastCompare = _interopRequireDefault(require("react-fast-compare"));
var _reactNative = require("react-native");
var _reactNativeReanimated = require("react-native-reanimated");
var _AppTourContext = require("./AppTourContext");
var _hook = require("./hook");
var _MashView = _interopRequireDefault(require("./MashView"));
var _math = require("./math");
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; }
/**
* Created by nghinv on Wed Jun 23 2021
* Copyright (c) 2021 nghinv@lumi.biz
*/
function AppTourView(_, ref) {
const [visible, setVisible] = (0, _react.useState)(false);
const {
nodes,
scenes,
sceneIndex,
options
} = (0, _react.useContext)(_AppTourContext.AppTourContext);
const {
emitEvent
} = (0, _hook.useEvent)();
const progress = (0, _reactNativeReanimated.useSharedValue)(0);
const currentStep = (0, _reactNativeReanimated.useSharedValue)(0);
const backHandler = (0, _react.useRef)();
const handleBackButton = () => {
if (options !== null && options !== void 0 && options.backAndroidToSkip) {
onStop();
}
return true;
};
const open = (0, _react.useCallback)(step => {
var _scenes$sceneIndex;
_reactNative.Keyboard.dismiss();
backHandler.current = _reactNative.BackHandler.addEventListener('hardwareBackPress', handleBackButton);
if (((_scenes$sceneIndex = scenes[sceneIndex]) === null || _scenes$sceneIndex === void 0 ? void 0 : _scenes$sceneIndex.length) > 0) {
setVisible(true);
currentStep.value = step !== null && step !== void 0 ? step : 0;
progress.value = (0, _reactNativeReanimated.withTiming)(1, _math.timingConfig);
const scene = scenes[sceneIndex][currentStep.value];
emitEvent('AppTourEvent', {
name: 'onStart',
step: currentStep.value,
node: nodes.value.find(n => n.id === scene.id),
scene
});
}
}, [sceneIndex, scenes, backHandler]);
const onStop = (0, _react.useCallback)(callback => {
if (backHandler.current) {
backHandler.current.remove();
}
progress.value = (0, _reactNativeReanimated.withTiming)(0, _math.timingConfig, () => {
(0, _reactNativeReanimated.runOnJS)(setVisible)(false);
if (callback) {
(0, _reactNativeReanimated.runOnJS)(callback)();
}
});
}, [backHandler]);
(0, _react.useEffect)(() => {
return () => {
if (backHandler.current) {
backHandler.current.remove();
}
};
}, [backHandler]);
(0, _react.useImperativeHandle)(ref, () => ({
nextStep: () => {
const nextValue = currentStep.value + 1;
const sceneLength = scenes[sceneIndex].length;
if (nextValue <= sceneLength - 1) {
currentStep.value = nextValue;
} else {
onStop();
}
},
preStep: () => {
const preValue = currentStep.value - 1;
if (preValue >= 0) {
currentStep.value = preValue;
} else {
onStop();
}
},
stop: cb => {
onStop(cb);
},
start: step => {
open(step);
},
currentStep: () => {
return currentStep.value;
}
}));
if (!visible) return null;
if (options !== null && options !== void 0 && options.nativeModal) {
return /*#__PURE__*/_react.default.createElement(_reactNative.Modal, {
visible: visible,
transparent: true,
animationType: "none"
}, /*#__PURE__*/_react.default.createElement(_MashView.default, {
onStop: onStop,
open: open,
progress: progress,
currentStep: currentStep,
scene: scenes[sceneIndex]
}));
}
return /*#__PURE__*/_react.default.createElement(_MashView.default, {
onStop: onStop,
open: open,
progress: progress,
currentStep: currentStep,
scene: scenes[sceneIndex]
});
}
var _default = /*#__PURE__*/_react.default.memo( /*#__PURE__*/(0, _react.forwardRef)(AppTourView), _reactFastCompare.default);
exports.default = _default;
//# sourceMappingURL=AppTourView.js.map