/**
 * 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 ] ; export const preKeydownBehavior = [ { matches : keyDef => modifierKeys . includes ( keyDef . key as ModififierKey ) , handle : ( keyDef , element , { keyboardMap , keyboardState } ) => { keyboardState . modifiers [ keyDef . key as ModififierKey ] = true ; if ( keyDef . key === 'AltGraph' ) { const ctrlKeyDef = keyboardMap . find ( k => k . key === 'Control' ) ?? { 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 ; } } , } , ] ; export const preKeyupBehavior = [ { 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 = [ { matches : keyDef => keyDef . key === 'AltGraph' , handle : ( keyDef , element , { keyboardMap , keyboardState } ) => { const ctrlKeyDef = keyboardMap . find ( k => k . key === 'Control' ) ?? { key : 'Control' , code : 'Control' } ; fireEvent . keyUp ( element , getKeyEventProps ( ctrlKeyDef , keyboardState ) ) ; } , } , ] ;