-
Notifications
You must be signed in to change notification settings - Fork 519
/
Beacon.js
116 lines (99 loc) · 2.72 KB
/
Beacon.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import React from 'react';
import PropTypes from 'prop-types';
import is from 'is-lite';
import { componentTypeWithRefs } from '../modules/propTypes';
export default class JoyrideBeacon extends React.Component {
constructor(props) {
super(props);
if (!props.beaconComponent) {
const head = document.head || document.getElementsByTagName('head')[0];
const style = document.createElement('style');
const css = `
@keyframes joyride-beacon-inner {
20% {
opacity: 0.9;
}
90% {
opacity: 0.7;
}
}
@keyframes joyride-beacon-outer {
0% {
transform: scale(1);
}
45% {
opacity: 0.7;
transform: scale(0.75);
}
100% {
opacity: 0.9;
transform: scale(1);
}
}
`;
style.type = 'text/css';
style.id = 'joyride-beacon-animation';
style.appendChild(document.createTextNode(css));
head.appendChild(style);
}
}
static propTypes = {
beaconComponent: componentTypeWithRefs,
locale: PropTypes.object.isRequired,
onClickOrHover: PropTypes.func.isRequired,
shouldFocus: PropTypes.bool.isRequired,
styles: PropTypes.object.isRequired,
};
componentDidMount() {
const { shouldFocus } = this.props;
if (process.env.NODE_ENV !== 'production') {
if (!is.domElement(this.beacon)) {
console.warn('beacon is not a valid DOM element'); //eslint-disable-line no-console
}
}
setTimeout(() => {
if (is.domElement(this.beacon) && shouldFocus) {
this.beacon.focus();
}
}, 0);
}
componentWillUnmount() {
const style = document.getElementById('joyride-beacon-animation');
if (style) {
style.parentNode.removeChild(style);
}
}
setBeaconRef = c => {
this.beacon = c;
};
render() {
const { beaconComponent, locale, onClickOrHover, styles } = this.props;
const props = {
'aria-label': locale.open,
onClick: onClickOrHover,
onMouseEnter: onClickOrHover,
ref: this.setBeaconRef,
title: locale.open,
};
let component;
if (beaconComponent) {
const BeaconComponent = beaconComponent;
component = <BeaconComponent {...props} />;
} else {
component = (
<button
key="JoyrideBeacon"
className="react-joyride__beacon"
style={styles.beacon}
type="button"
data-test-id="button-beacon"
{...props}
>
<span style={styles.beaconInner} />
<span style={styles.beaconOuter} />
</button>
);
}
return component;
}
}