-
Notifications
You must be signed in to change notification settings - Fork 786
add dark mode toggle to the editor UI#125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
beforeold
wants to merge
3
commits into
nexu-io:main
Choose a base branch
from
beforeold:dark-mode-toggle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| "use client"; | ||
|
|
||
| import { useSyncExternalStore } from "react"; | ||
| import { useT } from "@/lib/i18n"; | ||
|
|
||
| type Theme = "light" | "dark"; | ||
|
|
||
| // The boot script in layout.tsx applies data-theme before hydration, so the | ||
| // <html> attribute — not React state — is the source of truth for the theme. | ||
| function readTheme(): Theme { | ||
| return document.documentElement.dataset.theme === "dark" ? "dark" : "light"; | ||
| } | ||
|
|
||
| function subscribe(onChange: () => void): () => void { | ||
| const observer = new MutationObserver(onChange); | ||
| observer.observe(document.documentElement, { attributeFilter: ["data-theme"] }); | ||
| return () => observer.disconnect(); | ||
| } | ||
|
|
||
| export function ThemeToggle() { | ||
| // Server snapshot matches the SSR markup; useSyncExternalStore re-reads the | ||
| // live attribute before first paint, so a dark reload never shows the light | ||
| // icon, a stale aria-pressed, or a no-op first click. | ||
| const theme = useSyncExternalStore(subscribe, readTheme, () => "light"); | ||
| const t = useT(); | ||
|
|
||
| const toggle = () => { | ||
| const next: Theme = readTheme() === "dark" ? "light" : "dark"; | ||
| document.documentElement.dataset.theme = next; // observer re-renders us | ||
| try { | ||
| localStorage.setItem("html-anything-theme", next); | ||
| } catch { | ||
| /* private mode — theme still applies for this session */ | ||
| } | ||
| }; | ||
|
|
||
| const label = t("toolbar.toggleTheme"); | ||
| return ( | ||
| <button | ||
| onClick={toggle} | ||
| className="grid h-9 w-9 place-items-center rounded-full border text-[var(--ink-soft)] transition-all hover:border-[var(--ink)]/30 hover:text-[var(--ink)]" | ||
| style={{ background: "var(--surface)", borderColor: "var(--line)" }} | ||
| title={label} | ||
| aria-label={label} | ||
| aria-pressed={theme === "dark"} | ||
| > | ||
| {theme === "dark" ? ( | ||
| <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"> | ||
| <circle cx="12" cy="12" r="4" /> | ||
| <path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41" /> | ||
| </svg> | ||
| ) : ( | ||
| <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"> | ||
| <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" /> | ||
| </svg> | ||
| )} | ||
| </button> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes the storage-denied fallback, but the PR still ships both review follow-up fixes without any regression coverage. That matters because the two bugs already found on this branch were both in the theme bootstrap path: one made the first click a no-op after a dark reload, and the other skipped the
🔁 Powered by Looper · runner=reviewer · agent=codex · An autonomous AI dev team for your GitHub repos.prefers-color-schemefallback wheneverlocalStorage.getItem(...)threw. Since noe2e/uior Vitest case changed with these lines, either behavior can regress silently in the next refactor. Please add an automated test matrix for this bootstrap flow, for example an e2e case that seedshtml-anything-theme=dark, reloads, and asserts the toggle is pressed and switches to light on the first click, plus a case that makeslocalStorage.getItemthrow whilematchMedia("(prefers-color-scheme: dark)")returnstrueand then verifies<html data-theme="dark">after boot.