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

feat: add directives type support #863

Merged
merged 1 commit into from
Nov 30, 2021
Merged
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
16 changes: 13 additions & 3 deletions src/apis/createApp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type Vue from 'vue'
import { VueConstructor } from 'vue'
import { Directive } from '../component/directives'
import { getVueConstructor } from '../runtimeContext'
import { warn } from '../utils'

Expand All @@ -9,7 +10,8 @@ export interface App<T = any> {
use: VueConstructor['use']
mixin: VueConstructor['mixin']
component: VueConstructor['component']
directive: VueConstructor['directive']
directive(name: string): Directive | undefined
directive(name: string, directive: Directive): this
mount: Vue['$mount']
unmount: Vue['$destroy']
}
Expand All @@ -19,12 +21,19 @@ export function createApp(rootComponent: any, rootProps: any = undefined): App {

let mountedVM: Vue | undefined = undefined

return {
const app: App = {
config: V.config,
use: V.use.bind(V),
mixin: V.mixin.bind(V),
component: V.component.bind(V),
directive: V.directive.bind(V),
directive(name: string, dir?: Directive | undefined): any {
if (dir) {
V.directive(name, dir as any)
return app
} else {
return V.directive(name)
}
},
mount: (el, hydrating) => {
if (!mountedVM) {
mountedVM = new V({ propsData: rootProps, ...rootComponent })
Expand All @@ -51,4 +60,5 @@ export function createApp(rootComponent: any, rootProps: any = undefined): App {
}
},
}
return app
}
29 changes: 29 additions & 0 deletions src/component/directives.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { VNodeDirective, VNode } from 'vue'

export type DirectiveModifiers = Record<string, boolean>

export interface DirectiveBinding<V> extends Readonly<VNodeDirective> {
readonly modifiers: DirectiveModifiers
readonly value: V
readonly oldValue: V | null
}

export type DirectiveHook<T = any, Prev = VNode | null, V = any> = (
el: T,
binding: DirectiveBinding<V>,
vnode: VNode,
prevVNode: Prev
) => void

export interface ObjectDirective<T = any, V = any> {
bind?: DirectiveHook<T, any, V>
inserted?: DirectiveHook<T, any, V>
update?: DirectiveHook<T, any, V>
componentUpdated?: DirectiveHook<T, any, V>
unbind?: DirectiveHook<T, any, V>
}
export type FunctionDirective<T = any, V = any> = DirectiveHook<T, any, V>

export type Directive<T = any, V = any> =
| ObjectDirective<T, V>
| FunctionDirective<T, V>
8 changes: 8 additions & 0 deletions src/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ export {
ExtractPropTypes,
ExtractDefaultPropTypes,
} from './componentProps'

export {
DirectiveModifiers,
DirectiveBinding,
DirectiveHook,
ObjectDirective,
FunctionDirective,
} from './directives'