@heroui/navbar
Version:
A responsive navigation header positioned on top side of your page that includes support for branding, links, navigation, collapse and more.
304 lines (295 loc) • 11.6 kB
JavaScript
"use client";
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/navbar.tsx
var navbar_exports = {};
__export(navbar_exports, {
default: () => navbar_default
});
module.exports = __toCommonJS(navbar_exports);
var import_system3 = require("@heroui/system");
var import_react_utils4 = require("@heroui/react-utils");
var import_framer_motion2 = require("framer-motion");
var import_shared_utils3 = require("@heroui/shared-utils");
// src/navbar-transitions.ts
var import_framer_utils = require("@heroui/framer-utils");
var hideOnScrollVariants = {
visible: {
y: 0,
transition: {
ease: import_framer_utils.TRANSITION_EASINGS.easeOut
}
},
hidden: {
y: "-100%",
transition: {
ease: import_framer_utils.TRANSITION_EASINGS.easeIn
}
}
};
// src/use-navbar.ts
var import_react = require("react");
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_use_scroll_position = require("@heroui/use-scroll-position");
var import_use_resize = require("@heroui/use-resize");
var import_utils = 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_utils.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_use_resize.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_theme.cn)(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_shared_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_theme.cn)(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_theme.cn)(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
};
}
// src/navbar-context.ts
var import_react_utils2 = require("@heroui/react-utils");
var [NavbarProvider, useNavbarContext] = (0, import_react_utils2.createContext)({
name: "NavbarContext",
strict: true,
errorMessage: "useNavbarContext: `context` is undefined. Seems you forgot to wrap component within <Navbar />"
});
// src/navbar-menu.tsx
var import_system2 = require("@heroui/system");
var import_react_utils3 = require("@heroui/react-utils");
var import_shared_utils2 = require("@heroui/shared-utils");
var import_framer_motion = require("framer-motion");
var import_overlays2 = require("@react-aria/overlays");
var import_theme2 = require("@heroui/theme");
// src/navbar-menu-transitions.ts
var menuVariants = {
enter: {
height: "calc(100vh - var(--navbar-height))",
transition: {
duration: 0.3,
easings: "easeOut"
}
},
exit: {
height: 0,
transition: {
duration: 0.25,
easings: "easeIn"
}
}
};
// src/navbar-menu.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var domAnimation = () => import("@heroui/dom-animation").then((res) => res.default);
var NavbarMenu = (0, import_system2.forwardRef)((props, ref) => {
var _a, _b;
const { className, children, portalContainer, motionProps, style, ...otherProps } = props;
const domRef = (0, import_react_utils3.useDOMRef)(ref);
const { slots, isMenuOpen, height, disableAnimation, classNames } = useNavbarContext();
const styles = (0, import_theme2.cn)(classNames == null ? void 0 : classNames.menu, className);
if (disableAnimation) {
if (!isMenuOpen) return null;
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_overlays2.Overlay, { portalContainer, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"ul",
{
ref: domRef,
className: (_a = slots.menu) == null ? void 0 : _a.call(slots, { class: styles }),
"data-open": (0, import_shared_utils2.dataAttr)(isMenuOpen),
style: {
// @ts-expect-error
"--navbar-height": typeof height === "number" ? `${height}px` : height
},
...otherProps,
children
}
) });
}
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_framer_motion.AnimatePresence, { mode: "wait", children: isMenuOpen ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_overlays2.Overlay, { portalContainer, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_framer_motion.LazyMotion, { features: domAnimation, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_framer_motion.m.ul,
{
ref: domRef,
layoutScroll: true,
animate: "enter",
className: (_b = slots.menu) == null ? void 0 : _b.call(slots, { class: styles }),
"data-open": (0, import_shared_utils2.dataAttr)(isMenuOpen),
exit: "exit",
initial: "exit",
style: {
// @ts-expect-error
"--navbar-height": typeof height === "number" ? `${height}px` : height,
...style
},
variants: menuVariants,
...(0, import_shared_utils2.mergeProps)(motionProps, otherProps),
children
}
) }) }) : null });
});
NavbarMenu.displayName = "HeroUI.NavbarMenu";
var navbar_menu_default = NavbarMenu;
// src/navbar.tsx
var import_jsx_runtime2 = require("react/jsx-runtime");
var domAnimation2 = () => import("@heroui/dom-animation").then((res) => res.default);
var Navbar = (0, import_system3.forwardRef)((props, ref) => {
const { children, ...otherProps } = props;
const context = useNavbar({ ...otherProps, ref });
const Component = context.Component;
const [childrenWithoutMenu, menu] = (0, import_react_utils4.pickChildren)(children, navbar_menu_default);
const content = /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("header", { ...context.getWrapperProps(), children: childrenWithoutMenu }),
menu
] });
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(NavbarProvider, { value: context, children: context.shouldHideOnScroll ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_framer_motion2.LazyMotion, { features: domAnimation2, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
import_framer_motion2.m.nav,
{
animate: context.isHidden ? "hidden" : "visible",
initial: false,
variants: hideOnScrollVariants,
...(0, import_shared_utils3.mergeProps)(context.getBaseProps(), context.motionProps),
children: content
}
) }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Component, { ...context.getBaseProps(), children: content }) });
});
Navbar.displayName = "HeroUI.Navbar";
var navbar_default = Navbar;