fix(Slideover): remove dynamic component when closing (#1615)

Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
Eugen Istoc
2024-04-05 06:43:50 -04:00
committed by GitHub
parent e909884d03
commit 58faa1053b
2 changed files with 56 additions and 40 deletions

View File

@@ -8,19 +8,27 @@ export const modalInjectionKey: InjectionKey<ShallowRef<ModalState>> = Symbol('n
function _useModal () {
const modalState = inject(modalInjectionKey)
const isOpen = ref(false)
function open<T extends Component> (component: T, props?: Modal & ComponentProps<T>) {
if (!modalState) {
throw new Error('useModal() is called without provider')
}
modalState.value = {
component,
props: props ?? {}
}
isOpen.value = true
}
function close () {
if (!modalState) return
isOpen.value = false
modalState.value = {
component: 'div',
props: {}
@@ -31,6 +39,8 @@ function _useModal () {
* Allows updating the modal props
*/
function patch <T extends Component = {}> (props: Partial<Modal & ComponentProps<T>>) {
if (!modalState) return
modalState.value = {
...modalState.value,
props: {
@@ -41,11 +51,11 @@ function _useModal () {
}
return {
isOpen,
open,
close,
patch
patch,
isOpen
}
}
export const useModal = createSharedComposable(_useModal)
export const useModal = createSharedComposable(_useModal)