Dark Mode
Display your pages in dark mode, following a saved choice or the operating system setting
To display a page in dark mode, add the dark class to the <html> element:
<html lang="en" class="dark">The StellarAdmin.UI stylesheet contains the styles for both modes, and derives dark mode from the same theme variables as light mode, so your theme works in dark mode too.
Following the operating system setting
You can add the dark class on the server, such as from a setting the user saved, or in the browser. The script below adds it in the browser. It uses the choice saved in localStorage, and if there isn't one, the operating system setting. Put it in the <head> of your layout, before the stylesheets, so that it runs before the page is displayed:
<script>
(function () {
var saved = null;
try { saved = localStorage.getItem("theme"); } catch (e) { }
var prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
var dark = saved === "dark" || (saved !== "light" && prefersDark);
document.documentElement.classList.toggle("dark", dark);
document.documentElement.style.colorScheme = dark ? "dark" : "light";
})();
</script>The script also sets the color-scheme property, so that the browser displays its own controls, such as scrollbars, in the same mode as the page.
Displaying part of a page in dark mode
The dark class also works on any other element, such as a promotion or a sidebar. Everything inside the element is displayed in dark mode, and the rest of the page keeps its mode. To display part of a dark page in light mode, add the light class to the element.
You can nest the two classes. For example, a dark element inside a light element on a dark page is displayed in dark mode.
In the example below, the first section has the dark class and the second the light class. The sections set their background with the bg-background class from the Tailwind adapter, which is the page color of the section's mode. Without Tailwind CSS, use background: var(--sa-color-canvas).
<div class="flex w-full flex-col gap-6">
<section class="dark flex flex-col gap-4 rounded-xl bg-background p-6 text-foreground">
<div class="flex flex-col gap-1">
<h3 class="text-lg font-semibold">Northern lights in Tromsø</h3>
<p class="text-sm text-muted-foreground">Four nights under the aurora, with a guided night hunt and a husky ride.</p>
</div>
<div class="flex flex-wrap items-center gap-2">
<sa-input class="w-56" placeholder="Your email address"/>
<sa-button>Notify me</sa-button>
<sa-button variant="ButtonVariant.Ghost">Learn more</sa-button>
</div>
</section>
<section class="light flex flex-col gap-4 rounded-xl bg-background p-6 text-foreground">
<div class="flex flex-col gap-1">
<h3 class="text-lg font-semibold">Midsummer in Stockholm</h3>
<p class="text-sm text-muted-foreground">Three nights of near-endless daylight, with an archipelago cruise and a Midsummer Eve feast.</p>
</div>
<div class="flex flex-wrap items-center gap-2">
<sa-input class="w-56" placeholder="Your email address"/>
<sa-button>Notify me</sa-button>
<sa-button variant="ButtonVariant.Ghost">Learn more</sa-button>
</div>
</section>
<div class="flex flex-wrap items-center gap-2">
<sa-input class="w-56" placeholder="Your email address"/>
<sa-button>Notify me</sa-button>
<sa-button variant="ButtonVariant.Ghost">Learn more</sa-button>
</div>
</div>