Skip to content

Light/dark theme switch

An accessible switch that turns the whole site dark or light through CSS variables, remembers the choice and returns to the device setting in one click.

  • Tailwind
  • React
  • CSS
  • WordPress
Open in a new tab

Description

Placed in a tradesperson’s site header, the switch is a real “Dark mode” button with aria-pressed: screen readers announce “on” or “off”, and both Space and Enter toggle it. The choice is written to the data-theme attribute of the html element, the one every Forge component already reads: a single switch recolours the whole page without renaming any class, since each component takes its colours from its CSS variables. The chosen theme is remembered in the browser (localStorage) and applied by three lines in the head, before the first paint: no white flash on reload. Until the visitor picks one, the page follows the device’s light or dark setting, even if it changes during the visit; a “Back to device setting” link clears the choice and returns focus to the switch. A status line (role="status") says plainly which theme is active and why. The change spreads to other open tabs, and private browsing that blocks storage breaks nothing. Without JavaScript the button is hidden and the site simply follows the device. Sun and moon drawn in SVG, AA contrast in light and dark, track border visible at 3:1, Windows high contrast support, 44 px target, rem sizes, slide removed with prefers-reduced-motion. HTML/CSS, Tailwind, React (useSyncExternalStore, no dependency) and WordPress variants.

Code

Loading the code…

The code of this component in

Archive contents

interrupteur-bascule-clair-sombre-html-tailwind.zip3 files · 6 Ko

  • index.html8 Ko
  • README.md5 Ko
  • LICENSE.md1 Ko

Tags

Updated on December 2, 2026.

Reviews

No reviews yet.

In the same spirit

  • A recruitment form for small businesses: target position, introduction, CV checked in the browser and GDPR consent, with inline errors.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A quote request form whose labels, hints and error messages are full French sentences, still readable at 320 px: nothing overflows, nothing is cut off, and each error is tied to its field.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Visitors pick a package and tick options: the pre-tax total, VAT and total including tax update live, and the summary is sent with the contact form.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A short NPS survey: a 0 to 10 score as radio buttons, then a follow-up question adapted to whether the customer is a detractor, passive or promoter.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component