Skip to content

Commit 141f2c4

Browse files
authored
Merge branch 'main' into main
2 parents 6600872 + 7369d4f commit 141f2c4

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ import '@github/typing-effect-element'
2121
</typing-effect>
2222
```
2323

24+
## Accessibility
25+
26+
This component detects whether `prefers-reduced-motion` is set on the window:
27+
28+
```js
29+
window.matchMedia('(prefers-reduced-motion)').matches === true
30+
```
31+
32+
If this evaluates to true, any content lines provided will be appended immediately rather than being typed out with a delay.
33+
2434
## Browser support
2535

2636
Browsers without native [custom element support][support] require a [polyfill][].

karma.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function(config) {
1+
module.exports = function (config) {
22
config.set({
33
frameworks: ['mocha', 'chai'],
44
files: [

src/index.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ class TypingEffectElement extends HTMLElement {
3939
}
4040
}
4141

42+
get prefersReducedMotion(): boolean {
43+
return window.matchMedia('(prefers-reduced-motion)').matches
44+
}
45+
4246
get characterDelay(): number {
47+
if (this.prefersReducedMotion) {
48+
return 0
49+
}
4350
return Math.max(0, Math.min(Math.floor(Number(this.getAttribute('data-character-delay'))), 2_147_483_647)) || 40
4451
}
4552

@@ -51,6 +58,9 @@ class TypingEffectElement extends HTMLElement {
5158
}
5259

5360
get lineDelay(): number {
61+
if (this.prefersReducedMotion) {
62+
return 0
63+
}
5464
return Math.max(0, Math.min(Math.floor(Number(this.getAttribute('data-line-delay'))), 2_147_483_647)) || 40
5565
}
5666

@@ -82,12 +92,16 @@ async function typeLines(
8292
lineDelay: number
8393
): Promise<void> {
8494
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
85-
for (const character of lines[lineIndex].split('')) {
86-
await wait(characterDelay)
87-
contentElement.innerHTML += character
95+
if (characterDelay === 0) {
96+
contentElement.append(lines[lineIndex])
97+
} else {
98+
for (const character of lines[lineIndex].split('')) {
99+
await wait(characterDelay)
100+
contentElement.innerHTML += character
101+
}
88102
}
89103

90-
await wait(lineDelay)
104+
if (lineDelay !== 0) await wait(lineDelay)
91105
if (lineIndex < lines.length - 1) contentElement.append(document.createElement('br'))
92106
}
93107
}

test/test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ describe('typing-effect', function () {
6868
})
6969

7070
describe('delay attributes', function () {
71+
let realMatchMedia
72+
before(() => {
73+
realMatchMedia = window.matchMedia
74+
window.matchMedia = mediaString => {
75+
if (mediaString === '(prefers-reduced-motion)') {
76+
return {matches: false}
77+
}
78+
return realMatchMedia(mediaString)
79+
}
80+
})
81+
82+
after(() => {
83+
window.matchMedia = realMatchMedia
84+
})
85+
7186
it('uses defaults when no delays specified', function () {
7287
const typingEffectElement = document.createElement('typing-effect')
7388
document.body.append(typingEffectElement)
@@ -89,6 +104,32 @@ describe('typing-effect', function () {
89104
assert.equal(typingEffectElement.lineDelay, lineDelay)
90105
})
91106
})
107+
108+
describe('a11y considerations', function () {
109+
let realMatchMedia
110+
before(() => {
111+
realMatchMedia = window.matchMedia
112+
window.matchMedia = mediaString => {
113+
if (mediaString === '(prefers-reduced-motion)') {
114+
return {matches: true}
115+
}
116+
return realMatchMedia(mediaString)
117+
}
118+
})
119+
120+
after(() => {
121+
window.matchMedia = realMatchMedia
122+
})
123+
124+
it('sets delay to 0 when media query matches (prefers-reduced-motion)', function () {
125+
const typingEffectElement = document.createElement('typing-effect')
126+
document.body.append(typingEffectElement)
127+
128+
assert.equal(window.matchMedia('(prefers-reduced-motion)').matches, true)
129+
assert.equal(typingEffectElement.characterDelay, 0)
130+
assert.equal(typingEffectElement.lineDelay, 0)
131+
})
132+
})
92133
})
93134

94135
function once(element, eventName) {

0 commit comments

Comments
 (0)