Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/type of generic component #394

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions docs/content/2.get-started/1.guide/4.types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

The exported types in VueFinalModal.

## ComponentProps

```ts
export type ComponentProps = ComponentPublicInstance['$props']
```

## ModalId

```ts
Expand All @@ -33,8 +27,8 @@ export type ModalSlot = string | Component | ModalSlotOptions
export type UseModalOptions<P> = {
defaultModelValue?: boolean
keepAlive?: boolean
component?: Constructor<P>
attrs?: (RawProps & P) | ({} extends P ? null : never)
component?: Component<P>
attrs?: ComponentProps<Component<P>>
slots?: {
[key: string]: ModalSlot
}
Expand Down
2 changes: 1 addition & 1 deletion examples/vue3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.3.4",
"vue": "^3.3.7",
"vue-final-modal": "^4.4.5"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"postinstall": "pnpm build:vfm && pnpm prepare:module"
},
"dependencies": {
"vue": "^3.3.4"
"vue": "^3.3.7"
},
"devDependencies": {
"@antfu/eslint-config": "^0.37.0",
Expand All @@ -27,7 +27,7 @@
"@vue/test-utils": "^2.3.2",
"concurrently": "^7.6.0",
"eslint": "^8.36.0",
"pnpm": "^8.5.1",
"pnpm": "^8.10.0",
"sass": "^1.60.0",
"typescript": "^5.1.6",
"vite": "^4.3.9",
Expand Down
9 changes: 9 additions & 0 deletions packages/vue-final-modal/cypress/components/GenericComp.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script setup lang="ts" generic="T extends string">
defineProps<{
title: T
}>()
</script>

<template>
<div>{{ title }}</div>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Test focusTrap', () => {

const firstModal = useModal({
component: VueFinalModal,
attrs: { contentClass: 'first-modal-content' },
attrs: { background: 'this should have type error', contentClass: 'first-modal-content' },
slots: {
default: Form,
},
Expand Down
3 changes: 2 additions & 1 deletion packages/vue-final-modal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"@vueuse/core": "^9.13.0",
"@vueuse/integrations": "^9.13.0",
"focus-trap": "^7.4.0",
"vue": "^3.3.4"
"vue": "^3.3.7",
"vue-component-type-helpers": "^1.8.22"
},
"devDependencies": {
"@cypress/vue": "^5.0.5",
Expand Down
23 changes: 4 additions & 19 deletions packages/vue-final-modal/src/Modal.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
import type { App, CSSProperties, Component, ComponentPublicInstance, ComputedRef, Raw, Ref, VNodeProps } from 'vue'

export type ComponentProps = ComponentPublicInstance['$props']
import type { App, CSSProperties, Component, ComputedRef, Raw, Ref } from 'vue'
import type { ComponentProps } from 'vue-component-type-helpers'

export type ModalId = number | string | symbol
export type StyleValue = string | CSSProperties | (string | CSSProperties)[]

export interface Constructor<P = any> {
__isFragment?: never
__isTeleport?: never
__isSuspense?: never
new (...args: any[]): { $props: P }
}

export type RawProps = VNodeProps & {
// used to differ from a single VNode object as children
__v_isVNode?: never
// used to differ from Array children
[Symbol.iterator]?: never
} & Record<string, any>

export interface ModalSlotOptions { component: Raw<Component>; attrs?: Record<string, any> }
export type ModalSlot = string | Component | ModalSlotOptions

export type UseModalOptions<P> = {
defaultModelValue?: boolean
keepAlive?: boolean
component?: Constructor<P>
attrs?: (RawProps & P) | ({} extends P ? null : never)
component?: Component<P>
attrs?: ComponentProps<Component<P>>
slots?: {
[key: string]: ModalSlot
}
Expand Down
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
22 changes: 13 additions & 9 deletions packages/vue-final-modal/src/useApi.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { computed, inject, markRaw, nextTick, reactive, useAttrs } from 'vue'
import { tryOnUnmounted } from '@vueuse/core'
import type { Component } from 'vue'
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'

import type { ComponentProps, Constructor, InternalVfm, ModalSlot, ModalSlotOptions, RawProps, UseModalOptions, UseModalOptionsPrivate, UseModalReturnType, Vfm } from './Modal'
import type { InternalVfm, ModalSlot, ModalSlotOptions, UseModalOptions, UseModalOptionsPrivate, UseModalReturnType, Vfm } from './Modal'
import { activeVfm, getActiveVfm } from './plugin'
import { isString } from '~/utils'

type _ComponentProps = ComponentPublicInstance['$props']

/**
* Returns the vfm instance. Equivalent to using `$vfm` inside
* templates.
Expand Down Expand Up @@ -55,15 +59,15 @@ function withMarkRaw<P>(options: Partial<UseModalOptions<P>>, DefaultComponent:

return {
...rest,
component: markRaw(component || DefaultComponent) as Constructor<P>,
component: markRaw(component || DefaultComponent) as Component<P>,
slots,
}
}

/**
* 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 @@ -183,8 +187,8 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
}

export function useModalSlot<P>(options: {
component: Constructor<P>
attrs?: (RawProps & P) | ({} extends P ? null : never)
component: Component<P>
attrs?: ComponentProps<Component<P>>
}) {
return options
}
Expand All @@ -200,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 @@ -217,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
Loading
Loading