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

[Draft] Make Slider and Hue Accessible #848

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/components/common/Hue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -74,6 +76,7 @@ export class Hue extends (PureComponent || Component) {
onMouseDown={ this.handleMouseDown }
onTouchMove={ this.handleChange }
onTouchStart={ this.handleChange }
onKeyDown={ this.handleChange }
>
<style>{ `
.hue-horizontal {
Expand Down
4 changes: 1 addition & 3 deletions src/components/common/Swatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { handleFocus } from '../../helpers/interaction'

import Checkboard from './Checkboard'

const ENTER = 13

export const Swatch = ({ color, style, onClick = () => {}, onHover, title = color,
children, focus, focusStyle = {} }) => {
const transparent = color === 'transparent'
Expand All @@ -25,7 +23,7 @@ export const Swatch = ({ color, style, onClick = () => {}, onHover, title = colo
})

const handleClick = e => onClick(color, e)
const handleKeyDown = e => e.keyCode === ENTER && onClick(color, e)
const handleKeyDown = e => e.key === 'Enter' && onClick(color, e)
const handleHover = e => onHover(color, e)

const optionalEvents = {}
Expand Down
2 changes: 1 addition & 1 deletion src/components/slider/SliderPointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const SliderPointer = () => {
})

return (
<div style={ styles.picker } />
<div tabIndex={ 0 } style={ styles.picker } />
)
}

Expand Down
9 changes: 8 additions & 1 deletion src/components/slider/SliderSwatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ export const SliderSwatch = ({ hsl, offset, onClick = () => {}, active, first, l
source: 'hsl',
}, e)

const handleKeyDown = e => e.key === 'Enter' && handleClick(e)

return (
<div style={ styles.swatch } onClick={ handleClick } />
<div
tabIndex={ 0 }
style={ styles.swatch }
onClick={ e => handleClick(e) }
onKeyDown={ e => handleKeyDown(e) }
/>
)
}

Expand Down
39 changes: 31 additions & 8 deletions src/helpers/hue.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
export const calculateChange = (e, direction, hsl, container) => {
const containerWidth = container.clientWidth
const containerHeight = container.clientHeight
const x = typeof e.pageX === 'number' ? e.pageX : e.touches[0].pageX
const y = typeof e.pageY === 'number' ? e.pageY : e.touches[0].pageY
const left = x - (container.getBoundingClientRect().left + window.pageXOffset)
const top = y - (container.getBoundingClientRect().top + window.pageYOffset)

if (direction === 'vertical') {
const y = typeof e.pageY === 'number' ? e.pageY : e.touches[0].pageY
const top = y - (container.getBoundingClientRect().top + window.pageYOffset)

let h
if (top < 0) {
h = 359
Expand All @@ -27,17 +26,41 @@ export const calculateChange = (e, direction, hsl, container) => {
}
}
} else {
const prevH = hsl.h
// Each step is a 0.5% movement in the slider. Repeated key press is supported when
// onChange is used, but not onChangeComplete.
const ArrowStep = containerWidth / 200
const prevLeft = prevH * containerWidth / 360
let left
if (e.key === 'ArrowLeft') {
left = prevLeft - ArrowStep
}
if (e.key === 'ArrowRight') {
left = prevLeft + ArrowStep
}
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
// Prevent web-app from scrolling horizontally, only the slider should be moving.
e.preventDefault()
}
if (!left && left !== 0) {
let x
if (typeof e.pageX === 'number') {
x = e.pageX
} else if (e.touches && e.touches.length) {
x = e.touches[0].pageX
}
left = x - (container.getBoundingClientRect().left + window.pageXOffset)
}

let h
if (left < 0) {
h = 0
} else if (left > containerWidth) {
h = 359
} else {
const percent = (left * 100) / containerWidth
h = ((360 * percent) / 100)
h = 360 * left / containerWidth
}

if (hsl.h !== h) {
if ((h || h === 0) && prevH !== h) {
return {
h,
s: hsl.s,
Expand Down