UNPKG

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