From f3f47bb93c40d368635a1685a580f4007cc8104f Mon Sep 17 00:00:00 2001 From: eddyionescu Date: Sun, 26 Dec 2021 21:56:42 -0500 Subject: [PATCH] Make Slider and Hue Accessible This commit addresses issue https://github.com/casesandberg/react-color/issues/847 by making the hue and slider swatch focusable and controllable using arrow keys. It does this by setting tabIndex to zero and adding onKeyDown handlers to the SliderSwatch and Hue components. It also changes the detection of keys to the key property rather than keyCode which is deprecated (https://www.w3schools.com/jsref/event_key_keycode.asp). For updating the hue, changing the hue on the slider is implemented in hue.js for horizontal sliders only. For repeated key presses to work (ie. arrow key held down) the usage of onChange is required, in addition to onChangeComplete, otherwise the slider won't move while the arrow key is held down, rather needing to be pressed many times. Resolving this within the package is the scope of a future commit, as are tests and docs. --- src/components/common/Hue.js | 5 +++- src/components/common/Swatch.js | 4 +-- src/components/slider/SliderPointer.js | 2 +- src/components/slider/SliderSwatch.js | 9 +++++- src/helpers/hue.js | 39 ++++++++++++++++++++------ 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/components/common/Hue.js b/src/components/common/Hue.js index 96c6e87d..92fc6cfa 100644 --- a/src/components/common/Hue.js +++ b/src/components/common/Hue.js @@ -9,7 +9,9 @@ export class Hue extends (PureComponent || Component) { handleChange = (e) => { const change = hue.calculateChange(e, this.props.direction, this.props.hsl, this.container) - change && typeof this.props.onChange === 'function' && this.props.onChange(change, e) + if (change && typeof this.props.onChange === 'function') { + this.props.onChange(change, e) + } } handleMouseDown = (e) => { @@ -74,6 +76,7 @@ export class Hue extends (PureComponent || Component) { onMouseDown={ this.handleMouseDown } onTouchMove={ this.handleChange } onTouchStart={ this.handleChange } + onKeyDown={ this.handleChange } >