Skip to content

Commit f98bd8b

Browse files
committed
Adds tests and getters to set character and line delay to 0 when reduced motion is preferred by user.
1 parent 2579a1e commit f98bd8b

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"plugin:github/typescript"
77
],
88
"globals": {
9-
"CustomElementElement": "readonly"
9+
"CustomElementElement": "readonly",
10+
"module": true
1011
}
1112
}

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ class TypingEffectElement extends HTMLElement {
3939
}
4040
}
4141

42+
get prefersReducedMotion(): boolean {
43+
if (this.getAttribute('data-reduced-motion') === 'false') {
44+
return false
45+
} else {
46+
return window.matchMedia('(prefers-reduced-motion)').matches
47+
}
48+
}
49+
4250
get characterDelay(): number {
51+
if (this.prefersReducedMotion) {
52+
return 0
53+
}
4354
return Math.max(Math.min(0, Math.floor(Number(this.getAttribute('data-character-delay'))), 2_147_483_647)) || 40
4455
}
4556

@@ -51,6 +62,9 @@ class TypingEffectElement extends HTMLElement {
5162
}
5263

5364
get lineDelay(): number {
65+
if (this.prefersReducedMotion) {
66+
return 0
67+
}
5468
return Math.max(Math.min(0, Math.floor(Number(this.getAttribute('data-line-delay'))), 2_147_483_647)) || 40
5569
}
5670

test/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,34 @@ describe('typing-effect', function () {
7070
describe('delay attributes', function () {
7171
it('uses defaults when no delays specified', function () {
7272
const typingEffectElement = document.createElement('typing-effect')
73+
typingEffectElement.setAttribute('data-reduced-motion', false)
7374
document.body.append(typingEffectElement)
7475

7576
assert.equal(typingEffectElement.characterDelay, 40)
7677
assert.equal(typingEffectElement.lineDelay, 40)
7778
})
7879
})
80+
81+
describe('a11y considerations', function () {
82+
it('sets delay to 0 when media query matches (prefers-reduced-motion)', function () {
83+
const typingEffectElement = document.createElement('typing-effect')
84+
document.body.append(typingEffectElement)
85+
86+
assert.equal(window.matchMedia('(prefers-reduced-motion)').matches, true)
87+
assert.equal(typingEffectElement.characterDelay, 0)
88+
assert.equal(typingEffectElement.lineDelay, 0)
89+
})
90+
91+
it('uses data-reduced-motion attribute to override window media query', function () {
92+
const typingEffectElement = document.createElement('typing-effect')
93+
typingEffectElement.setAttribute('data-reduced-motion', false)
94+
document.body.append(typingEffectElement)
95+
96+
assert.equal(window.matchMedia('(prefers-reduced-motion)').matches, true)
97+
assert.equal(typingEffectElement.characterDelay, 40)
98+
assert.equal(typingEffectElement.lineDelay, 40)
99+
})
100+
})
79101
})
80102

81103
function once(element, eventName) {

0 commit comments

Comments
 (0)