-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Slideover): remove dynamic component when closing (#1615)
Co-authored-by: Benjamin Canac <[email protected]>
- Loading branch information
1 parent
e909884
commit 58faa10
Showing
2 changed files
with
56 additions
and
40 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 |
---|---|---|
@@ -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 ?? {} | ||
} | ||
function open<T extends Component> (component: T, props?: Slideover & ComponentProps<T>) { | ||
if (!slideoverState) { | ||
throw new Error('useSlideover() is called without provider') | ||
} | ||
|
||
isOpen.value = true | ||
slideoverState.value = { | ||
component, | ||
props: props ?? {} | ||
} | ||
|
||
function close () { | ||
if (!slideoverState) return | ||
isOpen.value = true | ||
} | ||
|
||
isOpen.value = false | ||
} | ||
function close () { | ||
if (!slideoverState) return | ||
|
||
isOpen.value = false | ||
|
||
/** | ||
* 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 | ||
} | ||
} | ||
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) |