UNPKG

@bottom-sheet/react-hooks

Version:

The hooks that power `@bottom-sheet/react-spring`

127 lines (116 loc) 5.33 kB
var $dYZEH$bottomsheetstatemachine = require("@bottom-sheet/state-machine"); var $dYZEH$react = require("react"); var $dYZEH$usesyncexternalstoreshim = require("use-sync-external-store/shim"); var $dYZEH$xstate = require("xstate"); function $parcel$export(e, n, v, s) { Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); } $parcel$export(module.exports, "useBottomSheetMachine", () => $882b6d93070905b3$export$a93bae334d1de14f); function $882b6d93070905b3$var$createStore({ initialHeight: initialHeight = (0, $dYZEH$bottomsheetstatemachine.defaultInitialHeight) , snapPoints: snapPoints = (0, $dYZEH$bottomsheetstatemachine.defaultSnapPoints) , unstable__requestAnimationFrame: unstable__requestAnimationFrame = false } = {}) { console.debug("createStore"); const service = (0, $dYZEH$xstate.interpret)((0, $dYZEH$bottomsheetstatemachine.BottomSheetMachine).withConfig({ actions: { setInitialHeight: (0, $dYZEH$bottomsheetstatemachine.assignInitialHeight)(typeof initialHeight === "function" ? initialHeight : ()=>initialHeight), setSnapPoints: (0, $dYZEH$bottomsheetstatemachine.assignSnapPoints)(snapPoints) } })); let snapshot = service.initialState; // transient is updated more frequently than the snapshot, outside of react render cycles let transient = snapshot; let rAF = 0; return { subscribe: (onStoreChange)=>{ console.debug("store.subscribe"); service.onTransition((state)=>{ // @TODO: flesh out the logic for when to notify react of state changes or not (as state updates can be expensive and we should be transient when possible) // @TODO: put updateSnapshot actions in the state machine as declared events // for now just re-render on every change and map out events in userland before abstracting them to the state machine console.groupCollapsed("service.onTransition"); transient = state; console.log(state.value, state.context); if (state.changed) { if (unstable__requestAnimationFrame) { cancelAnimationFrame(rAF); rAF = requestAnimationFrame(()=>{ console.group("onStoreChange"); console.log(state.value, state.context); transient = snapshot = state; onStoreChange(); console.groupEnd(); }); } else { console.group("onStoreChange"); console.log(state.value, state.context); transient = snapshot = state; onStoreChange(); console.groupEnd(); } } console.groupEnd(); }); console.debug("service.start"); service.start(); // return () => void service.stop() return ()=>{ console.debug("service.stop"); service.stop(); }; }, getSnapshot: ()=>snapshot, getTransientSnapshot: ()=>transient, dispatch (event) { return service.send(event); } }; } const $882b6d93070905b3$export$a93bae334d1de14f = function useBottomSheetMachine(props = {}) { const [store] = (0, $dYZEH$react.useState)(()=>$882b6d93070905b3$var$createStore(props)); /* // useState lets us create the store exactly once, which is a guarantee that useMemo doesn't provide const [store] = useState(() => { const service = interpret(BottomSheetMachine) const matches: typeof service.state.matches = (parentStateValue) => service.initialized ? service.state.matches(parentStateValue) : service.initialState.matches(parentStateValue) return { subscribe: (onStoreChange: () => void) => { console.log('subscribe called!') service.onTransition((state) => { // @TODO: flesh out the logic for when to notify react of state changes or not (as state updates can be expensive and we should be transient when possible) if (state.changed) { onStoreChange() } }) service.start() return () => void service.stop() }, getInitialized() { return service.initialized }, getSnapshot: () => service.initialized ? service.state : service.initialState, matches, dispatch(event: BottomSheetEvent) { return service.send(event) }, } }) // */ const state = (0, $dYZEH$usesyncexternalstoreshim.useSyncExternalStore)(store.subscribe, store.getSnapshot); /* const [inc, tick] = useState(0) useEffect(() => { const unsubscribe = store.subscribe(() => tick((inc) => ++inc)) return () => unsubscribe() }, []) // */ return (0, $dYZEH$react.useMemo)(()=>({ state: state, getTransientSnapshot: store.getTransientSnapshot, dispatch: store.dispatch }), [ state, store.dispatch, store.getTransientSnapshot ]); }; //# sourceMappingURL=index.js.map