This module provides tools to manage theme preferences (dark or light mode) in a Fresh v2 app. It includes:
localStorage
.localStorage
for
consistent experience across sessions.setTheme
for theme management and initial
setup scripts.Import the desired script (dark or light mode) in your app’s main entry point to set the initial theme.
import { defaultDarkModeScript, defaultLightModeScript, setTheme, } from 'jsr:@elsoul/fresh-theme'
Insert the desired theme script in the <head>
section of your app to
automatically apply the initial theme based on user settings or system
preferences.
import { asset } from 'fresh/runtime' import { define } from '@/utils/state.ts' import { defaultDarkModeScript } from 'jsr:@elsoul/fresh-theme' export default define.page(function App({ Component, state, url }) { return ( <html lang={state.locale || 'en'}> <head> <script dangerouslySetInnerHTML={{ __html: defaultDarkModeScript }} /> <link rel='stylesheet' href={asset('/styles.css')} /> </head> <body> <Component /> </body> </html> ) })
import { asset } from 'fresh/runtime' import { define } from '@/utils/state.ts' import { defaultLightModeScript } from 'jsr:@elsoul/fresh-theme' export default define.page(function App({ Component, state, url }) { return ( <html lang={state.locale || 'en'}> <head> <script dangerouslySetInnerHTML={{ __html: defaultLightModeScript }} /> <link rel='stylesheet' href={asset('/styles.css')} /> </head> <body> <Component /> </body> </html> ) })
Use the setTheme
function to programmatically change the theme between light
and dark modes. This function updates the theme preference in localStorage
and
adjusts the <html>
element class.
// Example: Setting the theme to dark mode setTheme('dark') // Example: Setting the theme to light mode setTheme('light')
defaultDarkModeScript
JavaScript code to initialize the theme with dark mode as the default. This
script applies the theme based on localStorage
or system preference if no
theme is set.
defaultLightModeScript
JavaScript code to initialize the theme with light mode as the default. This
script applies the theme based on localStorage
or system preference if no
theme is set.
setTheme(newTheme: 'dark' | 'light')
Sets the theme to 'dark'
or 'light'
and stores the choice in localStorage
.
Adjusts the <html>
element to reflect the new theme.
newTheme
- The desired theme, either 'dark'
or 'light'
.import { useTheme } from 'jsr:@elsoul/fresh-theme' function ThemeToggleButton() { const { theme, setTheme } = useTheme() const toggleTheme = () => { const newTheme = theme === 'dark' ? 'light' : 'dark' setTheme(newTheme) } return ( <button onClick={toggleTheme}> Switch to {theme === 'dark' ? 'Light' : 'Dark'} Mode </button> ) }
Bug reports and pull requests are welcome on GitHub at https://github.com/elsoul/fresh-theme This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The package is available as open source under the terms of the Apache-2.0 License.
Everyone interacting in the SKEET project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Add Package
deno add jsr:@elsoul/fresh-theme
Import symbol
import * as fresh_theme from "@elsoul/fresh-theme";
---- OR ----
Import directly with a jsr specifier
import * as fresh_theme from "jsr:@elsoul/fresh-theme";
Add Package
npx jsr add @elsoul/fresh-theme
Import symbol
import * as fresh_theme from "@elsoul/fresh-theme";
Add Package
yarn dlx jsr add @elsoul/fresh-theme
Import symbol
import * as fresh_theme from "@elsoul/fresh-theme";
Add Package
pnpm dlx jsr add @elsoul/fresh-theme
Import symbol
import * as fresh_theme from "@elsoul/fresh-theme";
Add Package
bunx jsr add @elsoul/fresh-theme
Import symbol
import * as fresh_theme from "@elsoul/fresh-theme";