Skip to content

Commit

Permalink
Accessibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Devlamynck committed Jan 30, 2025
1 parent 6a6e75c commit bee0527
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion elm/CookiesRegulation.elm
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ update msg model =
MsgOpenModal ->
( model
|> openModalAction
, Cmd.batch [ modalOpened (), modalBodySizeCmd, Dom.focus "cookies-regulation-modal" |> Task.attempt MsgFocus ]
, Cmd.batch [ modalOpened (), modalBodySizeCmd ]
)

MsgCloseModal ->
Expand Down
56 changes: 45 additions & 11 deletions js/cookies-regulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Elm } from './../elm/CookiesRegulation.elm';
import { v4 as uuidv4 } from 'uuid';

const DECISION_COOKIE_NAME = 'cookie_regulation_decision';
const SELECTORS = ':not([tabindex="-1"]):not([disabled]):not([type=hidden])';
const FOCUSABLE_SELECTOR = 'a[href]' + SELECTORS + ', button' + SELECTORS + ', textarea' + SELECTORS + ', input' + SELECTORS + ', select' + SELECTORS + ':not(.ts-hidden-accessible), *[tabindex]' + SELECTORS + ', *[contenteditable]' + SELECTORS + '';

class CookiesRegulation {
constructor() {
Expand Down Expand Up @@ -50,17 +52,9 @@ class CookiesRegulation {
},
});

window.cookiesRegulationBlock.ports.modalOpened.subscribe(
function () {
document.querySelector('body').classList.add('cookies-regulation-modal-open');
}
);

window.cookiesRegulationBlock.ports.modalClosed.subscribe(
function () {
document.querySelector('body').classList.remove('cookies-regulation-modal-open');
}
);
const that = this;
window.cookiesRegulationBlock.ports.modalOpened.subscribe(() => that.modalOpened(that));
window.cookiesRegulationBlock.ports.modalClosed.subscribe(() => that.modalClosed(that));

window.cookiesRegulationBlock.ports.initializeService.subscribe((serviceName) => this.initializeService(serviceName));
window.cookiesRegulationBlock.ports.setPreferences.subscribe((data) => this.setPreferences(data));
Expand Down Expand Up @@ -97,6 +91,46 @@ class CookiesRegulation {
window.location.reload();
}
}

modalOpened (that) {
document.querySelector('body').classList.add('cookies-regulation-modal-open');
const modal = document.getElementById('cookies-regulation-modal');

modal.addEventListener("keydown", that.loopFocus);
requestAnimationFrame(() => {
modal.querySelector(FOCUSABLE_SELECTOR).focus({ focusVisible: true });
});
}

modalClosed (that) {
document.querySelector('body').classList.remove('cookies-regulation-modal-open');

const modal = document.getElementById('cookies-regulation-modal');
modal.removeEventListener("keydown", that.loopFocus);
}

loopFocus (event) {
if (!(event.key === 'Tab' || event.keyCode === 9)) {
return;
}

const modal = document.getElementById('cookies-regulation-modal');
var focusableEls = modal.querySelectorAll(FOCUSABLE_SELECTOR);
var firstFocusableEl = focusableEls[0];
var lastFocusableEl = focusableEls[focusableEls.length - 1];

if (event.shiftKey) {
if (document.activeElement === firstFocusableEl) {
lastFocusableEl.focus({ focusVisible: true });
event.preventDefault();
}
} else {
if (document.activeElement === lastFocusableEl) {
firstFocusableEl.focus({ focusVisible: true });
event.preventDefault();
}
}
}
}

class ConfigurationLoader {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rich-id/cookies-regulation",
"version": "0.2.7",
"version": "0.2.8",
"scripts": {
"build": "parcel build js/cookies-regulation.js",
"build-release": "parcel build --no-source-maps js/cookies-regulation.js",
Expand Down

0 comments on commit bee0527

Please sign in to comment.