UNPKG

4.84 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = void 0;
9
10var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/extends"));
11
12var _objectSpread2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/objectSpread"));
13
14var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/objectWithoutProperties"));
15
16var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/defineProperty"));
17
18var _react = _interopRequireDefault(require("react"));
19
20var _lodash = _interopRequireDefault(require("lodash.throttle"));
21
22var _recompose = require("recompose");
23
24var _styles = require("@material-ui/core/styles");
25
26var _propTypes = _interopRequireDefault(require("prop-types"));
27
28/* eslint-disable import/no-extraneous-dependencies */
29// flow-disable-next-line
30var styles = function styles(_ref) {
31 var breakpoints = _ref.breakpoints;
32 return {
33 root: (0, _defineProperty2.default)({
34 transition: 'margin-top 0.2s'
35 }, breakpoints.up('sm'), {
36 marginTop: '0 !important'
37 })
38 };
39};
40
41var lifecycle = {
42 componentDidMount: function componentDidMount() {
43 var _this$props = this.props,
44 updateIsVisible = _this$props.updateIsVisible,
45 updateHeight = _this$props.updateHeight;
46 var cmp = document.getElementsByClassName('hide-on-scroll')[0];
47
48 if (!cmp) {
49 throw Error('You use hideOnScroll but no element with the hide-on-scroll was found');
50 }
51
52 var getFullHeight = function getFullHeight() {
53 return cmp.clientHeight + 11;
54 }; // The Material UI box-shadow height
55
56
57 var getHeight = function getHeight() {
58 return cmp.clientHeight;
59 };
60
61 updateHeight(getFullHeight());
62 window.onresize = (0, _lodash.default)(function () {
63 return updateHeight(getFullHeight());
64 });
65 var headroom = 20;
66 var current = window.scrollY;
67 var initial = current;
68 var last = current;
69 var lowest = current;
70 var highest = current;
71 var goingUp;
72 var goingDown;
73 window.onscroll = (0, _lodash.default)(function () {
74 current = window.scrollY;
75 goingUp = current < last;
76 goingDown = !goingUp;
77
78 if (initial !== current) {
79 if (current < getHeight()) {
80 updateIsVisible(true);
81 } else {
82 if (current < lowest - headroom) {
83 updateIsVisible(true);
84 }
85
86 if (current > highest + headroom) {
87 updateIsVisible(false);
88 }
89 }
90 }
91
92 last = current;
93
94 if (goingDown) {
95 if (current > lowest) {
96 lowest = current;
97 }
98
99 if (current > highest + headroom) {
100 highest = current - headroom;
101 }
102 }
103
104 if (goingUp) {
105 if (current < highest) {
106 highest = current;
107 }
108
109 if (current < lowest - headroom) {
110 lowest = current + headroom;
111 }
112 }
113 }, 100);
114 }
115};
116
117var hideOnScroll = function hideOnScroll(Cmp) {
118 var AutoHideCmp = function AutoHideCmp(_ref2) {
119 var classes = _ref2.classes,
120 children = _ref2.children,
121 height = _ref2.height,
122 isVisible = _ref2.isVisible,
123 style = _ref2.style,
124 updateIsVisible = _ref2.updateIsVisible,
125 updateHeight = _ref2.updateHeight,
126 rest = (0, _objectWithoutProperties2.default)(_ref2, ["classes", "children", "height", "isVisible", "style", "updateIsVisible", "updateHeight"]);
127 return _react.default.createElement(Cmp, (0, _extends2.default)({
128 classes: classes,
129 style: (0, _objectSpread2.default)({}, style, {
130 // eslint-disable-next-line no-nested-ternary
131 marginTop: isVisible ? 0 : height ? 0 - height : 0
132 })
133 }, rest), children);
134 };
135
136 AutoHideCmp.propTypes = {
137 classes: _propTypes.default.object.isRequired,
138 children: function children(props, propName, componentName) {
139 if (!Object.prototype.hasOwnProperty.call(props, propName)) {
140 throw new Error("Prop `".concat(propName, "` has type 'any' or 'mixed', but was not provided to `").concat(componentName, "`. Pass undefined or any other value."));
141 }
142 },
143 isVisible: _propTypes.default.bool.isRequired,
144 style: _propTypes.default.object,
145 height: _propTypes.default.number,
146 updateIsVisible: _propTypes.default.func.isRequired,
147 updateHeight: _propTypes.default.func.isRequired
148 };
149 return (0, _recompose.compose)((0, _recompose.withState)('isVisible', 'updateIsVisible', true), (0, _recompose.withState)('height', 'updateHeight', null), (0, _recompose.lifecycle)(lifecycle), (0, _styles.withStyles)(styles))(AutoHideCmp);
150};
151
152var _default = hideOnScroll;
153exports.default = _default;
\No newline at end of file