Skip to content

Commit

Permalink
fix: use ComponentProps, ComponentEmit to replace InstanceType
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterliu1003 committed Oct 30, 2023
1 parent 7bbb203 commit 67965a3
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Ref } from 'vue'
import { onBeforeUnmount, watch } from 'vue'
import type { ComponentProps } from 'vue-component-type-helpers'
import type CoreModal from './CoreModal.vue'

type BodyScrollOptions = {
Expand Down Expand Up @@ -233,7 +234,7 @@ export const enableBodyScroll = (targetElement?: HTMLElement) => {
}
}

export function useLockScroll(props: InstanceType<typeof CoreModal>['$props'], options: {
export function useLockScroll(props: ComponentProps<typeof CoreModal>, options: {
lockScrollEl: Ref<undefined | HTMLElement>
modelValueLocal: Ref<boolean>
}) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { ComponentEmit } from 'vue-component-type-helpers'
import type CoreModal from './CoreModal.vue'

type Event = 'beforeOpen' | 'beforeClose' | 'opened' | 'closed'

export function useEvent(emit: InstanceType<typeof CoreModal>['$emit']) {
export function useEvent(emit: ComponentEmit<typeof CoreModal>) {
function emitEvent(e: Event) {
switch (e) {
case 'beforeOpen':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Ref } from 'vue'
import { useFocusTrap as _useFocusTrap } from '@vueuse/integrations/useFocusTrap'
import type { ComponentProps } from 'vue-component-type-helpers'
import type CoreModal from './CoreModal.vue'

export function useFocusTrap(
props: InstanceType<typeof CoreModal>['$props'],
props: ComponentProps<typeof CoreModal>,
options: {
focusEl: Ref<undefined | HTMLDivElement>
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Ref } from 'vue'
import { ref, watch } from 'vue'
import type { ComponentEmit, ComponentProps } from 'vue-component-type-helpers'
import type CoreModal from './CoreModal.vue'

export function useModelValue(
props: InstanceType<typeof CoreModal>['$props'],
emit: InstanceType<typeof CoreModal>['$emit'],
props: ComponentProps<typeof CoreModal>,
emit: ComponentEmit<typeof CoreModal>,
): { modelValueLocal: Ref<boolean> } {
const modelValueLocal = ref<boolean>(!!props.modelValue)
watch(() => props.modelValue, (val) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Ref } from 'vue'
import { ref } from 'vue'
import type { ComponentEmit, ComponentProps } from 'vue-component-type-helpers'
import type CoreModal from './CoreModal.vue'

export function useToClose(
props: InstanceType<typeof CoreModal>['$props'],
emit: InstanceType<typeof CoreModal>['$emit'],
props: ComponentProps<typeof CoreModal>,
emit: ComponentEmit<typeof CoreModal>,
options: {
vfmRootEl: Ref<HTMLDivElement | undefined>
vfmContentEl: Ref<HTMLDivElement | undefined>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ComputedRef, Ref, TransitionProps } from 'vue'
import type { ComponentProps } from 'vue-component-type-helpers'
import { computed, nextTick, ref, watch } from 'vue'
import type CoreModal from './CoreModal.vue'

Expand Down Expand Up @@ -31,7 +32,7 @@ function useTransitionState(_visible = false): [Ref<boolean>, Ref<undefined | Tr
}

export function useTransition(
props: InstanceType<typeof CoreModal>['$props'],
props: ComponentProps<typeof CoreModal>,
options: {
modelValueLocal: Ref<boolean>
onEntering?: () => void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ref } from 'vue'
import type { ComponentProps } from 'vue-component-type-helpers'
import type CoreModal from './CoreModal.vue'

export function useZIndex(
props: InstanceType<typeof CoreModal>['$props'],
props: ComponentProps<typeof CoreModal>,
) {
const zIndex = ref<undefined | number>()

Expand Down
12 changes: 7 additions & 5 deletions packages/vue-final-modal/src/useApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { computed, inject, markRaw, nextTick, reactive, useAttrs } from 'vue'
import { tryOnUnmounted } from '@vueuse/core'
import type { Component, ComponentPublicInstance } from 'vue'
import type { ComponentEmit, ComponentProps } from 'vue-component-type-helpers'

import VueFinalModal from './components/VueFinalModal/VueFinalModal.vue'
import type CoreModal from './components/CoreModal/CoreModal.vue'
import { internalVfmSymbol } from './injectionSymbols'
Expand All @@ -9,7 +11,7 @@ import type { Constructor, InternalVfm, ModalSlot, ModalSlotOptions, RawProps, U
import { activeVfm, getActiveVfm } from './plugin'
import { isString } from '~/utils'

type ComponentProps = ComponentPublicInstance['$props']
type _ComponentProps = ComponentPublicInstance['$props']

/**
* Returns the vfm instance. Equivalent to using `$vfm` inside
Expand Down Expand Up @@ -65,7 +67,7 @@ function withMarkRaw<P>(options: Partial<UseModalOptions<P>>, DefaultComponent:
/**
* Create a dynamic modal.
*/
export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_options: UseModalOptions<P>): UseModalReturnType<P> {
export function useModal<P = ComponentProps<typeof VueFinalModal>>(_options: UseModalOptions<P>): UseModalReturnType<P> {
const options = reactive({
id: Symbol('useModal'),
modelValue: !!_options?.defaultModelValue,
Expand Down Expand Up @@ -202,7 +204,7 @@ export function pickModalProps(props: any, modalProps: any) {
}, {} as Record<string, any>)
}

export function byPassAllModalEvents(emit?: InstanceType<typeof CoreModal>['$emit']) {
export function byPassAllModalEvents(emit?: ComponentEmit<typeof CoreModal>) {
if (!emit)
return {}
return {
Expand All @@ -219,8 +221,8 @@ export function byPassAllModalEvents(emit?: InstanceType<typeof CoreModal>['$emi
}

export function useVfmAttrs(options: {
props: ComponentProps
modalProps: ComponentProps
props: _ComponentProps
modalProps: _ComponentProps
emit?: any
}) {
const { props, modalProps, emit } = options
Expand Down
3 changes: 2 additions & 1 deletion packages/vue-final-modal/src/useSwipeToClose.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useEventListener } from '@vueuse/core'
import type { Ref } from 'vue'
import { computed, ref, watch } from 'vue'
import type { ComponentProps } from 'vue-component-type-helpers'
import type CoreModal from './components/CoreModal/CoreModal.vue'
import { useSwipeable } from './useSwipeable'
import { clamp, noop } from './utils'

export function useSwipeToClose(
props: InstanceType<typeof CoreModal>['$props'],
props: ComponentProps<typeof CoreModal>,
options: {
vfmContentEl: Ref<HTMLDivElement | undefined>
modelValueLocal: Ref<boolean>
Expand Down

0 comments on commit 67965a3

Please sign in to comment.