Skip to content

Commit dcfafd2

Browse files
committed
Adds a condition for reduced motion where the data-reduced-motion attribute will override the window media match. If data-reduced-motion is set to true, the component will NOT animate. If data-reduced-motion is set to false, the component WILL animate.
1 parent f3165c6 commit dcfafd2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,32 @@ import '@github/typing-effect-element'
2323

2424
## Accessibility
2525

26-
This component detects whether `prefers-reduced-motion` is set on the window
26+
This component detects whether `prefers-reduced-motion` is set on the window:
2727

2828
```js
2929
window.matchMedia('(prefers-reduced-motion)').matches === true
3030
```
3131

3232
If this evaluates to true, any content lines provided will be appended immediately rather than being typed out with a delay.
3333

34+
The data attribute `data-reduced-motion` can be used to override the window media value.
35+
36+
```html
37+
<typing-effect
38+
data-lines='["Welcome to GitHub!", "Let’s begin the adventure"]'
39+
data-reduced-motion="true"> <!-- This will NOT animate -->
40+
<span data-target="typing-effect.content"></span>
41+
<span data-target="typing-effect.cursor">|</span>
42+
</typing-effect>
43+
44+
<typing-effect
45+
data-lines='["Welcome to GitHub!", "Let’s begin the adventure"]'
46+
data-reduced-motion="false"> <!-- This WILL animate -->
47+
<span data-target="typing-effect.content"></span>
48+
<span data-target="typing-effect.cursor">|</span>
49+
</typing-effect>
50+
```
51+
3452
## Browser support
3553

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

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class TypingEffectElement extends HTMLElement {
4343
get prefersReducedMotion(): boolean {
4444
if (this.getAttribute('data-reduced-motion') === 'false') {
4545
return false
46+
} else if (this.getAttribute('data-reduced-motion') === 'true') {
47+
return true
4648
} else {
4749
return window.matchMedia('(prefers-reduced-motion)').matches
4850
}

0 commit comments

Comments
 (0)