Skip to content

Commit

Permalink
fix(useMagicKeys): fix default alias map, close vitest-dev#437
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 8, 2021
1 parent ae56165 commit 44fa25b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
10 changes: 10 additions & 0 deletions packages/core/useMagicKeys/aliasMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const DefaultMagicKeysAliasMap: Readonly<Record<string, string>> = {
ctrl: 'control',
command: 'meta',
cmd: 'meta',
option: 'alt',
up: 'arrowup',
down: 'arrowdown',
left: 'arrowleft',
right: 'arrowright',
}
10 changes: 4 additions & 6 deletions packages/core/useMagicKeys/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,21 @@ whenever(
)
```

### Key Alias
### Key Aliasing

```ts
import { useMagicKeys, whenever } from '@vueuse/core'

const { shift_cool } = useMagicKeys({
alias: {
aliasMap: {
cool: 'space'
}
})

whenever(cool, () => console.log('Shift + Space have been pressed'))
whenever(shift_cool, () => console.log('Shift + Space have been pressed'))
```

By default, we have some preconfigured alias for common practices.

For example: `ctrl` -> `control` and `option` -> `meta`.
By default, we have some [preconfigured alias for common practices](https://github.com/vueuse/vueuse/blob/main/packages/core/useMagicKeys/aliasMap.ts).

### Custom Event Handler

Expand Down
12 changes: 3 additions & 9 deletions packages/core/useMagicKeys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { computed, ComputedRef, reactive, ref, unref } from 'vue-demi'
import { MaybeRef, noop } from '@vueuse/shared'
import { useEventListener } from '../useEventListener'
import { defaultWindow } from '../_configurable'
import { DefaultMagicKeysAliasMap } from './aliasMap'

export interface UseMagicKeysOptions<Reactive extends Boolean> {
/**
Expand Down Expand Up @@ -43,15 +44,6 @@ export interface UseMagicKeysOptions<Reactive extends Boolean> {
onEventFired?: (e: KeyboardEvent) => void | boolean
}

export const DefaultMagicKeysAliasMap: Readonly<Record<string, string>> = {
ctrl: 'control',
option: 'meta',
up: 'arrowup',
down: 'arrowdown',
left: 'arrowleft',
right: 'arrowright',
}

export interface MagicKeysInternal {
/**
* A Set of currently pressed keys,
Expand Down Expand Up @@ -151,3 +143,5 @@ export function useMagicKeys(options: UseMagicKeysOptions<boolean> = {}): any {

return proxy as any
}

export { DefaultMagicKeysAliasMap } from './aliasMap'

0 comments on commit 44fa25b

Please sign in to comment.