UNPKG

@nativescript-community/ui-popover

Version:
55 lines 2.25 kB
// import { GestureRootView } from '@nativescript-community/gesturehandler'; import { View } from '@nativescript/core'; import { createElement } from 'svelte-native/dom'; // eslint-disable-next-line no-duplicate-imports import { showPopover as nativeShowPopover } from '..'; const modalStack = []; export function resolveComponentElement(viewSpec, props) { const dummy = createElement('fragment', window.document); const viewInstance = new viewSpec({ target: dummy, props }); const element = dummy.firstElement(); return { element, viewInstance }; } export function showPopover(modalOptions, onCreated) { const { view, anchor, props = {}, ...options } = modalOptions; // Get this before any potential new frames are created by component below const anchorView = anchor instanceof View ? anchor : anchor.nativeView; const componentInstanceInfo = resolveComponentElement(view, props); const modalView = componentInstanceInfo.element.nativeView; // if (!(modalView instanceof GestureRootView)) { // const gestureView = new GestureRootView(); // gestureView.height = modalView.height; // gestureView.addChild(modalView); // modalView = gestureView; // } onCreated?.(componentInstanceInfo); return new Promise(async (resolve, reject) => { let resolved = false; const closeCallback = (result) => { if (resolved) return; options.onDismiss?.(); modalStack.pop(); resolved = true; resolve(result); componentInstanceInfo.viewInstance.$destroy(); // don't let an exception in destroy kill the promise callback }; try { modalStack.push(nativeShowPopover(modalView, { ...options, anchor: anchorView, onDismiss: closeCallback })); } catch (err) { console.error(err); reject(err); } }); } export async function closePopover(result) { const modalPageInstanceInfo = modalStack[modalStack.length - 1]; if (modalPageInstanceInfo) { return modalPageInstanceInfo.close(result); } } export function isPopoverOpened() { return modalStack.length > 0; } //# sourceMappingURL=index.js.map