1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = void 0;
|
5 |
|
6 | var _react = _interopRequireWildcard(require("react"));
|
7 |
|
8 | var _classnames = _interopRequireDefault(require("classnames"));
|
9 |
|
10 | var _utils = require("./utils");
|
11 |
|
12 | var _offset = _interopRequireDefault(require("./offset"));
|
13 |
|
14 | var _windowScroll = _interopRequireDefault(require("window-scroll"));
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
19 |
|
20 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
21 |
|
22 | function _inheritsLoose(subClass, superClass) { subClass.prototype.__proto__ = superClass && superClass.prototype; subClass.__proto__ = superClass; }
|
23 |
|
24 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
25 |
|
26 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
27 |
|
28 | function isValid(d) {
|
29 | return d !== undefined && d !== null;
|
30 | }
|
31 |
|
32 | function getNum(val) {
|
33 | return val !== undefined ? parseInt(val) : 0;
|
34 | }
|
35 |
|
36 | function getValidVal() {
|
37 | for (var i = 0; i < arguments.length; i++) {
|
38 | if (isValid(i < 0 || arguments.length <= i ? undefined : arguments[i])) {
|
39 | return i < 0 || arguments.length <= i ? undefined : arguments[i];
|
40 | }
|
41 | }
|
42 | }
|
43 |
|
44 | function isFn(val) {
|
45 | return typeof val === "function";
|
46 | }
|
47 |
|
48 | var Sticky =
|
49 |
|
50 | function (_Component) {
|
51 | function Sticky() {
|
52 | var _temp, _this;
|
53 |
|
54 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
55 | args[_key] = arguments[_key];
|
56 | }
|
57 |
|
58 | return (_temp = _this = _Component.call.apply(_Component, [this].concat(args)) || this, _defineProperty(_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
|
59 | isSticky: false,
|
60 | window: {
|
61 | height: window.innerHeight,
|
62 | width: window.innerWidth
|
63 | }
|
64 | }), "StickyRef", _react.default.createRef()), _temp) || _assertThisInitialized(_this);
|
65 | }
|
66 |
|
67 | var _proto = Sticky.prototype;
|
68 |
|
69 | _proto.getContainerNode = function getContainerNode() {
|
70 | var stickyContainer = this.StickyRef;
|
71 | this.container = stickyContainer && stickyContainer.current;
|
72 | return this.container;
|
73 | };
|
74 |
|
75 | _proto.componentDidMount = function componentDidMount() {
|
76 | var _this2 = this;
|
77 |
|
78 | this.getContainerNode();
|
79 | this.initCloneContainerNode();
|
80 | this.registerEvents();
|
81 | setTimeout(function () {
|
82 | _this2.initSticky();
|
83 | });
|
84 | };
|
85 |
|
86 | _proto.initSticky = function initSticky() {
|
87 | var _this3 = this;
|
88 |
|
89 | this.ifSticky(function () {
|
90 | _this3.sticky();
|
91 | }, function () {
|
92 | _this3.sticky(false);
|
93 | });
|
94 | };
|
95 |
|
96 | _proto.componentWillUnmount = function componentWillUnmount() {
|
97 | var _this4 = this;
|
98 |
|
99 | this.sticky(false);
|
100 | this.cancelEvents();
|
101 | setTimeout(function () {
|
102 | _this4.wrapperNode.remove();
|
103 | });
|
104 | };
|
105 |
|
106 | _proto.onScrollHandler = function onScrollHandler(context) {
|
107 | var createState = context.props.createState;
|
108 |
|
109 | var handler = context.bindHandler || function () {
|
110 | requestAnimationFrame(function () {
|
111 | if (createState) {
|
112 | var state = createState();
|
113 |
|
114 | if (state) {
|
115 | context.setState(state);
|
116 | }
|
117 | }
|
118 |
|
119 | context.ifSticky(function () {
|
120 | context.setState({
|
121 | isSticky: true,
|
122 | window: {
|
123 | height: window.innerHeight,
|
124 | width: window.innerWidth
|
125 | }
|
126 | });
|
127 | context.sticky();
|
128 | }, function () {
|
129 | context.setState({
|
130 | isSticky: false,
|
131 | window: {
|
132 | height: window.innerHeight,
|
133 | width: window.innerWidth
|
134 | }
|
135 | });
|
136 | context.sticky(false);
|
137 | });
|
138 | });
|
139 | };
|
140 |
|
141 | context.bindHandler = handler;
|
142 | return handler;
|
143 | };
|
144 |
|
145 | _proto.setStyle = function setStyle(node, styles) {
|
146 | var style = node.style;
|
147 | Object.keys(styles).forEach(function (name) {
|
148 | style[name] = styles[name];
|
149 | });
|
150 | };
|
151 |
|
152 | _proto.sticky = function sticky(isSticky) {
|
153 | if (isSticky === void 0) {
|
154 | isSticky = true;
|
155 | }
|
156 |
|
157 | var positionNode = this.getPositionNode();
|
158 | var nodeData = this.getNodeData(positionNode);
|
159 | var self = this;
|
160 |
|
161 | if (this.props.edge == "top") {
|
162 | if (isSticky) {
|
163 | this.setStyle(this.container, _extends({
|
164 | position: "fixed",
|
165 | width: nodeData.width + "px",
|
166 | height: nodeData.height + "px",
|
167 | top: this.props.triggerDistance + "px",
|
168 | left: nodeData.offsetLeft + "px",
|
169 | zIndex: self.props.zIndex || "100000"
|
170 | }, this.props.stickiedStyle));
|
171 | if (this.sticking) return;
|
172 | this.sticking = true;
|
173 | } else {
|
174 | if (!this.sticking) return;
|
175 | self.setStyle(self.container, _extends({
|
176 | left: "",
|
177 | zIndex: "",
|
178 | width: "",
|
179 | height: "",
|
180 | position: "",
|
181 | top: ""
|
182 | }, self.props.unstickiedStyle));
|
183 | this.sticking = false;
|
184 | }
|
185 | } else {
|
186 | if (isSticky) {
|
187 | this.setStyle(this.container, _extends({
|
188 | position: "fixed",
|
189 | width: nodeData.width + "px",
|
190 | height: nodeData.height + "px",
|
191 | bottom: self.props.triggerDistance + "px",
|
192 | left: nodeData.offsetLeft + "px",
|
193 | zIndex: self.props.zIndex || "100000"
|
194 | }, this.props.stickiedStyle));
|
195 | if (this.sticking) return;
|
196 | this.sticking = true;
|
197 | } else {
|
198 | if (!this.sticking) return;
|
199 | this.setStyle(this.container, {
|
200 | bottom: self.props.triggerDistance + "px"
|
201 | });
|
202 | var containerNode = this.container;
|
203 | self.setStyle(self.container, _extends({
|
204 | left: "",
|
205 | zIndex: "",
|
206 | width: "",
|
207 | height: "",
|
208 | position: "",
|
209 | bottom: ""
|
210 | }, self.props.unstickiedStyle));
|
211 | this.sticking = false;
|
212 | }
|
213 | }
|
214 | };
|
215 |
|
216 | _proto.getPositionNode = function getPositionNode() {
|
217 | var node = null;
|
218 | if (this.sticking) node = this.wrapperNode;else node = this.container;
|
219 | return node;
|
220 | };
|
221 |
|
222 | _proto.ifSticky = function ifSticky(ok, faild) {
|
223 | var positionNode = this.getPositionNode();
|
224 | var nodeData = this.getNodeData(positionNode);
|
225 | var winData = this.getNodeData(window);
|
226 | var self = this;
|
227 | var edge = self.props.edge;
|
228 | var getStickyBoundary = self.props.getStickyBoundary;
|
229 | var triggerDistance = self.props.triggerDistance;
|
230 | var isNotSticky = this.state.notSticky;
|
231 |
|
232 | if (isFn(getStickyBoundary)) {
|
233 | if (!getStickyBoundary()) return faild.call(self);
|
234 | }
|
235 |
|
236 | if (isNotSticky) {
|
237 | return faild.call(self);
|
238 | }
|
239 |
|
240 | if (edge != "top") {
|
241 | if (winData.scrollTop + winData.height < nodeData.offsetTop + nodeData.height + triggerDistance) {
|
242 | return ok.call(self);
|
243 | }
|
244 | } else {
|
245 | if (winData.scrollTop > nodeData.offsetTop - triggerDistance) {
|
246 | return ok.call(self);
|
247 | }
|
248 | }
|
249 |
|
250 | faild.call(self);
|
251 | };
|
252 |
|
253 | _proto.getNodeData = function getNodeData(node) {
|
254 | var clientHeight = node.clientHeight,
|
255 | clientWidth = node.clientWidth,
|
256 | innerHeight = node.innerHeight,
|
257 | innerWidth = node.innerWidth;
|
258 |
|
259 | if (node !== window) {
|
260 | var offset = (0, _offset.default)(node);
|
261 | var offsetLeft = offset ? offset.left : 0;
|
262 | var offsetTop = offset ? offset.top : 0;
|
263 | var rect = node.getBoundingClientRect();
|
264 | var style = (0, _utils.getCompStyle)(node);
|
265 | return {
|
266 | offsetLeft: offsetLeft - getNum(style["margin-left"]),
|
267 | offsetTop: offsetTop - getNum(style["margin-top"]),
|
268 | width: rect.width,
|
269 | height: rect.height
|
270 | };
|
271 | } else {
|
272 | return {
|
273 | height: window.innerHeight,
|
274 | width: window.innerWidth,
|
275 | scrollTop: window.pageYOffset,
|
276 | scrollLeft: window.pageXOffset
|
277 | };
|
278 | }
|
279 |
|
280 | return this.nodeData;
|
281 | };
|
282 |
|
283 | _proto.initCloneContainerNode = function initCloneContainerNode() {
|
284 | if (this.wrapperNode) return this.wrapperNode;
|
285 | var oldNode = this.getContainerNode();
|
286 | var nodeData = this.getNodeData(oldNode);
|
287 | this.wrapperNode = document.createElement("div");
|
288 | this.wrapperNode.style.height = nodeData.height + "px";
|
289 | this.wrapperNode.classList.add("sticky-wrapper");
|
290 | oldNode.parentNode.insertBefore(this.wrapperNode, oldNode);
|
291 | this.wrapperNode.appendChild(oldNode);
|
292 | };
|
293 |
|
294 | _proto.cancelEvents = function cancelEvents() {
|
295 | window.removeEventListener("scroll", this.onScrollHandler(this));
|
296 | window.removeEventListener("resize", this.onScrollHandler(this));
|
297 | };
|
298 |
|
299 | _proto.registerEvents = function registerEvents() {
|
300 | window.addEventListener("scroll", this.onScrollHandler(this));
|
301 | window.addEventListener("resize", this.onScrollHandler(this));
|
302 | };
|
303 |
|
304 | _proto.renderContainer = function renderContainer() {
|
305 | var children = this.props.children;
|
306 | return _react.default.createElement("div", {
|
307 | ref: this.StickyRef,
|
308 | className: "sticky-container",
|
309 | style: this.props.style
|
310 | }, typeof children === "function" ? children(this.state) : children);
|
311 | };
|
312 |
|
313 | _proto.render = function render() {
|
314 | return this.renderContainer();
|
315 | };
|
316 |
|
317 | _inheritsLoose(Sticky, _Component);
|
318 |
|
319 | return Sticky;
|
320 | }(_react.Component);
|
321 |
|
322 | _defineProperty(Sticky, "defaultProps", {
|
323 | edge: "bottom",
|
324 | triggerDistance: 0
|
325 | });
|
326 |
|
327 | var _default = Sticky;
|
328 | exports.default = _default; |
\ | No newline at end of file |