Flarum 2 標題列的晝/夜/系統主題切換按鈕。
AI 翻譯
Day/night/system theme toggle button for the Flarum 2 header.
取自儲存庫 README
正在載入完整 README。 以下為已儲存的摘錄。
A small, theme-agnostic day/night/system picker for the Flarum 2 header. Drops into any Flarum 2 theme that respects the native signal (every theme that ships with or extends Flarum 2 does, including the default).
A compact icon button next to Search / Sign Up / Log In. The icon reflects the active choice (☾ dark, ☀ light, ◐ system).
Five choices, each with a matching icon. The active choice is marked with a checkmark.檢視完整 README ↗A small, theme-agnostic day/night/system picker for the Flarum 2 header. Drops into any Flarum 2 theme that respects the native <html data-theme="…"> signal (every theme that ships with or extends Flarum 2 does, including the default).
A compact icon button next to Search / Sign Up / Log In. The icon reflects the active choice (☾ dark, ☀ light, ◐ system).
Five choices, each with a matching icon. The active choice is marked with a checkmark.
On phone-width viewports Flarum re-renders HeaderSecondary inside .App-drawer. The button switches to a labeled, full-width menu row so it lines up with Search / Sign Up / Log In.
<html data-theme="…"> and localStorage — no theme-specific CSS, so it composes with any well-behaved Flarum 2 theme (default, Aurora, or any third-party theme).prefers-color-scheme for light/dark and prefers-contrast: more for the -hc variants — so an accessibility user with macOS / Windows "Increase contrast" turned on automatically gets the right palette.localStorage under ernestdefoe-theme-toggle.choice and re-applied on every page load before paint, so there's no FOUC."◐ Theme" row inside the mobile slide-out drawer (where icon-only buttons would float disconnected from the surrounding Sign Up / Log In rows).locale/en.yml and can be overridden in your forum's locale files..d.ts files. npm run check-typings runs tsc --noEmit.^2.0^8.3composer require ernestdefoe/theme-toggle
php flarum cache:clear
Then enable Theme Toggle from the admin panel's Extensions page.
cd js
npm install
npm run build # production bundle (writes js/dist/forum.js)
npm run dev # watch mode for development
npm run check-typings # tsc --noEmit, requires `composer install` at repo root first
The TypeScript path mapping in js/tsconfig.json points at ../vendor/flarum/core/js/dist-typings/, so composer install at the repo root must run before check-typings can resolve flarum/* imports.
extend.php Flarum extension bootstrap
composer.json Package manifest
locale/en.yml User-facing strings
less/forum.less Header + drawer styling
js/
forum.ts Webpack entry point
tsconfig.json Extends flarum-tsconfig
src/forum/
index.ts Initializer + HeaderSecondary hook
theme.ts Choice/storage helpers + media listener
components/ThemeToggle.tsx The Dropdown component
dist/forum.js Compiled bundle (committed)
screenshots/ Images in this README
Flarum 2 ships four data-theme values: light, light-hc, dark, and dark-hc. Themes are expected to define their dark tokens as the default and their light tokens under [data-theme^='light'], with a high-contrast layer under [data-theme^='light-hc'] / [data-theme^='dark-hc']. Because those selectors use ^= (prefix match), setting data-theme="light-hc" activates both the regular light palette and its HC overrides via the cascade.
This extension just sets that attribute client-side:
theme.ts reads the saved choice from localStorage and writes the corresponding string to <html data-theme="…"> before the initializer runs, so the page never paints in the wrong palette.ThemeToggle component reads the same choice for its icon and dropdown state.localStorage, <html> is updated, and Mithril redraws the button.matchMedia listener for prefers-color-scheme and prefers-contrast: more re-applies the choice live.No theme-specific CSS, no setting payload, no server round-trip.
Override any string by adding the matching key under ernestdefoe-theme-toggle.forum.toggle.* in your forum's locale file:
| Key | Default |
|---|---|
label | Theme |
option_dark | Dark |
option_dark_hc | Dark (high contrast) |
option_light | Light |
option_light_hc | Light (high contrast) |
option_system | System |
MIT © Ernest Defoe