From 738b512d9c4731de56b1bf60122eb4f2e4cb6843 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 1 Jun 2025 09:37:49 -0500 Subject: [PATCH] ThemeProvider nitpicks --- src/components/ThemeProvider.tsx | 6 +++--- src/hooks/useTheme.ts | 14 ++++++++------ src/lib/ThemeContext.ts | 15 +++++++++++++++ src/lib/theme-context.ts | 15 --------------- 4 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 src/lib/ThemeContext.ts delete mode 100644 src/lib/theme-context.ts diff --git a/src/components/ThemeProvider.tsx b/src/components/ThemeProvider.tsx index 8fdf742..078d4c3 100644 --- a/src/components/ThemeProvider.tsx +++ b/src/components/ThemeProvider.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from "react" -import { Theme, ThemeProviderContext } from "@/lib/theme-context" +import { type Theme, ThemeContext } from "@/lib/ThemeContext" type ThemeProviderProps = { children: React.ReactNode @@ -61,9 +61,9 @@ export function ThemeProvider({ } return ( - + {children} - + ) } diff --git a/src/hooks/useTheme.ts b/src/hooks/useTheme.ts index 937d68c..be0c18d 100644 --- a/src/hooks/useTheme.ts +++ b/src/hooks/useTheme.ts @@ -1,11 +1,13 @@ import { useContext } from "react" -import { ThemeProviderContext } from "@/lib/theme-context" +import { ThemeContext, type ThemeContextType } from "@/lib/ThemeContext" -export const useTheme = () => { - const context = useContext(ThemeProviderContext) +/** Hook to get and set the active theme. */ +export function useTheme(): ThemeContextType { + const context = useContext(ThemeContext); - if (context === undefined) - throw new Error("useTheme must be used within a ThemeProvider") + if (!context) { + throw new Error("useTheme must be used within a ThemeProvider"); + } - return context + return context; } \ No newline at end of file diff --git a/src/lib/ThemeContext.ts b/src/lib/ThemeContext.ts new file mode 100644 index 0000000..902edf9 --- /dev/null +++ b/src/lib/ThemeContext.ts @@ -0,0 +1,15 @@ +import { createContext } from "react"; + +export type Theme = "dark" | "light" | "system"; + +export type ThemeContextType = { + theme: Theme; + setTheme: (theme: Theme) => void; +}; + +const initialState: ThemeContextType = { + theme: "system", + setTheme: () => undefined, +}; + +export const ThemeContext = createContext(initialState); \ No newline at end of file diff --git a/src/lib/theme-context.ts b/src/lib/theme-context.ts deleted file mode 100644 index 638a1cc..0000000 --- a/src/lib/theme-context.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { createContext } from "react"; - -export type Theme = "dark" | "light" | "system"; - -export type ThemeProviderState = { - theme: Theme; - setTheme: (theme: Theme) => void; -}; - -const initialState: ThemeProviderState = { - theme: "system", - setTheme: () => null, -}; - -export const ThemeProviderContext = createContext(initialState); \ No newline at end of file