optimizely-oui
Version:
Optimizely's Component Library.
238 lines (200 loc) • 9.79 kB
JavaScript
function _typeof(obj) { 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); }
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 _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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
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 _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 PropTypes from "prop-types";
import React from "react";
import ButtonRow from "../ButtonRow";
import classNames from "classnames";
var DockedFooter =
/*#__PURE__*/
function (_React$Component) {
_inherits(DockedFooter, _React$Component);
function DockedFooter(props) {
var _this;
_classCallCheck(this, DockedFooter);
_this = _possibleConstructorReturn(this, _getPrototypeOf(DockedFooter).call(this, props));
_defineProperty(_assertThisInitialized(_this), "domNodesPresent", function () {
return !!_this.dockedFooterRef.current && !!_this.props.scrollRef;
});
_defineProperty(_assertThisInitialized(_this), "setEventListeners", function () {
if (!_this.state.listenersSet && _this.domNodesPresent()) {
var scrollContainer = _this.props.scrollRef;
window.addEventListener("resize", _this.throttle(3, function () {
return _this.setIsDocked();
}), {
passive: false
}, {
useCapture: true
});
scrollContainer.addEventListener("scroll", _this.throttle(3, function () {
return _this.setAtBottom();
}), {
passive: true
});
scrollContainer.addEventListener("wheel", _this.throttle(3, function () {
return _this.setAtBottom();
}), {
passive: true
});
scrollContainer.addEventListener("click", function () {
return _this.setIsDocked();
}, {
passive: true
});
scrollContainer.addEventListener("keydown", function () {
return _this.setIsDocked();
}, {
passive: true
});
_this.setState({
listenersSet: true
});
}
});
_defineProperty(_assertThisInitialized(_this), "throttle", function (delay, fn) {
var lastCall = 0;
return function () {
var now = new Date().getTime();
if (now - lastCall < delay) {
return;
}
lastCall = now;
return fn.apply(void 0, arguments);
};
});
_defineProperty(_assertThisInitialized(_this), "setIsDocked", function (callback) {
if (_this.domNodesPresent()) {
var shouldDockByHeight = _this.dockedFooterRef.current.offsetTop >= _this.props.scrollRef.offsetHeight - _this.dockedFooterRef.current.offsetHeight;
var newState = {
isDocked: shouldDockByHeight
};
return _this.setState(newState, function () {
_this.setAtBottom();
});
}
_this.setAtBottom();
});
_defineProperty(_assertThisInitialized(_this), "setAtBottom", function () {
if (_this.domNodesPresent()) {
var atBottom = _this.props.scrollRef.scrollHeight - _this.props.scrollRef.scrollTop === _this.props.scrollRef.clientHeight;
var newState = {
atBottom: atBottom,
isDocked: !atBottom
};
_this.setState(newState);
}
});
_this.state = {
isDocked: true,
listenersSet: false,
atBottom: true
};
_this.dockedFooterRef = React.createRef();
return _this;
}
_createClass(DockedFooter, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
if (this.props.scrollRef !== prevProps.scrollRef) {
this.setIsDocked();
this.setEventListeners();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
window.removeEventListener("resize", this.throttle(3, this.shouldDock));
window.removeEventListener("mousemove", this.throttle(5, this.setIsDocked), {
passive: false
}, {
useCapture: true
});
this.props.scrollRef.removeEventListener("scroll", this.throttle(3, this.setAtBottom), {
passive: true
});
this.props.scrollRef.removeEventListener("wheel", this.throttle(3, this.setAtBottom), {
passive: true
});
this.props.scrollRef.removeEventListener("click", this.shouldDock, {
passive: true
});
this.props.scrollRef.removeEventListener("keydown", this.shouldDock, {
passive: true
});
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
centerGroup = _this$props.centerGroup,
className = _this$props.className,
includesMargin = _this$props.includesMargin,
leftGroup = _this$props.leftGroup,
rightGroup = _this$props.rightGroup,
scrollRef = _this$props.scrollRef,
testSection = _this$props.testSection,
props = _objectWithoutProperties(_this$props, ["centerGroup", "className", "includesMargin", "leftGroup", "rightGroup", "scrollRef", "testSection"]);
return React.createElement("div", _extends({
"data-test-section": testSection,
ref: this.dockedFooterRef,
className: classNames({
"oui-docked-footer": true,
"oui-docked-footer--is-docked": this.state.isDocked,
"push-double--top": includesMargin
}, className)
}, props), React.createElement(ButtonRow, {
leftGroup: leftGroup,
centerGroup: centerGroup,
rightGroup: rightGroup,
testSection: testSection
}));
}
}]);
return DockedFooter;
}(React.Component);
DockedFooter.propTypes = {
/**
* Any components to be included in the DockedFooter
*/
centerGroup: PropTypes.arrayOf(PropTypes.element),
/** CSS class names. */
className: PropTypes.string,
/**
* Used to determine if there should be top margin
*/
includesMargin: PropTypes.bool,
/**
* Array of buttons for left side
*/
leftGroup: PropTypes.arrayOf(PropTypes.element),
/**
* Array of buttons for right side
*/
rightGroup: PropTypes.arrayOf(PropTypes.element),
/**
* Ref from parent element for DockedFooter to set listeners
*/
scrollRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
current: PropTypes.elementType
})]),
/**
* Identifier used to create data-test-section attributes for testing.
*/
testSection: PropTypes.string.isRequired
};
DockedFooter.defaultProps = {
scrollRef: null,
includesMargin: false,
testSection: ""
};
export default DockedFooter;