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)

View File

@@ -1,55 +1,61 @@
import { ref, inject } from 'vue'
import { createSharedComposable } from '@vueuse/core'
import type { ShallowRef, Component, InjectionKey } from 'vue'
import { createSharedComposable } from '@vueuse/core'
import type { ComponentProps } from '../types/component'
import type { Slideover, SlideoverState } from '../types/slideover'
export const slidOverInjectionKey: InjectionKey<ShallowRef<SlideoverState>> =
Symbol('nuxt-ui.slideover')
export const slidOverInjectionKey: InjectionKey<ShallowRef<SlideoverState>> = Symbol('nuxt-ui.slideover')
function _useSlideover () {
const slideoverState = inject(slidOverInjectionKey)
const isOpen = ref(false)
const slideoverState = inject(slidOverInjectionKey)
function open<T extends Component> (component: T, props?: Slideover & ComponentProps<T>) {
if (!slideoverState) {
throw new Error('useSlideover() is called without provider')
}
const isOpen = ref(false)
slideoverState.value = {
component,
props: props ?? {}
}
isOpen.value = true
function open<T extends Component> (component: T, props?: Slideover & ComponentProps<T>) {
if (!slideoverState) {
throw new Error('useSlideover() is called without provider')
}
function close () {
if (!slideoverState) return
isOpen.value = false
slideoverState.value = {
component,
props: props ?? {}
}
/**
* Allows updating the slideover props
*/
function patch<T extends Component = {}> (props: Partial<Slideover & ComponentProps<T>>) {
if (!slideoverState) return
isOpen.value = true
}
slideoverState.value = {
...slideoverState.value,
props: {
...slideoverState.value.props,
...props
}
}
function close () {
if (!slideoverState) return
isOpen.value = false
slideoverState.value = {
component: 'div',
props: {}
}
return {
open,
close,
patch,
isOpen
}
/**
* Allows updating the slideover props
*/
function patch<T extends Component = {}> (props: Partial<Slideover & ComponentProps<T>>) {
if (!slideoverState) return
slideoverState.value = {
...slideoverState.value,
props: {
...slideoverState.value.props,
...props
}
}
}
return {
open,
close,
patch,
isOpen
}
}
export const useSlideover = createSharedComposable(_useSlideover)