@cosmicdrift/kumiko-renderer-web 0.206.0 → 0.207.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-renderer-web",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.207.0",
|
|
4
4
|
"description": "Web-platform bindings for @cosmicdrift/kumiko-renderer. HTML default-primitives, browser history-based navigation, EventSource-backed live events, and a one-call createKumikoApp that mounts the whole stack via react-dom.",
|
|
5
5
|
"license": "BUSL-1.1",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"./styles.css": "./src/styles.css"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@cosmicdrift/kumiko-dispatcher-live": "0.
|
|
20
|
-
"@cosmicdrift/kumiko-headless": "0.
|
|
21
|
-
"@cosmicdrift/kumiko-renderer": "0.
|
|
19
|
+
"@cosmicdrift/kumiko-dispatcher-live": "0.207.0",
|
|
20
|
+
"@cosmicdrift/kumiko-headless": "0.207.0",
|
|
21
|
+
"@cosmicdrift/kumiko-renderer": "0.207.0",
|
|
22
22
|
"@radix-ui/react-dialog": "^1.1.15",
|
|
23
23
|
"@radix-ui/react-dropdown-menu": "^2.1.16",
|
|
24
24
|
"@radix-ui/react-label": "^2.1.8",
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { describe, expect, mock, test } from "bun:test";
|
|
2
2
|
import type { LocaleResolver } from "@cosmicdrift/kumiko-headless";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
createStaticLocaleResolver,
|
|
5
|
+
kumikoDefaultTranslations,
|
|
6
|
+
LocaleProvider,
|
|
7
|
+
} from "@cosmicdrift/kumiko-renderer";
|
|
4
8
|
import { render as _render, screen, waitFor } from "@testing-library/react";
|
|
5
9
|
import userEvent from "@testing-library/user-event";
|
|
6
10
|
import type { ReactNode } from "react";
|
|
7
11
|
import { LanguageSwitcher } from "../layout/language-switcher";
|
|
8
12
|
|
|
9
|
-
// Tests
|
|
10
|
-
// (setLocale + subscribe)
|
|
11
|
-
//
|
|
12
|
-
// Radix
|
|
13
|
+
// Tests exercise the LanguageSwitcher with both a stateful stub resolver
|
|
14
|
+
// (setLocale + subscribe) and a stateless resolver, covering the two
|
|
15
|
+
// branches: the switcher only renders when setLocale is present.
|
|
16
|
+
// Radix DropdownMenu opens on pointerdown, so userEvent is used instead of
|
|
13
17
|
// fireEvent.click.
|
|
14
18
|
|
|
15
19
|
function makeStatefulResolver(initial: string): LocaleResolver {
|
|
@@ -33,7 +37,11 @@ function makeStatefulResolver(initial: string): LocaleResolver {
|
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
function renderWithResolver(resolver: LocaleResolver, ui: ReactNode) {
|
|
36
|
-
return _render(
|
|
40
|
+
return _render(
|
|
41
|
+
<LocaleProvider resolver={resolver} fallbackBundles={[kumikoDefaultTranslations]}>
|
|
42
|
+
{ui}
|
|
43
|
+
</LocaleProvider>,
|
|
44
|
+
);
|
|
37
45
|
}
|
|
38
46
|
|
|
39
47
|
const locales = [
|
|
@@ -54,9 +62,9 @@ describe("LanguageSwitcher", () => {
|
|
|
54
62
|
test("active locale shown via shorthand", () => {
|
|
55
63
|
const resolver = makeStatefulResolver("de");
|
|
56
64
|
renderWithResolver(resolver, <LanguageSwitcher locales={locales} testId="lang" />);
|
|
57
|
-
//
|
|
58
|
-
//
|
|
59
|
-
// getByText
|
|
65
|
+
// The trigger shows the locale code in the DOM. Tailwind only
|
|
66
|
+
// uppercases it visually via CSS, the text node stays lowercase.
|
|
67
|
+
// That is fine, getByText sees the DOM text.
|
|
60
68
|
expect(screen.getByText("de")).toBeTruthy();
|
|
61
69
|
});
|
|
62
70
|
|
|
@@ -78,18 +86,23 @@ describe("LanguageSwitcher", () => {
|
|
|
78
86
|
expect(resolver.setLocale).toHaveBeenCalledWith("en");
|
|
79
87
|
});
|
|
80
88
|
|
|
89
|
+
test("trigger accessible name follows the active locale (en → Language)", () => {
|
|
90
|
+
const resolver = makeStatefulResolver("en");
|
|
91
|
+
renderWithResolver(resolver, <LanguageSwitcher locales={locales} testId="lang" />);
|
|
92
|
+
expect(screen.getByRole("button", { name: "Language" })).toBeTruthy();
|
|
93
|
+
});
|
|
94
|
+
|
|
81
95
|
test("matches active locale via language-root (de-AT → de)", async () => {
|
|
82
96
|
const user = userEvent.setup();
|
|
83
97
|
const resolver = makeStatefulResolver("de-AT");
|
|
84
98
|
renderWithResolver(resolver, <LanguageSwitcher locales={locales} testId="lang" />);
|
|
85
|
-
//
|
|
86
|
-
//
|
|
99
|
+
// The trigger shows "DE", derived from de-AT. The active marker in the
|
|
100
|
+
// dropdown must sit on "Deutsch", not "English".
|
|
87
101
|
await user.click(screen.getByRole("button", { name: "Sprache" }));
|
|
88
102
|
await waitFor(() => {
|
|
89
|
-
// Radix
|
|
90
|
-
//
|
|
91
|
-
//
|
|
92
|
-
// Tricks.
|
|
103
|
+
// Radix CheckboxItem marks active via aria-checked="true". The check
|
|
104
|
+
// icon (lucide) lives in the ItemIndicator and is only visible when
|
|
105
|
+
// checked, so the ARIA variant is robust against rendering quirks.
|
|
93
106
|
const deItem = screen.getByText("Deutsch").closest('[role="menuitemcheckbox"]');
|
|
94
107
|
expect(deItem?.getAttribute("aria-checked")).toBe("true");
|
|
95
108
|
const enItem = screen.getByText("English").closest('[role="menuitemcheckbox"]');
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
// LanguageSwitcher —
|
|
2
|
-
// LocaleResolver.setLocale
|
|
3
|
-
//
|
|
1
|
+
// LanguageSwitcher — dropdown that switches the app locale via
|
|
2
|
+
// LocaleResolver.setLocale. Built on Radix DropdownMenu, same stack
|
|
3
|
+
// as UserMenu/TenantSwitcher.
|
|
4
4
|
//
|
|
5
|
-
//
|
|
6
|
-
// (
|
|
7
|
-
// stateful
|
|
8
|
-
//
|
|
5
|
+
// Renders nothing if the resolver doesn't offer a setLocale method
|
|
6
|
+
// (static resolver) — the app dev then immediately sees they need to
|
|
7
|
+
// wire up a stateful resolver before the switcher becomes visible in
|
|
8
|
+
// the UI.
|
|
9
9
|
//
|
|
10
|
-
// Icon
|
|
11
|
-
//
|
|
12
|
-
//
|
|
10
|
+
// Icon slot is optional: the framework doesn't pull in lucide-react
|
|
11
|
+
// itself; an app that doesn't want an icon import gets the globe
|
|
12
|
+
// unicode glyph (🌐) as default.
|
|
13
13
|
|
|
14
|
-
import { useLocale } from "@cosmicdrift/kumiko-renderer";
|
|
14
|
+
import { useLocale, useTranslation } from "@cosmicdrift/kumiko-renderer";
|
|
15
15
|
import { type ReactNode, useMemo } from "react";
|
|
16
16
|
import { cn } from "../lib/cn";
|
|
17
17
|
import {
|
|
@@ -22,19 +22,19 @@ import {
|
|
|
22
22
|
} from "../primitives/dropdown-menu";
|
|
23
23
|
|
|
24
24
|
export type LocaleOption = {
|
|
25
|
-
/** BCP-47
|
|
26
|
-
* resolver.setLocale()
|
|
25
|
+
/** BCP-47 code, e.g. "de", "en-US", "fr-CA". Passed through 1:1 to
|
|
26
|
+
* resolver.setLocale(). */
|
|
27
27
|
readonly code: string;
|
|
28
|
-
/**
|
|
28
|
+
/** Human-readable label shown in the dropdown. */
|
|
29
29
|
readonly label: string;
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
export type LanguageSwitcherProps = {
|
|
33
|
-
/**
|
|
33
|
+
/** Selectable locales. Order = display order in the menu. */
|
|
34
34
|
readonly locales: readonly LocaleOption[];
|
|
35
|
-
/** Icon
|
|
35
|
+
/** Icon slot left of the button label. Default: 🌐. */
|
|
36
36
|
readonly icon?: ReactNode;
|
|
37
|
-
/** aria-label + title
|
|
37
|
+
/** aria-label + title of the trigger. Default: translated "kumiko.nav.language". */
|
|
38
38
|
readonly label?: string;
|
|
39
39
|
readonly testId?: string;
|
|
40
40
|
};
|
|
@@ -42,15 +42,17 @@ export type LanguageSwitcherProps = {
|
|
|
42
42
|
export function LanguageSwitcher({
|
|
43
43
|
locales,
|
|
44
44
|
icon = "🌐",
|
|
45
|
-
label
|
|
45
|
+
label,
|
|
46
46
|
testId,
|
|
47
47
|
}: LanguageSwitcherProps): ReactNode {
|
|
48
48
|
const resolver = useLocale();
|
|
49
|
+
const t = useTranslation();
|
|
50
|
+
const resolvedLabel = label ?? t("kumiko.nav.language");
|
|
49
51
|
|
|
50
52
|
const activeLocale = resolver.locale();
|
|
51
|
-
//
|
|
52
|
-
//
|
|
53
|
-
//
|
|
53
|
+
// Matches either exact ("de-DE") or the language root ("de") against
|
|
54
|
+
// the available options. So the switcher shows "German" active when
|
|
55
|
+
// the browser reports "de-AT" but the option is just "de".
|
|
54
56
|
const activeOption = useMemo(() => {
|
|
55
57
|
const exact = locales.find((o) => o.code === activeLocale);
|
|
56
58
|
if (exact) return exact;
|
|
@@ -59,7 +61,7 @@ export function LanguageSwitcher({
|
|
|
59
61
|
}, [locales, activeLocale]);
|
|
60
62
|
|
|
61
63
|
if (resolver.setLocale === undefined) {
|
|
62
|
-
// Stateless
|
|
64
|
+
// Stateless resolver → no switching possible. No noise in the topbar.
|
|
63
65
|
return null;
|
|
64
66
|
}
|
|
65
67
|
|
|
@@ -70,8 +72,8 @@ export function LanguageSwitcher({
|
|
|
70
72
|
<DropdownMenuTrigger asChild>
|
|
71
73
|
<button
|
|
72
74
|
type="button"
|
|
73
|
-
aria-label={
|
|
74
|
-
title={
|
|
75
|
+
aria-label={resolvedLabel}
|
|
76
|
+
title={resolvedLabel}
|
|
75
77
|
data-testid={testId}
|
|
76
78
|
className={cn(
|
|
77
79
|
"inline-flex h-8 items-center gap-1.5 rounded-md border bg-background px-2 text-sm",
|
|
@@ -85,7 +87,7 @@ export function LanguageSwitcher({
|
|
|
85
87
|
</span>
|
|
86
88
|
</button>
|
|
87
89
|
</DropdownMenuTrigger>
|
|
88
|
-
<DropdownMenuContent align="end" className="min-w-[10rem]" aria-label={
|
|
90
|
+
<DropdownMenuContent align="end" className="min-w-[10rem]" aria-label={resolvedLabel}>
|
|
89
91
|
{locales.map((opt) => (
|
|
90
92
|
<DropdownMenuCheckboxItem
|
|
91
93
|
key={opt.code}
|