|
| 1 | +import { |
| 2 | + createContext, |
| 3 | + createRef, |
| 4 | + useContext, |
| 5 | + useMemo, |
| 6 | + useRef, |
| 7 | + useState, |
| 8 | + useLayoutEffect, |
| 9 | +} from "react"; |
| 10 | + |
| 11 | +interface MaintenanceEndingBannerContextType { |
| 12 | + ref: React.RefObject<HTMLDivElement>; |
| 13 | + height: number; |
| 14 | + y: number; |
| 15 | + isExpanded: boolean; |
| 16 | + toggleExpanded: () => void; |
| 17 | +} |
| 18 | + |
| 19 | +const MaintenanceEndingBannerContext = |
| 20 | + createContext<MaintenanceEndingBannerContextType>({ |
| 21 | + ref: createRef<HTMLDivElement>(), |
| 22 | + height: 0, |
| 23 | + y: 0, |
| 24 | + isExpanded: true, |
| 25 | + toggleExpanded: () => {}, |
| 26 | + }); |
| 27 | + |
| 28 | +export const MaintenanceEndingBannerProvider = ({ |
| 29 | + children, |
| 30 | +}: React.PropsWithChildren) => { |
| 31 | + const ref = useRef<HTMLDivElement>(null); |
| 32 | + const [height, setHeight] = useState(0); |
| 33 | + const [y, setY] = useState(0); |
| 34 | + const [isExpanded, setIsExpanded] = useState(true); |
| 35 | + |
| 36 | + const toggleExpanded = () => { |
| 37 | + setIsExpanded((prev) => !prev); |
| 38 | + }; |
| 39 | + |
| 40 | + useLayoutEffect(() => { |
| 41 | + let frameId: number | null = null; |
| 42 | + |
| 43 | + const updatePosition = () => { |
| 44 | + if (ref.current) { |
| 45 | + const rect = ref.current.getBoundingClientRect(); |
| 46 | + |
| 47 | + setY(rect.top); |
| 48 | + setHeight(rect.height); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + const throttledUpdate = () => { |
| 53 | + // Context skipping update - frame already queued |
| 54 | + if (frameId) return; |
| 55 | + |
| 56 | + frameId = requestAnimationFrame(() => { |
| 57 | + updatePosition(); |
| 58 | + frameId = null; |
| 59 | + }); |
| 60 | + }; |
| 61 | + |
| 62 | + updatePosition(); |
| 63 | + |
| 64 | + window.addEventListener("scroll", throttledUpdate, { passive: true }); |
| 65 | + window.addEventListener("resize", throttledUpdate); |
| 66 | + |
| 67 | + return () => { |
| 68 | + window.removeEventListener("scroll", throttledUpdate); |
| 69 | + window.removeEventListener("resize", throttledUpdate); |
| 70 | + |
| 71 | + if (frameId) { |
| 72 | + cancelAnimationFrame(frameId); |
| 73 | + } |
| 74 | + }; |
| 75 | + }, []); |
| 76 | + |
| 77 | + const value = useMemo( |
| 78 | + () => ({ |
| 79 | + ref, |
| 80 | + height, |
| 81 | + y, |
| 82 | + isExpanded, |
| 83 | + toggleExpanded, |
| 84 | + }), |
| 85 | + [ref, height, y, isExpanded, toggleExpanded], |
| 86 | + ); |
| 87 | + |
| 88 | + return ( |
| 89 | + <MaintenanceEndingBannerContext.Provider value={value}> |
| 90 | + {children} |
| 91 | + </MaintenanceEndingBannerContext.Provider> |
| 92 | + ); |
| 93 | +}; |
| 94 | + |
| 95 | +export const useMaintenanceEndingBannerContext = () => { |
| 96 | + const context = useContext(MaintenanceEndingBannerContext); |
| 97 | + if (!context) { |
| 98 | + throw new Error( |
| 99 | + "useMaintenanceEndingBannerContext must be used within a MaintenanceEndingBannerProvider", |
| 100 | + ); |
| 101 | + } |
| 102 | + return context; |
| 103 | +}; |
0 commit comments