Device Context
This commit is contained in:
@@ -3,51 +3,34 @@
|
||||
import { Geist, Geist_Mono } from "next/font/google";
|
||||
import "./globals.css";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import * as rdd from "react-device-detect";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import { DeviceProvider } from "@/lib/context/DeviceContext";
|
||||
import MobileNav from "@/lib/components/MobileNav";
|
||||
|
||||
const geistSans = Geist({
|
||||
variable: "--font-geist-sans",
|
||||
subsets: ["latin"],
|
||||
variable: "--font-geist-sans",
|
||||
subsets: ["latin"],
|
||||
});
|
||||
|
||||
const geistMono = Geist_Mono({
|
||||
variable: "--font-geist-mono",
|
||||
subsets: ["latin"],
|
||||
variable: "--font-geist-mono",
|
||||
subsets: ["latin"],
|
||||
});
|
||||
|
||||
export default function RootLayout({
|
||||
children,
|
||||
children,
|
||||
}: Readonly<{
|
||||
children: React.ReactNode;
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
const router = useRouter();
|
||||
|
||||
const [isSafari, setIsSafari] = useState<boolean>(false);
|
||||
const [isMobile, setIsMobile] = useState<boolean>(false);
|
||||
|
||||
useEffect(() => {
|
||||
setIsSafari(rdd.isSafari);
|
||||
setIsMobile(rdd.isMobile);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
}, [isMobile, isSafari, router]);
|
||||
|
||||
return (
|
||||
<html data-theme="drinky" lang="en">
|
||||
<body
|
||||
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||
suppressHydrationWarning={true}
|
||||
>
|
||||
{children}
|
||||
{isMobile && (
|
||||
<MobileNav />
|
||||
)}
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
return (
|
||||
<html data-theme="drinky" lang="en">
|
||||
<body
|
||||
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||
suppressHydrationWarning={true}
|
||||
>
|
||||
<DeviceProvider>
|
||||
{children}
|
||||
</DeviceProvider>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
|
||||
35
src/lib/context/DeviceContext.tsx
Normal file
35
src/lib/context/DeviceContext.tsx
Normal file
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import React, { createContext, useContext, useEffect, useState } from "react";
|
||||
import * as rdd from "react-device-detect";
|
||||
|
||||
interface DeviceContextProps {
|
||||
isSafari: boolean;
|
||||
isMobile: boolean;
|
||||
}
|
||||
|
||||
const DeviceContext = createContext<DeviceContextProps | undefined>(undefined);
|
||||
|
||||
export const DeviceProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [isSafari, setIsSafari] = useState<boolean>(false);
|
||||
const [isMobile, setIsMobile] = useState<boolean>(false);
|
||||
|
||||
useEffect(() => {
|
||||
setIsSafari(rdd.isSafari);
|
||||
setIsMobile(rdd.isMobile);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<DeviceContext.Provider value={{ isSafari, isMobile }}>
|
||||
{children}
|
||||
</DeviceContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useDevice = (): DeviceContextProps => {
|
||||
const context = useContext(DeviceContext);
|
||||
if (!context) {
|
||||
throw new Error("useDevice must be used within a DeviceProvider");
|
||||
}
|
||||
return context;
|
||||
};
|
||||
Reference in New Issue
Block a user