Button (Toggle)

A toggle button is similar to a checkbox because it is either selected or not selected, and every toggle button can operate independent of all other toggle buttons. In fact, a checkbox can be an acceptable alternative to a toggle button.

Turn on a screen reader to experience this example in action.

Toggle Button (ARIA)

Initial HTML Markup

        <button class="deque-button deque-button-toggle" id="toggleHtml">Toggle Button (HTML)</button>
        <span role="button" class="deque-button deque-button-toggle" id="toggleAria">Toggle Button (ARIA)</span>


Required: The complete JavaScript file (for all patterns in the library): deque-patterns.min.js

Key parts of the JavaScript file

Note: The code below is functional only in the context of the complete JavaScript file.

In the @button section:

	function bindElementToInputs(element, behavior) {
	  if (element.tagName.toUpperCase() !== 'BUTTON') {
	    (0, _keyboardUtils.onElementSpace)(element, function (e) {
	    (0, _keyboardUtils.onElementEnter)(element, function (e) {
	  element.addEventListener('click', behavior);
	function assignRoleIfNecessary(element) {
	  if (element.tagName.toLowerCase() !== 'button') {
	    element.setAttribute('role', 'button');
	    element.setAttribute('tabindex', '0');
	function initializeIcon(element) {
	  var icon = document.createElement('span');
	  icon.setAttribute('aria-hidden', true);
	  element.insertBefore(icon, element.firstChild);
	function initializeButton(element, behavior) {
	  bindElementToInputs(element, behavior);
	function initializeToggleButton(element, toggleFunction) {
	  var initialState = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
	  element.setAttribute('aria-pressed', initialState);
	  if (!toggleFunction) {
	    throw new Error('You must provide a toggle function to a toggle button. It should return \'true\' when the button should have a \'pressed\' state, and \'false\' otherwise.');
	  function toggle(e) {
	    var toggledOn = toggleFunction(e);
	    element.setAttribute('aria-pressed', toggledOn ? 'true' : 'false');
	  bindElementToInputs(element, toggle);

In the @keyboardUtils section:

	var KEYS = exports.KEYS = {
	  TAB: 9,
	  ENTER: 13,
	  SHIFT: 16,
	  CTRL: 17,
	  ALT: 18,
	  ESCAPE: 27,
	  SPACE: 32,
	  LEFT: 37,
	  RIGHT: 39,
	  UP: 38,
	  DOWN: 40,
	  F10: 121,
	  HOME: 36,
	  END: 35,
	  PAGE_UP: 33,
	  PAGE_DOWN: 34
	function bindElementToEventValue(element, eventName, testValue, handler) {
	  function localHandler(e) {
	    if (e.which === testValue) {
	  element.addEventListener(eventName, localHandler);
	  return function () {
	    element.removeEventListener(eventName, localHandler);
	function bindElementToKeypressValue(element, testValue, handler) {
	  return bindElementToEventValue(element, 'keypress', testValue, handler);
	function bindElementToKeydownValue(element, testValue, handler) {
	  return bindElementToEventValue(element, 'keydown', testValue, handler);
	function onElementEnter(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.ENTER, handler);
	function onElementEscape(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.ESCAPE, handler);
	function onElementSpace(element, handler) {
	  return bindElementToKeypressValue(element, KEYS.SPACE, handler);
	function onElementLeft(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.LEFT, handler);
	function onElementRight(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.RIGHT, handler);
	function onElementUp(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.UP, handler);
	function onElementDown(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.DOWN, handler);
	function onElementHome(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.HOME, handler);
	function onElementEnd(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.END, handler);
	function onElementPageUp(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.PAGE_UP, handler);
	function onElementPageDown(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.PAGE_DOWN, handler);
	function onElementF10(element, handler) {
	  return bindElementToKeydownValue(element, KEYS.F10, handler);
	function isAlphaNumeric(charCode) {
	  return charCode >= 48 && charCode <= 90 /* numbers, uppercase letters */
	  || charCode >= 97 && charCode <= 122 /* lowercase letters */;
	function onElementCharacter(element, handler) {
	  function localHandler(e) {
	    var charCode = e.which;
	    if (isAlphaNumeric(charCode)) {
	  element.addEventListener('keypress', localHandler);
	  return function () {
	    element.removeEventListener('keypress', localHandler);
	function trapEnter(element) {
	  onElementEnter(element, function (e) {

Required: Initialization JavaScript (with functionality specific to individual pattern instances):

var toggleHtml = document.getElementById('toggleHtml');
var toggleAria = document.getElementById('toggleAria');

function toggleHandler(e) {
  var currentlyPressed = e.target.getAttribute('aria-pressed') === 'true';
  return !currentlyPressed;

deque.button.initializeToggleButton(toggleHtml, toggleHandler);
deque.button.initializeToggleButton(toggleAria, toggleHandler);


Required: The complete CSS file (for all patterns in the library): deque-patterns.min.css

Key styles within the CSS file (other styles may also be necessary):

[role='button'].deque-button.deque-button-toggle {
  min-width: auto;
[role='button'].deque-button.deque-button-toggle[aria-pressed='true'] {
  background-color: #2f2f2f;
  color: #ffffff;
  border: 1px solid #000000;
[role='button'].deque-button.deque-button-toggle[aria-pressed='true']:hover {
  background-color: #2f2f2f;
  color: #ffffff;
[role='button'].deque-button.deque-button-toggle[aria-pressed='true']::before {
  font-family: 'mwf-glyphs';
  content: '\E73E';
  font-size: 12px;
  font-weight: bold;
  padding-right: 5px;
  float: left;
.deque-button {
  font-family: system-ui;
[role='button'].deque-button {
  white-space: normal;

Implementation Instructions

Step 1: Add Dependencies

CSS: Add deque-patterns.min.css in the <head> of the document.

<link rel="stylesheet" type="text/css" href="deque-patterns.min.css">

JavaScript: Add a script link to deque-patterns.min.js to the bottom of the page.

<script type="text/javascript" src="deque-patterns.min.js"></script>

Fonts: Font glyphs are required for this pattern (as currently styled). The CSS file refers to them in a folder called "_fonts" as a child element of the folder in which the CSS file resides. The fonts, from the Microsoft Web Framework (MWF) are available at these links:

Step 2: Add HTML

  • Wrap the HTML in <div class="deque-wrapper">, for styling purposes.
  • Identify the elements to be used as buttons with a unique ID. The element can be a <span> or any other element.

Step 3: Add JavaScript

Call deque.button.initializeToggleButton with both of the following:

  1. the element you want to behave like a button
  2. the function you want executed when the button is activated


Your function must return either true or false, because its return value determines the value of aria-pressed on the button element.