Skip to content

Commit 15333fa

Browse files
fix(a11y): add sr-only text per block tag
1 parent d15b1a1 commit 15333fa

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/index.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export default class SplitText {
2323
words = [];
2424
lines = [];
2525
originals = [];
26-
ariaLabel = [];
2726
lineParents = [];
2827
elements = [];
2928
options = {};
@@ -99,25 +98,36 @@ export default class SplitText {
9998
}
10099

101100
if (!this.options.noAriaLabel) {
102-
const span = document.createElement('span');
103-
span.classList.add('sr-only');
104-
span.style.setProperty('position', 'absolute');
105-
span.style.setProperty('width', '1px');
106-
span.style.setProperty('height', '1px');
107-
span.style.setProperty('padding', '0');
108-
span.style.setProperty('margin', '-1px');
109-
span.style.setProperty('overflow', 'hidden');
110-
span.style.setProperty('clip', 'rect(0, 0, 0, 0)');
111-
span.style.setProperty('white-space', 'nowrap');
112-
span.style.setProperty('border', '0');
113-
span.textContent = this.ariaLabel.join(' ');
114-
element.appendChild(span);
101+
const blockTags = toArray(element.childNodes).filter((child) => BLOCK_TAGS.includes(child.tagName));
102+
if (blockTags.length) {
103+
blockTags.forEach((child) => {
104+
this.createAriaLabel(child);
105+
});
106+
} else {
107+
element.appendChild(this.createAriaLabel(element));
108+
}
115109
}
116110
});
117111

118112
this.isSplit = true;
119113
}
120114

115+
createAriaLabel(element) {
116+
const span = document.createElement('span');
117+
span.classList.add('sr-only');
118+
span.style.setProperty('position', 'absolute');
119+
span.style.setProperty('width', '1px');
120+
span.style.setProperty('height', '1px');
121+
span.style.setProperty('padding', '0');
122+
span.style.setProperty('margin', '-1px');
123+
span.style.setProperty('overflow', 'hidden');
124+
span.style.setProperty('clip', 'rect(0, 0, 0, 0)');
125+
span.style.setProperty('white-space', 'nowrap');
126+
span.style.setProperty('border', '0');
127+
span.textContent = element.textContent;
128+
element.appendChild(span);
129+
}
130+
121131
/**
122132
* There are some occasions where the lines after being balanced, exceeds the element bounding box
123133
* this check prevents that, and takes into accout also very long kebab cased string.
@@ -146,7 +156,7 @@ export default class SplitText {
146156
revert() {
147157
if (this.originals.length === 0) return;
148158
this.elements.forEach((el, i) => (el.innerHTML = this.originals[i]));
149-
[this.lines, this.words, this.chars, this.ariaLabel, this.originals].forEach((arr) => (arr.length = 0));
159+
[this.lines, this.words, this.chars, this.originals].forEach((arr) => (arr.length = 0));
150160
this.isSplit = false;
151161
}
152162

@@ -481,8 +491,8 @@ export default class SplitText {
481491
next.remove();
482492
} else {
483493
lp.__lines[lineIndex].appendChild(next);
484-
toArray(next.childNodes).forEach((e) => (e.__line = globalLineIndex));
485-
next.__line = globalLineIndex;
494+
toArray(next.childNodes).forEach((e) => (e.__lineIndex = globalLineIndex));
495+
next.__lineIndex = globalLineIndex;
486496
}
487497
});
488498
lp.__lines.forEach((line) => lp.appendChild(line));
@@ -511,7 +521,6 @@ export default class SplitText {
511521
el.className = key;
512522
el.textContent = text;
513523
el.setAttribute('aria-hidden', true);
514-
if (key === 'word') this.ariaLabel.push(text);
515524
return parent.appendChild(el);
516525
}
517526

0 commit comments

Comments
 (0)