Blog · 30 November 2026
Beam theme toggle: a free dark mode switch with a wave of light
Beam is the theme toggle from our own website, rebuilt as one free JavaScript file. Press it and the new theme sweeps across the page from the button as a rippling front of light with a mint edge. It works on any site, needs no framework, and falls back to an instant switch in older browsers. Download it below and add it in three steps.
- Published
- Related services
- Web applications & SaaS
Download the files
There are two files. The script is everything you need on a real site. The demo is a single HTML page you can open on your computer to try the effect before you add it anywhere. Keep them in the same folder when you open the demo.
Both are free for personal and commercial projects under the MIT license. Keep the licence line at the top of the script.
The toggle. One file, about 6 KB, no dependencies.
Open it next to the script to try the effect.
Add it to any website in three steps
The toggle keeps the theme on the html element as data-theme="light" or data-theme="dark". Your styles read that attribute, so the toggle never needs to know your colours.
First, put the script before the closing body tag and add a button with the data-beam-toggle attribute. The script finds the button and wires it up on its own.
Second, give your colours two values: one for light and one for dark. CSS variables make this a few lines.
Third, add the small snippet to the head of the page. It applies the saved theme before the page paints, so a visitor who chose dark mode never sees a flash of the light page.
<button data-beam-toggle aria-label="Switch theme">◐</button>
<script src="/beam-theme-toggle.js"></script>:root { --bg: #F4F6F9; --text: #111827; }
[data-theme="dark"] { --bg: #0B1220; --text: #F4F6F9; }
body { background: var(--bg); color: var(--text); }<script>
try {
var t = localStorage.getItem("theme");
if (t) document.documentElement.setAttribute("data-theme", t);
} catch (e) {}
</script>Options
Every option can be set as a data attribute on the button, for example data-rim-color="#7C3AED", or passed to BeamThemeToggle.init in JavaScript.
| Option | Data attribute | Default | What it does |
|---|---|---|---|
| duration | data-duration | 1000 | Length of the sweep in milliseconds |
| rim | data-rim | 7 | Width of the coloured edge in pixels |
| rimColor | data-rim-color | #00D9A0 | Colour of the edge of light |
| storageKey | data-storage-key | theme | Where the choice is saved; empty means do not remember |
| attribute | data-attribute | data-theme | The attribute on html that holds the theme |
BeamThemeToggle.init(document.querySelector("#theme-button"), {
duration: 800,
rimColor: "#7C3AED",
storageKey: "my-site-theme",
});
// React to changes anywhere on the page
document.addEventListener("beam:theme", (e) => console.log(e.detail.theme));Use it in React or Next.js
Copy beam-theme-toggle.js into the public folder and load it once, for example with the Next.js Script component in your root layout. Then render a button with the data-beam-toggle attribute in your header. The script wires buttons that exist when the page loads.
For a button that mounts later, call BeamThemeToggle.init on it after it mounts. Calling it twice on the same button is safe; the second call is ignored.
"use client";
import { useEffect, useRef } from "react";
declare global {
interface Window { BeamThemeToggle?: { init: (el: Element, opts?: object) => void } }
}
export function ThemeButton() {
const ref = useRef<HTMLButtonElement>(null);
useEffect(() => {
if (ref.current) window.BeamThemeToggle?.init(ref.current);
}, []);
return <button ref={ref} aria-label="Switch theme">◐</button>;
}Let an AI coding assistant add it for you
If you build with Claude Code, Cursor, Copilot or ChatGPT, download the script into your project and give the assistant the prompt below. It tells the assistant what the file does and the three places it has to touch, so it does not try to rewrite the animation.
Check the result in a browser: press the button, reload the page in dark mode, and try the keyboard (Tab to the button, then Enter).
I added beam-theme-toggle.js (a vanilla JS theme toggle) to this project.
Wire it in without changing the file:
1. Serve it as a static file and load it once on every page, before </body>
(in Next.js: public/ folder + next/script in the root layout).
2. Put a <button data-beam-toggle aria-label="Switch theme"> in the site header
that matches our design.
3. Make our colours theme-aware: every colour should come from CSS variables
with light values on :root and dark values on [data-theme="dark"].
4. Add the no-flash snippet to <head> that reads localStorage "theme"
and sets data-theme on <html> before paint.
Then list the files you changed.How it works
When you press the button, the script asks the browser for a view transition. The browser takes a picture of the page in the old theme, applies the new theme, and gives the script both pictures to animate.
The script cuts the new picture with a clip-path polygon: a straight edge at 45 degrees that starts at the button and travels across the screen, with a ripple that grows in the middle of the move and settles at both ends. It cuts the old picture a few pixels ahead of that edge, so the mint background shows through the gap as a line of light. The polygon is calculated for 48 frames over one second and played with the Web Animations API.
This is the same effect as the toggle on aantechnologies.com, packaged so it does not depend on React or our design system.
Accessibility and browser support
The toggle is a real button, so it works with the keyboard and screen readers, and its label changes to say which theme it will switch to.
- Visitors who ask their system for reduced motion get an instant switch with no sweep.
- Browsers without the View Transitions API also switch instantly, so nothing breaks.
- On the first visit it follows the visitor's system setting (light or dark) until they choose.
- Colour transitions on the page are paused during the sweep, so text does not fade twice.
Want details like this on your site?
We build websites and web apps where small interactions like this one are part of the design, not an afterthought. Tell us what you are working on and we will send a written estimate.
Sources
How we can help
Questions we get asked
Is the Beam theme toggle free to use?
Yes. It is released under the MIT license, so you can use it in personal and commercial projects. Keep the licence line at the top of the file.
Does it need React, Tailwind or a build step?
No. It is one plain JavaScript file. Add a script tag and a button with the data-beam-toggle attribute. It also works inside React, Next.js, Vue or any other framework.
What happens in browsers that do not support view transitions?
The theme switches instantly, without the sweep. The page and the saved choice work the same way.
Can I change the colour of the line of light?
Yes. Set data-rim-color on the button, or pass rimColor to BeamThemeToggle.init. You can also change its width with rim and the speed with duration.