11import { hyphenate } from '@vue/shared'
2-
3- const systemModifiers = [ 'ctrl' , 'shift' , 'alt' , 'meta' ]
2+ import { Directive } from 'test-dts'
43
54type KeyedEvent = KeyboardEvent | MouseEvent | TouchEvent
65
7- const modifierGuards : Record <
8- string ,
9- ( e : Event , modifiers : string [ ] ) => void | boolean
10- > = {
11- stop : e => e . stopPropagation ( ) ,
12- prevent : e => e . preventDefault ( ) ,
13- self : e => e . target !== e . currentTarget ,
14- ctrl : e => ! ( e as KeyedEvent ) . ctrlKey ,
15- shift : e => ! ( e as KeyedEvent ) . shiftKey ,
16- alt : e => ! ( e as KeyedEvent ) . altKey ,
17- meta : e => ! ( e as KeyedEvent ) . metaKey ,
18- left : e => 'button' in e && ( e as MouseEvent ) . button !== 0 ,
19- middle : e => 'button' in e && ( e as MouseEvent ) . button !== 1 ,
20- right : e => 'button' in e && ( e as MouseEvent ) . button !== 2 ,
21- exact : ( e , modifiers ) =>
6+ type SystemModifiers = 'ctrl' | 'shift' | 'alt' | 'meta'
7+ type CompatModifiers = keyof typeof keyNames
8+
9+ export type VOnModifiers = SystemModifiers | ModifierGuards | CompatModifiers
10+
11+ const systemModifiers : Array < SystemModifiers > = [ 'ctrl' , 'shift' , 'alt' , 'meta' ]
12+
13+ const modifierGuards = {
14+ stop : ( e : Event ) => e . stopPropagation ( ) ,
15+ prevent : ( e : Event ) => e . preventDefault ( ) ,
16+ self : ( e : Event ) => e . target !== e . currentTarget ,
17+ ctrl : ( e : Event ) => ! ( e as KeyedEvent ) . ctrlKey ,
18+ shift : ( e : Event ) => ! ( e as KeyedEvent ) . shiftKey ,
19+ alt : ( e : Event ) => ! ( e as KeyedEvent ) . altKey ,
20+ meta : ( e : Event ) => ! ( e as KeyedEvent ) . metaKey ,
21+ left : ( e : Event ) => 'button' in e && ( e as MouseEvent ) . button !== 0 ,
22+ middle : ( e : Event ) => 'button' in e && ( e as MouseEvent ) . button !== 1 ,
23+ right : ( e : Event ) => 'button' in e && ( e as MouseEvent ) . button !== 2 ,
24+ exact : ( e : Event , modifiers : string [ ] ) =>
2225 systemModifiers . some ( m => ( e as any ) [ `${ m } Key` ] && ! modifiers . includes ( m ) )
2326}
2427
28+ type ModifierGuards = keyof typeof modifierGuards
29+
2530/**
2631 * @private
2732 */
28- export const withModifiers = ( fn : Function , modifiers : string [ ] ) => {
33+ export const withModifiers = (
34+ fn : Function ,
35+ modifiers : Array < ModifierGuards | SystemModifiers >
36+ ) => {
2937 return ( event : Event , ...args : unknown [ ] ) => {
3038 for ( let i = 0 ; i < modifiers . length ; i ++ ) {
3139 const guard = modifierGuards [ modifiers [ i ] ]
@@ -37,7 +45,7 @@ export const withModifiers = (fn: Function, modifiers: string[]) => {
3745
3846// Kept for 2.x compat.
3947// Note: IE11 compat for `spacebar` and `del` is removed for now.
40- const keyNames : Record < string , string | string [ ] > = {
48+ const keyNames = {
4149 esc : 'escape' ,
4250 space : ' ' ,
4351 up : 'arrow-up' ,
@@ -56,10 +64,14 @@ export const withKeys = (fn: Function, modifiers: string[]) => {
5664 const eventKey = hyphenate ( event . key )
5765 if (
5866 // None of the provided key modifiers match the current event key
59- ! modifiers . some ( k => k === eventKey || keyNames [ k ] === eventKey )
67+ ! modifiers . some (
68+ k => k === eventKey || keyNames [ k as CompatModifiers ] === eventKey
69+ )
6070 ) {
6171 return
6272 }
6373 return fn ( event )
6474 }
6575}
76+
77+ export type VOnDirective = Directive < any , any , Modifiers >
0 commit comments