-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (50 loc) · 1.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React from "react";
const className = "bdchauvette-react-blink";
const animationName = `${className}-keyframes`;
let styleSheetWasAdded = false;
const BlinkContext = React.createContext(false);
export function BlinkProvider({ disabled = false, children }) {
return React.createElement(
BlinkContext.Provider,
{ value: disabled },
children
);
}
export class Blink extends React.Component {
componentDidMount() {
if (!styleSheetWasAdded) {
addStyleSheet();
styleSheetWasAdded = true;
}
}
render() {
return React.createElement(BlinkContext.Consumer, null, (disabled) =>
React.createElement(
"div",
disabled ? null : { className },
this.props.children
)
);
}
}
function addStyleSheet() {
let $style = document.createElement("style");
document.head.appendChild($style);
let sheet = $style.sheet;
const rules = [
`@keyframes ${animationName} {
0%, 50% { opacity: 1; }
100% { opacity: 0; }
}`,
`.${className} {
animation: ${animationName} 2s step-start infinite;
}`,
`@media screen and (prefers-reduced-motion: reduce) {
.${className} {
animation: none;
}
}`,
];
rules.forEach((rule) => sheet.insertRule(rule, sheet.length - 1));
return $style;
}