Skip to content

Commit

Permalink
Added keyboard accessibility for checkboxes.
Browse files Browse the repository at this point in the history
Updated Components to improve checkbox accessibility.
  • Loading branch information
Bilge committed Dec 11, 2024
1 parent 973f08d commit 867c04e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/Build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
ref: gh-pages
path: gh-pages
Expand All @@ -33,8 +33,7 @@ jobs:
run: composer validate

- name: Cache dependencies
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: vendor
key: php-${{ hashFiles('composer.lock') }}
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/Storybook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
repository: 250/Storybook
path: sb
Expand All @@ -24,8 +24,7 @@ jobs:
php-version: 8.2

- name: Cache PHP dependencies
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: vendor
key: php-${{ hashFiles('composer.lock') }}
Expand Down
2 changes: 1 addition & 1 deletion assets/js/250.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class S250 {

// UI stuff.
S250.initRatingColourGradient();
Checkbox.initTristateCheckboxes();
Checkbox.initCheckboxes();
S250.initChevrons();

// User stuff.
Expand Down
23 changes: 20 additions & 3 deletions assets/js/Checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare global {
}

export default class Checkbox {
public static initTristateCheckboxes() {
public static initCheckboxes() {
document.querySelectorAll<HTMLDivElement>('[role=checkbox]').forEach(div => {
const
radios = [...div.querySelectorAll('input')],
Expand Down Expand Up @@ -57,8 +57,25 @@ export default class Checkbox {
}
});

// Cycle states when user presses [spacebar].
div.addEventListener('keydown', ev => ev.key === ' ' && go2(++currentState % 3));
// Cycle states when pressing [spacebar].
div.addEventListener('keydown', ev => {
if (ev.key === ' ') {
go2(++currentState % 3);

ev.preventDefault();
}
});
});

// Toggle checked state for binary checkboxes when pressing [spacebar].
document.querySelectorAll<HTMLElement>('label[tabindex]:has(input[type=checkbox])').forEach(label =>
label.addEventListener('keydown', ev => {
if (ev.key === ' ') {
label.click();

ev.preventDefault();
}
})
);
}
}
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion storybook/stories/Checkbox.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import template from '@components/checkbox.twig';
import Checkbox from '../../assets/js/Checkbox';

// Only respond to Storybook emulated DOM loaded event to prevent double-loading.
addEventListener('DOMContentLoaded', e => e.isTrusted || Checkbox.initTristateCheckboxes());
addEventListener('DOMContentLoaded', e => e.isTrusted || Checkbox.initCheckboxes());

export default {
title: 'Form/Checkbox',
Expand Down

0 comments on commit 867c04e

Please sign in to comment.