165 lines
5.7 KiB
Plaintext
165 lines
5.7 KiB
Plaintext
"use client";
|
|
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// src/use-navbar.ts
|
|
var use_navbar_exports = {};
|
|
__export(use_navbar_exports, {
|
|
useNavbar: () => useNavbar
|
|
});
|
|
module.exports = __toCommonJS(use_navbar_exports);
|
|
var import_system = require("@heroui/system");
|
|
var import_theme = require("@heroui/theme");
|
|
var import_react_utils = require("@heroui/react-utils");
|
|
var import_shared_utils = require("@heroui/shared-utils");
|
|
var import_react = require("react");
|
|
var import_utils = require("@react-aria/utils");
|
|
var import_use_scroll_position = require("@heroui/use-scroll-position");
|
|
var import_utils2 = require("@react-stately/utils");
|
|
var import_overlays = require("@react-aria/overlays");
|
|
function useNavbar(originalProps) {
|
|
var _a, _b;
|
|
const globalContext = (0, import_system.useProviderContext)();
|
|
const [props, variantProps] = (0, import_system.mapPropsVariants)(originalProps, import_theme.navbar.variantKeys);
|
|
const {
|
|
ref,
|
|
as,
|
|
parentRef,
|
|
height = "4rem",
|
|
shouldHideOnScroll = false,
|
|
disableScrollHandler = false,
|
|
shouldBlockScroll = true,
|
|
onScrollPositionChange,
|
|
isMenuOpen: isMenuOpenProp,
|
|
isMenuDefaultOpen,
|
|
onMenuOpenChange = () => {
|
|
},
|
|
motionProps,
|
|
className,
|
|
classNames,
|
|
...otherProps
|
|
} = props;
|
|
const Component = as || "nav";
|
|
const disableAnimation = (_b = (_a = originalProps.disableAnimation) != null ? _a : globalContext == null ? void 0 : globalContext.disableAnimation) != null ? _b : false;
|
|
const domRef = (0, import_react_utils.useDOMRef)(ref);
|
|
const prevWidth = (0, import_react.useRef)(0);
|
|
const navHeight = (0, import_react.useRef)(0);
|
|
const [isHidden, setIsHidden] = (0, import_react.useState)(false);
|
|
const handleMenuOpenChange = (0, import_react.useCallback)(
|
|
(isOpen) => {
|
|
onMenuOpenChange(isOpen || false);
|
|
},
|
|
[onMenuOpenChange]
|
|
);
|
|
const [isMenuOpen, setIsMenuOpen] = (0, import_utils2.useControlledState)(
|
|
isMenuOpenProp,
|
|
isMenuDefaultOpen != null ? isMenuDefaultOpen : false,
|
|
handleMenuOpenChange
|
|
);
|
|
const updateWidth = () => {
|
|
if (domRef.current) {
|
|
const width = domRef.current.offsetWidth;
|
|
if (width !== prevWidth.current) {
|
|
prevWidth.current = width;
|
|
}
|
|
}
|
|
};
|
|
(0, import_overlays.usePreventScroll)({
|
|
isDisabled: !(shouldBlockScroll && isMenuOpen)
|
|
});
|
|
(0, import_utils.useResizeObserver)({
|
|
ref: domRef,
|
|
onResize: () => {
|
|
var _a2;
|
|
const currentWidth = (_a2 = domRef.current) == null ? void 0 : _a2.offsetWidth;
|
|
const scrollWidth = window.innerWidth - document.documentElement.clientWidth;
|
|
if (currentWidth && currentWidth + scrollWidth == prevWidth.current) {
|
|
return;
|
|
}
|
|
if (currentWidth !== prevWidth.current) {
|
|
updateWidth();
|
|
setIsMenuOpen(false);
|
|
}
|
|
}
|
|
});
|
|
(0, import_react.useEffect)(() => {
|
|
var _a2;
|
|
updateWidth();
|
|
navHeight.current = ((_a2 = domRef.current) == null ? void 0 : _a2.offsetHeight) || 0;
|
|
}, []);
|
|
const slots = (0, import_react.useMemo)(
|
|
() => (0, import_theme.navbar)({
|
|
...variantProps,
|
|
disableAnimation,
|
|
hideOnScroll: shouldHideOnScroll
|
|
}),
|
|
[(0, import_shared_utils.objectToDeps)(variantProps), disableAnimation, shouldHideOnScroll]
|
|
);
|
|
const baseStyles = (0, import_shared_utils.clsx)(classNames == null ? void 0 : classNames.base, className);
|
|
(0, import_use_scroll_position.useScrollPosition)({
|
|
elementRef: parentRef,
|
|
isEnabled: shouldHideOnScroll || !disableScrollHandler,
|
|
callback: ({ prevPos, currPos }) => {
|
|
onScrollPositionChange == null ? void 0 : onScrollPositionChange(currPos.y);
|
|
if (shouldHideOnScroll) {
|
|
setIsHidden((prev) => {
|
|
const next = currPos.y > prevPos.y && currPos.y > navHeight.current;
|
|
return next !== prev ? next : prev;
|
|
});
|
|
}
|
|
}
|
|
});
|
|
const getBaseProps = (props2 = {}) => ({
|
|
...(0, import_utils.mergeProps)(otherProps, props2),
|
|
"data-hidden": (0, import_shared_utils.dataAttr)(isHidden),
|
|
"data-menu-open": (0, import_shared_utils.dataAttr)(isMenuOpen),
|
|
ref: domRef,
|
|
className: slots.base({ class: (0, import_shared_utils.clsx)(baseStyles, props2 == null ? void 0 : props2.className) }),
|
|
style: {
|
|
"--navbar-height": typeof height === "number" ? `${height}px` : height,
|
|
...otherProps == null ? void 0 : otherProps.style,
|
|
...props2 == null ? void 0 : props2.style
|
|
}
|
|
});
|
|
const getWrapperProps = (props2 = {}) => ({
|
|
...props2,
|
|
"data-menu-open": (0, import_shared_utils.dataAttr)(isMenuOpen),
|
|
className: slots.wrapper({ class: (0, import_shared_utils.clsx)(classNames == null ? void 0 : classNames.wrapper, props2 == null ? void 0 : props2.className) })
|
|
});
|
|
return {
|
|
Component,
|
|
slots,
|
|
domRef,
|
|
height,
|
|
isHidden,
|
|
disableAnimation,
|
|
shouldHideOnScroll,
|
|
isMenuOpen,
|
|
classNames,
|
|
setIsMenuOpen,
|
|
motionProps,
|
|
getBaseProps,
|
|
getWrapperProps
|
|
};
|
|
}
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
useNavbar
|
|
});
|