/
index.js
156 lines (133 loc) · 4.51 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React, { Component, PropTypes } from 'react';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
import FeaturePointer from '../FeaturePointer';
import { getElementTranslate } from '../../utils/dom-helpers';
// A helpful onboarding utility that:
// - includes a helpful message explaining the feature.
// - positions an item to the center, so a feature can be highlighted
// - controls whether or not to render an element
class FeatureHighlight extends Component {
constructor(props) {
super(props);
this.state = {};
}
componentDidMount() {
this.transpose({ initial: true });
}
componentDidUpdate() {
this.transpose();
}
transpose({ initial = false } = {}) {
const {
centerHorizontally,
centerVertically,
animateInitialPosition,
} = this.props;
window.requestAnimationFrame(() => {
// If the element has been removed in the last frame, abort!
if (!this.elem) {
return;
}
// Add a transition to the element unless this is the initial transposition,
// and we've elected to not animate the original one.
if (animateInitialPosition || !initial) {
const transform = `transform ${this.props.transposeLength}ms`;
const opacity = `opacity ${this.props.fadeLength}ms`;
this.elem.style.transition = [transform, opacity].join(', ');
}
const { top, left, width, height } = this.elem.getBoundingClientRect();
// We want to ignore vertical scroll position.
// Otherwise, the feature will move when the props change if the user
// has scrolled, and it's weird/disorientating.
const verticalScrollPos = window.scrollY;
const horizontalCenterPos = (window.innerWidth / 2) - (width / 2);
const verticalCenterPos = (window.innerHeight / 2) - (height / 2) - verticalScrollPos;
const [translateX, translateY] = getElementTranslate(this.elem);
console.log(this.elem, translateX, translateY, typeof translateX, typeof translateY);
let offsetLeft;
if (centerHorizontally) {
offsetLeft = horizontalCenterPos - left + translateX;
} else {
offsetLeft = 0;
}
let offsetTop;
if (centerVertically) {
offsetTop = verticalCenterPos - top + translateY;
} else {
offsetTop = 0;
}
if (offsetLeft || offsetTop) {
this.elem.style.transform = `translate(${offsetLeft}px, ${offsetTop}px)`;
} else {
this.elem.style.transform = '';
}
});
}
// eslint-disable-next-line consistent-return
renderPointer() {
const relevantPointerOptions = this.props.pointerOptions.find(p => (
p.render
));
if (relevantPointerOptions) {
return <FeaturePointer key="pointer" {...relevantPointerOptions} />;
}
}
render() {
// If we've specified that we want this element removed from the DOM,
// don't even render it.
if (this.props.removeFeature) {
return null;
}
return (
<div
className={this.props.className}
ref={elem => { this.elem = elem; }}
style={{
position: this.props.style.position || 'relative',
opacity: this.props.showFeature ? 1 : 0,
pointerEvents: this.props.showFeature ? '' : 'none',
}}
>
<ReactCSSTransitionGroup
transitionName="gradient"
transitionAppear
transitionEnterTimeout={3000}
transitionAppearTimeout={3000}
transitionLeaveTimeout={4000}
>
{this.props.gradient && <div className="feature-highlight-gradient" />}
</ReactCSSTransitionGroup>
{this.props.children}
<ReactCSSTransitionGroup
transitionName="pointer"
transitionEnterTimeout={2500}
transitionLeaveTimeout={500}
>
{this.renderPointer()}
</ReactCSSTransitionGroup>
</div>
);
}
}
FeatureHighlight.propTypes = {
children: PropTypes.node,
style: PropTypes.object,
className: PropTypes.string,
centerHorizontally: PropTypes.bool,
centerVertically: PropTypes.bool,
gradient: PropTypes.bool,
animateInitialPosition: PropTypes.bool,
transposeLength: PropTypes.number,
fadeLength: PropTypes.number,
showFeature: PropTypes.bool,
removeFeature: PropTypes.bool,
pointerOptions: PropTypes.array, // validated in FeaturePointer component
};
FeatureHighlight.defaultProps = {
style: {},
animateInitialPosition: false,
transposeLength: 1000,
fadeLength: 1600,
pointerOptions: [],
};
export default FeatureHighlight;