Theming
Focus UI includes built-in dark mode support with automatic system detection and manual control via JavaScript API.
How It Works
Focus UI supports three theme modes:
- Auto (default) - Follows system preference via
prefers-color-scheme - Light - Uses the system light palette regardless of system setting
- Dark - Forces dark mode regardless of system setting
Theme preference is persisted in localStorage and automatically restored on page load.
Theme Toggle Button
The library binds .focus-theme-toggle on initialization. Use that binding to cycle themes once per click. For an explicit choice, call FocusUI.Theme.set(mode) from an application-owned control.
<button type="button" class="focus-theme-toggle">
<span class="focus-theme-toggle-icon" aria-hidden="true">
<i class="focus-theme-icon-light fa fa-sun"></i>
<i class="focus-theme-icon-dark fa fa-moon"></i>
<i class="focus-theme-icon-auto fa fa-circle-half-stroke"></i>
</span>
<span class="focus-theme-toggle-label">Theme</span>
</button>
Compact Version
An icon-only toggle with an accessible name. These examples use Font Awesome for the decorative icons. If your app does not load it, use the text-labelled toggle or supply your own icons.
<button type="button" class="focus-theme-toggle focus-theme-toggle-compact" aria-label="Change theme">
<span class="focus-theme-toggle-icon" aria-hidden="true">
<i class="focus-theme-icon-light fa fa-sun"></i>
<i class="focus-theme-icon-dark fa fa-moon"></i>
<i class="focus-theme-icon-auto fa fa-circle-half-stroke"></i>
</span>
</button>
In Navbar
The toggle follows its navbar variant. In a focus-navbar-light, it uses the same text colors as the navigation links.
<nav class="focus-navbar">
<a href="#" class="focus-navbar-brand">My App</a>
<div class="focus-navbar-left">
<a href="#" class="focus-nav-link">Home</a>
<a href="#" class="focus-nav-link">About</a>
</div>
<button type="button" class="focus-theme-toggle focus-theme-toggle-compact" aria-label="Change theme">
<span class="focus-theme-toggle-icon" aria-hidden="true">
<i class="focus-theme-icon-light fa fa-sun"></i>
<i class="focus-theme-icon-dark fa fa-moon"></i>
<i class="focus-theme-icon-auto fa fa-circle-half-stroke"></i>
</span>
</button>
</nav>
JavaScript API
For programmatic theme control, use the FocusUI.Theme service.
Get Current Theme
const theme = FocusUI.Theme.get()
const effective = FocusUI.Theme.getEffective()
Set Theme
FocusUI.Theme.set('dark')
FocusUI.Theme.set('light')
FocusUI.Theme.set('auto')
Toggle Theme
FocusUI.Theme.toggle()
Listen for Changes
document.addEventListener('focus-theme-change', (e) => {
console.log('Theme changed to:', e.detail.theme)
})
Methods
| Method | Returns | Description |
|---|---|---|
FocusUI.Theme.get() |
string | Current theme: 'light', 'dark', or 'auto' |
FocusUI.Theme.set(theme) |
void | Set theme to 'light', 'dark', or 'auto' |
FocusUI.Theme.toggle() |
void | Cycle: light → dark → auto → light |
FocusUI.Theme.getEffective() |
string | Actual theme: 'light' or 'dark' (resolves auto) |
FocusUI.Theme.init() |
void | Initialize theme (auto-called on page load) |
CSS Theming
By default, Focus UI uses CSS media queries for automatic dark mode. No JavaScript required.
@media (prefers-color-scheme: dark) {
:root:where(:not([data-focus-theme="light"])) {
--focus-color-bg: #111827;
--focus-color-text: #e5e7eb;
}
}
Custom Theme Styles
To add your own theme-specific styles:
[data-focus-theme="light"] .my-component {
background: white;
}
[data-focus-theme="dark"] .my-component {
background: #1f2937;
}
@media (prefers-color-scheme: dark) {
.my-component {
background: #1f2937;
}
}
Tip: When writing theme-aware CSS, handle both the [data-focus-theme] attribute (manual override) and the @media (prefers-color-scheme) query (auto mode) for complete coverage.
CSS Variables by Theme
These CSS custom properties automatically adapt based on the active theme:
| Variable | Light Mode | Dark Mode |
|---|---|---|
--focus-color-bg |
#f4f6f9 | #111827 |
--focus-color-text |
#495057 | #e5e7eb |
--focus-color-text-muted |
#6c757d | #9ca3af |
--focus-color-surface |
#ffffff | #1f2937 |
--focus-color-card-bg |
#ffffff | #1f2937 |
--focus-color-border |
#ced4da | #374151 |
--focus-color-hover |
rgba(0,0,0,0.04) | rgba(255,255,255,0.08) |
Use --focus-color-warning-alert-text to customize tinted warning alerts. By default, alerts use --focus-color-warning-text in light mode and #fcd34d in dark mode. Solid warning surfaces such as badges use --focus-color-warning-text in every theme.
CSS Classes Reference
| Class | Description |
|---|---|
.focus-theme-toggle | Theme toggle button container |
.focus-theme-toggle-compact | Icon-only version (no label) |
.focus-theme-toggle-icon | Icon wrapper element |
.focus-theme-toggle-label | Text label (hidden in compact mode) |
.focus-theme-icon-light | Icon shown when theme is 'light' |
.focus-theme-icon-dark | Icon shown when theme is 'dark' |
.focus-theme-icon-auto | Icon shown when theme is 'auto' |
Semantic text and filled controls
Use the --focus-color-*-foreground tokens for semantic text on neutral surfaces. Filled controls use optional --focus-color-*-solid and --focus-color-on-*-solid overrides, falling back to the existing base colors and default foregrounds.
When customizing a base color, also set its foreground token for semantic text and --focus-color-primary-foreground-hover for primary link hover text. Button hover colors can be customized with --focus-color-*-solid-hover. Primary buttons also support --focus-color-on-primary-solid-hover. Check contrast for each custom pair in both themes.
[data-focus-theme="dark"] {
--focus-color-primary-foreground: #93c5fd;
--focus-color-primary-solid: #1d4ed8;
--focus-color-on-primary-solid: #ffffff;
--focus-color-primary-solid-hover: #1e40af;
--focus-color-on-primary-solid-hover: #ffffff;
}