32 lines
857 B
TypeScript
32 lines
857 B
TypeScript
"use client";
|
|
|
|
import type { ReactNode } from "react";
|
|
import { createContext, useState } from "react";
|
|
import { useEffect } from "react";
|
|
|
|
export const ResponsiveContext = createContext<boolean | undefined>(undefined);
|
|
|
|
export default function ResponsiveProvider({
|
|
children,
|
|
}: {
|
|
children: ReactNode;
|
|
}) {
|
|
const [isMobile, setIsMobile] = useState<boolean | undefined>(undefined);
|
|
|
|
useEffect(() => {
|
|
if (typeof window === "undefined") return;
|
|
const handleResize = () => setIsMobile(window.innerWidth < 640);
|
|
|
|
handleResize();
|
|
window.addEventListener("resize", handleResize);
|
|
|
|
return () => window.removeEventListener("resize", handleResize);
|
|
}, [setIsMobile]);
|
|
|
|
return (
|
|
<ResponsiveContext.Provider value={isMobile}>
|
|
{children}
|
|
</ResponsiveContext.Provider>
|
|
);
|
|
}
|