Skip to content

Commit

Permalink
Use key instead of keyCode
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Jul 7, 2022
1 parent 23171fb commit de2fd11
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions packages/components/src/focal-point-picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
__experimentalUseDragging as useDragging,
useInstanceId,
} from '@wordpress/compose';
import { UP, DOWN, LEFT, RIGHT } from '@wordpress/keycodes';

/**
* Internal dependencies
Expand All @@ -28,6 +27,8 @@ import {
} from './styles/focal-point-picker-style';
import { INITIAL_BOUNDS } from './utils';

const arrowKeyList = [ 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowUp' ];

export default function FocalPointPicker( {
autoPlay = true,
className,
Expand Down Expand Up @@ -126,14 +127,15 @@ export default function FocalPointPicker( {
};

const arrowKeyStep = ( event ) => {
const { keyCode, shiftKey } = event;
if ( ! [ UP, DOWN, LEFT, RIGHT ].includes( keyCode ) ) return;
const { key, shiftKey } = event;
if ( ! arrowKeyList.includes( key ) ) return;

event.preventDefault();
const value = { x, y };
const step = shiftKey ? 0.1 : 0.01;
const delta = keyCode === UP || keyCode === LEFT ? -1 * step : step;
const axis = keyCode === UP || keyCode === DOWN ? 'y' : 'x';
const delta =
key === 'ArrowUp' || key === 'ArrowLeft' ? -1 * step : step;
const axis = key === 'ArrowUp' || key === 'ArrowDown' ? 'y' : 'x';
value[ axis ] = parseFloat( value[ axis ] ) + delta;
sendPoint( getFinalValue( value ) );
};
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/focal-point-picker/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe( 'FocalPointPicker', () => {
// Focus and press arrow up
const dragArea = getByRole( 'button' );
dragArea.focus();
fireEvent.keyDown( dragArea, { charCode: 0, keyCode: 38 } );
fireEvent.keyDown( dragArea, { key: 'ArrowUp' } );
expect( spyChange ).toHaveBeenCalledWith( {
x: '0.14',
y: '0.61',
Expand Down

0 comments on commit de2fd11

Please sign in to comment.