Skip to content

Commit 05061e9

Browse files
authored
Merge pull request #429 from vallemar/bottomsheet-vue-3
feat(BottomSheet): add vue 3 support
2 parents e0ccc86 + 03a7c59 commit 05061e9

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

packages/bottomsheet/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export function openBottomSheet(args) {
122122

123123
##
124124

125-
### NativeScript + Vue
125+
### NativeScript + Vue 2
126126
```typescript
127127
import Vue from 'nativescript-vue';
128128
import BottomSheetPlugin from '@nativescript-community/ui-material-bottomsheet/vue';
@@ -141,6 +141,32 @@ const options: VueBottomSheetOptions = {
141141
this.$showBottomSheet(MyComponent, options)
142142
```
143143

144+
### NativeScript + Vue 3
145+
```typescript
146+
import { createApp } from 'nativescript-vue';
147+
import { BottomSheetPlugin } from '@nativescript-community/ui-material-bottomsheet/vue3';
148+
import { install } from "@nativescript-community/ui-material-bottomsheet";
149+
install();
150+
151+
const app = createApp(...);
152+
app.use(BottomSheetPlugin);
153+
```
154+
Then you can show a Vue component:
155+
```typescript
156+
import { useBottomSheet } from "@nativescript-community/ui-material-bottomsheet/vue3";
157+
import MyComponent from 'MyComponent.vue';
158+
159+
160+
const options: VueBottomSheetOptions = {
161+
...
162+
};
163+
164+
const { showBottomSheet, closeBottomSheet } = useBottomSheet()
165+
166+
showBottomSheet(MyComponent, options);
167+
closeBottomSheet();
168+
```
169+
144170
##
145171

146172
### NativeScript + Angular

src/bottomsheet/vue3/index.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)