Skip to content

Commit

Permalink
[A11Y] Make checkboxes focusable (#3014)
Browse files Browse the repository at this point in the history
* Add extra feature to a11y focusring mixin

* Add visually hidden CSS class and mixin

* Visually hide checkboxes (keep in focus/a11y tree)

* Place checkbox focus ring around display element

* Improve mobile checkbox/switch accessibility
  • Loading branch information
davwheat authored Aug 16, 2021
1 parent 51ce89b commit 83529e2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
4 changes: 3 additions & 1 deletion js/src/common/components/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export default class Checkbox extends Component {
return (
<label className={className}>
<input type="checkbox" checked={this.attrs.state} disabled={this.attrs.disabled} onchange={withAttr('checked', this.onchange.bind(this))} />
<div className="Checkbox-display">{this.getDisplay()}</div>
<div className="Checkbox-display" aria-hidden="true">
{this.getDisplay()}
</div>
{vnode.children}
</label>
);
Expand Down
32 changes: 28 additions & 4 deletions less/common/Checkbox.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,42 @@
display: block;
cursor: pointer;
margin: 0;
position: relative;

input[type="checkbox"] {
position: absolute;
z-index: 1;

opacity: 0;

top: 0;
bottom: 0;
left: 0;
right: 0;

& input[type="checkbox"] {
display: none;
width: 100%;
height: 100%;

cursor: pointer;

.add-keyboard-focus-ring-nearby("+ .Checkbox-display");
}
}
.Checkbox--switch {
padding-left: 65px;
@left-pad: 65px;

padding-left: @left-pad;
margin: 5px 0;

input[type="checkbox"] {
top: -4px;
width: 50px;
height: 28px;
}

.Checkbox-display {
float: left;
margin-left: -65px;
margin-left: -@left-pad;
margin-top: -4px;
}
}
Expand Down
36 changes: 34 additions & 2 deletions less/common/mixins/accessibility.less
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
*
* For example...
*
*? button { .addKeyboardFocusRing(":focus-within") }
*? button { .add-keyboard-focus-ring(":focus-within") }
* becomes
*? button:focus-within { <styles> }
*
* AND
*
*? button { .addKeyboardFocusRing(" :focus-within") }
*? button { .add-keyboard-focus-ring(" :focus-within") }
* becomes
*? button :focus-within { <styles> }
*/
Expand All @@ -57,6 +57,38 @@
}
}

/**
* This mixin allows support for a custom element nearby the focused one
* to have a focus style applied to it
*
* For example...
*
*? button { .add-keyboard-focus-ring-nearby("+ .myOtherElement") }
* becomes
*? button:-moz-focusring + .myOtherElement { <styles> }
*? button:focus-within + .myOtherElement { <styles> }
*/
.add-keyboard-focus-ring-nearby(@nearbySelector) {
@realNearbySelector: ~"@{nearbySelector}";

// We need to declare these separately, otherwise
// browsers will ignore `:focus-visible` as they
// don't understand `:-moz-focusring`

// These are the keyboard-only versions of :focus
&:-moz-focusring {
@{realNearbySelector} {
#private.__focus-ring-styles();
}
}

&:focus-visible {
@{realNearbySelector} {
#private.__focus-ring-styles();
}
}
}

/**
* Allows an offset to be supplied for an a11y
* outline.
Expand Down
10 changes: 10 additions & 0 deletions less/common/scaffolding.less
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,13 @@ blockquote ol:last-child {
font-size: 18px;
color: @muted-more-color;
}

.visually-hidden {
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}

0 comments on commit 83529e2

Please sign in to comment.