-
-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
ThemeModeScript
- avoid page flicker on reload on SSR applica…
…tion (NextJS, Remix) (#1056) * feat: create `ThemeModeScript` to avoid page flicker on reload when dark mode is set; add script to root layout on homepage + docs * refactor: move `ThemeModeScript` into `components` directory --------- Co-authored-by: Sebastian Sutu <[email protected]>
- Loading branch information
1 parent
2e1202c
commit 4f0399b
Showing
4 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from 'react'; | ||
import type { ThemeMode } from '../../helpers/use-theme-mode'; | ||
|
||
export interface ThemeModeScriptProps extends React.ComponentPropsWithoutRef<'script'> { | ||
mode?: ThemeMode; | ||
} | ||
|
||
export const ThemeModeScript = ({ mode, ...others }: ThemeModeScriptProps) => { | ||
return ( | ||
<script | ||
{...others} | ||
data-flowbite-theme-mode-script | ||
dangerouslySetInnerHTML={{ | ||
__html: getScript({ mode, defaultMode: 'light', localStorageKey: 'flowbite-theme-mode' }), | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
function getScript({ | ||
mode, | ||
defaultMode, | ||
localStorageKey, | ||
}: { | ||
mode?: ThemeMode; | ||
defaultMode: ThemeMode; | ||
localStorageKey: string; | ||
}) { | ||
return ` | ||
try { | ||
const mode = window.localStorage.getItem('${localStorageKey}') ?? '${mode}' ?? '${defaultMode}'; | ||
const computedMode = | ||
mode === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : mode; | ||
if (computedMode === 'dark') { | ||
document.documentElement.classList.add('dark'); | ||
} else { | ||
document.documentElement.classList.remove('dark'); | ||
} | ||
} catch (e) {} | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ThemeModeScript'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4f0399b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flowbite-react – ./
flowbite-react-phi.vercel.app
flowbite-react-themesberg.vercel.app
flowbite-react-git-main-themesberg.vercel.app
flowbite-react.com
www.flowbite-react.com