@integreat-app/react-sticky-headroom 1.2.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/index.cjs +228 -0
- package/index.cjs.map +1 -0
- package/index.d.ts +85 -17
- package/index.d.ts.map +1 -0
- package/index.js +155 -242
- package/index.js.map +1 -0
- package/{index.js.flow → index.tsx} +82 -93
- package/package.json +62 -65
package/index.js
CHANGED
|
@@ -1,259 +1,172 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
}, function (props) {
|
|
41
|
-
return props.translateY;
|
|
42
|
-
}, function (props) {
|
|
43
|
-
return props.transition === NORMAL_TRANSITION && !props.static ? 'transition: transform 0.2s ease-out;' : '';
|
|
44
|
-
}, function (props) {
|
|
45
|
-
return props.transition === PINNED_TO_STATIC ? (0, _styledComponents.css)(["animation-name:", ";"], keyframesMoveUpFrom(props.animateUpFrom)) : '';
|
|
46
|
-
}, function (props) {
|
|
47
|
-
return props.static ? 'transition: none;' : '';
|
|
48
|
-
});
|
|
49
|
-
var keyframesMoveUpFrom = function keyframesMoveUpFrom(from) {
|
|
50
|
-
return (0, _styledComponents.keyframes)(["from{transform:translateY(", "px)}to{transform:translateY(0)}"], Math.max(from, 0));
|
|
51
|
-
};
|
|
52
|
-
var Headroom = /*#__PURE__*/function (_React$PureComponent) {
|
|
53
|
-
_inherits(Headroom, _React$PureComponent);
|
|
54
|
-
var _super = _createSuper(Headroom);
|
|
55
|
-
function Headroom() {
|
|
56
|
-
var _this;
|
|
57
|
-
_classCallCheck(this, Headroom);
|
|
58
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
59
|
-
args[_key] = arguments[_key];
|
|
60
|
-
}
|
|
61
|
-
_this = _super.call.apply(_super, [this].concat(args));
|
|
62
|
-
_defineProperty(_assertThisInitialized(_this), "state", {
|
|
63
|
-
mode: STATIC,
|
|
64
|
-
transition: NO_TRANSITION,
|
|
65
|
-
animateUpFrom: null
|
|
66
|
-
});
|
|
67
|
-
_defineProperty(_assertThisInitialized(_this), "lastKnownScrollTop", 0);
|
|
68
|
-
_defineProperty(_assertThisInitialized(_this), "update", function () {
|
|
69
|
-
var currentScrollTop = _this.getScrollTop();
|
|
70
|
-
var newState = {};
|
|
71
|
-
if (currentScrollTop === _this.lastKnownScrollTop) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
var direction = _this.lastKnownScrollTop < currentScrollTop ? DOWNWARDS : UPWARDS;
|
|
75
|
-
newState.mode = _this.determineMode(currentScrollTop, direction);
|
|
76
|
-
newState.transition = _this.determineTransition(newState.mode, direction);
|
|
77
|
-
var _this$props = _this.props,
|
|
78
|
-
onStickyTopChanged = _this$props.onStickyTopChanged,
|
|
79
|
-
height = _this$props.height,
|
|
80
|
-
scrollHeight = _this$props.scrollHeight,
|
|
81
|
-
pinStart = _this$props.pinStart;
|
|
82
|
-
if (_this.state.mode === PINNED && newState.mode === STATIC) {
|
|
83
|
-
// animation in the special case from pinned to static
|
|
84
|
-
newState.animateUpFrom = currentScrollTop - pinStart;
|
|
85
|
-
}
|
|
86
|
-
if (onStickyTopChanged && newState.mode !== _this.state.mode && height) {
|
|
87
|
-
onStickyTopChanged(Headroom.calcStickyTop(newState.mode, height, scrollHeight));
|
|
88
|
-
}
|
|
89
|
-
_this.setState(newState);
|
|
90
|
-
_this.lastKnownScrollTop = currentScrollTop;
|
|
91
|
-
});
|
|
92
|
-
_defineProperty(_assertThisInitialized(_this), "handleEvent", function () {
|
|
93
|
-
window.requestAnimationFrame(_this.update);
|
|
94
|
-
});
|
|
95
|
-
return _this;
|
|
96
|
-
}
|
|
97
|
-
_createClass(Headroom, [{
|
|
98
|
-
key: "getScrollTop",
|
|
99
|
-
value:
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import styled, { css, keyframes } from 'styled-components';
|
|
3
|
+
const DIRECTION_UP = 'up';
|
|
4
|
+
const DIRECTION_DOWN = 'down';
|
|
5
|
+
const MODE_UNPINNED = 'unpinned';
|
|
6
|
+
const MODE_PINNED = 'pinned';
|
|
7
|
+
const MODE_STATIC = 'static';
|
|
8
|
+
const TRANSITION_NONE = 'none';
|
|
9
|
+
const TRANSITION_NORMAL = 'normal';
|
|
10
|
+
const TRANSITION_PINNED_TO_STATIC = 'pinned-to-static';
|
|
11
|
+
const HeaderWrapper = styled.div([
|
|
12
|
+
"position:",
|
|
13
|
+
";top:",
|
|
14
|
+
"px;z-index:",
|
|
15
|
+
";transform:translateY(",
|
|
16
|
+
"px);animation-duration:0.2s;animation-timing-function:ease-out;",
|
|
17
|
+
" ",
|
|
18
|
+
" ",
|
|
19
|
+
""
|
|
20
|
+
], (props)=>props.$positionStickyDisabled ? 'static' : 'sticky', (props)=>props.$top, (props)=>props.$zIndex, (props)=>props.$translateY, (props)=>props.$transition === TRANSITION_NORMAL && !props.$static ? 'transition: transform 0.2s ease-out;' : '', (props)=>props.$transition === TRANSITION_PINNED_TO_STATIC && props.$animateUpFrom !== null ? css([
|
|
21
|
+
"animation-name:",
|
|
22
|
+
";"
|
|
23
|
+
], keyframesMoveUpFrom(props.$animateUpFrom)) : '', (props)=>props.$static ? 'transition: none;' : '');
|
|
24
|
+
const keyframesMoveUpFrom = (from)=>keyframes([
|
|
25
|
+
"from{transform:translateY(",
|
|
26
|
+
"px)}to{transform:translateY(0)}"
|
|
27
|
+
], Math.max(from, 0));
|
|
28
|
+
class Headroom extends React.PureComponent {
|
|
29
|
+
static defaultProps = {
|
|
30
|
+
pinStart: 0,
|
|
31
|
+
zIndex: 1,
|
|
32
|
+
parent: window.document.documentElement
|
|
33
|
+
};
|
|
34
|
+
state = {
|
|
35
|
+
mode: MODE_STATIC,
|
|
36
|
+
transition: TRANSITION_NONE,
|
|
37
|
+
animateUpFrom: null
|
|
38
|
+
};
|
|
39
|
+
/** the very last scrollTop which we know about (to determine direction changes) */ lastKnownScrollTop = 0;
|
|
100
40
|
/**
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
41
|
+
* @returns {number} the current scrollTop position of the window
|
|
42
|
+
*/ getScrollTop() {
|
|
43
|
+
const parent = this.props.parent;
|
|
44
|
+
if (parent && parent.scrollTop !== undefined && parent !== document.documentElement) {
|
|
45
|
+
return parent.scrollTop;
|
|
46
|
+
}
|
|
47
|
+
if (parent !== document.documentElement) {
|
|
48
|
+
console.warn('Could not determine scrollTop from parent for StickyHeadroom. Defaulting to window.pageYOffset.');
|
|
49
|
+
}
|
|
50
|
+
if (window.pageYOffset === undefined) {
|
|
51
|
+
console.error('window.pageYOffset is undefined. Defaulting to 0.');
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
112
54
|
return window.pageYOffset;
|
|
113
|
-
} else if (window.scrollTop !== undefined) {
|
|
114
|
-
return window.scrollTop;
|
|
115
|
-
} else if (document.documentElement) {
|
|
116
|
-
return document.documentElement.scrollTop;
|
|
117
|
-
} else if (document.body) {
|
|
118
|
-
return document.body.scrollTop;
|
|
119
|
-
} else {
|
|
120
|
-
throw new Error('Could not determine scrollTop!');
|
|
121
|
-
}
|
|
122
55
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
value: function componentDidMount() {
|
|
126
|
-
this.addScrollListener(this.props.parent);
|
|
56
|
+
componentDidMount() {
|
|
57
|
+
this.addScrollListener(this.props.parent);
|
|
127
58
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
console.debug('Property parent of Headroom is null. Assuming, parent will be set soon...');
|
|
137
|
-
}
|
|
59
|
+
addScrollListener(parent) {
|
|
60
|
+
if (parent === window.document.documentElement) {
|
|
61
|
+
window.addEventListener('scroll', this.handleEvent);
|
|
62
|
+
} else if (parent) {
|
|
63
|
+
parent.addEventListener('scroll', this.handleEvent);
|
|
64
|
+
} else {
|
|
65
|
+
console.debug("'parent' prop of Headroom is null. Assuming, it will be set soon...");
|
|
66
|
+
}
|
|
138
67
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
parent.removeEventListener('scroll', this.handleEvent);
|
|
146
|
-
}
|
|
68
|
+
removeScrollListener(parent) {
|
|
69
|
+
if (parent === window.document.documentElement) {
|
|
70
|
+
window.removeEventListener('scroll', this.handleEvent);
|
|
71
|
+
} else if (parent) {
|
|
72
|
+
parent.removeEventListener('scroll', this.handleEvent);
|
|
73
|
+
}
|
|
147
74
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
this.addScrollListener(this.props.parent);
|
|
154
|
-
}
|
|
75
|
+
componentDidUpdate(prevProps) {
|
|
76
|
+
if (prevProps.parent !== this.props.parent) {
|
|
77
|
+
this.removeScrollListener(prevProps.parent);
|
|
78
|
+
this.addScrollListener(this.props.parent);
|
|
79
|
+
}
|
|
155
80
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
value: function componentWillUnmount() {
|
|
159
|
-
this.removeScrollListener(this.props.parent);
|
|
81
|
+
componentWillUnmount() {
|
|
82
|
+
this.removeScrollListener(this.props.parent);
|
|
160
83
|
}
|
|
161
|
-
|
|
162
84
|
/**
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
} else {
|
|
177
|
-
return this.props.pinStart >= scrollTop;
|
|
178
|
-
}
|
|
85
|
+
* If we're already static and pinStart + scrollHeight >= scrollTop, then we should stay static.
|
|
86
|
+
* If we're not already static, then we should set the header static, only when pinStart >= scrollTop (regardless of
|
|
87
|
+
* scrollHeight, so the header doesn't jump up, when scrolling upwards to the trigger).
|
|
88
|
+
* Else we shouldn't set it static.
|
|
89
|
+
* @param scrollTop the currentScrollTop position
|
|
90
|
+
* @param direction the current direction
|
|
91
|
+
* @returns {boolean} if we should set the header static
|
|
92
|
+
*/ shouldSetStatic(scrollTop, direction) {
|
|
93
|
+
if (this.state.mode === MODE_STATIC || this.state.mode === MODE_PINNED && direction === DIRECTION_DOWN) {
|
|
94
|
+
return this.props.pinStart + this.props.scrollHeight >= scrollTop;
|
|
95
|
+
} else {
|
|
96
|
+
return this.props.pinStart >= scrollTop;
|
|
97
|
+
}
|
|
179
98
|
}
|
|
180
|
-
|
|
181
99
|
/**
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
} else {
|
|
193
|
-
return direction === UPWARDS ? PINNED : UNPINNED;
|
|
194
|
-
}
|
|
100
|
+
* Determines the mode depending on the scrollTop position and the current direction
|
|
101
|
+
* @param {number} scrollTop
|
|
102
|
+
* @param {string} direction
|
|
103
|
+
* @returns {string} the next mode of Headroom
|
|
104
|
+
*/ determineMode(scrollTop, direction) {
|
|
105
|
+
if (this.shouldSetStatic(scrollTop, direction)) {
|
|
106
|
+
return MODE_STATIC;
|
|
107
|
+
} else {
|
|
108
|
+
return direction === DIRECTION_UP ? MODE_PINNED : MODE_UNPINNED;
|
|
109
|
+
}
|
|
195
110
|
}
|
|
196
|
-
|
|
197
111
|
/**
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
return this.state.transition ===
|
|
211
|
-
}
|
|
212
|
-
// mode is not static, transition when moving upwards or when we've lastly did the transition
|
|
213
|
-
return direction === UPWARDS || this.state.transition === NORMAL_TRANSITION ? NORMAL_TRANSITION : NO_TRANSITION;
|
|
112
|
+
* @returns {TransitionType} determines the kind of transition
|
|
113
|
+
*/ determineTransition(mode, direction) {
|
|
114
|
+
// Handle special case: If we're pinned and going to static, we need a special transition using css animation
|
|
115
|
+
if (this.state.mode === MODE_PINNED && mode === MODE_STATIC) {
|
|
116
|
+
return TRANSITION_PINNED_TO_STATIC;
|
|
117
|
+
}
|
|
118
|
+
// If mode is static, then no transition, because we're already in the right spot
|
|
119
|
+
// (and want to change transform and top properties seamlessly)
|
|
120
|
+
if (mode === MODE_STATIC) {
|
|
121
|
+
return this.state.transition === TRANSITION_NONE ? TRANSITION_NONE : TRANSITION_PINNED_TO_STATIC;
|
|
122
|
+
}
|
|
123
|
+
// mode is not static, transition when moving upwards or when we've lastly did the transition
|
|
124
|
+
return direction === DIRECTION_UP || this.state.transition === TRANSITION_NORMAL ? TRANSITION_NORMAL : TRANSITION_NONE;
|
|
214
125
|
}
|
|
215
|
-
|
|
216
126
|
/**
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}, children);
|
|
127
|
+
* Checks the current scrollTop position and updates the state accordingly
|
|
128
|
+
*/ update = ()=>{
|
|
129
|
+
const currentScrollTop = this.getScrollTop();
|
|
130
|
+
const newState = {};
|
|
131
|
+
if (currentScrollTop === this.lastKnownScrollTop) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const direction = this.lastKnownScrollTop < currentScrollTop ? DIRECTION_DOWN : DIRECTION_UP;
|
|
135
|
+
newState.mode = this.determineMode(currentScrollTop, direction);
|
|
136
|
+
newState.transition = this.determineTransition(newState.mode, direction);
|
|
137
|
+
const { onStickyTopChanged, height, scrollHeight, pinStart } = this.props;
|
|
138
|
+
if (this.state.mode === MODE_PINNED && newState.mode === MODE_STATIC) {
|
|
139
|
+
// animation in the special case from pinned to static
|
|
140
|
+
newState.animateUpFrom = currentScrollTop - pinStart;
|
|
141
|
+
}
|
|
142
|
+
if (onStickyTopChanged && newState.mode !== this.state.mode && height) {
|
|
143
|
+
onStickyTopChanged(Headroom.calcStickyTop(newState.mode, height, scrollHeight));
|
|
144
|
+
}
|
|
145
|
+
this.setState(newState);
|
|
146
|
+
this.lastKnownScrollTop = currentScrollTop;
|
|
147
|
+
};
|
|
148
|
+
handleEvent = ()=>{
|
|
149
|
+
window.requestAnimationFrame(this.update);
|
|
150
|
+
};
|
|
151
|
+
static calcStickyTop(mode, height, scrollHeight) {
|
|
152
|
+
return mode === MODE_PINNED ? height : height - scrollHeight;
|
|
244
153
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
154
|
+
render() {
|
|
155
|
+
const { children, scrollHeight, positionStickyDisabled, zIndex, className } = this.props;
|
|
156
|
+
const { mode, transition, animateUpFrom } = this.state;
|
|
157
|
+
const transform = mode === MODE_UNPINNED ? -scrollHeight : 0;
|
|
158
|
+
const ownStickyTop = mode === MODE_STATIC ? -scrollHeight : 0;
|
|
159
|
+
return /*#__PURE__*/ React.createElement(HeaderWrapper, {
|
|
160
|
+
className: className,
|
|
161
|
+
$translateY: transform,
|
|
162
|
+
$top: ownStickyTop,
|
|
163
|
+
$transition: transition,
|
|
164
|
+
$positionStickyDisabled: !!positionStickyDisabled,
|
|
165
|
+
$static: mode === MODE_STATIC,
|
|
166
|
+
$animateUpFrom: animateUpFrom,
|
|
167
|
+
$zIndex: zIndex
|
|
168
|
+
}, children);
|
|
249
169
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
_defineProperty(Headroom, "defaultProps", {
|
|
254
|
-
pinStart: 0,
|
|
255
|
-
zIndex: 1,
|
|
256
|
-
parent: window.document.documentElement
|
|
257
|
-
});
|
|
258
|
-
var _default = Headroom;
|
|
259
|
-
exports.default = _default;
|
|
170
|
+
}
|
|
171
|
+
export default Headroom;
|
|
172
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.tsx"],"sourcesContent":["import React from 'react'\nimport styled, { css, keyframes } from 'styled-components'\n\nconst DIRECTION_UP = 'up'\nconst DIRECTION_DOWN = 'down'\n\nconst MODE_UNPINNED = 'unpinned'\nconst MODE_PINNED = 'pinned'\nconst MODE_STATIC = 'static'\n\nconst TRANSITION_NONE = 'none'\nconst TRANSITION_NORMAL = 'normal'\nconst TRANSITION_PINNED_TO_STATIC = 'pinned-to-static'\n\ntype ModeType = typeof MODE_PINNED | typeof MODE_UNPINNED | typeof MODE_STATIC\ntype DirectionType = typeof DIRECTION_UP | typeof DIRECTION_DOWN\ntype TransitionType = typeof TRANSITION_NONE | typeof TRANSITION_NORMAL | typeof TRANSITION_PINNED_TO_STATIC\n\ntype PropsType = {\n /** The child node to be displayed as a header */\n children: React.ReactNode,\n /** The maximum amount of px the header should move up when scrolling */\n scrollHeight: number,\n /** The minimum scrollTop position where the transform should start */\n pinStart: number,\n /** Used for calculating the stickyTop position of an ancestor */\n height?: number,\n /** Fired, when Headroom changes its state. Passes stickyTop of the ancestor. */\n onStickyTopChanged?: (stickyTop: number) => void,\n /** True, if sticky position should be disabled (e.g. for edge 16 support) */\n positionStickyDisabled?: boolean,\n /** The parent element firing the scroll event. Defaults to document.documentElement */\n parent?: HTMLElement | null,\n /** The z-index used by the wrapper. Defaults to 1. */\n zIndex?: number,\n /** A classname for applying custom styles to the wrapper. Use at your own risk. */\n className?: string\n}\n\ntype StateType = {\n mode: ModeType,\n transition: TransitionType,\n animateUpFrom: number | null\n}\n\nconst HeaderWrapper = styled.div<{\n $positionStickyDisabled: boolean,\n $translateY: number,\n $transition: TransitionType,\n $animateUpFrom: number | null,\n $zIndex?: number,\n $top: number,\n $static: boolean\n}>`\n position: ${props => props.$positionStickyDisabled ? 'static' : 'sticky'};\n top: ${props => props.$top}px;\n z-index: ${props => props.$zIndex};\n transform: translateY(${props => props.$translateY}px);\n animation-duration: 0.2s;\n animation-timing-function: ease-out;\n ${props => props.$transition === TRANSITION_NORMAL && !props.$static\n ? 'transition: transform 0.2s ease-out;'\n : ''}\n ${props => props.$transition === TRANSITION_PINNED_TO_STATIC && props.$animateUpFrom !== null\n ? css`\n animation-name: ${keyframesMoveUpFrom(props.$animateUpFrom)};\n `\n : ''}\n ${props => props.$static ? 'transition: none;' : ''}\n`\n\nconst keyframesMoveUpFrom = (from: number) => keyframes`\n from {\n transform: translateY(${Math.max(from, 0)}px)\n }\n\n to {\n transform: translateY(0)\n }\n`\n\nclass Headroom extends React.PureComponent<PropsType, StateType> {\n static defaultProps: { pinStart: number, zIndex: number, parent: HTMLElement | null } = {\n pinStart: 0,\n zIndex: 1,\n parent: window.document.documentElement\n }\n\n state: StateType = {\n mode: MODE_STATIC,\n transition: TRANSITION_NONE,\n animateUpFrom: null\n }\n\n /** the very last scrollTop which we know about (to determine direction changes) */\n lastKnownScrollTop: number = 0\n\n /**\n * @returns {number} the current scrollTop position of the window\n */\n getScrollTop (): number {\n const parent = this.props.parent\n if (parent && parent.scrollTop !== undefined && parent !== document.documentElement) {\n return parent.scrollTop\n }\n if (parent !== document.documentElement) {\n console.warn('Could not determine scrollTop from parent for StickyHeadroom. Defaulting to window.pageYOffset.')\n }\n if (window.pageYOffset === undefined) {\n console.error('window.pageYOffset is undefined. Defaulting to 0.')\n return 0\n }\n return window.pageYOffset\n }\n\n componentDidMount () {\n this.addScrollListener(this.props.parent)\n }\n\n addScrollListener (parent?: HTMLElement | null) {\n if (parent === window.document.documentElement) {\n window.addEventListener('scroll', this.handleEvent)\n } else if (parent) {\n parent.addEventListener('scroll', this.handleEvent)\n } else {\n console.debug(\"'parent' prop of Headroom is null. Assuming, it will be set soon...\")\n }\n }\n\n removeScrollListener (parent?: HTMLElement | null) {\n if (parent === window.document.documentElement) {\n window.removeEventListener('scroll', this.handleEvent)\n } else if (parent) {\n parent.removeEventListener('scroll', this.handleEvent)\n }\n }\n\n componentDidUpdate (prevProps: PropsType) {\n if (prevProps.parent !== this.props.parent) {\n this.removeScrollListener(prevProps.parent)\n this.addScrollListener(this.props.parent)\n }\n }\n\n componentWillUnmount () {\n this.removeScrollListener(this.props.parent)\n }\n\n /**\n * If we're already static and pinStart + scrollHeight >= scrollTop, then we should stay static.\n * If we're not already static, then we should set the header static, only when pinStart >= scrollTop (regardless of\n * scrollHeight, so the header doesn't jump up, when scrolling upwards to the trigger).\n * Else we shouldn't set it static.\n * @param scrollTop the currentScrollTop position\n * @param direction the current direction\n * @returns {boolean} if we should set the header static\n */\n shouldSetStatic (scrollTop: number, direction: DirectionType): boolean {\n if (\n this.state.mode === MODE_STATIC ||\n (this.state.mode === MODE_PINNED && direction === DIRECTION_DOWN)\n ) {\n return this.props.pinStart + this.props.scrollHeight >= scrollTop\n } else {\n return this.props.pinStart >= scrollTop\n }\n }\n\n /**\n * Determines the mode depending on the scrollTop position and the current direction\n * @param {number} scrollTop\n * @param {string} direction\n * @returns {string} the next mode of Headroom\n */\n determineMode (scrollTop: number, direction: DirectionType): ModeType {\n if (this.shouldSetStatic(scrollTop, direction)) {\n return MODE_STATIC\n } else {\n return direction === DIRECTION_UP ? MODE_PINNED : MODE_UNPINNED\n }\n }\n\n /**\n * @returns {TransitionType} determines the kind of transition\n */\n determineTransition (mode: ModeType, direction: DirectionType): TransitionType {\n // Handle special case: If we're pinned and going to static, we need a special transition using css animation\n if (this.state.mode === MODE_PINNED && mode === MODE_STATIC) {\n return TRANSITION_PINNED_TO_STATIC\n }\n // If mode is static, then no transition, because we're already in the right spot\n // (and want to change transform and top properties seamlessly)\n if (mode === MODE_STATIC) {\n return this.state.transition === TRANSITION_NONE\n ? TRANSITION_NONE\n : TRANSITION_PINNED_TO_STATIC\n }\n // mode is not static, transition when moving upwards or when we've lastly did the transition\n return direction === DIRECTION_UP ||\n this.state.transition === TRANSITION_NORMAL\n ? TRANSITION_NORMAL\n : TRANSITION_NONE\n }\n\n /**\n * Checks the current scrollTop position and updates the state accordingly\n */\n update: () => void = () => {\n const currentScrollTop = this.getScrollTop()\n const newState: Partial<StateType> = {}\n if (currentScrollTop === this.lastKnownScrollTop) {\n return\n }\n const direction =\n this.lastKnownScrollTop < currentScrollTop ? DIRECTION_DOWN : DIRECTION_UP\n newState.mode = this.determineMode(currentScrollTop, direction)\n newState.transition = this.determineTransition(newState.mode, direction)\n\n const { onStickyTopChanged, height, scrollHeight, pinStart } = this.props\n if (this.state.mode === MODE_PINNED && newState.mode === MODE_STATIC) {\n // animation in the special case from pinned to static\n newState.animateUpFrom = currentScrollTop - pinStart\n }\n if (onStickyTopChanged && newState.mode !== this.state.mode && height) {\n onStickyTopChanged(Headroom.calcStickyTop(newState.mode, height, scrollHeight))\n }\n this.setState(newState as StateType)\n this.lastKnownScrollTop = currentScrollTop\n }\n\n handleEvent: () => void = () => {\n window.requestAnimationFrame(this.update)\n }\n\n static calcStickyTop (\n mode: ModeType,\n height: number,\n scrollHeight: number\n ): number {\n return mode === MODE_PINNED ? height : height - scrollHeight\n }\n\n render (): React.ReactElement {\n const {\n children,\n scrollHeight,\n positionStickyDisabled,\n zIndex,\n className\n } = this.props\n const {\n mode,\n transition,\n animateUpFrom\n } = this.state\n const transform = mode === MODE_UNPINNED ? -scrollHeight : 0\n const ownStickyTop = mode === MODE_STATIC ? -scrollHeight : 0\n return <HeaderWrapper\n className={className}\n $translateY={transform}\n $top={ownStickyTop}\n $transition={transition}\n $positionStickyDisabled={!!positionStickyDisabled}\n $static={mode === MODE_STATIC}\n $animateUpFrom={animateUpFrom}\n $zIndex={zIndex}>\n {children}\n </HeaderWrapper>\n }\n}\n\nexport default Headroom\n"],"names":["DIRECTION_UP","DIRECTION_DOWN","MODE_UNPINNED","MODE_PINNED","MODE_STATIC","TRANSITION_NONE","TRANSITION_NORMAL","TRANSITION_PINNED_TO_STATIC","HeaderWrapper","styled","div","props","$positionStickyDisabled","$top","$zIndex","$translateY","$transition","$static","$animateUpFrom","css","keyframesMoveUpFrom","from","keyframes","Math","max","Headroom","React","PureComponent","defaultProps","pinStart","zIndex","parent","window","document","documentElement","state","mode","transition","animateUpFrom","lastKnownScrollTop","getScrollTop","scrollTop","undefined","console","warn","pageYOffset","error","componentDidMount","addScrollListener","addEventListener","handleEvent","debug","removeScrollListener","removeEventListener","componentDidUpdate","prevProps","componentWillUnmount","shouldSetStatic","direction","scrollHeight","determineMode","determineTransition","update","currentScrollTop","newState","onStickyTopChanged","height","calcStickyTop","setState","requestAnimationFrame","render","children","positionStickyDisabled","className","transform","ownStickyTop"],"mappings":";;;;+BA+QA;;;eAAA;;;8DA/QkB;0EACqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEvC,MAAMA,eAAe;AACrB,MAAMC,iBAAiB;AAEvB,MAAMC,gBAAgB;AACtB,MAAMC,cAAc;AACpB,MAAMC,cAAc;AAEpB,M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