Skip to content

ThemeButton

Light Mode First

With this approach, you declare your light colors first and then use [data-theme="dark"] to set your dark colors.

Show code
:root {
--light-bg: transparent;
--light-text: #3d50f5;
}
[data-theme="dark"] {
--light-bg: #678fff;
--light-text: #222;
}
.light-paragraph {
padding: 8px;
border-radius: 8px;
background-color: var(--light-bg);
color: var(--light-text);
}

Dark Mode First

With this approach, you declare your dark colors first and then use [data-theme="light"] to set your light colors.

Show code
:root {
--dark-bg: #b36200;
--dark-text: #111;
}
[data-theme="light"] {
--dark-bg: transparent;
--dark-text: #ff8c00;
}
.dark-paragraph {
background-color: var(--dark-bg);
padding: 8px;
border-radius: 8px;
color: var(--dark-text);
}

Load theme on page load

If you want to load the theme before the page is rendered, you can import load-theme.ts file in your index.html or respective entry point:

<script
type="module"
src="node_modules/@hrc/toggle-theme/dist/load-theme.js"
></script>

If you are using Astro, you can use loadTheme util in your layout:

<script>
import { loadTheme } from "@hrc/toggle-theme";
loadTheme();
</script>

Custom key for localStorage

Use the customKey prop to set a custom key for localStorage to store the theme.

export function WithCustomKeyDemo() {
return (
<ThemeButton
customKey="my-custom-key"
lightElement={{/* element for light mode */}}
darkElement={{/* element for dark mode */}}
/>
);
}

API

See documentation below for reference to all of the props, classes and CSS variables (custom properties) available for <ThemeButton />: