Merge pull request #3726 from matrix-org/t3chguy/fix_ToggleSwitch_a11y
Fix ToggleSwitch A11Y (trapping tab and switch v. checkbox)
This commit is contained in:
commit
2313285be2
1 changed files with 6 additions and 20 deletions
|
@ -19,46 +19,32 @@ import React from "react";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
|
|
||||||
import {KeyCode} from "../../../Keyboard";
|
import sdk from "../../../index";
|
||||||
|
|
||||||
// Controlled Toggle Switch element
|
// Controlled Toggle Switch element, written with Accessibility in mind
|
||||||
const ToggleSwitch = ({checked, disabled=false, onChange, ...props}) => {
|
const ToggleSwitch = ({checked, disabled=false, onChange, ...props}) => {
|
||||||
const _onClick = (e) => {
|
const _onClick = (e) => {
|
||||||
e.stopPropagation();
|
|
||||||
e.preventDefault();
|
|
||||||
if (disabled) return;
|
if (disabled) return;
|
||||||
|
|
||||||
onChange(!checked);
|
onChange(!checked);
|
||||||
};
|
};
|
||||||
|
|
||||||
const _onKeyDown = (e) => {
|
|
||||||
e.stopPropagation();
|
|
||||||
e.preventDefault();
|
|
||||||
if (disabled) return;
|
|
||||||
|
|
||||||
if (e.keyCode === KeyCode.ENTER || e.keyCode === KeyCode.SPACE) {
|
|
||||||
onChange(!checked);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const classes = classNames({
|
const classes = classNames({
|
||||||
"mx_ToggleSwitch": true,
|
"mx_ToggleSwitch": true,
|
||||||
"mx_ToggleSwitch_on": checked,
|
"mx_ToggleSwitch_on": checked,
|
||||||
"mx_ToggleSwitch_enabled": !disabled,
|
"mx_ToggleSwitch_enabled": !disabled,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const AccessibleButton = sdk.getComponent("elements.AccessibleButton");
|
||||||
return (
|
return (
|
||||||
<div {...props}
|
<AccessibleButton {...props}
|
||||||
className={classes}
|
className={classes}
|
||||||
onClick={_onClick}
|
onClick={_onClick}
|
||||||
onKeyDown={_onKeyDown}
|
role="switch"
|
||||||
role="checkbox"
|
|
||||||
aria-checked={checked}
|
aria-checked={checked}
|
||||||
aria-disabled={disabled}
|
aria-disabled={disabled}
|
||||||
tabIndex={0}
|
|
||||||
>
|
>
|
||||||
<div className="mx_ToggleSwitch_ball" />
|
<div className="mx_ToggleSwitch_ball" />
|
||||||
</div>
|
</AccessibleButton>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue