@findify/react-components
Version:
Findify react UI components
133 lines (107 loc) • 6.86 kB
JavaScript
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
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; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
/**
* @module components/common/Sticky
*/
import { createFactory, useRef, useState, useEffect } from 'react';
import withTheme from "../../../helpers/withTheme";
import view from "./view";
var styles = {
"wrapper": "findify-components-common--sticky__wrapper",
"container": "findify-components-common--sticky__container",
"static": "findify-components-common--sticky__static",
"stuck": "findify-components-common--sticky__stuck",
"sticky": "findify-components-common--sticky__sticky"
};
var factory = /*#__PURE__*/createFactory(view);
var initial = 'static',
stuck = 'stuck',
sticky = 'sticky';
/** Function used to apply sticky styles */
var applyStyles = function applyStyles(element, styles) {
if (!styles) element.removeAttribute('style');
for (var key in styles) {
element.style[key] = styles[key] + 'px';
}
};
/** Props that Sticky component accepts */
var Sticky = function Sticky(_ref) {
var _ref$offset = _ref.offset,
offset = _ref$offset === void 0 ? 25 : _ref$offset,
_ref$minHeight = _ref.minHeight,
minHeight = _ref$minHeight === void 0 ? 0 : _ref$minHeight,
stickToTop = _ref.stickToTop,
props = _objectWithoutProperties(_ref, ["offset", "minHeight", "stickToTop"]);
var root = useRef(null);
var sizer = useRef(null);
var container = useRef(null);
var _useState = useState(initial),
_useState2 = _slicedToArray(_useState, 2),
state = _useState2[0],
setState = _useState2[1];
useEffect(function () {
var handleScroll = function handleScroll() {
if (!container.current || !root.current) return;
var rootBound = root.current.getBoundingClientRect();
var containerBound = container.current.getBoundingClientRect();
var _sizer$current$getBou = sizer.current.getBoundingClientRect(),
width = _sizer$current$getBou.width;
var shouldStick = stickToTop ? rootBound.top - offset < 0 : containerBound.height < rootBound.height && rootBound.top - offset < 0;
if (stickToTop) {
if (shouldStick) {
setState(sticky);
applyStyles(root.current, {
height: rootBound.height
});
return applyStyles(container.current, {
width: width,
maxHeight: rootBound.height
});
} else {
setState(initial);
applyStyles(root.current);
return applyStyles(container.current);
}
}
if (!shouldStick) {
applyStyles(container.current);
return setState(initial);
}
if (!stickToTop && rootBound.bottom <= minHeight) {
applyStyles(container.current, {
width: width,
maxHeight: minHeight
});
return setState(stuck);
}
var height = rootBound.bottom - offset;
var styles = {
width: width,
maxHeight: height > window.innerHeight && window.innerHeight - offset || height,
top: offset
};
applyStyles(container.current, styles);
return setState(sticky);
};
document.addEventListener('scroll', handleScroll, true);
return function () {
document.removeEventListener('scroll', handleScroll, true);
};
}, []);
return factory(_objectSpread(_objectSpread({}, props), {}, {
state: state,
registerRoot: root,
registerSizer: sizer,
registerContainer: container
}));
};
export default withTheme(styles)(Sticky);