-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(admin): keep locale menus consistent with the admin theme #2903
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
MatsudaTsunenori
wants to merge
8
commits into
emdash-cms:main
Choose a base branch
from
MatsudaTsunenori:codex/fix-dark-locale-switcher-restored
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 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d3c3fa9
fix(admin): keep locale menus consistent with the admin theme
MatsudaTsunenori d00d214
fix(admin): preserve byline edits when reselecting the active locale
MatsudaTsunenori d1a27f1
test(admin): wait for locale option focus before keyboard input
MatsudaTsunenori b99f85d
test(admin): use ESM extension for locale switcher test import
MatsudaTsunenori d157250
test(admin): stabilize locale switcher visual selector
MatsudaTsunenori 3579bc8
ci: accept visual baselines (requested by @ascorbic)
emdashbot[bot] e70c7ea
Merge upstream/main into codex/fix-dark-locale-switcher-restored
MatsudaTsunenori 6ff1195
test: isolate locale filter E2E data
MatsudaTsunenori 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@emdash-cms/admin": patch | ||
| --- | ||
|
|
||
| Fixes locale switcher menus appearing with a light background in dark mode. The selected locale, default-locale indicator, and `All locales` label remain visible when the menu is closed. | ||
|
|
||
| Reselecting the current locale preserves unsaved byline edits. |
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
141 changes: 141 additions & 0 deletions
141
packages/admin/tests/components/LocaleSwitcher.test.tsx
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,141 @@ | ||
| import { setupI18n } from "@lingui/core"; | ||
| import { msg } from "@lingui/core/macro"; | ||
| import { I18nProvider } from "@lingui/react"; | ||
| import * as React from "react"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { userEvent } from "vitest/browser"; | ||
|
|
||
| import { LocaleSwitcher } from "../../src/components/LocaleSwitcher.js"; | ||
| // oxlint-disable-next-line import/no-unassigned-import -- Browser test verifies Kumo's computed theme surface. | ||
| import "@cloudflare/kumo/styles/standalone"; | ||
|
|
||
| import { render } from "../utils/render.js"; | ||
|
|
||
| const defaultLocaleIndicator = msg` (default)`; | ||
|
|
||
| function ControlledSwitcher({ initialValue = "en", showAll = false }) { | ||
| const [value, setValue] = React.useState(initialValue); | ||
| return ( | ||
| <LocaleSwitcher | ||
| locales={["en", "fr"]} | ||
| defaultLocale="en" | ||
| value={value} | ||
| onChange={setValue} | ||
| showAll={showAll} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| describe("LocaleSwitcher", () => { | ||
| it("opens the locale menu and displays the selected locale", async () => { | ||
| const screen = await render(<ControlledSwitcher />); | ||
| const localeSwitcher = screen.getByRole("combobox", { name: "Locale" }); | ||
| await expect.element(localeSwitcher).toHaveTextContent(/^EN \(default\)$/); | ||
| await userEvent.click(localeSwitcher); | ||
|
|
||
| await expect.element(screen.getByRole("option", { name: "EN (default)" })).toBeVisible(); | ||
| await expect | ||
| .element(screen.getByRole("option", { name: "All locales" })) | ||
| .not.toBeInTheDocument(); | ||
| await userEvent.click(screen.getByRole("option", { name: "FR" })); | ||
| await expect.element(localeSwitcher).toHaveTextContent(/^FR$/); | ||
| await expect.element(screen.getByRole("listbox")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("displays all locales initially and after selecting it again", async () => { | ||
| const screen = await render(<ControlledSwitcher initialValue="" showAll />); | ||
| const localeSwitcher = screen.getByRole("combobox", { name: "Locale" }); | ||
| await expect.element(localeSwitcher).toHaveTextContent(/^All locales$/); | ||
|
|
||
| await userEvent.click(localeSwitcher); | ||
| await userEvent.click(screen.getByRole("option", { name: "EN (default)" })); | ||
| await expect.element(localeSwitcher).toHaveTextContent(/^EN \(default\)$/); | ||
|
|
||
| await userEvent.click(localeSwitcher); | ||
| await userEvent.click(screen.getByRole("option", { name: "All locales" })); | ||
| await expect.element(localeSwitcher).toHaveTextContent(/^All locales$/); | ||
| }); | ||
|
|
||
| it("supports keyboard selection and cancels without changing the locale", async () => { | ||
| const screen = await render(<ControlledSwitcher />); | ||
| const localeSwitcher = screen.getByRole("combobox", { name: "Locale" }); | ||
| await userEvent.tab(); | ||
| await expect.element(localeSwitcher).toHaveFocus(); | ||
| await userEvent.keyboard("{Enter}"); | ||
| await expect.element(screen.getByRole("listbox")).toBeVisible(); | ||
| await expect.element(screen.getByRole("option", { name: "EN (default)" })).toHaveFocus(); | ||
| await userEvent.keyboard("{End}"); | ||
| await expect.element(screen.getByRole("option", { name: "FR" })).toHaveFocus(); | ||
| await userEvent.keyboard("{Enter}"); | ||
| await expect.element(localeSwitcher).toHaveTextContent(/^FR$/); | ||
| await expect.element(localeSwitcher).toHaveFocus(); | ||
|
|
||
| await userEvent.keyboard("{Enter}"); | ||
| await expect.element(screen.getByRole("listbox")).toBeVisible(); | ||
| await expect.element(screen.getByRole("option", { name: "FR" })).toHaveFocus(); | ||
| await userEvent.keyboard("{Home}"); | ||
| await expect.element(screen.getByRole("option", { name: "EN (default)" })).toHaveFocus(); | ||
| await userEvent.keyboard("{Escape}"); | ||
| await expect.element(localeSwitcher).toHaveTextContent(/^FR$/); | ||
| await expect.element(screen.getByRole("listbox")).not.toBeInTheDocument(); | ||
| await expect.element(localeSwitcher).toHaveFocus(); | ||
| }); | ||
|
|
||
| it("uses the existing translation for the default-locale indicator", async () => { | ||
| const arabicI18n = setupI18n(); | ||
| arabicI18n.load("ar", { [defaultLocaleIndicator.id]: " الافتراضي" }); | ||
| arabicI18n.activate("ar"); | ||
| const screen = await render(<ControlledSwitcher />, { | ||
| wrapper: ({ children }) => <I18nProvider i18n={arabicI18n}>{children}</I18nProvider>, | ||
| }); | ||
|
|
||
| const localeSwitcher = screen.getByRole("combobox", { name: "Locale" }); | ||
| await expect.element(localeSwitcher).toHaveTextContent(/^EN الافتراضي$/); | ||
| await userEvent.click(localeSwitcher); | ||
| await expect.element(screen.getByRole("option", { name: "EN الافتراضي" })).toBeVisible(); | ||
| }); | ||
|
|
||
| it.each(["sm", "md"] as const)( | ||
| "renders the open %s locale menu with the dark theme surface", | ||
| async (size) => { | ||
| const previousMode = document.documentElement.getAttribute("data-mode"); | ||
| const surfaceProbe = document.createElement("div"); | ||
| surfaceProbe.style.backgroundColor = "var(--color-kumo-base)"; | ||
| document.body.append(surfaceProbe); | ||
| document.documentElement.setAttribute("data-mode", "dark"); | ||
|
|
||
| try { | ||
| const screen = await render( | ||
| <LocaleSwitcher | ||
| locales={["en", "fr"]} | ||
| defaultLocale="en" | ||
| value="en" | ||
| onChange={vi.fn()} | ||
| size={size} | ||
| />, | ||
| ); | ||
|
|
||
| await userEvent.click(screen.getByRole("combobox", { name: "Locale" })); | ||
| const listbox = screen.getByRole("listbox"); | ||
| await expect.element(listbox).toBeVisible(); | ||
| const popup = listbox.element().parentElement; | ||
| expect(popup).not.toBeNull(); | ||
|
|
||
| const darkSurface = getComputedStyle(surfaceProbe).backgroundColor; | ||
| document.documentElement.setAttribute("data-mode", "light"); | ||
| const lightSurface = getComputedStyle(surfaceProbe).backgroundColor; | ||
| document.documentElement.setAttribute("data-mode", "dark"); | ||
|
|
||
| expect(darkSurface).not.toBe(lightSurface); | ||
| expect(getComputedStyle(popup!).backgroundColor).toBe(darkSurface); | ||
| } finally { | ||
| surfaceProbe.remove(); | ||
| if (previousMode) { | ||
| document.documentElement.setAttribute("data-mode", previousMode); | ||
| } else { | ||
| document.documentElement.removeAttribute("data-mode"); | ||
| } | ||
| } | ||
| }, | ||
| ); | ||
| }); |
Oops, something went wrong.
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.