
Company News
Socket Joins New OpenJS Program to Fund Node.js Security Work
Socket is joining the OpenJS Security Stewardship Program to fund Node.js vulnerability research, maintainer remediation, and security releases.
@zachleat/solar-eclipse-toggle
Advanced tools
An accessible light/dark theme toggle button that follows your system preference by default.
<solar-eclipse-toggle> Web ComponentAn accessible light/dark theme toggle button that follows your system preference by default.
npm install @zachleat/solar-eclipse-toggle
Include the stylesheet and the script on your web site:
<link rel="stylesheet" href="solar-eclipse-toggle.css">
<script type="module" src="solar-eclipse-toggle.js"></script>
The stylesheet is also exported as @zachleat/solar-eclipse-toggle/style.css.
Define the icons once as reusable symbols (copy the full paths from demo.html):
<svg aria-hidden="true" style="position: absolute; width: 0; height: 0; overflow: hidden">
<symbol id="se-icon-moon" viewBox="0 0 512 512"><!-- Font Awesome moon --></symbol>
<symbol id="se-icon-sun" viewBox="0 -32 576 576"><!-- Font Awesome sun --></symbol>
<symbol id="se-icon-half" viewBox="0 0 512 512"><!-- Font Awesome circle-half-stroke --></symbol>
</svg>
Then use the component markup:
<solar-eclipse-toggle>
<button type="button" disabled>
<span class="se-label-dark"><svg aria-hidden="true"><use href="#se-icon-moon"/></svg>Use dark theme</span>
<span class="se-label-light"><svg aria-hidden="true"><use href="#se-icon-sun"/></svg>Use light theme</span>
<span class="se-label-unknown"><svg aria-hidden="true"><use href="#se-icon-half"/></svg>Theme</span>
<span class="se-system">System</span>
</button>
</solar-eclipse-toggle>
Add this to your <head> to apply a saved theme before first paint:
<script>
try {
var theme = localStorage.getItem("theme");
if(theme === "light" || theme === "dark") {
document.documentElement.setAttribute("data-theme", theme);
}
} catch(e) {}
</script>
Write your dark styles for both the system preference and the override:
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
color-scheme: dark;
/* dark styles */
}
}
:root[data-theme="dark"] {
color-scheme: dark;
/* dark styles */
}
prefers-color-scheme until a visitor picks the other theme, which is saved to localStorage.<button> named for what it does next (“Use dark theme”), with no aria-pressed state.role="status" live region, since screen readers don’t reliably announce name changes..se-label-unknown with SYSTEM.storage-key: localStorage key. Default: themestatus-light, status-dark, status-auto: announcements. Defaults: Light theme on, Dark theme on, matching your systemtheme-change event, with event.detail of { theme, auto }.status-* attributes.Add ?nodefine to the script URL to skip the customElements.define call:
<script type="module">
import { SolarEclipseToggle } from "./solar-eclipse-toggle.js?nodefine";
SolarEclipseToggle.define();
</script>
The stylesheet targets the solar-eclipse-toggle tag name.
Styles are in the solar-eclipse-toggle cascade layer, so your own styles win. Colors use custom properties, with light and dark naming the current page theme:
--se-light-color, --se-light-border, --se-light-icon, --se-light-hover, --se-light-system-bg, --se-light-system-color--se-dark-color, --se-dark-border, --se-dark-icon, --se-dark-hover, --se-dark-system-bg, --se-dark-system-color--se-focusDemo icons are Font Awesome Free (CC BY 4.0).
FAQs
An accessible light/dark theme toggle button that follows your system preference by default.
The npm package @zachleat/solar-eclipse-toggle receives a total of 94 weekly downloads. As such, @zachleat/solar-eclipse-toggle popularity was classified as not popular.
We found that @zachleat/solar-eclipse-toggle demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.

Company News
Socket is joining the OpenJS Security Stewardship Program to fund Node.js vulnerability research, maintainer remediation, and security releases.

Security News
Two compromised GitHub Actions were re-enabled with malicious tags intact, exposing thousands of downstream repositories to Mini Shai-Hulud.

Research
/Security News
A malicious Firefox extension fetches its payload after installation to evade detection, steal Google session cookies, and automate account takeover.