Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wweitzel committed Dec 21, 2023
1 parent 9f8e7ce commit d4bc232
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/hooks/useTheme.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {createContext, useContext, useState} from 'react';
import {getPreferredTheme, setDocumentTheme} from '../lib/utils';
import {getPreferredTheme, setDocumentTheme, Theme} from '../lib/theme';

interface ThemeContext {
theme: Theme;
Expand All @@ -10,7 +10,7 @@ const currentTheme = getPreferredTheme();

const ThemeContext = createContext<ThemeContext>({theme: currentTheme, setTheme: () => null});

export const ThemeProvider = ({children}: {children: React.ReactNode}) => {
export function ThemeProvider({children}: {children: React.ReactNode}) {
const [theme, setTheme] = useState(currentTheme);

function setGlobalTheme(theme: Theme) {
Expand All @@ -23,8 +23,8 @@ export const ThemeProvider = ({children}: {children: React.ReactNode}) => {
{children}
</ThemeContext.Provider>
);
};
}

export const useTheme = () => {
export function useTheme() {
return useContext(ThemeContext);
};
}
15 changes: 15 additions & 0 deletions src/lib/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type Theme = 'dark' | 'light';

export function getPreferredTheme(): Theme {
let storedTheme = localStorage.getItem('top90-theme');
if (storedTheme) {
return storedTheme as Theme;
}

return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}

export function setDocumentTheme(theme: Theme) {
localStorage.setItem('top90-theme', theme);
document.documentElement.setAttribute('data-bs-theme', theme);
}
14 changes: 0 additions & 14 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
export function getPreferredTheme(): Theme {
let storedTheme = localStorage.getItem('top90-theme');
if (storedTheme) {
return storedTheme as Theme;
}

return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}

export function setDocumentTheme(theme: Theme) {
localStorage.setItem('top90-theme', theme);
document.documentElement.setAttribute('data-bs-theme', theme);
}

export function cloudfrontEnabled() {
return import.meta.env.VITE_TOP90_CLOUDFRONT_ENABLED !== 'false';
}
2 changes: 0 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
declare module '*.png';

type Theme = 'dark' | 'light';

0 comments on commit d4bc232

Please sign in to comment.