Skip to content

Commit

Permalink
fix: fix theme on laravel
Browse files Browse the repository at this point in the history
  • Loading branch information
irsyadadl committed Oct 7, 2024
1 parent bdafea6 commit b2aaf6d
Showing 1 changed file with 49 additions and 52 deletions.
101 changes: 49 additions & 52 deletions src/resources/stubs/laravel/theme-provider.stub
Original file line number Diff line number Diff line change
@@ -1,74 +1,71 @@
import React from 'react';
import * as React from 'react'

type Theme = 'dark' | 'light' | 'system';
type Theme = 'dark' | 'light' | 'system'

type ThemeProviderProps = {
children: React.ReactNode;
defaultTheme?: Theme;
storageKey?: string;
};
children: React.ReactNode
defaultTheme?: Theme
storageKey?: string
}

type ThemeProviderState = {
theme: Theme;
setTheme: (theme: Theme) => void;
};
theme: Theme
setTheme: (theme: Theme) => void
}

const initialState: ThemeProviderState = {
theme: 'system',
setTheme: () => null,
};
theme: 'system',
setTheme: () => null
}

const ThemeProviderContext =
React.createContext<ThemeProviderState>(initialState);
const ThemeProviderContext = React.createContext<ThemeProviderState>(initialState)

export function ThemeProvider({
children,
defaultTheme = 'system',
storageKey = 'vite-ui-theme',
...props
children,
defaultTheme = 'system',
storageKey = 'vite-ui-theme',
...props
}: ThemeProviderProps) {
const [theme, setTheme] = React.useState<Theme>(
() => (localStorage.getItem(storageKey) as Theme) || defaultTheme,
);
const [theme, setTheme] = React.useState<Theme>(() => (localStorage.getItem(storageKey) as Theme) || defaultTheme)

React.useEffect(() => {
const root = window.document.documentElement;
React.useEffect(() => {
const root = window.document.documentElement

root.classList.remove('light', 'dark');
root.classList.remove('light', 'dark')

if (theme === 'system') {
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)')
.matches
? 'dark'
: 'light';
if (theme === 'system') {
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'

root.classList.add(systemTheme);
return;
root.classList.add(systemTheme)
return
}

if (theme === 'light' || theme === 'dark') {
root.classList.add(theme)
}
}, [theme])

const value = {
theme,
setTheme: (newTheme: Theme) => {
if (newTheme === 'light' || newTheme === 'dark' || newTheme === 'system') {
localStorage.setItem(storageKey, newTheme)
setTheme(newTheme)
}
}
}

root.classList.add(theme);
}, [theme]);

const value = {
theme,
setTheme: (theme: Theme) => {
localStorage.setItem(storageKey, theme);
setTheme(theme);
},
};

return (
<ThemeProviderContext.Provider {...props} value={value}>
{children}
</ThemeProviderContext.Provider>
);
return (
<ThemeProviderContext.Provider {...props} value={value}>
{children}
</ThemeProviderContext.Provider>
)
}

export const useTheme = () => {
const context = React.useContext(ThemeProviderContext);
const context = React.useContext(ThemeProviderContext)

if (context === undefined)
throw new Error('useTheme must be used within a ThemeProvider');
if (context === undefined) throw new Error('useTheme must be used within a ThemeProvider')

return context;
};
return context
}

0 comments on commit b2aaf6d

Please sign in to comment.