-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(keyboard): apply modifier state (#815)
- Loading branch information
1 parent
684451f
commit e9635f6
Showing
17 changed files
with
256 additions
and
148 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* This file should contain behavior for modifier keys: | ||
* https://www.w3.org/TR/uievents-key/#keys-modifier | ||
*/ | ||
|
||
import {fireEvent} from '@testing-library/dom' | ||
import {getKeyEventProps} from '../../utils' | ||
import {behaviorPlugin} from '../types' | ||
|
||
const modifierKeys = [ | ||
'Alt', | ||
'AltGraph', | ||
'Control', | ||
'Fn', | ||
'Meta', | ||
'Shift', | ||
'Symbol', | ||
] as const | ||
type ModififierKey = typeof modifierKeys[number] | ||
|
||
const modifierLocks = [ | ||
'CapsLock', | ||
'FnLock', | ||
'NumLock', | ||
'ScrollLock', | ||
'SymbolLock', | ||
] as const | ||
type ModififierLockKey = typeof modifierLocks[number] | ||
|
||
// modifierKeys switch on the modifier BEFORE the keydown event | ||
export const preKeydownBehavior: behaviorPlugin[] = [ | ||
{ | ||
matches: keyDef => modifierKeys.includes(keyDef.key as ModififierKey), | ||
handle: (keyDef, element, {keyboardMap, keyboardState}) => { | ||
keyboardState.modifiers[keyDef.key as ModififierKey] = true | ||
|
||
// AltGraph produces an extra keydown for Control | ||
// The modifier does not change | ||
if (keyDef.key === 'AltGraph') { | ||
const ctrlKeyDef = keyboardMap.find( | ||
k => k.key === 'Control', | ||
) ?? /* istanbul ignore next */ {key: 'Control', code: 'Control'} | ||
fireEvent.keyDown(element, getKeyEventProps(ctrlKeyDef, keyboardState)) | ||
} | ||
}, | ||
}, | ||
{ | ||
matches: keyDef => modifierLocks.includes(keyDef.key as ModififierLockKey), | ||
handle: (keyDef, element, {keyboardState}) => { | ||
const key = keyDef.key as ModififierLockKey | ||
keyboardState.modifierPhase[key] = keyboardState.modifiers[key] | ||
|
||
if (!keyboardState.modifierPhase[key]) { | ||
keyboardState.modifiers[key] = true | ||
} | ||
}, | ||
}, | ||
] | ||
|
||
// modifierKeys switch off the modifier BEFORE the keyup event | ||
export const preKeyupBehavior: behaviorPlugin[] = [ | ||
{ | ||
matches: keyDef => modifierKeys.includes(keyDef.key as ModififierKey), | ||
handle: (keyDef, element, {keyboardState}) => { | ||
keyboardState.modifiers[keyDef.key as ModififierKey] = false | ||
}, | ||
}, | ||
{ | ||
matches: keyDef => modifierLocks.includes(keyDef.key as ModififierLockKey), | ||
handle: (keyDef, element, {keyboardState}) => { | ||
const key = keyDef.key as ModififierLockKey | ||
|
||
if (keyboardState.modifierPhase[key]) { | ||
keyboardState.modifiers[key] = false | ||
} | ||
}, | ||
}, | ||
] | ||
|
||
export const postKeyupBehavior: behaviorPlugin[] = [ | ||
// AltGraph produces an extra keyup for Control | ||
// The modifier does not change | ||
{ | ||
matches: keyDef => keyDef.key === 'AltGraph', | ||
handle: (keyDef, element, {keyboardMap, keyboardState}) => { | ||
const ctrlKeyDef = keyboardMap.find( | ||
k => k.key === 'Control', | ||
) ?? /* istanbul ignore next */ {key: 'Control', code: 'Control'} | ||
fireEvent.keyUp(element, getKeyEventProps(ctrlKeyDef, keyboardState)) | ||
}, | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {keyboardKey, keyboardState} from '../../keyboard/types' | ||
import {getUIEventModifiers} from './getUIEventModifiers' | ||
|
||
export function getKeyEventProps(keyDef: keyboardKey, state: keyboardState) { | ||
return { | ||
key: keyDef.key, | ||
code: keyDef.code, | ||
...getUIEventModifiers(state), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type {keyboardState} from '../../keyboard' | ||
|
||
export function getUIEventModifiers(keyboardState: keyboardState) { | ||
return { | ||
altKey: keyboardState.modifiers.Alt, | ||
ctrlKey: keyboardState.modifiers.Control, | ||
metaKey: keyboardState.modifiers.Meta, | ||
shiftKey: keyboardState.modifiers.Shift, | ||
modifierAltGraph: keyboardState.modifiers.AltGraph, | ||
modifierCapsLock: keyboardState.modifiers.CapsLock, | ||
modifierFn: keyboardState.modifiers.Fn, | ||
modifierFnLock: keyboardState.modifiers.FnLock, | ||
modifierNumLock: keyboardState.modifiers.NumLock, | ||
modifierScrollLock: keyboardState.modifiers.ScrollLock, | ||
modifierSymbol: keyboardState.modifiers.Symbol, | ||
modifierSymbolLock: keyboardState.modifiers.SymbolLock, | ||
} | ||
} |
Oops, something went wrong.