lucid-ui
Version:
A UI component library from AppNexus.
231 lines (184 loc) • 12 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
import _isNumber from "lodash/isNumber";
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); }
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 _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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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; }
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { omitProps } from '../../util/component-types';
import { getAbsoluteBoundingClientRect } from '../../util/dom-helpers';
var cx = lucidClassNames.bind('&-StickySection');
var node = PropTypes.node,
number = PropTypes.number,
object = PropTypes.object,
string = PropTypes.string;
var StickySection = /*#__PURE__*/function (_React$Component) {
_inherits(StickySection, _React$Component);
var _super = _createSuper(StickySection);
function StickySection() {
var _this;
_classCallCheck(this, StickySection);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "scrollContainer", /*#__PURE__*/React.createRef());
_defineProperty(_assertThisInitialized(_this), "stickySection", /*#__PURE__*/React.createRef());
_defineProperty(_assertThisInitialized(_this), "stickyFrame", /*#__PURE__*/React.createRef());
_defineProperty(_assertThisInitialized(_this), "state", {
isAboveFold: false,
containerRect: {
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
frameLeft: 0,
scrollWidth: 0
}
});
_defineProperty(_assertThisInitialized(_this), "handleScroll", function () {
var _this$props = _this.props,
lowerBound = _this$props.lowerBound,
_this$props$topOffset = _this$props.topOffset,
topOffset = _this$props$topOffset === void 0 ? 0 : _this$props$topOffset;
var _this$state = _this.state,
isAboveFold = _this$state.isAboveFold,
containerRect = _this$state.containerRect;
var nextContainerRect = _this.getContainerRect();
if (window.pageYOffset + topOffset >= nextContainerRect.top) {
if (!isAboveFold) {
_this.setState({
isAboveFold: true
});
}
} else {
if (isAboveFold) {
_this.setState({
isAboveFold: false
});
}
}
if (_isNumber(lowerBound) && window.pageYOffset >= lowerBound) {
_this.setState({
isAboveFold: false
});
}
if (containerRect.bottom !== nextContainerRect.bottom || containerRect.height !== nextContainerRect.height || containerRect.left !== nextContainerRect.left || containerRect.right !== nextContainerRect.right || containerRect.top !== nextContainerRect.top || containerRect.width !== nextContainerRect.width || containerRect.scrollWidth !== nextContainerRect.scrollWidth || containerRect.frameLeft !== nextContainerRect.frameLeft) {
_this.setState({
containerRect: nextContainerRect
});
}
});
_defineProperty(_assertThisInitialized(_this), "getContainerRect", function () {
var containerRect = getAbsoluteBoundingClientRect(_this.scrollContainer.current);
var stickyRect = _this.stickySection.current.getBoundingClientRect();
var frameRect = _this.stickyFrame.current.getBoundingClientRect();
return {
bottom: containerRect.top + stickyRect.height,
height: stickyRect.height,
left: containerRect.left,
right: containerRect.left + stickyRect.width,
top: containerRect.top,
scrollWidth: _this.stickySection.current.scrollWidth,
width: containerRect.width,
frameLeft: frameRect.left
};
});
return _this;
}
_createClass(StickySection, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
setTimeout(function () {
_this2.setState({
containerRect: _this2.getContainerRect()
});
_this2.handleScroll();
}, 1);
window.addEventListener('scroll', this.handleScroll, true);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll, true);
}
}, {
key: "render",
value: function render() {
var _this$props2 = this.props,
children = _this$props2.children,
className = _this$props2.className,
style = _this$props2.style,
_this$props2$topOffse = _this$props2.topOffset,
topOffset = _this$props2$topOffse === void 0 ? 0 : _this$props2$topOffse,
viewportWidth = _this$props2.viewportWidth,
passThroughs = _objectWithoutProperties(_this$props2, ["children", "className", "style", "topOffset", "viewportWidth"]);
var _this$state2 = this.state,
isAboveFold = _this$state2.isAboveFold,
containerRect = _this$state2.containerRect;
return /*#__PURE__*/React.createElement("div", _extends({}, omitProps(passThroughs, undefined, Object.keys(StickySection.propTypes)), {
className: cx('&', className),
style: _objectSpread(_objectSpread({}, isAboveFold ? {
height: containerRect.height,
visibility: 'hidden'
} : {}), style),
ref: this.scrollContainer
}), /*#__PURE__*/React.createElement("div", {
className: cx('&-sticky-frame'),
ref: this.stickyFrame,
style: _objectSpread(_objectSpread({}, isAboveFold ? {
visibility: 'visible',
position: 'fixed',
top: topOffset,
width: _isNumber(viewportWidth) ? viewportWidth : containerRect.width,
height: containerRect.height,
overflow: 'hidden'
} : {}), style)
}, /*#__PURE__*/React.createElement("div", {
className: cx('&-sticky-section'),
ref: this.stickySection,
style: _objectSpread(_objectSpread({}, isAboveFold ? {
position: 'absolute',
top: 0,
left: containerRect.left - containerRect.frameLeft || 0,
width: containerRect.scrollWidth,
height: containerRect.height
} : {
position: 'relative'
}), style)
}, children)));
}
}]);
return StickySection;
}(React.Component);
_defineProperty(StickySection, "displayName", 'StickySection');
_defineProperty(StickySection, "peek", {
description: "\n\t\t\t`StickySection` can be wrapped around any content to make it _stick_\n\t\t\tto the top edge of the screen when a user scrolls beyond its initial\n\t\t\tlocation.\n\t\t",
categories: ['helpers']
});
_defineProperty(StickySection, "propTypes", {
children: node,
className: string,
style: object,
lowerBound: number,
viewportWidth: number,
topOffset: number
});
export default StickySection;