Skip to content

Commit 6558afd

Browse files
committed
chore: add vOn directive as global
1 parent 7754d7e commit 6558afd

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed
Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
import { hyphenate } from '@vue/shared'
2-
3-
const systemModifiers = ['ctrl', 'shift', 'alt', 'meta']
2+
import { Directive } from 'test-dts'
43

54
type 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>

packages/runtime-dom/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { isFunction, isString, isHTMLTag, isSVGTag, extend } from '@vue/shared'
1818
import { TransitionProps } from './components/Transition'
1919
import { TransitionGroupProps } from './components/TransitionGroup'
2020
import { vShow } from './directives/vShow'
21+
import { VOnDirective } from './directives/vOn'
2122

2223
declare module '@vue/reactivity' {
2324
export interface RefUnwrapBailTypes {
@@ -35,6 +36,7 @@ declare module '@vue/runtime-core' {
3536

3637
interface GlobalDirectives {
3738
vShow: typeof vShow
39+
vOn: VOnDirective
3840
}
3941
}
4042

0 commit comments

Comments
 (0)