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.

170 lines (169 loc) 4.93 kB
import { actionSheetEventManager } from "./eventmanager"; import { providerRegistryStack, sheetsRegistry } from "./provider"; let baseZindex = 999; const ids = []; const refs = {}; function getSheetStack() { return ids.map((id) => { var _a; return { id: id.split(":")[0], context: ((_a = id.split(":")) == null ? void 0 : _a[1]) || "global" }; }); } function isRenderedOnTop(id, context) { return context ? ids[ids.length - 1] === `${id}:${context}` : ids[ids.length - 1].startsWith(id); } function setBaseZIndexForActionSheets(zIndex) { baseZindex = zIndex; } function getZIndexFromStack(id, context) { const index = ids.indexOf(`${id}:${context}`); if (index > -1) { return baseZindex + index + 1; } return baseZindex; } class _SheetManager { context(options) { if (!options) options = {}; if (!(options == null ? void 0 : options.context)) { for (const context of providerRegistryStack.slice().reverse()) { if (context.startsWith("$$-auto") && !context.includes(options == null ? void 0 : options.id)) { options.context = context; break; } } } return options.context; } /** * Show the ActionSheet with an id. * * @param id id of the ActionSheet to show * @param options */ async show(id, options) { return new Promise((resolve) => { const currentContext = this.context({ ...options, id }); const handler = (data, context = "global") => { var _a; if (context !== "global" && currentContext && currentContext !== context) return; (_a = options == null ? void 0 : options.onClose) == null ? void 0 : _a.call(options, data); sub == null ? void 0 : sub.unsubscribe(); resolve(data); }; const sub = actionSheetEventManager.subscribe(`onclose_${id}`, handler); let isRegisteredWithSheetProvider = false; for (const ctx in sheetsRegistry) { for (const _id in sheetsRegistry[ctx]) { if (_id === id) { isRegisteredWithSheetProvider = true; } } } actionSheetEventManager.publish( isRegisteredWithSheetProvider ? `show_wrap_${id}` : `show_${id}`, options == null ? void 0 : options.payload, currentContext || "global" ); }); } /** * An async hide function. This is useful when you want to show one ActionSheet after closing another. * * @param id id of the ActionSheet to show * @param data */ async hide(id, options) { const currentContext = this.context({ ...options, id }); return new Promise((resolve) => { let isRegisteredWithSheetProvider = false; for (const _id of ids) { if (_id === `${id}:${currentContext}`) { isRegisteredWithSheetProvider = true; break; } } const hideHandler = (data, context = "global") => { if (context !== "global" && currentContext && currentContext !== context) return; sub == null ? void 0 : sub.unsubscribe(); resolve(data); }; const sub = actionSheetEventManager.subscribe( `onclose_${id}`, hideHandler ); actionSheetEventManager.publish( isRegisteredWithSheetProvider ? `hide_wrap_${id}` : `hide_${id}`, options == null ? void 0 : options.payload, !isRegisteredWithSheetProvider ? "global" : currentContext ); }); } /** * Hide all the opened ActionSheets. * * @param id Hide all sheets for the specific id. */ hideAll(id) { ids.forEach((_id) => { var _a; if (id && !_id.startsWith(id)) return; actionSheetEventManager.publish(`hide_${(_a = _id.split(":")) == null ? void 0 : _a[0]}`); }); } registerRef = (id, context, instance) => { refs[`${id}:${context}`] = instance; }; /** * * Get internal ref of a sheet by the given id. * * @param id Id of the sheet * @param context Context in which the sheet is rendered. Normally this function returns the top most rendered sheet ref automatically. */ get = (id, context) => { if (!context) { for (const ctx of providerRegistryStack.slice().reverse()) { for (const _id in sheetsRegistry[ctx]) { if (_id === id) { context = ctx; break; } } } } return refs[`${id}:${context}`]; }; add = (id, context) => { if (ids.indexOf(id) < 0) { ids[ids.length] = `${id}:${context}`; } }; remove = (id, context) => { if (ids.indexOf(`${id}:${context}`) > -1) { ids.splice(ids.indexOf(`${id}:${context}`)); } }; } const SheetManager = new _SheetManager(); export { SheetManager, getSheetStack, getZIndexFromStack, isRenderedOnTop, setBaseZIndexForActionSheets }; //# sourceMappingURL=sheetmanager.js.map