import { createContext, useContext, useEffect, useState, type ReactNode, } from "react"; interface PaymentBannerContextValue { bannerHeight: number; setBannerHeight: (height: number) => void; } const PaymentBannerContext = createContext( null, ); export function PaymentBannerProvider({ children }: { children: ReactNode }) { const [bannerHeight, setBannerHeight] = useState(0); useEffect(() => { // Note: CSS vars allow us to reduce re-renders of child-components and correct // CSS animations. Prefer using the CSS var over consuming the hook below to adjust layouts. // Example: // -
--> correct height calculation document.documentElement.style.setProperty( "--banner-height", `${bannerHeight}px`, ); }, [bannerHeight]); return ( {children} ); } export function usePaymentBannerHeight() { const context = useContext(PaymentBannerContext); if (!context) { throw new Error( "usePaymentBannerHeight must be used within PaymentBannerProvider", ); } return context; }