UNPKG

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