UNPKG

react-native-actions-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.

305 lines (304 loc) 14.7 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { actionSheetEventManager } from './eventmanager'; import { providerRegistryStack, sheetsRegistry } from './provider'; var baseZindex = 999; // Array of all the ids of ActionSheets currently rendered in the app. var renderedSheetIds = []; var refs = {}; /** * Get rendered action sheets stack * @returns */ export function getSheetStack() { return renderedSheetIds.map(function (id) { var _a = id.split(':'), sheetId = _a[0], context = _a[1]; return { id: sheetId, context: context || 'global', }; }); } /** * A function that checks whether the action sheet with the given id is rendered on top or not. * @param id * @param context * @returns */ export function isRenderedOnTop(id, context) { return context ? renderedSheetIds[renderedSheetIds.length - 1] === "".concat(id, ":").concat(context) : renderedSheetIds[renderedSheetIds.length - 1].startsWith(id); } /** * Set the base zIndex upon which action sheets will be stacked. Should be called once in the global space. * * Default `baseZIndex` is `999`. * * @param zIndex */ export function setBaseZIndexForActionSheets(zIndex) { baseZindex = zIndex; } /** * Since non modal based sheets are stacked one above the other, they need to have * different zIndex for gestures to work correctly. * @param id * @param context * @returns */ export function getZIndexFromStack(id, context) { var index = renderedSheetIds.indexOf("".concat(id, ":").concat(context)); if (index > -1) { return baseZindex + index + 1; } return baseZindex; } var _SheetManager = /** @class */ (function () { function _SheetManager() { this.registerRef = function (id, context, instance) { refs["".concat(id, ":").concat(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. */ this.get = function (id, context) { if (!context) { for (var _i = 0, _a = renderedSheetIds.reverse(); _i < _a.length; _i++) { var _id = _a[_i]; if (_id.includes("".concat(id, ":"))) { context = _id.split(':')[1]; } } } return refs["".concat(id, ":").concat(context)]; }; this.add = function (id, context) { if (renderedSheetIds.indexOf(id) < 0) { renderedSheetIds[renderedSheetIds.length] = "".concat(id, ":").concat(context || 'global'); } }; this.remove = function (id, context) { if (renderedSheetIds.indexOf("".concat(id, ":").concat(context)) > -1) { renderedSheetIds.splice(renderedSheetIds.indexOf("".concat(id, ":").concat(context || 'global'))); } }; } _SheetManager.prototype.context = function (options) { if (!options) options = {}; if (!(options === null || options === void 0 ? void 0 : options.context)) { // If no context is provided, use to current top most context // to render the sheet. for (var _i = 0, _a = providerRegistryStack.slice().reverse(); _i < _a.length; _i++) { var context = _a[_i]; // We only automatically select nested sheet providers. if (context.startsWith('$$-auto') || context === 'global') { options.context = context; break; } } } return options.context; }; /** * Show the ActionSheet with an id. * * @param id id of the ActionSheet to show * @param options */ _SheetManager.prototype.show = function (id, options) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve) { var currentContext = options === null || options === void 0 ? void 0 : options.context; if (!currentContext) { // If no context is provided, use to current top most context // to render the sheet. for (var _i = 0, _a = providerRegistryStack.slice().reverse(); _i < _a.length; _i++) { var context = _a[_i]; // We only automatically select nested sheet providers. if (context.startsWith('$$-auto') || context === 'global') { currentContext = context; break; } } } var handler = function (data, context) { var _a; if (context === void 0) { context = 'global'; } if (currentContext !== context) return; (_a = options === null || options === void 0 ? void 0 : options.onClose) === null || _a === void 0 ? void 0 : _a.call(options, data); sub === null || sub === void 0 ? void 0 : sub.unsubscribe(); resolve(data); }; var sub = actionSheetEventManager.subscribe("onclose_".concat(id), handler); // Check if the sheet is registered. var isRegisteredWithSheetProvider = false; for (var _id in sheetsRegistry) { if (_id === id) { isRegisteredWithSheetProvider = true; } } actionSheetEventManager.publish(isRegisteredWithSheetProvider ? "show_wrap_".concat(id) : "show_".concat(id), options === null || options === void 0 ? void 0 : options.payload, currentContext || 'global', options === null || options === void 0 ? void 0 : options.overrideProps, options === null || options === void 0 ? void 0 : options.snapIndex); })]; }); }); }; /** * Update an active ActionSheet with new payload or override it's props. */ _SheetManager.prototype.update = function (id, options) { return __awaiter(this, void 0, void 0, function () { var renderedSheets, _i, renderedSheets_1, sheet, shouldUpdate; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!options || !id) return [2 /*return*/]; renderedSheets = this.getActiveSheets(id); if (!renderedSheets.length) { if (__DEV__) { console.warn('Found no sheets to update with id: ', id); } return [2 /*return*/]; } if (!options.shouldUpdate) return [3 /*break*/, 5]; _i = 0, renderedSheets_1 = renderedSheets; _b.label = 1; case 1: if (!(_i < renderedSheets_1.length)) return [3 /*break*/, 4]; sheet = renderedSheets_1[_i]; return [4 /*yield*/, ((_a = options.shouldUpdate) === null || _a === void 0 ? void 0 : _a.call(options, sheet))]; case 2: shouldUpdate = _b.sent(); if (shouldUpdate) { actionSheetEventManager.publish("update_".concat(sheet.id), options === null || options === void 0 ? void 0 : options.payload, sheet.context || 'global', options === null || options === void 0 ? void 0 : options.overrideProps); } _b.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [3 /*break*/, 6]; case 5: actionSheetEventManager.publish("update_".concat(id), options === null || options === void 0 ? void 0 : options.payload, renderedSheets.pop().context || 'global', options === null || options === void 0 ? void 0 : options.overrideProps); _b.label = 6; case 6: return [2 /*return*/]; } }); }); }; /** * 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 */ _SheetManager.prototype.hide = function (id, options) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve) { var currentContext = options === null || options === void 0 ? void 0 : options.context; if (!currentContext) { for (var _i = 0, _a = providerRegistryStack.slice().reverse(); _i < _a.length; _i++) { var context = _a[_i]; if (context.startsWith('$$-auto') || context === 'global') { if (renderedSheetIds.indexOf("".concat(id, ":").concat(context)) === -1) continue; currentContext = context; break; } } } var hideHandler = function (data, context) { if (context === void 0) { context = 'global'; } if (context !== 'global' && currentContext && currentContext !== context) return; sub === null || sub === void 0 ? void 0 : sub.unsubscribe(); resolve(data); }; var sub = actionSheetEventManager.subscribe("onclose_".concat(id), hideHandler); actionSheetEventManager.publish("hide_wrap_".concat(id), options === null || options === void 0 ? void 0 : options.payload, currentContext); })]; }); }); }; /** * Hide all the opened ActionSheets. * * @param id Hide all sheets for the specific id. */ _SheetManager.prototype.hideAll = function (id) { renderedSheetIds.forEach(function (_id) { var _a; if (id && !_id.startsWith(id)) return; actionSheetEventManager.publish("hide_".concat((_a = _id.split(':')) === null || _a === void 0 ? void 0 : _a[0])); }); }; /** * Get all rendered sheets for a Sheet Id. */ _SheetManager.prototype.getActiveSheets = function (id) { var _this = this; return renderedSheetIds .filter(function (renderdId) { return renderdId.startsWith(id); }) .map(function (renderdId) { var _a = renderdId.split(':'), id = _a[0], context = _a[1]; return { id: id, context: context, ref: _this.get(id, context), }; }); }; return _SheetManager; }()); /** * SheetManager is used to imperitively show/hide any ActionSheet with a * unique id prop. */ export var SheetManager = new _SheetManager();