Device Context
This commit is contained in:
@@ -3,10 +3,7 @@
|
|||||||
import { Geist, Geist_Mono } from "next/font/google";
|
import { Geist, Geist_Mono } from "next/font/google";
|
||||||
import "./globals.css";
|
import "./globals.css";
|
||||||
|
|
||||||
import { useEffect, useState } from "react";
|
import { DeviceProvider } from "@/lib/context/DeviceContext";
|
||||||
import * as rdd from "react-device-detect";
|
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
|
|
||||||
import MobileNav from "@/lib/components/MobileNav";
|
import MobileNav from "@/lib/components/MobileNav";
|
||||||
|
|
||||||
const geistSans = Geist({
|
const geistSans = Geist({
|
||||||
@@ -24,29 +21,15 @@ export default function RootLayout({
|
|||||||
}: Readonly<{
|
}: 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 (
|
return (
|
||||||
<html data-theme="drinky" lang="en">
|
<html data-theme="drinky" lang="en">
|
||||||
<body
|
<body
|
||||||
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||||
suppressHydrationWarning={true}
|
suppressHydrationWarning={true}
|
||||||
>
|
>
|
||||||
|
<DeviceProvider>
|
||||||
{children}
|
{children}
|
||||||
{isMobile && (
|
</DeviceProvider>
|
||||||
<MobileNav />
|
|
||||||
)}
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</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