Created
June 26, 2017 14:59
-
-
Save lindskogen/2dda6c441fb0a0b6ee68c850082b32d9 to your computer and use it in GitHub Desktop.
Accessible checkbox in react
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { PureComponent } from 'react'; | |
import cx from 'classnames'; | |
import './checkbox.styl'; | |
const SPACE_KEY = 32; | |
const filterSpacePressed = (event, callback) => { | |
if (event.which === SPACE_KEY) { | |
event.preventDefault(); | |
callback(event); | |
} | |
}; | |
const Checkbox = ({ disabled, checked, onChange }) => | |
<div | |
role="checkbox" | |
aria-checked={checked} | |
tabIndex={disabled ? -1 : 0} | |
className={cx('roleCheckbox', { checked, disabled })} | |
onClick={!disabled && onChange} | |
onKeyDown={!disabled && (event => filterSpacePressed(event, onChange))}> | |
{checked ? '✔' : null} | |
</div>; | |
export default Checkbox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.roleCheckbox | |
display flex | |
align-items center | |
justify-content center | |
border-radius 4px | |
border 2px solid #999 | |
width 26px | |
height 26px | |
svg | |
fill white | |
&.checked | |
background-color #2574a8 | |
border-color #2574a8 | |
&.disabled | |
background-color #999 | |
border-color #999 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment