Spaces:
Build error
Build error
File size: 3,340 Bytes
75fefa7 |
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
"use client";
import { usePathname } from "next/navigation";
import React, {
createContext,
useContext,
useEffect,
useRef,
useState,
} from "react";
interface HeaderContextType {
dropdownContent: React.ReactNode;
setDropdownContent: (content: React.ReactNode) => void;
clearDropdown: (force?: boolean) => void;
resetDropdownTimeout: () => void;
dropdownKey: number;
headerHeight: React.RefObject<number>;
headerTop: React.RefObject<number>;
}
const HeaderContext = createContext<HeaderContextType | undefined>(undefined);
export const HeaderProvider = ({ children }: { children: React.ReactNode }) => {
const [dropdownContent, setDropdownContent] = useState<React.ReactNode>(null);
const [dropdownKey, setDropdownKey] = useState(0);
const headerHeight = useRef(0);
const headerTop = useRef(0);
const pathname = usePathname();
const timeout = useRef<number | null>(null);
const clearDropdown = (force?: boolean) => {
if (force) {
setDropdownContent(null);
return;
}
if (timeout.current) {
clearTimeout(timeout.current);
}
timeout.current = window.setTimeout(() => {
setDropdownContent(null);
}, 500);
};
const resetDropdownTimeout = () => {
if (timeout.current) {
clearTimeout(timeout.current);
}
};
useEffect(() => {
const header = document.querySelector(".header") as HTMLElement;
if (header) {
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
headerHeight.current = entry.contentRect.height;
}
});
resizeObserver.observe(header);
headerHeight.current = header.clientHeight;
headerTop.current = header.getBoundingClientRect().top;
const onScroll = () => {
headerTop.current = header.getBoundingClientRect().top;
};
window.addEventListener("scroll", onScroll, { passive: true });
return () => {
resizeObserver.disconnect();
window.removeEventListener("scroll", onScroll);
};
}
}, [pathname]);
return (
<HeaderContext.Provider
value={{
dropdownContent,
setDropdownContent: (content) => {
resetDropdownTimeout();
if (content === dropdownContent) return;
setDropdownKey((prev) => prev + 1);
setDropdownContent(content);
},
clearDropdown,
resetDropdownTimeout,
dropdownKey,
headerHeight,
headerTop,
}}
>
{children}
</HeaderContext.Provider>
);
};
export const useHeaderContext = () => {
const context = useContext(HeaderContext);
if (!context) {
throw new Error("useHeaderContext must be used within a HeaderProvider");
}
return context;
};
export const useHeaderHeight = () => {
const [headerHeight, setHeaderHeight] = useState(0);
useEffect(() => {
const header = document.querySelector(".header");
if (header) {
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
setHeaderHeight(entry.contentRect.height);
}
});
resizeObserver.observe(header);
setHeaderHeight(header.clientHeight);
return () => {
resizeObserver.disconnect();
};
}
}, []);
return { headerHeight };
};
|