-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ModalManager - refactor modal hook to use
modal
store
- Loading branch information
1 parent
ae6e131
commit 23f8be8
Showing
8 changed files
with
177 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<script lang="ts" setup> | ||
import { computed, ref } from 'vue'; | ||
import { useStore } from 'vuex'; | ||
import AppModal from '@shell/components/AppModal.vue'; | ||
const store = useStore(); | ||
const isOpen = computed(() => store.getters['modal/isOpen']); | ||
const component = computed(() => store.getters['modal/component']); | ||
const componentProps = computed(() => store.getters['modal/componentProps']); | ||
const resources = computed(() => store.getters['modal/resources']); | ||
const closeOnClickOutside = computed(() => store.getters['modal/closeOnClickOutside']); | ||
const modalWidth = computed(() => store.getters['modal/modalWidth']); | ||
// const modalSticky = computed(() => store.getters['modal/modalSticky']); // TODO: Implement sticky modals | ||
const backgroundClosing = ref<Function | null>(null); | ||
function close() { | ||
if (!isOpen.value) return; | ||
if (backgroundClosing.value) { | ||
backgroundClosing.value(); | ||
} | ||
store.commit('modal/closeModal'); | ||
} | ||
function registerBackgroundClosing(fn: Function) { | ||
backgroundClosing.value = fn; | ||
} | ||
</script> | ||
|
||
<template> | ||
<app-modal | ||
v-if="isOpen && component" | ||
:click-to-close="closeOnClickOutside" | ||
:width="modalWidth" | ||
:style="{ '--prompt-modal-width': modalWidth }" | ||
@close="close" | ||
> | ||
<component | ||
:is="component" | ||
v-bind="componentProps || {}" | ||
:resources="resources" | ||
:register-background-closing="registerBackgroundClosing" | ||
@close="close" | ||
/> | ||
</app-modal> | ||
</template> | ||
|
||
<style lang='scss'> | ||
.promptModal-modal { | ||
border-radius: var(--border-radius); | ||
overflow: scroll; | ||
max-height: 100vh; | ||
& ::-webkit-scrollbar-corner { | ||
background: rgba(0,0,0,0); | ||
} | ||
} | ||
</style> |
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
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
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,71 @@ | ||
import { markRaw, Component } from 'vue'; | ||
import { MutationTree, GetterTree, ActionTree } from 'vuex'; | ||
|
||
export interface ModalState { | ||
isOpen: boolean; | ||
component: Component | null; | ||
componentProps: Record<string, any>; | ||
resources: any[]; | ||
closeOnClickOutside: boolean; | ||
modalWidth: string; | ||
modalSticky: boolean; | ||
} | ||
|
||
const state = (): ModalState => ({ | ||
isOpen: false, | ||
component: null, | ||
componentProps: {}, | ||
resources: [], | ||
closeOnClickOutside: false, | ||
modalWidth: '600px', | ||
modalSticky: false | ||
}); | ||
|
||
const getters: GetterTree<ModalState, any> = { | ||
isOpen: (state) => state.isOpen, | ||
component: (state) => state.component, | ||
componentProps: (state) => state.componentProps, | ||
resources: (state) => state.resources, | ||
closeOnClickOutside: (state) => state.closeOnClickOutside, | ||
modalWidth: (state) => state.modalWidth, | ||
modalSticky: (state) => state.modalSticky, | ||
}; | ||
|
||
const mutations: MutationTree<ModalState> = { | ||
openModal(state, payload: { | ||
component: Component; | ||
componentProps?: Record<string, any>; | ||
resources?: any[]; | ||
closeOnClickOutside?: boolean; | ||
modalWidth?: string; | ||
modalSticky?: boolean; | ||
}) { | ||
state.isOpen = true; | ||
state.component = markRaw(payload.component); | ||
state.componentProps = payload.componentProps || {}; | ||
state.resources = Array.isArray(payload.resources) ? payload.resources : (payload.resources ? [payload.resources] : []); | ||
state.closeOnClickOutside = payload.closeOnClickOutside ?? false; | ||
state.modalWidth = payload.modalWidth || '600px'; | ||
state.modalSticky = payload.modalSticky ?? false; | ||
}, | ||
|
||
closeModal(state) { | ||
state.isOpen = false; | ||
state.component = null; | ||
state.componentProps = {}; | ||
state.resources = []; | ||
state.closeOnClickOutside = false; | ||
state.modalWidth = '600px'; | ||
state.modalSticky = false; | ||
} | ||
}; | ||
|
||
const actions: ActionTree<ModalState, any> = {}; | ||
|
||
export default { | ||
namespaced: true, | ||
state, | ||
getters, | ||
mutations, | ||
actions | ||
}; |
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