Component live demo
↑ Click me!
Provides system or user-defined color scheme preference, with a toggle mechanism. Settings are persisted, component is progressively enhanced and flash of unstyled content avoided. Also, it will provide an easier way to target theme with CSS / SCSS / JS.
Provides system or user-defined color scheme preference, with a toggle mechanism.
Settings are persisted, component is progressively enhanced and flash of mis-styled content avoided.
Also, this provides an easier way to target theme with CSS selector / SCSS mixins / JS DOM selector.
data-color-mode="dark|light"
attribute on root documentpnpm i astro-color-mode
---
import { ColorMode } from 'astro-color-mode';
// ...
---
<!-- ... -->
<head>
<!-- Place component inside `HEAD` tag -->
<ColorMode />
<!-- ... -->
</head>
Use data-color-mode-switch
where you want to toggle theme setting,
on an any element, somewhere inside BODY
tag:
<!-- ... -->
<body>
<!-- ... -->
<button data-color-mode-switch>Toggle COLOR MODE 💡</button>
<!-- ... -->
</body>
<html data-color-mode="light|dark">
<!-- ... -->
</html>
SCSS mixins registration in astro.config.mjs
:
export default defineConfig({
// ...
vite: {
css: {
preprocessorOptions: {
scss: {
additionalData: `
@use "astro-color-mode/use-color-mode.scss" as *;
`,
},
},
},
},
});
Then, use it like this in your stylesheets:
.my-layout {
:is(.my-link, body) {
@include color-mode(light) {
color: black;
background-color: white;
// ...
}
@include color-mode(dark) {
color: white;
background-color: black;
// ...
}
}
}
.some-class {
@include color-mode(light) {
color: blue;
background-color: yellow;
// ...
}
@include color-mode(dark) {
color: yellow;
background-color: blue;
// ...
}
}
@media
query support for SCSS mixinsAll notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.
Note: Version bump only for package @julian_cataldo/astro-color-mode
Note: Version bump only for package @julian_cataldo/astro-color-mode
Note: Version bump only for package @julian_cataldo/astro-color-mode
Note: Version bump only for package @julian_cataldo/astro-color-mode
Note: Version bump only for package @julian_cataldo/astro-color-mode
Note: Version bump only for package @julian_cataldo/astro-color-mode