UNPKG

9.83 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var _extends = _interopDefault(require('@babel/runtime/helpers/extends'));
8var _objectWithoutPropertiesLoose = _interopDefault(require('@babel/runtime/helpers/objectWithoutPropertiesLoose'));
9var _inheritsLoose = _interopDefault(require('@babel/runtime/helpers/inheritsLoose'));
10var React = _interopDefault(require('react'));
11var renderprops = require('react-spring/renderprops');
12
13var El = renderprops.Globals.defaultElement;
14var AnimatedDiv = renderprops.animated(El);
15
16var _React$createContext = React.createContext(null),
17 Provider = _React$createContext.Provider,
18 Consumer = _React$createContext.Consumer;
19
20function getScrollType(horizontal) {
21 return horizontal ? 'scrollLeft' : 'scrollTop';
22}
23
24var START_TRANSLATE_3D = 'translate3d(0px,0px,0px)';
25var START_TRANSLATE = 'translate(0px,0px)';
26var ParallaxLayer =
27/*#__PURE__*/
28function (_React$PureComponent) {
29 _inheritsLoose(ParallaxLayer, _React$PureComponent);
30
31 function ParallaxLayer() {
32 return _React$PureComponent.apply(this, arguments) || this;
33 }
34
35 var _proto = ParallaxLayer.prototype;
36
37 _proto.componentDidMount = function componentDidMount() {
38 var parent = this.parent;
39
40 if (parent) {
41 parent.layers = parent.layers.concat(this);
42 parent.update();
43 }
44 };
45
46 _proto.componentWillUnmount = function componentWillUnmount() {
47 var _this = this;
48
49 var parent = this.parent;
50
51 if (parent) {
52 parent.layers = parent.layers.filter(function (layer) {
53 return layer !== _this;
54 });
55 parent.update();
56 }
57 };
58
59 _proto.setPosition = function setPosition(height, scrollTop, immediate) {
60 if (immediate === void 0) {
61 immediate = false;
62 }
63
64 var config = this.parent.props.config;
65 var targetScroll = Math.floor(this.props.offset) * height;
66 var offset = height * this.props.offset + targetScroll * this.props.speed;
67 var to = parseFloat(-(scrollTop * this.props.speed) + offset);
68 this.controller.update({
69 translate: to,
70 config: config,
71 immediate: immediate
72 });
73 };
74
75 _proto.setHeight = function setHeight(height, immediate) {
76 if (immediate === void 0) {
77 immediate = false;
78 }
79
80 var config = this.parent.props.config;
81 var to = parseFloat(height * this.props.factor);
82 this.controller.update({
83 space: to,
84 config: config,
85 immediate: immediate
86 });
87 };
88
89 _proto.initialize = function initialize() {
90 var props = this.props;
91 var parent = this.parent;
92 var targetScroll = Math.floor(props.offset) * parent.space;
93 var offset = parent.space * props.offset + targetScroll * props.speed;
94 var to = parseFloat(-(parent.current * props.speed) + offset);
95 this.controller = new renderprops.Controller({
96 space: parent.space * props.factor,
97 translate: to
98 });
99 };
100
101 _proto.renderLayer = function renderLayer() {
102 var _extends2;
103
104 var _this$props = this.props,
105 style = _this$props.style,
106 children = _this$props.children,
107 offset = _this$props.offset,
108 speed = _this$props.speed,
109 factor = _this$props.factor,
110 className = _this$props.className,
111 props = _objectWithoutPropertiesLoose(_this$props, ["style", "children", "offset", "speed", "factor", "className"]);
112
113 var horizontal = this.parent.props.horizontal;
114 var translate3d = this.controller.interpolations.translate.interpolate(function (x) {
115 return horizontal ? "translate3d(" + x + "px,0,0)" : "translate3d(0," + x + "px,0)";
116 });
117 return React.createElement(AnimatedDiv, _extends({}, props, {
118 className: className,
119 style: _extends((_extends2 = {
120 position: 'absolute',
121 backgroundSize: 'auto',
122 backgroundRepeat: 'no-repeat',
123 willChange: 'transform'
124 }, _extends2[horizontal ? 'height' : 'width'] = '100%', _extends2[horizontal ? 'width' : 'height'] = this.controller.interpolations.space, _extends2.WebkitTransform = translate3d, _extends2.MsTransform = translate3d, _extends2.transform = translate3d, _extends2), style)
125 }), children);
126 };
127
128 _proto.render = function render() {
129 var _this2 = this;
130
131 return React.createElement(Consumer, null, function (parent) {
132 if (parent && !_this2.parent) {
133 _this2.parent = parent;
134
135 _this2.initialize();
136 }
137
138 return _this2.renderLayer();
139 });
140 };
141
142 return ParallaxLayer;
143}(React.PureComponent);
144ParallaxLayer.defaultProps = {
145 factor: 1,
146 offset: 0,
147 speed: 0
148};
149var Parallax =
150/*#__PURE__*/
151function (_React$PureComponent2) {
152 _inheritsLoose(Parallax, _React$PureComponent2);
153
154 // TODO keep until major release
155 function Parallax(props) {
156 var _this3;
157
158 _this3 = _React$PureComponent2.call(this) || this;
159
160 _this3.moveItems = function () {
161 _this3.layers.forEach(function (layer) {
162 return layer.setPosition(_this3.space, _this3.current);
163 });
164
165 _this3.busy = false;
166 };
167
168 _this3.scrollerRaf = function () {
169 return renderprops.Globals.requestFrame(_this3.moveItems);
170 };
171
172 _this3.onScroll = function (event) {
173 var horizontal = _this3.props.horizontal;
174
175 if (!_this3.busy) {
176 _this3.busy = true;
177
178 _this3.scrollerRaf();
179
180 _this3.current = event.target[getScrollType(horizontal)];
181 }
182 };
183
184 _this3.update = function () {
185 var _this3$props = _this3.props,
186 scrolling = _this3$props.scrolling,
187 horizontal = _this3$props.horizontal;
188 var scrollType = getScrollType(horizontal);
189 if (!_this3.container) return;
190 _this3.space = _this3.container[horizontal ? 'clientWidth' : 'clientHeight'];
191 if (scrolling) _this3.current = _this3.container[scrollType];else _this3.container[scrollType] = _this3.current = _this3.offset * _this3.space;
192 if (_this3.content) _this3.content.style[horizontal ? 'width' : 'height'] = _this3.space * _this3.props.pages + "px";
193
194 _this3.layers.forEach(function (layer) {
195 layer.setHeight(_this3.space, true);
196 layer.setPosition(_this3.space, _this3.current, true);
197 });
198 };
199
200 _this3.updateRaf = function () {
201 renderprops.Globals.requestFrame(_this3.update); // Some browsers don't fire on maximize
202
203 setTimeout(_this3.update, 150);
204 };
205
206 _this3.scrollStop = function (event) {
207 return _this3.controller.stop();
208 };
209
210 _this3.state = {
211 ready: false
212 };
213 _this3.layers = [];
214 _this3.space = 0;
215 _this3.current = 0;
216 _this3.offset = 0;
217 _this3.busy = false;
218 _this3.controller = new renderprops.Controller({
219 scroll: 0
220 });
221 return _this3;
222 }
223
224 var _proto2 = Parallax.prototype;
225
226 _proto2.scrollTo = function scrollTo(offset) {
227 var _this$props2 = this.props,
228 horizontal = _this$props2.horizontal,
229 config = _this$props2.config;
230 var scrollType = getScrollType(horizontal);
231 this.scrollStop();
232 this.offset = offset;
233 var target = this.container;
234 this.controller.update({
235 scroll: offset * this.space,
236 config: config,
237 onFrame: function onFrame(_ref) {
238 var scroll = _ref.scroll;
239 return target[scrollType] = scroll;
240 }
241 });
242 };
243
244 _proto2.componentDidMount = function componentDidMount() {
245 window.addEventListener('resize', this.updateRaf, false);
246 this.update();
247 this.setState({
248 ready: true
249 });
250 };
251
252 _proto2.componentWillUnmount = function componentWillUnmount() {
253 window.removeEventListener('resize', this.updateRaf, false);
254 };
255
256 _proto2.componentDidUpdate = function componentDidUpdate() {
257 this.update();
258 };
259
260 _proto2.render = function render() {
261 var _this4 = this,
262 _extends3;
263
264 var _this$props3 = this.props,
265 style = _this$props3.style,
266 innerStyle = _this$props3.innerStyle,
267 pages = _this$props3.pages,
268 id = _this$props3.id,
269 className = _this$props3.className,
270 scrolling = _this$props3.scrolling,
271 children = _this$props3.children,
272 horizontal = _this$props3.horizontal;
273 var overflow = scrolling ? 'scroll' : 'hidden';
274 return React.createElement(El, {
275 ref: function ref(node) {
276 return _this4.container = node;
277 },
278 onScroll: this.onScroll,
279 onWheel: scrolling ? this.scrollStop : null,
280 onTouchStart: scrolling ? this.scrollStop : null,
281 style: _extends({
282 position: 'absolute',
283 width: '100%',
284 height: '100%',
285 overflow: overflow,
286 overflowY: horizontal ? 'hidden' : overflow,
287 overflowX: horizontal ? overflow : 'hidden',
288 WebkitOverflowScrolling: 'touch',
289 WebkitTransform: START_TRANSLATE,
290 MsTransform: START_TRANSLATE,
291 transform: START_TRANSLATE_3D
292 }, style),
293 id: id,
294 className: className
295 }, this.state.ready && React.createElement(El, {
296 ref: function ref(node) {
297 return _this4.content = node;
298 },
299 style: _extends((_extends3 = {
300 position: 'absolute'
301 }, _extends3[horizontal ? 'height' : 'width'] = '100%', _extends3.WebkitTransform = START_TRANSLATE, _extends3.MsTransform = START_TRANSLATE, _extends3.transform = START_TRANSLATE_3D, _extends3.overflow = 'hidden', _extends3[horizontal ? 'width' : 'height'] = this.space * pages, _extends3), innerStyle)
302 }, React.createElement(Provider, {
303 value: this
304 }, children)));
305 };
306
307 return Parallax;
308}(React.PureComponent);
309Parallax.Layer = ParallaxLayer;
310Parallax.defaultProps = {
311 config: renderprops.config.slow,
312 scrolling: true,
313 horizontal: false
314};
315
316exports.Parallax = Parallax;
317exports.ParallaxLayer = ParallaxLayer;