Skip to content

Commit fdf7236

Browse files
committed
feat(hmr): improved modal HMR
1 parent 0e7406d commit fdf7236

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/plugins/modals.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
unref,
88
warn,
99
} from '@vue/runtime-core';
10-
import { NSVElement } from '../dom';
10+
import { NSVElement, NSVRoot } from '../dom';
1111
import { createNativeView } from '../runtimeHelpers';
1212
import { isObject } from '@vue/shared';
1313

@@ -74,29 +74,56 @@ export async function $showModal<T = any>(
7474

7575
return new Promise((resolve) => {
7676
let isResolved = false;
77-
let view = createNativeView(component, options.props);
77+
let isReloading = false;
78+
let root = new NSVRoot();
79+
80+
const reloadModal = () => {
81+
isReloading = true;
82+
closeModal();
83+
// reopening is done in `closeCallback`
84+
};
85+
86+
let view = createNativeView(component, options.props, {
87+
reload: reloadModal,
88+
});
7889

79-
const closeModal = (...args: any[]) => modalContent.closeModal(...args);
8090
const closeCallback = (data?: T) => {
8191
if (isResolved) return;
92+
93+
if (isReloading) {
94+
view.unmount();
95+
view.mount(root);
96+
openModal({
97+
// todo: for this to work nicely, we'd need to add `animated: false` to `closeModal` as well
98+
// but not currently possible without a core change.
99+
// animated: false,
100+
});
101+
isReloading = false;
102+
103+
return;
104+
}
105+
82106
isResolved = true;
83107
view.unmount();
84108
view = null;
85109

86110
resolve(data);
87111
};
88112

113+
const openModal = (additionalOptions?: Partial<ShowModalOptions>) => {
114+
modalTarget.showModal(view.nativeView, {
115+
...options,
116+
context: null,
117+
closeCallback,
118+
...additionalOptions,
119+
});
120+
};
121+
const closeModal = (...args: any[]) => view.nativeView?.closeModal(...args);
122+
89123
view.context.config.globalProperties.$closeModal = closeModal;
90124
view.context.config.globalProperties.$modal = { close: closeModal };
91125

92-
view.mount();
93-
94-
const modalContent = view.nativeView;
95-
96-
modalTarget.showModal(modalContent, {
97-
...options,
98-
context: null,
99-
closeCallback,
100-
});
126+
view.mount(root);
127+
openModal();
101128
});
102129
}

0 commit comments

Comments
 (0)