Skip to content

Commit

Permalink
Merge pull request #1158 from nicozerr/a11y-fix
Browse files Browse the repository at this point in the history
A11y fixes
  • Loading branch information
AmauriC authored Oct 17, 2023
2 parents 374c8fa + 1ff5dad commit 4bbd5e7
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions tarteaucitron.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ var tarteaucitron = {
html = '',
index,
orientation = 'Top',
modalAttrs = '',
cat = ['ads', 'analytic', 'api', 'comment', 'social', 'support', 'video', 'other'],
i;

Expand Down Expand Up @@ -419,10 +420,14 @@ var tarteaucitron = {
orientation = 'Bottom';
}

if (tarteaucitron.parameters.orientation === 'middle' || tarteaucitron.parameters.orientation === 'popup') {
modalAttrs = ' role="dialog" aria-modal="true" aria-labelledby="tac_title"';
}

if (tarteaucitron.parameters.highPrivacy && !tarteaucitron.parameters.AcceptAllCta) {
html += '<div tabindex="-1" id="tarteaucitronAlertBig" class="tarteaucitronAlertBig' + orientation + '">';
html += '<div tabindex="-1" id="tarteaucitronAlertBig" class="tarteaucitronAlertBig' + orientation + '"' + modalAttrs + '>';
//html += '<div class="tarteaucitronAlertBigWrapper">';
html += ' <span id="tarteaucitronDisclaimerAlert">';
html += ' <span id="tarteaucitronDisclaimerAlert" role="paragraph">';
html += ' ' + tarteaucitron.lang.alertBigPrivacy;
html += ' </span>';
//html += ' <span class="tarteaucitronAlertBigBtnWrapper">';
Expand All @@ -440,9 +445,9 @@ var tarteaucitron = {
//html += '</div>';
html += '</div>';
} else {
html += '<div tabindex="-1" id="tarteaucitronAlertBig" class="tarteaucitronAlertBig' + orientation + '">';
html += '<div tabindex="-1" id="tarteaucitronAlertBig" class="tarteaucitronAlertBig' + orientation + '"' + modalAttrs + '>';
//html += '<div class="tarteaucitronAlertBigWrapper">';
html += ' <span id="tarteaucitronDisclaimerAlert">';
html += ' <span id="tarteaucitronDisclaimerAlert" role="paragraph">';

if (tarteaucitron.parameters.highPrivacy) {
html += ' ' + tarteaucitron.lang.alertBigPrivacy;
Expand Down Expand Up @@ -778,6 +783,15 @@ var tarteaucitron = {
});
}

// accessibility: on click on "Allow" in the site (not in TAC module), move focus to the loaded service's parent
var allowBtnsInSite = document.querySelectorAll(".tarteaucitronAllow:not(#tarteaucitronRoot .tarteaucitronAllow)");
for (i = 0; i < allowBtnsInSite.length; i++) {
tarteaucitron.addClickEventToElement(allowBtnsInSite[i], function () {
this.closest('.tac_activate').parentNode.setAttribute("tabindex","-1");
this.closest('.tac_activate').parentNode.focus();
});
}

var allowBtns = document.getElementsByClassName("tarteaucitronAllow");
for (i = 0; i < allowBtns.length; i++) {
tarteaucitron.addClickEventToElement(allowBtns[i], function () {
Expand Down Expand Up @@ -985,6 +999,10 @@ var tarteaucitron = {
if (property == "display" && value == "block" && id == "tarteaucitronBack") {
document.getElementById(id).style["opacity"] = "0.7";
}

if (property == "display" && value == "block" && id == "tarteaucitronAlertBig" && (tarteaucitron.parameters.orientation == "middle"|| tarteaucitron.parameters.orientation == "popup")) {
tarteaucitron.userInterface.focusTrap('tarteaucitronAlertBig');
}
}
}
},
Expand Down Expand Up @@ -1258,7 +1276,7 @@ var tarteaucitron = {
if (document.getElementsByTagName('body')[0].classList !== undefined) {
document.getElementsByTagName('body')[0].classList.add('tarteaucitron-modal-open');
}
tarteaucitron.userInterface.focusTrap();
tarteaucitron.userInterface.focusTrap('tarteaucitron');
tarteaucitron.userInterface.jsSizing('main');

//ie compatibility
Expand Down Expand Up @@ -1327,15 +1345,15 @@ var tarteaucitron = {

if (typeof(window.dispatchEvent) === 'function') {window.dispatchEvent(tacClosePanelEvent);}
},
"focusTrap": function() {
"focusTrap": function(parentElement) {
"use strict";

var focusableEls,
firstFocusableEl,
lastFocusableEl,
filtered;

focusableEls = document.getElementById('tarteaucitron').querySelectorAll('a[href], button');
focusableEls = document.getElementById(parentElement).querySelectorAll('a[href], button');
filtered = [];

// get only visible items
Expand All @@ -1349,7 +1367,7 @@ var tarteaucitron = {
lastFocusableEl = filtered[filtered.length - 1];

//loop focus inside tarteaucitron
document.getElementById('tarteaucitron').addEventListener("keydown", function (evt) {
document.getElementById(parentElement).addEventListener("keydown", function (evt) {

if ( evt.key === 'Tab' || evt.keyCode === 9 ) {

Expand Down

0 comments on commit 4bbd5e7

Please sign in to comment.