Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components: ColorPicker: replace global shortcut event handlers with local ones #34508

Merged
merged 2 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions packages/components/src/color-picker/alpha.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ import { noop } from 'lodash';
*/ */
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { Component, createRef } from '@wordpress/element'; import { Component, createRef } from '@wordpress/element';
import { TAB } from '@wordpress/keycodes'; import {
TAB,
UP,
DOWN,
RIGHT,
LEFT,
PAGEUP,
PAGEDOWN,
HOME,
END,
} from '@wordpress/keycodes';
import { pure } from '@wordpress/compose'; import { pure } from '@wordpress/compose';


/** /**
* Internal dependencies * Internal dependencies
*/ */
import { calculateAlphaChange } from './utils'; import { calculateAlphaChange } from './utils';
import KeyboardShortcuts from '../keyboard-shortcuts';


export class Alpha extends Component { export class Alpha extends Component {
constructor() { constructor() {
Expand All @@ -54,6 +63,7 @@ export class Alpha extends Component {
this.handleChange = this.handleChange.bind( this ); this.handleChange = this.handleChange.bind( this );
this.handleMouseDown = this.handleMouseDown.bind( this ); this.handleMouseDown = this.handleMouseDown.bind( this );
this.handleMouseUp = this.handleMouseUp.bind( this ); this.handleMouseUp = this.handleMouseUp.bind( this );
this.handleKeyDown = this.handleKeyDown.bind( this );
} }


componentWillUnmount() { componentWillUnmount() {
Expand Down Expand Up @@ -109,18 +119,35 @@ export class Alpha extends Component {
this.unbindEventListeners(); this.unbindEventListeners();
} }


preventKeyEvents( event ) {
if ( event.keyCode === TAB ) {
return;
}
event.preventDefault();
}

unbindEventListeners() { unbindEventListeners() {
window.removeEventListener( 'mousemove', this.handleChange ); window.removeEventListener( 'mousemove', this.handleChange );
window.removeEventListener( 'mouseup', this.handleMouseUp ); window.removeEventListener( 'mouseup', this.handleMouseUp );
} }


handleKeyDown( event ) {
const { keyCode, shiftKey } = event;
const shortcuts = {
[ UP ]: () => this.increase( shiftKey ? 0.1 : 0.01 ),
[ RIGHT ]: () => this.increase( shiftKey ? 0.1 : 0.01 ),
[ PAGEUP ]: () => this.increase( 0.1 ),
[ END ]: () => this.increase( 1 ),
[ DOWN ]: () => this.decrease( shiftKey ? 0.1 : 0.01 ),
[ LEFT ]: () => this.decrease( shiftKey ? 0.1 : 0.01 ),
[ PAGEDOWN ]: () => this.decrease( 0.1 ),
[ HOME ]: () => this.decrease( 1 ),
};

for ( const code in shortcuts ) {
if ( code === String( keyCode ) ) {
shortcuts[ keyCode ]();
}
}

if ( keyCode !== TAB ) {
event.preventDefault();
}
}

render() { render() {
const { rgb } = this.props; const { rgb } = this.props;
const rgbString = `${ rgb.r },${ rgb.g },${ rgb.b }`; const rgbString = `${ rgb.r },${ rgb.g },${ rgb.b }`;
Expand All @@ -129,23 +156,7 @@ export class Alpha extends Component {
}; };
const pointerLocation = { left: `${ rgb.a * 100 }%` }; const pointerLocation = { left: `${ rgb.a * 100 }%` };


const shortcuts = {
up: () => this.increase(),
right: () => this.increase(),
'shift+up': () => this.increase( 0.1 ),
'shift+right': () => this.increase( 0.1 ),
pageup: () => this.increase( 0.1 ),
end: () => this.increase( 1 ),
down: () => this.decrease(),
left: () => this.decrease(),
'shift+down': () => this.decrease( 0.1 ),
'shift+left': () => this.decrease( 0.1 ),
pagedown: () => this.decrease( 0.1 ),
home: () => this.decrease( 1 ),
};

return ( return (
<KeyboardShortcuts shortcuts={ shortcuts }>
<div className="components-color-picker__alpha"> <div className="components-color-picker__alpha">
<div <div
className="components-color-picker__alpha-gradient" className="components-color-picker__alpha-gradient"
Expand All @@ -171,12 +182,11 @@ export class Alpha extends Component {
) } ) }
className="components-color-picker__alpha-pointer" className="components-color-picker__alpha-pointer"
style={ pointerLocation } style={ pointerLocation }
onKeyDown={ this.preventKeyEvents } onKeyDown={ this.handleKeyDown }
/> />
</div> </div>
{ /* eslint-enable jsx-a11y/no-static-element-interactions */ } { /* eslint-enable jsx-a11y/no-static-element-interactions */ }
</div> </div>
</KeyboardShortcuts>
); );
} }
} }
Expand Down
68 changes: 38 additions & 30 deletions packages/components/src/color-picker/hue.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@ import { noop } from 'lodash';
import { compose, pure, withInstanceId } from '@wordpress/compose'; import { compose, pure, withInstanceId } from '@wordpress/compose';
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { Component, createRef } from '@wordpress/element'; import { Component, createRef } from '@wordpress/element';
import { TAB } from '@wordpress/keycodes'; import {
TAB,
UP,
DOWN,
RIGHT,
LEFT,
PAGEUP,
PAGEDOWN,
HOME,
END,
} from '@wordpress/keycodes';


/** /**
* Internal dependencies * Internal dependencies
*/ */
import { calculateHueChange } from './utils'; import { calculateHueChange } from './utils';
import KeyboardShortcuts from '../keyboard-shortcuts';
import { VisuallyHidden } from '../visually-hidden'; import { VisuallyHidden } from '../visually-hidden';


export class Hue extends Component { export class Hue extends Component {
Expand All @@ -55,6 +64,7 @@ export class Hue extends Component {
this.handleChange = this.handleChange.bind( this ); this.handleChange = this.handleChange.bind( this );
this.handleMouseDown = this.handleMouseDown.bind( this ); this.handleMouseDown = this.handleMouseDown.bind( this );
this.handleMouseUp = this.handleMouseUp.bind( this ); this.handleMouseUp = this.handleMouseUp.bind( this );
this.handleKeyDown = this.handleKeyDown.bind( this );
} }


componentWillUnmount() { componentWillUnmount() {
Expand Down Expand Up @@ -107,39 +117,40 @@ export class Hue extends Component {
this.unbindEventListeners(); this.unbindEventListeners();
} }


preventKeyEvents( event ) {
if ( event.keyCode === TAB ) {
return;
}
event.preventDefault();
}

unbindEventListeners() { unbindEventListeners() {
window.removeEventListener( 'mousemove', this.handleChange ); window.removeEventListener( 'mousemove', this.handleChange );
window.removeEventListener( 'mouseup', this.handleMouseUp ); window.removeEventListener( 'mouseup', this.handleMouseUp );
} }


handleKeyDown( event ) {
const { keyCode, shiftKey } = event;
const shortcuts = {
[ UP ]: () => this.increase( shiftKey ? 10 : 1 ),
[ RIGHT ]: () => this.increase( shiftKey ? 10 : 1 ),
[ PAGEUP ]: () => this.increase( 10 ),
[ END ]: () => this.increase( 359 ),
[ DOWN ]: () => this.decrease( shiftKey ? 10 : 1 ),
[ LEFT ]: () => this.decrease( shiftKey ? 10 : 1 ),
[ PAGEDOWN ]: () => this.decrease( 10 ),
[ HOME ]: () => this.decrease( 359 ),
};

for ( const code in shortcuts ) {
if ( code === String( keyCode ) ) {
shortcuts[ keyCode ]();
}
}

if ( keyCode !== TAB ) {
event.preventDefault();
}
}

render() { render() {
const { hsl = {}, instanceId } = this.props; const { hsl = {}, instanceId } = this.props;

const pointerLocation = { left: `${ ( hsl.h * 100 ) / 360 }%` }; const pointerLocation = { left: `${ ( hsl.h * 100 ) / 360 }%` };
const shortcuts = {
up: () => this.increase(),
right: () => this.increase(),
'shift+up': () => this.increase( 10 ),
'shift+right': () => this.increase( 10 ),
pageup: () => this.increase( 10 ),
end: () => this.increase( 359 ),
down: () => this.decrease(),
left: () => this.decrease(),
'shift+down': () => this.decrease( 10 ),
'shift+left': () => this.decrease( 10 ),
pagedown: () => this.decrease( 10 ),
home: () => this.decrease( 359 ),
};


return ( return (
<KeyboardShortcuts shortcuts={ shortcuts }>
<div className="components-color-picker__hue"> <div className="components-color-picker__hue">
<div className="components-color-picker__hue-gradient" /> <div className="components-color-picker__hue-gradient" />
{ /* eslint-disable jsx-a11y/no-static-element-interactions */ } { /* eslint-disable jsx-a11y/no-static-element-interactions */ }
Expand All @@ -163,20 +174,17 @@ export class Hue extends Component {
aria-describedby={ `components-color-picker__hue-description-${ instanceId }` } aria-describedby={ `components-color-picker__hue-description-${ instanceId }` }
className="components-color-picker__hue-pointer" className="components-color-picker__hue-pointer"
style={ pointerLocation } style={ pointerLocation }
onKeyDown={ this.preventKeyEvents } onKeyDown={ this.handleKeyDown }
/> />
<VisuallyHidden <VisuallyHidden
as="p" as="p"
id={ `components-color-picker__hue-description-${ instanceId }` } id={ `components-color-picker__hue-description-${ instanceId }` }
> >
{ __( { __( 'Move the arrow left or right to change hue.' ) }
'Move the arrow left or right to change hue.'
) }
</VisuallyHidden> </VisuallyHidden>
</div> </div>
{ /* eslint-enable jsx-a11y/no-static-element-interactions */ } { /* eslint-enable jsx-a11y/no-static-element-interactions */ }
</div> </div>
</KeyboardShortcuts>
); );
} }
} }
Expand Down
63 changes: 37 additions & 26 deletions packages/components/src/color-picker/saturation.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,15 +35,24 @@ import { clamp, noop, throttle } from 'lodash';
*/ */
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { Component, createRef } from '@wordpress/element'; import { Component, createRef } from '@wordpress/element';
import { TAB } from '@wordpress/keycodes'; import {
TAB,
UP,
DOWN,
RIGHT,
LEFT,
PAGEUP,
PAGEDOWN,
HOME,
END,
} from '@wordpress/keycodes';
import { compose, pure, withInstanceId } from '@wordpress/compose'; import { compose, pure, withInstanceId } from '@wordpress/compose';


/** /**
* Internal dependencies * Internal dependencies
*/ */
import { calculateSaturationChange } from './utils'; import { calculateSaturationChange } from './utils';
import Button from '../button'; import Button from '../button';
import KeyboardShortcuts from '../keyboard-shortcuts';
import { VisuallyHidden } from '../visually-hidden'; import { VisuallyHidden } from '../visually-hidden';


export class Saturation extends Component { export class Saturation extends Component {
Expand All @@ -60,6 +69,7 @@ export class Saturation extends Component {
this.handleChange = this.handleChange.bind( this ); this.handleChange = this.handleChange.bind( this );
this.handleMouseDown = this.handleMouseDown.bind( this ); this.handleMouseDown = this.handleMouseDown.bind( this );
this.handleMouseUp = this.handleMouseUp.bind( this ); this.handleMouseUp = this.handleMouseUp.bind( this );
this.handleKeyDown = this.handleKeyDown.bind( this );
} }


componentWillUnmount() { componentWillUnmount() {
Expand Down Expand Up @@ -119,42 +129,44 @@ export class Saturation extends Component {
this.unbindEventListeners(); this.unbindEventListeners();
} }


preventKeyEvents( event ) {
if ( event.keyCode === TAB ) {
return;
}
event.preventDefault();
}

unbindEventListeners() { unbindEventListeners() {
window.removeEventListener( 'mousemove', this.handleChange ); window.removeEventListener( 'mousemove', this.handleChange );
window.removeEventListener( 'mouseup', this.handleMouseUp ); window.removeEventListener( 'mouseup', this.handleMouseUp );
} }


handleKeyDown( event ) {
const { keyCode, shiftKey } = event;
const shortcuts = {
[ UP ]: () => this.brighten( shiftKey ? 0.1 : 0.01 ),
[ PAGEUP ]: () => this.brighten( 1 ),
[ DOWN ]: () => this.brighten( shiftKey ? -0.1 : -0.01 ),
[ PAGEDOWN ]: () => this.brighten( -1 ),
[ RIGHT ]: () => this.saturate( shiftKey ? 0.1 : 0.01 ),
[ END ]: () => this.saturate( 1 ),
[ LEFT ]: () => this.saturate( shiftKey ? -0.1 : -0.01 ),
[ HOME ]: () => this.saturate( -1 ),
};

for ( const code in shortcuts ) {
if ( code === String( keyCode ) ) {
shortcuts[ keyCode ]();
}
}

if ( keyCode !== TAB ) {
event.preventDefault();
}
}

render() { render() {
const { hsv, hsl, instanceId } = this.props; const { hsv, hsl, instanceId } = this.props;
const pointerLocation = { const pointerLocation = {
top: `${ -hsv.v + 100 }%`, top: `${ -hsv.v + 100 }%`,
left: `${ hsv.s }%`, left: `${ hsv.s }%`,
}; };
const shortcuts = {
up: () => this.brighten(),
'shift+up': () => this.brighten( 0.1 ),
pageup: () => this.brighten( 1 ),
down: () => this.brighten( -0.01 ),
'shift+down': () => this.brighten( -0.1 ),
pagedown: () => this.brighten( -1 ),
right: () => this.saturate(),
'shift+right': () => this.saturate( 0.1 ),
end: () => this.saturate( 1 ),
left: () => this.saturate( -0.01 ),
'shift+left': () => this.saturate( -0.1 ),
home: () => this.saturate( -1 ),
};


/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */ /* eslint-disable jsx-a11y/no-noninteractive-element-interactions */
return ( return (
<KeyboardShortcuts shortcuts={ shortcuts }>
<div <div
style={ { background: `hsl(${ hsl.h },100%, 50%)` } } style={ { background: `hsl(${ hsl.h },100%, 50%)` } }
className="components-color-picker__saturation-color" className="components-color-picker__saturation-color"
Expand All @@ -171,7 +183,7 @@ export class Saturation extends Component {
aria-describedby={ `color-picker-saturation-${ instanceId }` } aria-describedby={ `color-picker-saturation-${ instanceId }` }
className="components-color-picker__saturation-pointer" className="components-color-picker__saturation-pointer"
style={ pointerLocation } style={ pointerLocation }
onKeyDown={ this.preventKeyEvents } onKeyDown={ this.handleKeyDown }
/> />
<VisuallyHidden <VisuallyHidden
id={ `color-picker-saturation-${ instanceId }` } id={ `color-picker-saturation-${ instanceId }` }
Expand All @@ -181,7 +193,6 @@ export class Saturation extends Component {
) } ) }
</VisuallyHidden> </VisuallyHidden>
</div> </div>
</KeyboardShortcuts>
); );
/* eslint-enable jsx-a11y/no-noninteractive-element-interactions */ /* eslint-enable jsx-a11y/no-noninteractive-element-interactions */
} }
Expand Down
Loading