|
| 1 | +import { App, createApp } from 'nativescript-vue3'; |
| 2 | +import { Frame, View, ViewBase } from '@nativescript/core'; |
| 3 | +import { BottomSheetOptions } from '../bottomsheet'; |
| 4 | + |
| 5 | +const modalStack = []; |
| 6 | + |
| 7 | +const useBottomSheet = () => { |
| 8 | + const showBottomSheet = (component: any, options: VueBottomSheetOptions) => showSheet(component, options); |
| 9 | + const closeBottomSheet = (...args) => closeSheet(args); |
| 10 | + |
| 11 | + return { |
| 12 | + showBottomSheet, |
| 13 | + closeBottomSheet |
| 14 | + }; |
| 15 | +}; |
| 16 | + |
| 17 | +const showSheet = (component, options: VueBottomSheetOptions) => |
| 18 | + new Promise((resolve: any) => { |
| 19 | + let resolved = false; |
| 20 | + |
| 21 | + let navEntryInstance = createNativeView(component, { |
| 22 | + props: options.props |
| 23 | + }).mount(); |
| 24 | + |
| 25 | + const viewAttached = (options.view as View) ?? Frame.topmost().currentPage; |
| 26 | + |
| 27 | + viewAttached.showBottomSheet( |
| 28 | + Object.assign({}, options, { |
| 29 | + view: navEntryInstance.$el.nativeView, |
| 30 | + closeCallback: (...args) => { |
| 31 | + if (resolved) { |
| 32 | + return; |
| 33 | + } |
| 34 | + resolved = true; |
| 35 | + if (navEntryInstance && navEntryInstance) { |
| 36 | + options.closeCallback && options.closeCallback.apply(undefined, args); |
| 37 | + resolve(...args); |
| 38 | + navEntryInstance.$emit('bottomsheet:close'); |
| 39 | + navEntryInstance.$el = null; |
| 40 | + navEntryInstance = null; |
| 41 | + modalStack.splice(modalStack.length, 1); |
| 42 | + } |
| 43 | + } |
| 44 | + }) |
| 45 | + ); |
| 46 | + modalStack.push(navEntryInstance); |
| 47 | + }); |
| 48 | +const closeSheet = (...args) => { |
| 49 | + const modalPageInstanceInfo = modalStack[modalStack.length - 1]; |
| 50 | + if (modalPageInstanceInfo) { |
| 51 | + modalPageInstanceInfo.$el.nativeView.closeBottomSheet(args); |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +const BottomSheetPlugin = { |
| 56 | + install(app) { |
| 57 | + const globals = app.config.globalProperties; |
| 58 | + |
| 59 | + globals.$showBottomSheet = showSheet; |
| 60 | + globals.$closeBottomSheet = closeSheet; |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +const createNativeView = (component: any, props?: any): App => createApp(component, props); |
| 65 | + |
| 66 | +declare module '@vue/runtime-core' { |
| 67 | + interface ComponentCustomProperties { |
| 68 | + $showBottomSheet: (component: any, options: VueBottomSheetOptions) => Promise<any>; |
| 69 | + $closeBottomSheet(...args); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +interface VueBottomSheetOptions extends Partial<BottomSheetOptions> { |
| 74 | + view?: string | ViewBase; |
| 75 | + props?: any; |
| 76 | +} |
| 77 | + |
| 78 | +export { BottomSheetPlugin, VueBottomSheetOptions, useBottomSheet }; |
0 commit comments