@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.
197 lines (196 loc) • 6.3 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var sheetmanager_exports = {};
__export(sheetmanager_exports, {
SheetManager: () => SheetManager,
getSheetStack: () => getSheetStack,
getZIndexFromStack: () => getZIndexFromStack,
isRenderedOnTop: () => isRenderedOnTop,
setBaseZIndexForActionSheets: () => setBaseZIndexForActionSheets
});
module.exports = __toCommonJS(sheetmanager_exports);
var import_eventmanager = require("./eventmanager");
var import_provider = require("./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 import_provider.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 = import_eventmanager.actionSheetEventManager.subscribe(`onclose_${id}`, handler);
let isRegisteredWithSheetProvider = false;
for (const ctx in import_provider.sheetsRegistry) {
for (const _id in import_provider.sheetsRegistry[ctx]) {
if (_id === id) {
isRegisteredWithSheetProvider = true;
}
}
}
import_eventmanager.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 = import_eventmanager.actionSheetEventManager.subscribe(
`onclose_${id}`,
hideHandler
);
import_eventmanager.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;
import_eventmanager.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 import_provider.providerRegistryStack.slice().reverse()) {
for (const _id in import_provider.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();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SheetManager,
getSheetStack,
getZIndexFromStack,
isRenderedOnTop,
setBaseZIndexForActionSheets
});
//# sourceMappingURL=sheetmanager.js.map