Skip to content

Commit

Permalink
fix(/src/lib/components/Flowbite): fix window is not defined in `ne…
Browse files Browse the repository at this point in the history
…xt.js` (#652)

Thanks @fsioni --

* fix(themecontext): fixing `ReferenceError: window is not defined error with Next.js

The variable `userPreferenceIsDark` was initialized with a value from the window object. This was causing an error with Next because of SSR. I added the check of the window, if it's on the server side, set to white

* fix(themecontext): fixing `ReferenceError: window is not defined` error with Next.js

The variable`userPreferenceIsDark` was initialized with a value from the window object. This was
causing an error with Next because of SSR. I added the check of the window, if it's on the server
side the value is set to `false`
  • Loading branch information
fsioni authored Mar 5, 2023
1 parent 863a789 commit 8fd9ddc
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/components/Flowbite/ThemeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function useTheme(): ThemeContextProps {
}

export const useThemeMode = (): [Mode, React.Dispatch<React.SetStateAction<Mode>>, () => void] => {
const userPreferenceIsDark = () => window.matchMedia?.('(prefers-color-scheme: dark)').matches;
const userPreferenceIsDark = () => windowExists() && window.matchMedia?.('(prefers-color-scheme: dark)').matches;
const getPrefersColorScheme = (): Mode => (userPreferenceIsDark() ? 'dark' : 'light');
const onToggleMode = () => {
const newMode = mode === 'dark' ? 'light' : 'dark';
Expand Down

0 comments on commit 8fd9ddc

Please sign in to comment.