UNPKG

@nghinv/react-native-app-tour

Version:
100 lines (96 loc) 2.42 kB
/** * Created by nghinv on Wed Jun 23 2021 * Copyright (c) 2021 nghinv@lumi.biz */ import React, { useCallback } from 'react'; import { useSharedValue } from 'react-native-reanimated'; import { EventRegister } from 'react-native-event-listeners'; import { AppTourContext } from './AppTourContext'; export function useAppTour() { const { nodes, setSceneIndex, options } = React.useContext(AppTourContext); const addNode = useCallback(node => { const nodesValue = nodes.value; const findIndex = nodesValue.findIndex(n => n.id === node.id); if (findIndex !== -1) { nodesValue[findIndex] = node; nodes.value = nodesValue; } else { nodesValue.push(node); nodes.value = nodesValue; } }, [nodes]); return { addNode, nodes, setSceneIndex, debug: options === null || options === void 0 ? void 0 : options.debug }; } export function useEvent() { const addEventListener = useCallback((eventName, callback) => { return EventRegister.addEventListener(eventName, callback); }, []); const removeEventListener = useCallback(id => { if (typeof id === 'string') { return EventRegister.removeEventListener(id); } return false; }, []); const removeAllListeners = useCallback(() => { EventRegister.removeAllListeners(); }, []); const emitEvent = useCallback((eventName, data) => { EventRegister.emit(eventName, data); }, []); return { addEventListener, removeEventListener, removeAllListeners, emitEvent }; } export const useVectorLayout = (x = 0, y = 0, width = 0, height = 0) => { const targetX = useSharedValue(x); const targetY = useSharedValue(y); const targetWidth = useSharedValue(width); const targetHeight = useSharedValue(height); return { x: targetX, y: targetY, width: targetWidth, height: targetHeight }; }; export const useVector = (x = 0, y = 0) => { const targetX = useSharedValue(x); const targetY = useSharedValue(y); return { x: targetX, y: targetY }; }; export const setVectorLayout = (target, { x = 0, y = 0, width = 0, height = 0 }) => { 'worklet'; target.x.value = x; target.y.value = y; target.width.value = width; target.height.value = height; }; export const setVector = (target, { x = 0, y = 0 }) => { 'worklet'; target.x.value = x; target.y.value = y; }; //# sourceMappingURL=hook.js.map