@dabapps/roe
Version:
A collection of React components, styles, mixins, and atomic CSS classes to aid with the development of web applications.
118 lines • 5.09 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var resize_observer_1 = require("@juggle/resize-observer");
var classNames = require("classnames");
var React = require("react");
var store_1 = require("../../store");
var utils_1 = require("../../utils");
var NavBar = function (props) {
var children = props.children, className = props.className, fixed = props.fixed, shy = props.shy, noShadow = props.noShadow, _a = props.component, Component = _a === void 0 ? 'div' : _a, remainingProps = __rest(props, ["children", "className", "fixed", "shy", "noShadow", "component"]);
var resizeObserverRef = React.useRef(null);
var previousScrollYRef = React.useRef(utils_1.getScrollOffset().y);
var mountTimeRef = React.useRef();
var _b = React.useState(false), hidden = _b[0], setHidden = _b[1];
var _c = React.useState(null), navBar = _c[0], setNavBar = _c[1];
React.useEffect(function () {
var _a;
var notifyAppRoot = function () {
store_1.default.setState({
hasFixedNavBar: Boolean(shy || fixed),
navBarHeight: navBar instanceof HTMLElement
? navBar.getBoundingClientRect().height
: undefined,
});
};
var hideOrShowNavBar = function () {
var y = utils_1.getScrollOffset().y;
if (typeof mountTimeRef.current === 'undefined' ||
new Date().getTime() < mountTimeRef.current + 250) {
previousScrollYRef.current = y;
return;
}
/* istanbul ignore else */
if (navBar instanceof HTMLElement) {
var height = navBar.getBoundingClientRect().height;
/* istanbul ignore else */
if (y > previousScrollYRef.current + height / 2 && y > height) {
setHidden(true);
previousScrollYRef.current = y;
}
else if (y < previousScrollYRef.current - height / 2) {
setHidden(false);
previousScrollYRef.current = y;
}
}
};
// Add/remove resize observer subscriptions when sticky or fixed changes
if (shy || fixed) {
if (navBar instanceof HTMLElement) {
resizeObserverRef.current = new resize_observer_1.ResizeObserver(notifyAppRoot);
resizeObserverRef.current.observe(navBar);
}
}
else {
(_a = resizeObserverRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
}
// Notify app root of new shy/fixed and nav bar height
notifyAppRoot();
// Subscribe/unsubscribe from scroll/resize
if (shy) {
window.addEventListener('scroll', hideOrShowNavBar);
window.addEventListener('resize', hideOrShowNavBar);
}
else {
window.removeEventListener('scroll', hideOrShowNavBar);
window.removeEventListener('resize', hideOrShowNavBar);
}
// Remove listeners on unmount
return function () {
var _a;
(_a = resizeObserverRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
window.removeEventListener('scroll', hideOrShowNavBar);
window.removeEventListener('resize', hideOrShowNavBar);
};
}, [shy, fixed, navBar]);
React.useEffect(function () {
mountTimeRef.current = new Date().getTime();
return function () {
store_1.default.setState({
hasFixedNavBar: false,
});
};
}, []);
var myClassNames = [
'nav-bar',
fixed || shy ? 'fixed' : null,
shy ? 'shy' : null,
hidden ? 'hidden' : null,
noShadow ? 'no-shadow' : null,
className,
];
// Cast necessary otherwise types are too complex
var CastComponent = Component;
return (React.createElement(CastComponent, __assign({ ref: setNavBar }, remainingProps, { className: classNames(myClassNames) }), children));
};
exports.default = React.memo(NavBar);
//# sourceMappingURL=nav-bar.js.map