Search results

Accessibility in React Toggle Switch Button component

09 Jun 2021 / 2 minutes to read

The web accessibility makes web content and web applications more accessible for people with disabilities. It especially helps in dynamic content change and development of advanced user interface controls with AJAX, HTML, JavaScript, and related technologies.

Switch provides built-in compliance with WAI-ARIA specifications. WAI-ARIA support is achieved through the attributes like aria-checked and aria-disabled. It helps the people with disabilities by providing information about the widget for assistive technology in the screen readers, such as screen readers.

Properties Functionality
role Indicates the switch component.
aria-checked Indicates whether the input is checked, unchecked.
aria-disabled Indicates that the element is perceivable but disabled, so it is not editable or otherwise operable.

Keyboard interaction

Keyboard shortcuts Actions
Space When the switch has focus, pressing the Space key changes the state of the switch.
Source
Preview
app.tsx
index.html
index.css
app.jsx
Copied to clipboard
import { enableRipple } from '@syncfusion/ej2-base';
import { SwitchComponent } from '@syncfusion/ej2-react-buttons';
import * as React from 'react';
import * as ReactDom from 'react-dom';

enableRipple(true);

// To render Switch.
class App extends React.Component<{}, {}> {
  public render() {
    return (
        <table className='size'>
            <tr>
                <td className='lSize'>Checked</td>
                <td>
                    <SwitchComponent checked={true} />
                </td>
            </tr>
            <tr>
                <td className='lSize'>Unchecked</td>
                <td>
                    <SwitchComponent />
                </td>
            </tr>
        </table>
    );
  }
}
ReactDom.render(<App />, document.getElementById('switch'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Switch</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/fabric.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='switch'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>
Copied to clipboard
#loader {
  color: #008cff;
  height: 40px;
  width: 30%;
  position: absolute;
  top: 45%;
  left: 45%;
}

.e-switch-wrapper {
  margin-top: 4px;
}

.size tr td {
  padding: 10px;
}

.size .lSize {
  font-family: "Roboto", "Segoe UI", "GeezaPro", "DejaVu Serif", "sans-serif";
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}
Copied to clipboard
import { enableRipple } from '@syncfusion/ej2-base';
import { SwitchComponent } from '@syncfusion/ej2-react-buttons';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
// To render Switch.
class App extends React.Component {
    render() {
        return (<table className='size'>
            <tr>
                <td className='lSize'>Checked</td>
                <td>
                    <SwitchComponent checked={true}/>
                </td>
            </tr>
            <tr>
                <td className='lSize'>Unchecked</td>
                <td>
                    <SwitchComponent />
                </td>
            </tr>
        </table>);
    }
}
ReactDom.render(<App />, document.getElementById('switch'));