use checkbox input for toggle (#872)

main
Michael Fix 5 years ago committed by repo-ranger[bot]
parent c4f82089e6
commit 884e6c526c

@ -10,44 +10,45 @@ class Toggle extends React.PureComponent {
toggle = () => this.props.onChange(!this.props.enabled) toggle = () => this.props.onChange(!this.props.enabled)
handleKeydown = e => {
if (e.key === 'Enter') this.toggle()
if (e.key === ' ') {
e.preventDefault()
this.toggle()
}
}
render() { render() {
// TODO use input[type=["checkbox"] /> instead of div
return ( return (
<div <div className={`toggle ${this.props.className}`}>
role="checkbox"
tabIndex={0}
className={`toggle ${this.props.className}`}
onClick={this.toggle}
aria-checked={this.props.enabled}
onKeyDown={this.handleKeydown}
>
<label className="label">{this.props.label}</label> <label className="label">{this.props.label}</label>
<input
type="checkbox"
checked={this.props.enabled}
onClick={this.toggle}
aria-checked={this.props.enabled}
/>
{this.props.enabled ? <Checkmark /> : <div className="checkmark-disabled" />} {this.props.enabled ? <Checkmark /> : <div className="checkmark-disabled" />}
<style jsx> <style jsx>
{` {`
.toggle { .toggle {
position: relative;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: ${this.props.center ? 'center' : 'space-between'}; justify-content: space-between;
cursor: pointer;
user-select: none; user-select: none;
padding: ${this.props.padding || '8px 12px 8px 8px'}; padding: ${this.props.padding || '8px 12px 8px 8px'};
outline: none;
} }
.toggle { input {
outline: none; cursor: pointer;
margin: 0;
width: 100%;
height: 100%;
box-sizing: border-box;
position: absolute;
opacity: 0;
top: 0;
left: 0;
right: 0;
bottom: 0;
} }
.toggle:focus-within :global(svg), input:focus + :global(svg),
.toggle:focus-within .checkmark-disabled { input:focus + .checkmark-disabled {
outline: 4px auto -webkit-focus-ring-color; outline: 4px auto -webkit-focus-ring-color;
} }

Loading…
Cancel
Save