Skip to content

Commit 58aac86

Browse files
dan-halegenu
andauthored
feat(useOverlay): return promise on open method (#4592)
Co-authored-by: Eugen Istoc <[email protected]>
1 parent b8b74a0 commit 58aac86

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

docs/content/2.composables/use-overlay.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ async function openModal() {
2424
- The `useOverlay` composable is created using `createSharedComposable`, ensuring that the same overlay state is shared across your entire application.
2525

2626
::note
27-
In order to return a value from the overlay, the `overlay.open().instance.result` can be awaited. In order for this to work, however, the **overlay component must emit a `close` event**. See example below for details.
27+
In order to return a value from the overlay, the `overlay.open().instance` can be awaited. In order for this to work, however, the **overlay component must emit a `close` event**. See example below for details.
2828
::
2929

30+
3031
## API
3132

3233
### `create(component: T, options: OverlayOptions): OverlayInstance`
@@ -166,7 +167,7 @@ const openModalB = async () => {
166167
// Open modalB, and wait for its result
167168
const modalBInstance = modalB.open()
168169
169-
const input = await modalBInstance.result
170+
const input = await modalBInstance
170171
171172
// Pass the result from modalB to the slideover, and open it
172173
slideoverA.open({ input })

src/runtime/composables/useOverlay.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type OverlayInstance<T extends Component> = Omit<ManagedOverlayOptionsPrivate<T>
5656

5757
type OpenedOverlay<T extends Component> = Omit<OverlayInstance<T>, 'open' | 'close' | 'patch' | 'modelValue' | 'resolvePromise'> & {
5858
result: Promise<CloseEventArgType<ComponentEmit<T>>>
59-
}
59+
} & Promise<CloseEventArgType<ComponentEmit<T>>>
6060

6161
function _useOverlay() {
6262
const overlays = shallowReactive<Overlay[]>([])
@@ -96,13 +96,14 @@ function _useOverlay() {
9696

9797
overlay.isOpen = true
9898
overlay.isMounted = true
99+
const result = new Promise<any>(resolve => overlay.resolvePromise = resolve)
99100

100-
return {
101+
return Object.assign(result, {
101102
id,
102103
isMounted: overlay.isMounted,
103104
isOpen: overlay.isOpen,
104-
result: new Promise<any>(resolve => overlay.resolvePromise = resolve)
105-
}
105+
result
106+
})
106107
}
107108

108109
const close = (id: symbol, value?: any): void => {

test/composables/useOverlay.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ describe('useOverlay', () => {
143143
expect(result).toBe('test-result')
144144
})
145145

146+
it('should directly return promise that resolves when closed', async () => {
147+
const modal = overlay.create(MockModal)
148+
const instance = modal.open()
149+
150+
// Simulate closing the modal
151+
setTimeout(() => {
152+
modal.close('test-result')
153+
}, 0)
154+
155+
const result = await instance
156+
expect(result).toBe('test-result')
157+
})
158+
146159
it('should close an overlay', () => {
147160
const modal = overlay.create(MockModal)
148161

0 commit comments

Comments
 (0)