UNPKG

@crossed/sheet

Version:

A Cross Platform(Android & iOS) ActionSheet with a robust and flexible api, native performance and zero dependency code for react native. Create anything you want inside ActionSheet.

165 lines (164 loc) 6.78 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var provider_exports = {}; __export(provider_exports, { SheetProvider: () => SheetProvider, SheetRefContext: () => SheetRefContext, providerRegistryStack: () => providerRegistryStack, registerSheet: () => registerSheet, sheetsRegistry: () => sheetsRegistry, useProviderContext: () => useProviderContext, useSheetIDContext: () => useSheetIDContext, useSheetPayload: () => useSheetPayload, useSheetRef: () => useSheetRef }); module.exports = __toCommonJS(provider_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_react = __toESM(require("react")); var import_eventmanager = require("./eventmanager"); const providerRegistryStack = []; const sheetsRegistry = { global: {} }; function registerSheet(id, Sheet, ...contexts) { if (!id || !Sheet) return; if (!contexts || contexts.length === 0) contexts = ["global"]; for (const context of contexts) { const registry = !sheetsRegistry[context] ? sheetsRegistry[context] = {} : sheetsRegistry[context]; registry[id] = Sheet; import_eventmanager.actionSheetEventManager.publish(`${context}-on-register`); } } function SheetProvider({ context = "global", children }) { const [, forceUpdate] = (0, import_react.useReducer)((x) => x + 1, 0); const sheetIds = Object.keys( sheetsRegistry[context] || sheetsRegistry.global || {} ); const onRegister = import_react.default.useCallback(() => { forceUpdate(); }, [forceUpdate]); (0, import_react.useEffect)(() => { providerRegistryStack.indexOf(context) > -1 ? providerRegistryStack.indexOf(context) : providerRegistryStack.push(context) - 1; const unsub = import_eventmanager.actionSheetEventManager.subscribe( `${context}-on-register`, onRegister ); return () => { providerRegistryStack.splice(providerRegistryStack.indexOf(context), 1); unsub == null ? void 0 : unsub.unsubscribe(); }; }, [context, onRegister]); const renderSheet = (sheetId) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RenderSheet, { id: sheetId, context }, sheetId); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ children, sheetIds.map(renderSheet) ] }); } const ProviderContext = (0, import_react.createContext)("global"); const SheetIDContext = (0, import_react.createContext)(void 0); const SheetRefContext = (0, import_react.createContext)( {} ); const SheetPayloadContext = (0, import_react.createContext)(void 0); const useProviderContext = () => (0, import_react.useContext)(ProviderContext); const useSheetIDContext = () => (0, import_react.useContext)(SheetIDContext); function useSheetRef(_id) { return (0, import_react.useContext)(SheetRefContext); } function useSheetPayload(_id) { return (0, import_react.useContext)(SheetPayloadContext); } const RenderSheet = ({ id, context }) => { var _a, _b; const [payload, setPayload] = (0, import_react.useState)(); const [visible, setVisible] = (0, import_react.useState)(false); const ref = (0, import_react.useRef)(null); const Sheet = context.startsWith("$$-auto-") ? (_a = sheetsRegistry == null ? void 0 : sheetsRegistry.global) == null ? void 0 : _a[id] : sheetsRegistry[context] ? (_b = sheetsRegistry[context]) == null ? void 0 : _b[id] : void 0; const onShow = import_react.default.useCallback( (data, ctx = "global") => { if (ctx !== context) return; setPayload(data); setVisible(true); }, [context] ); const onClose = import_react.default.useCallback( (_data, ctx = "global") => { if (context !== ctx) return; setVisible(false); setTimeout(() => { setPayload(void 0); }, 1); }, [context] ); const onHide = import_react.default.useCallback( (data, ctx = "global") => { import_eventmanager.actionSheetEventManager.publish(`hide_${id}`, data, ctx); }, [id] ); (0, import_react.useEffect)(() => { if (visible) { import_eventmanager.actionSheetEventManager.publish(`show_${id}`, payload, context); } }, [context, id, payload, visible]); (0, import_react.useEffect)(() => { const subs = [ import_eventmanager.actionSheetEventManager.subscribe(`show_wrap_${id}`, onShow), import_eventmanager.actionSheetEventManager.subscribe(`onclose_${id}`, onClose), import_eventmanager.actionSheetEventManager.subscribe(`hide_wrap_${id}`, onHide) ]; return () => { subs.forEach((s) => s.unsubscribe()); }; }, [id, context, onShow, onHide, onClose]); if (!Sheet) return null; return !visible ? null : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ProviderContext.Provider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SheetIDContext.Provider, { value: id, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SheetRefContext.Provider, { value: ref, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SheetPayloadContext.Provider, { value: payload, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Sheet, { sheetId: id, payload }) }) }) }) }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SheetProvider, SheetRefContext, providerRegistryStack, registerSheet, sheetsRegistry, useProviderContext, useSheetIDContext, useSheetPayload, useSheetRef }); //# sourceMappingURL=provider.js.map