-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Composables: Add dialog queue composable
Signed-off-by: Arturo Manzoli <arturomanzoli@gmail.com>
- Loading branch information
1 parent
0349e2a
commit 0e273d9
Showing
2 changed files
with
117 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Component, computed, defineComponent, h, Ref, ref } from 'vue' | ||
|
||
/** | ||
* Queued component to be displayed. Usually a dialog. | ||
*/ | ||
export interface QueuedComponent { | ||
/** | ||
* The component reference (e.g. Tutorial or VehicleDiscoveryDialog) | ||
*/ | ||
component: Component | ||
/** | ||
* Optional props to pass into the component | ||
*/ | ||
props?: Record<string, unknown> | ||
} | ||
|
||
/** | ||
* Component queue to manage the order of components to be displayed. | ||
*/ | ||
export interface ComponentQueue { | ||
/** | ||
* Enqueue a dialog to be displayed. | ||
*/ | ||
enqueueDialog: (item: QueuedComponent) => void | ||
/** | ||
* Clear the queue. | ||
*/ | ||
clear: () => void | ||
/** | ||
* The queue itself. | ||
*/ | ||
queue: Ref<QueuedComponent[]> | ||
/** | ||
* The current dialog to be displayed. | ||
*/ | ||
currentDialog: Ref<QueuedComponent | null> | ||
/** | ||
* Whether the dialog is visible or not. | ||
*/ | ||
isDialogVisible: Ref<boolean> | ||
/** | ||
* The wrapped dialog component. | ||
*/ | ||
WrappedDialog: Ref<Component | null> | ||
} | ||
|
||
export const useComponentQueue = (): ComponentQueue => { | ||
const queue: Ref<QueuedComponent[]> = ref([]) | ||
const isDialogVisible: Ref<boolean> = ref(true) | ||
|
||
const enqueueDialog = (item: QueuedComponent): void => { | ||
queue.value.push(item) | ||
} | ||
|
||
const clear = (): void => { | ||
queue.value = [] | ||
} | ||
|
||
const currentDialog = computed(() => queue.value[0] || null) | ||
|
||
const closeCurrentDialog = (): void => { | ||
queue.value.shift() | ||
isDialogVisible.value = true | ||
} | ||
|
||
const WrappedDialog = computed(() => { | ||
if (!currentDialog.value) return null | ||
|
||
return defineComponent({ | ||
name: 'WrappedDialog', | ||
setup() { | ||
const handleUpdate = (value: boolean): void => { | ||
if (!value) { | ||
closeCurrentDialog() | ||
} | ||
} | ||
|
||
return () => | ||
h(currentDialog.value!.component, { | ||
...currentDialog.value!.props, | ||
'modelValue': isDialogVisible.value, | ||
'onUpdate:modelValue': handleUpdate, | ||
}) | ||
}, | ||
}) | ||
}) | ||
|
||
return { | ||
enqueueDialog, | ||
clear, | ||
queue, | ||
currentDialog, | ||
isDialogVisible, | ||
WrappedDialog, | ||
} | ||
} |