1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
|
4 |
|
5 | var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
|
6 |
|
7 | _Object$defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 |
|
11 | exports["default"] = void 0;
|
12 |
|
13 | var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/extends"));
|
14 |
|
15 | var _objectSpread2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/objectSpread"));
|
16 |
|
17 | var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/objectWithoutProperties"));
|
18 |
|
19 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/defineProperty"));
|
20 |
|
21 | var _react = _interopRequireDefault(require("react"));
|
22 |
|
23 | var _lodash = _interopRequireDefault(require("lodash.throttle"));
|
24 |
|
25 | var _withState = _interopRequireDefault(require("recompose/withState"));
|
26 |
|
27 | var _lifecycle = _interopRequireDefault(require("recompose/lifecycle"));
|
28 |
|
29 | var _compose = _interopRequireDefault(require("recompose/compose"));
|
30 |
|
31 | var _withStyles = _interopRequireDefault(require("@material-ui/core/styles/withStyles"));
|
32 |
|
33 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
34 |
|
35 |
|
36 |
|
37 | var 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 |
|
48 | var 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 | };
|
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 |
|
124 | var 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 |
|
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 |
|
159 | var _default = hideOnScroll;
|
160 | exports["default"] = _default; |
\ | No newline at end of file |