Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
A11Y: Fix VoiceOver issues and make improvements (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
curiousdannii authored Aug 1, 2023
1 parent 93b1233 commit 686a058
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
29 changes: 20 additions & 9 deletions assets/javascripts/lib/apply-spoiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,32 @@ function isInteractive(event) {
return event.defaultPrevented || event.target.closest(INTERACTIVE_SELECTOR);
}

function noTextSelected() {
return window.getSelection() + "" === "";
}

function setAttributes(element, attributes) {
Object.entries(attributes).forEach(([key, value]) => {
if (value === null) {
element.removeAttribute(key);
} else {
element.setAttribute(key, value);
}
});
}

function _setSpoilerHidden(element) {
const spoilerHiddenAttributes = {
role: "button",
tabindex: "0",
"data-spoiler-state": "blurred",
"aria-expanded": false,
"aria-label": I18n.t("spoiler.label.show"),
"aria-live": "polite",
};

// Set default attributes & classes on spoiler
Object.entries(spoilerHiddenAttributes).forEach(([key, value]) => {
element.setAttribute(key, value);
});
setAttributes(element, spoilerHiddenAttributes);
element.classList.add("spoiler-blurred");

// Set aria-hidden for all children of the spoiler
Expand All @@ -50,14 +63,12 @@ function _setSpoilerVisible(element) {
const spoilerVisibleAttributes = {
"data-spoiler-state": "revealed",
"aria-expanded": true,
"aria-label": I18n.t("spoiler.label.hide"),
"aria-live": "polite",
"aria-label": null,
role: null,
};

// Set attributes & classes for when spoiler is visible
Object.entries(spoilerVisibleAttributes).forEach(([key, value]) => {
element.setAttribute(key, value);
});
setAttributes(element, spoilerVisibleAttributes);
element.classList.remove("spoiler-blurred");

// Remove aria-hidden for all children of the spoiler when visible
Expand All @@ -70,7 +81,7 @@ function toggleSpoiler(event, element) {
if (element.getAttribute("data-spoiler-state") === "blurred") {
_setSpoilerVisible(element);
event.preventDefault();
} else if (!isInteractive(event)) {
} else if (!isInteractive(event) && noTextSelected()) {
_setSpoilerHidden(element);
}
}
Expand Down
1 change: 0 additions & 1 deletion config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ en:
title: Blur Spoiler
label:
show: "Show hidden content"
hide: "Hide content"
composer:
spoiler_text: "This text will be blurred"

0 comments on commit 686a058

Please sign in to comment.