UNPKG

backpack-ui

Version:
143 lines (108 loc) 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of"); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck"); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require("babel-runtime/helpers/createClass"); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn"); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require("babel-runtime/helpers/inherits"); var _inherits3 = _interopRequireDefault(_inherits2); var _class, _temp; var _react = require("react"); var _react2 = _interopRequireDefault(_react); var _propTypes = require("prop-types"); var _propTypes2 = _interopRequireDefault(_propTypes); var _noScroll = require("no-scroll"); var _noScroll2 = _interopRequireDefault(_noScroll); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var ModalWrapper = (_temp = _class = function (_React$Component) { (0, _inherits3.default)(ModalWrapper, _React$Component); function ModalWrapper(props) { (0, _classCallCheck3.default)(this, ModalWrapper); var _this = (0, _possibleConstructorReturn3.default)(this, (ModalWrapper.__proto__ || (0, _getPrototypeOf2.default)(ModalWrapper)).call(this, props)); _this.state = { open: false, scrollPosition: 0 }; _this.toggleOpen = _this.toggleOpen.bind(_this); _this.handleNavigateAway = _this.handleNavigateAway.bind(_this); return _this; } (0, _createClass3.default)(ModalWrapper, [{ key: "componentDidMount", value: function componentDidMount() { window.onbeforeunload = this.handleNavigateAway; if (this.props.hash) { if (typeof window !== "undefined" && window.location.hash === this.props.hash) { this.toggleOpen(); } } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.handleNavigateAway(); } }, { key: "toggleOpen", value: function toggleOpen() { var _this2 = this; this.setState({ open: !this.state.open }, function () { if (_this2.state.open) { if (!_this2.props.enableWindowScrolling) { _noScroll2.default.on(); ModalWrapper.scrollTo(0, 0); _this2.setState({ scrollPosition: parseInt(document.documentElement.style.top.replace("px", ""), 10) * -1 }); } } else { if (!_this2.props.enableWindowScrolling) { _noScroll2.default.off(); ModalWrapper.scrollTo(0, _this2.state.scrollPosition); } if (typeof window !== "undefined" && window.location.hash === _this2.props.hash && history.pushState) { history.pushState("", "", window.location.pathname); } } }); } // eslint-disable-next-line class-methods-use-this }, { key: "handleNavigateAway", value: function handleNavigateAway() { if (!this.props.enableWindowScrolling) { _noScroll2.default.off(); } } }, { key: "render", value: function render() { return this.props.children(this.state.open, this.toggleOpen); } }]); return ModalWrapper; }(_react2.default.Component), _class.scrollTo = function () { var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; if (typeof window !== "undefined") { window.scrollTo(x, y); } }, _temp); ModalWrapper.propTypes = { children: _propTypes2.default.func.isRequired, enableWindowScrolling: _propTypes2.default.bool, hash: _propTypes2.default.string }; ModalWrapper.defaultProps = { enableWindowScrolling: false, hash: null }; exports.default = ModalWrapper;