Skip to content

Instantly share code, notes, and snippets.

@lindskogen
Created June 26, 2017 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lindskogen/2dda6c441fb0a0b6ee68c850082b32d9 to your computer and use it in GitHub Desktop.
Save lindskogen/2dda6c441fb0a0b6ee68c850082b32d9 to your computer and use it in GitHub Desktop.
Accessible checkbox in react
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;
.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