Repository navigation
feat(gui): icon theme switch sharing a row with the zoom stepper #6579
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
Merged
Merged
Changes from all commits
Commits
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
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,34 @@ | ||
| import { IconMonitor, IconMoon, IconSun } from "../icons"; | ||
| import { useT, type TKey } from "../i18n/shared"; | ||
|
|
||
| export type ThemeMode = "light" | "dark" | "system"; | ||
|
|
||
| const MODES: ReadonlyArray<{ mode: ThemeMode; tkey: TKey; Icon: typeof IconSun }> = [ | ||
| { mode: "light", tkey: "theme.light", Icon: IconSun }, | ||
| { mode: "dark", tkey: "theme.dark", Icon: IconMoon }, | ||
| { mode: "system", tkey: "theme.system", Icon: IconMonitor }, | ||
| ]; | ||
|
|
||
| interface ThemeSwitchProps { | ||
| theme: ThemeMode; | ||
| onChange: (mode: ThemeMode) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Icon-only theme switch for the sidebar foot. All three modes stay visible and the current one | ||
| * is filled, so the user picks a mode in one click instead of cycling through a single button | ||
| * whose next state is hidden. Each button carries its mode name as tooltip and accessible name. | ||
| */ | ||
| export function ThemeSwitch({ theme, onChange }: ThemeSwitchProps) { | ||
| const t = useT(); | ||
| return ( | ||
| <div className="theme-switch" role="group" aria-label={t("theme.label")}> | ||
| {MODES.map(({ mode, tkey, Icon }) => ( | ||
| <button key={mode} type="button" className="theme-switch__btn" aria-pressed={theme === mode} | ||
| aria-label={t(tkey)} title={t(tkey)} onClick={() => onChange(mode)}> | ||
| <Icon aria-hidden /> | ||
| </button> | ||
| ))} | ||
| </div> | ||
| ); | ||
| } |
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,86 @@ | ||
| import { afterEach, expect, test } from "bun:test"; | ||
| import { Window } from "happy-dom"; | ||
| import { act } from "react"; | ||
| import { createRoot, type Root } from "react-dom/client"; | ||
| import { ThemeSwitch, type ThemeMode } from "../src/components/theme-switch"; | ||
| import { en } from "../src/i18n/en"; | ||
| import { ko } from "../src/i18n/ko"; | ||
| import { LanguageProvider } from "../src/i18n/provider"; | ||
| import { getActiveLocale, setActiveLocale, type Locale } from "../src/i18n/shared"; | ||
|
|
||
| const globals = ["document", "window", "navigator", "localStorage", "IS_REACT_ACT_ENVIRONMENT"] as const; | ||
| const modes: ThemeMode[] = ["light", "dark", "system"]; | ||
| let previous: Record<(typeof globals)[number], unknown>; | ||
| let previousLocale: Locale; | ||
| let root: Root | null = null; | ||
|
|
||
| function mount(theme: ThemeMode, onChange: (mode: ThemeMode) => void, locale: "en" | "ko" = "en") { | ||
| previousLocale = getActiveLocale(); | ||
| const win = new Window({ url: "http://127.0.0.1:10100/" }); | ||
| previous = Object.fromEntries(globals.map(k => [k, Reflect.get(globalThis, k)])) as typeof previous; | ||
| win.localStorage.setItem("ocx-lang", locale); | ||
| // Earlier DOM suites may leave non-writable globals; keep ours writable for later suites. | ||
| Object.defineProperties(globalThis, { | ||
| document: { configurable: true, writable: true, value: win.document }, | ||
| window: { configurable: true, writable: true, value: win }, | ||
| navigator: { configurable: true, writable: true, value: win.navigator }, | ||
| localStorage: { configurable: true, writable: true, value: win.localStorage }, | ||
| IS_REACT_ACT_ENVIRONMENT: { configurable: true, writable: true, value: true }, | ||
| }); | ||
| const host = win.document.createElement("div") as never as HTMLElement; | ||
| win.document.body.appendChild(host as never); | ||
| root = createRoot(host); | ||
| const render = (mode: ThemeMode) => act(() => { | ||
| root!.render(<LanguageProvider><ThemeSwitch theme={mode} onChange={onChange} /></LanguageProvider>); | ||
| }); | ||
| render(theme); | ||
| return { host, render }; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| if (root) act(() => root!.unmount()); | ||
| root = null; | ||
| for (const k of globals) Object.defineProperty(globalThis, k, { configurable: true, writable: true, value: previous?.[k] }); | ||
| setActiveLocale(previousLocale); | ||
| }); | ||
|
|
||
| for (const [locale, dict] of [["en", en], ["ko", ko]] as const) { | ||
| test(`theme choices have ordered ${locale} accessible names and decorative icons`, () => { | ||
| const { host } = mount("system", () => {}, locale); | ||
| const group = host.querySelector('[role="group"]'); | ||
| expect(group?.classList.contains("theme-switch")).toBe(true); | ||
| expect(group?.getAttribute("aria-label")).toBe(dict["theme.label"]); | ||
| const buttons = Array.from(group!.querySelectorAll("button")); | ||
| expect(buttons).toHaveLength(3); | ||
| expect(buttons.map(button => button.getAttribute("aria-label"))).toEqual(modes.map(mode => dict[`theme.${mode}`])); | ||
| buttons.forEach((button, index) => { | ||
| expect(button.type).toBe("button"); | ||
| expect(button.classList.contains("theme-switch__btn")).toBe(true); | ||
| expect(button.title).toBe(dict[`theme.${modes[index]!}`]); | ||
| expect(button.textContent?.trim()).toBe(""); | ||
| expect(button.querySelectorAll("svg")).toHaveLength(1); | ||
| expect(button.querySelector("svg")?.getAttribute("aria-hidden")).toBe("true"); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| test("exactly the controlled theme is pressed, including after re-render", () => { | ||
| const { host, render } = mount("light", () => {}); | ||
| for (const mode of modes) { | ||
| render(mode); | ||
| const buttons = Array.from(host.querySelectorAll("button")); | ||
| expect(buttons.map(button => button.getAttribute("aria-pressed"))).toEqual(modes.map(choice => String(choice === mode))); | ||
| expect(host.querySelectorAll('[aria-pressed="true"]')).toHaveLength(1); | ||
| } | ||
| }); | ||
|
|
||
| test("dark, system and light clicks report their own mode", () => { | ||
| const calls: ThemeMode[] = []; | ||
| const { host } = mount("light", mode => calls.push(mode)); | ||
| const [light, dark, system] = Array.from(host.querySelectorAll("button")); | ||
| for (const button of [dark, system, light]) act(() => { button!.click(); }); | ||
| expect(calls).toEqual(["dark", "system", "light"]); | ||
| // Selection stays controlled by the prop until the parent re-renders. | ||
| expect(light!.getAttribute("aria-pressed")).toBe("true"); | ||
| expect(host.querySelectorAll('[aria-pressed="true"]')).toHaveLength(1); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.