@canonical/summon-application 0.43.0 → 0.46.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/README.md +57 -11
- package/dist/esm/application/react/index.js +175 -21
- package/dist/esm/application/react/index.js.map +1 -1
- package/dist/esm/application/react/templates/README.md.ejs +33 -1
- package/dist/esm/application/react/templates/package.json.ejs +25 -18
- package/dist/esm/application/react/templates/src/client/entry.tsx.ejs +10 -16
- package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx.ejs +5 -5
- package/dist/esm/application/react/templates/src/domains/dashboard/DashboardBreadcrumbs.tsx.ejs +35 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/DashboardLayout.tsx.ejs +39 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/InstancesPage.test.tsx.ejs +144 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/InstancesPage.tsx.ejs +281 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/OverviewPage.test.tsx.ejs +76 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/OverviewPage.tsx.ejs +224 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/SettingsPage.test.tsx.ejs +64 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/SettingsPage.tsx.ejs +142 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/constants.ts.ejs +229 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/hooks/index.ts +2 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/hooks/types.ts +13 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/hooks/useDashboardNavigation.ts.ejs +94 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/projectBreadcrumbs.test.ts +78 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/projectBreadcrumbs.ts +48 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/routes.ts +21 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/selectRelativeTime.test.ts +19 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/selectRelativeTime.ts +24 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/styles.css +98 -0
- package/dist/esm/application/react/templates/src/domains/dashboard/types.ts.ejs +61 -0
- package/dist/esm/application/react/templates/src/domains/marketing/HomePage.tsx.ejs +14 -9
- package/dist/esm/application/react/templates/src/i18n/ar.ts.ejs +173 -0
- package/dist/esm/application/react/templates/src/i18n/en.ts.ejs +172 -0
- package/dist/esm/application/react/templates/src/i18n/fr.ts.ejs +170 -0
- package/dist/esm/application/react/templates/src/lib/ApplicationShell/ApplicationShell.stories.tsx +91 -0
- package/dist/esm/application/react/templates/src/lib/ApplicationShell/ApplicationShell.test.tsx.ejs +139 -0
- package/dist/esm/application/react/templates/src/lib/ApplicationShell/ApplicationShell.tsx.ejs +78 -0
- package/dist/esm/application/react/templates/src/lib/ApplicationShell/index.ts +2 -0
- package/dist/esm/application/react/templates/src/lib/ApplicationShell/styles.css +43 -0
- package/dist/esm/application/react/templates/src/lib/ApplicationShell/types.ts +20 -0
- package/dist/esm/application/react/templates/src/lib/CanonicalLogo/CanonicalLogo.tsx.ejs +51 -0
- package/dist/esm/application/react/templates/src/lib/CanonicalLogo/index.ts +2 -0
- package/dist/esm/application/react/templates/src/lib/CanonicalLogo/styles.css +31 -0
- package/dist/esm/application/react/templates/src/lib/CanonicalLogo/types.ts +11 -0
- package/dist/esm/application/react/templates/src/lib/CodeMessage/CodeMessage.test.tsx +33 -0
- package/dist/esm/application/react/templates/src/lib/CodeMessage/CodeMessage.tsx +33 -0
- package/dist/esm/application/react/templates/src/lib/CodeMessage/index.ts +2 -0
- package/dist/esm/application/react/templates/src/lib/CodeMessage/types.ts +16 -0
- package/dist/esm/application/react/templates/src/lib/ExampleComponent/styles.css +1 -1
- package/dist/esm/application/react/templates/src/lib/LazyComponent/{LazyComponent.tsx → LazyComponent.tsx.ejs} +26 -0
- package/dist/esm/application/react/templates/src/lib/LoadingMessage/LoadingMessage.test.tsx.ejs +20 -0
- package/dist/esm/application/react/templates/src/lib/LoadingMessage/LoadingMessage.tsx.ejs +21 -0
- package/dist/esm/application/react/templates/src/lib/LoadingMessage/index.ts +1 -0
- package/dist/esm/application/react/templates/src/lib/LocaleSelector/{LocaleSelector.tsx → LocaleSelector.tsx.ejs} +23 -0
- package/dist/esm/application/react/templates/src/lib/Navigation/Navigation.test.tsx.ejs +87 -0
- package/dist/esm/application/react/templates/src/lib/Navigation/Navigation.tsx.ejs +3 -0
- package/dist/esm/application/react/templates/src/lib/RouterLink/RouterLink.test.tsx +98 -0
- package/dist/esm/application/react/templates/src/lib/RouterLink/RouterLink.tsx +65 -0
- package/dist/esm/application/react/templates/src/lib/RouterLink/index.ts +1 -0
- package/dist/esm/application/react/templates/src/lib/ThemeSelector/ThemeSelector.tsx.ejs +29 -14
- package/dist/esm/application/react/templates/src/lib/TopBar/TopBar.stories.tsx.ejs +29 -0
- package/dist/esm/application/react/templates/src/lib/TopBar/TopBar.test.tsx.ejs +113 -0
- package/dist/esm/application/react/templates/src/lib/TopBar/TopBar.tsx.ejs +45 -0
- package/dist/esm/application/react/templates/src/lib/TopBar/index.ts +2 -0
- package/dist/esm/application/react/templates/src/lib/TopBar/styles.css +49 -0
- package/dist/esm/application/react/templates/src/lib/TopBar/types.ts +8 -0
- package/dist/esm/application/react/templates/src/lib/index.ts.ejs +10 -0
- package/dist/esm/application/react/templates/src/routes.tsx.ejs +70 -20
- package/dist/esm/application/react/templates/src/server/entry.tsx.ejs +6 -7
- package/dist/esm/application/react/templates/src/sitemap/getSitemapItems.ts.ejs +5 -0
- package/dist/esm/application/react/templates/src/styles/app.css +25 -12
- package/dist/esm/application/react/templates/src/testing/browserStubs.ts +57 -0
- package/dist/esm/application/react/templates/src/testing/fixtures.ts.ejs +29 -0
- package/dist/esm/application/react/templates/src/testing/renderWithProviders.tsx.ejs +64 -0
- package/dist/esm/application/react/templates/test/e2e/servers.e2e.ts.ejs +37 -1
- package/dist/esm/application/react/templates/vite-plugin-ds-icons.ts +7 -2
- package/dist/esm/application/react/templates/vitest.config.ts +6 -2
- package/dist/esm/application/react/textHelpers.js +9 -0
- package/dist/esm/application/react/textHelpers.js.map +1 -0
- package/dist/esm/shared/versions.js +35 -21
- package/dist/esm/shared/versions.js.map +1 -1
- package/dist/types/application/react/index.d.ts +1 -0
- package/dist/types/application/react/index.d.ts.map +1 -1
- package/dist/types/application/react/textHelpers.d.ts +18 -0
- package/dist/types/application/react/textHelpers.d.ts.map +1 -0
- package/dist/types/shared/versions.d.ts +41 -4
- package/dist/types/shared/versions.d.ts.map +1 -1
- package/package.json +9 -9
- package/dist/esm/application/react/templates/src/i18n/ar.ts +0 -73
- package/dist/esm/application/react/templates/src/i18n/en.ts +0 -72
- package/dist/esm/application/react/templates/src/i18n/fr.ts +0 -69
- /package/dist/esm/application/react/templates/src/domains/catalog/{ErrorBoundary.tests.tsx.ejs → ErrorBoundary.test.tsx.ejs} +0 -0
- /package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.tests.tsx.ejs → ProductList.test.tsx.ejs} +0 -0
- /package/dist/esm/application/react/templates/src/i18n/{negotiation.tests.ts → negotiation.test.ts} +0 -0
- /package/dist/esm/application/react/templates/src/lib/ClientOnly/{ClientOnly.tests.tsx → ClientOnly.test.tsx} +0 -0
- /package/dist/esm/application/react/templates/src/lib/ExampleComponent/{ExampleComponent.tests.tsx → ExampleComponent.test.tsx} +0 -0
- /package/dist/esm/application/react/templates/src/lib/LocaleSelector/{LocaleSelector.tests.tsx → LocaleSelector.test.tsx} +0 -0
- /package/dist/esm/application/react/templates/src/relay/{environment.tests.ts → environment.test.ts} +0 -0
- /package/dist/esm/application/react/templates/src/relay/{schema.tests.ts → schema.test.ts} +0 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import { fireEvent, screen } from "@testing-library/react";
|
|
3
|
+
<% } else { -%>
|
|
4
|
+
import { screen } from "@testing-library/react";
|
|
5
|
+
<% } -%>
|
|
6
|
+
<% if (intl) { -%>
|
|
7
|
+
import { afterEach, beforeAll, describe, expect, it } from "vitest";
|
|
8
|
+
<% } else { -%>
|
|
9
|
+
import { beforeAll, describe, expect, it } from "vitest";
|
|
10
|
+
<% } -%>
|
|
11
|
+
import { installMatchMedia } from "../../testing/browserStubs.js";
|
|
12
|
+
import renderWithProviders from "../../testing/renderWithProviders.js";
|
|
13
|
+
import Navigation from "./Navigation.js";
|
|
14
|
+
|
|
15
|
+
// jsdom ships no matchMedia; ThemeSelector's usePreferredTheme queries it.
|
|
16
|
+
beforeAll(() => {
|
|
17
|
+
installMatchMedia();
|
|
18
|
+
});
|
|
19
|
+
<% if (intl) { -%>
|
|
20
|
+
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
// The locale source persists to a cookie and reflects <html lang dir>;
|
|
23
|
+
// reset both so tests stay independent.
|
|
24
|
+
// biome-ignore lint/suspicious/noDocumentCookie: test cleanup
|
|
25
|
+
document.cookie = "locale=; max-age=0; path=/";
|
|
26
|
+
document.documentElement.removeAttribute("lang");
|
|
27
|
+
document.documentElement.removeAttribute("dir");
|
|
28
|
+
});
|
|
29
|
+
<% } -%>
|
|
30
|
+
|
|
31
|
+
describe("Navigation component", () => {
|
|
32
|
+
it("renders the<% if (intl) { %> English<% } %> chrome<% if (intl) { %> by default<% } %>", () => {
|
|
33
|
+
renderWithProviders(<Navigation />);
|
|
34
|
+
|
|
35
|
+
expect(
|
|
36
|
+
screen.getByRole("navigation", { name: "Main" }),
|
|
37
|
+
).toBeInTheDocument();
|
|
38
|
+
expect(screen.getByRole("link", { name: "Home" })).toBeInTheDocument();
|
|
39
|
+
<% if (relay) { -%>
|
|
40
|
+
expect(screen.getByRole("link", { name: "Catalog" })).toBeInTheDocument();
|
|
41
|
+
<% } -%>
|
|
42
|
+
expect(
|
|
43
|
+
screen.getByRole("combobox", { name: "Color theme" }),
|
|
44
|
+
).toBeInTheDocument();
|
|
45
|
+
});
|
|
46
|
+
<% if (intl) { -%>
|
|
47
|
+
|
|
48
|
+
it("switches every visible string when the selector picks French", () => {
|
|
49
|
+
renderWithProviders(<Navigation />);
|
|
50
|
+
|
|
51
|
+
fireEvent.change(screen.getByRole("combobox", { name: "Language" }), {
|
|
52
|
+
target: { value: "fr" },
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Links, the theme selector, and the locale selector itself re-translate…
|
|
56
|
+
expect(screen.getByRole("link", { name: "Accueil" })).toBeInTheDocument();
|
|
57
|
+
<% if (relay) { -%>
|
|
58
|
+
expect(screen.getByRole("link", { name: "Catalogue" })).toBeInTheDocument();
|
|
59
|
+
<% } -%>
|
|
60
|
+
expect(
|
|
61
|
+
screen.getByRole("combobox", { name: "Thème de couleur" }),
|
|
62
|
+
).toBeInTheDocument();
|
|
63
|
+
expect(
|
|
64
|
+
screen.getByRole("combobox", { name: "Langue" }),
|
|
65
|
+
).toBeInTheDocument();
|
|
66
|
+
expect(
|
|
67
|
+
screen.queryByRole("link", { name: "Home" }),
|
|
68
|
+
).not.toBeInTheDocument();
|
|
69
|
+
// …and the choice persists for the server's next negotiation.
|
|
70
|
+
expect(document.cookie).toContain("locale=fr");
|
|
71
|
+
expect(document.documentElement.lang).toBe("fr");
|
|
72
|
+
expect(document.documentElement.dir).toBe("ltr");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("flips the document to right-to-left for Arabic", () => {
|
|
76
|
+
renderWithProviders(<Navigation />);
|
|
77
|
+
|
|
78
|
+
fireEvent.change(screen.getByRole("combobox", { name: "Language" }), {
|
|
79
|
+
target: { value: "ar" },
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
expect(screen.getByRole("link", { name: "الرئيسية" })).toBeInTheDocument();
|
|
83
|
+
expect(document.documentElement.lang).toBe("ar");
|
|
84
|
+
expect(document.documentElement.dir).toBe("rtl");
|
|
85
|
+
});
|
|
86
|
+
<% } -%>
|
|
87
|
+
});
|
|
@@ -41,6 +41,9 @@ export default function Navigation(): ReactElement {
|
|
|
41
41
|
<% } else { -%>
|
|
42
42
|
<Link to="contact">Contact</Link>
|
|
43
43
|
<% } -%>
|
|
44
|
+
<% } -%>
|
|
45
|
+
<% if (appShell) { -%>
|
|
46
|
+
<Link to="dashboard"><%- msg("nav.dashboard", "Dashboard") %></Link>
|
|
44
47
|
<% } -%>
|
|
45
48
|
<Link search={{ auth: "1" }} to="account">
|
|
46
49
|
<% if (intl) { -%>
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { fireEvent, screen } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
|
+
import renderWithProviders from "../../testing/renderWithProviders.js";
|
|
4
|
+
import RouterLink from "./RouterLink.js";
|
|
5
|
+
|
|
6
|
+
describe("RouterLink", () => {
|
|
7
|
+
it("renders an anchor with the given href, class and children", () => {
|
|
8
|
+
renderWithProviders(
|
|
9
|
+
<RouterLink className="row" href="/dashboard">
|
|
10
|
+
Overview
|
|
11
|
+
</RouterLink>,
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const link = screen.getByRole("link", { name: "Overview" });
|
|
15
|
+
|
|
16
|
+
expect(link).toHaveAttribute("href", "/dashboard");
|
|
17
|
+
expect(link).toHaveClass("row");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("forwards aria-current as given", () => {
|
|
21
|
+
renderWithProviders(
|
|
22
|
+
<RouterLink aria-current="page" href="/dashboard">
|
|
23
|
+
Overview
|
|
24
|
+
</RouterLink>,
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
expect(screen.getByRole("link", { name: "Overview" })).toHaveAttribute(
|
|
28
|
+
"aria-current",
|
|
29
|
+
"page",
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("a plain click", () => {
|
|
34
|
+
it("navigates client-side to the route the URL matches", () => {
|
|
35
|
+
const { router } = renderWithProviders(
|
|
36
|
+
<RouterLink href="/dashboard/instances">Instances</RouterLink>,
|
|
37
|
+
"/dashboard",
|
|
38
|
+
);
|
|
39
|
+
const navigate = vi.spyOn(router, "navigate");
|
|
40
|
+
const link = screen.getByRole("link", { name: "Instances" });
|
|
41
|
+
|
|
42
|
+
const notPrevented = fireEvent.click(link);
|
|
43
|
+
|
|
44
|
+
expect(notPrevented).toBe(false);
|
|
45
|
+
expect(navigate).toHaveBeenCalledWith(
|
|
46
|
+
"dashboardInstances",
|
|
47
|
+
expect.objectContaining({ params: {} }),
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("leaves a URL the router does not match to the browser", () => {
|
|
52
|
+
const { router } = renderWithProviders(
|
|
53
|
+
<RouterLink href="https://ubuntu.com/legal">Legal</RouterLink>,
|
|
54
|
+
);
|
|
55
|
+
const navigate = vi.spyOn(router, "navigate");
|
|
56
|
+
|
|
57
|
+
const notPrevented = fireEvent.click(
|
|
58
|
+
screen.getByRole("link", { name: "Legal" }),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
expect(notPrevented).toBe(true);
|
|
62
|
+
expect(navigate).not.toHaveBeenCalled();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe("a modified click", () => {
|
|
67
|
+
it.each([["ctrlKey"], ["metaKey"], ["shiftKey"], ["altKey"]])(
|
|
68
|
+
"with %s is left to the browser",
|
|
69
|
+
(modifier) => {
|
|
70
|
+
const { router } = renderWithProviders(
|
|
71
|
+
<RouterLink href="/dashboard">Overview</RouterLink>,
|
|
72
|
+
);
|
|
73
|
+
const navigate = vi.spyOn(router, "navigate");
|
|
74
|
+
|
|
75
|
+
const notPrevented = fireEvent.click(
|
|
76
|
+
screen.getByRole("link", { name: "Overview" }),
|
|
77
|
+
{ [modifier]: true },
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(notPrevented).toBe(true);
|
|
81
|
+
expect(navigate).not.toHaveBeenCalled();
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
it("with the middle button is left to the browser", () => {
|
|
86
|
+
const { router } = renderWithProviders(
|
|
87
|
+
<RouterLink href="/dashboard">Overview</RouterLink>,
|
|
88
|
+
);
|
|
89
|
+
const navigate = vi.spyOn(router, "navigate");
|
|
90
|
+
|
|
91
|
+
fireEvent.click(screen.getByRole("link", { name: "Overview" }), {
|
|
92
|
+
button: 1,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
expect(navigate).not.toHaveBeenCalled();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { LinkComponentProps } from "@canonical/react-ds-global";
|
|
2
|
+
import { useRouter } from "@canonical/router-react";
|
|
3
|
+
import type { MouseEvent, ReactElement } from "react";
|
|
4
|
+
|
|
5
|
+
function hasModifierKey(event: MouseEvent<HTMLAnchorElement>): boolean {
|
|
6
|
+
return event.altKey || event.ctrlKey || event.metaKey || event.shiftKey;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The link component handed to design-system components that render their
|
|
11
|
+
* own links (`SideNavigation`, `Breadcrumbs`) through a `LinkComponent` prop.
|
|
12
|
+
*
|
|
13
|
+
* Those components pass a plain `href`, while the router navigates by route
|
|
14
|
+
* name, so the URL is matched back to a route on click: a URL this app routes
|
|
15
|
+
* is navigated client-side, and anything else — an external URL, a modified
|
|
16
|
+
* or middle click — is left to the browser. `aria-current` is forwarded as
|
|
17
|
+
* given, because the design-system component owns the active state.
|
|
18
|
+
*
|
|
19
|
+
* `import { RouterLink } from "#lib/index.js";`
|
|
20
|
+
*/
|
|
21
|
+
export default function RouterLink({
|
|
22
|
+
href,
|
|
23
|
+
className,
|
|
24
|
+
children,
|
|
25
|
+
"aria-current": ariaCurrent,
|
|
26
|
+
}: LinkComponentProps): ReactElement {
|
|
27
|
+
const router = useRouter();
|
|
28
|
+
|
|
29
|
+
function handleClick(event: MouseEvent<HTMLAnchorElement>) {
|
|
30
|
+
if (
|
|
31
|
+
href === undefined ||
|
|
32
|
+
event.defaultPrevented ||
|
|
33
|
+
event.button !== 0 ||
|
|
34
|
+
hasModifierKey(event)
|
|
35
|
+
) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const match = router.match(href);
|
|
40
|
+
|
|
41
|
+
if (match?.kind !== "route") {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
// The match carries a name and arguments the router produced itself, so
|
|
47
|
+
// they satisfy `navigate` by construction; the registered route map's
|
|
48
|
+
// per-name argument types cannot be recovered from a runtime string.
|
|
49
|
+
router.navigate(
|
|
50
|
+
match.name as never,
|
|
51
|
+
{ params: match.params, search: match.search } as never,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<a
|
|
57
|
+
aria-current={ariaCurrent}
|
|
58
|
+
className={className}
|
|
59
|
+
href={href}
|
|
60
|
+
onClick={handleClick}
|
|
61
|
+
>
|
|
62
|
+
{children}
|
|
63
|
+
</a>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./RouterLink.js";
|
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
<% if (intl) { -%>
|
|
2
2
|
import { useTranslation } from "@canonical/i18n-react";
|
|
3
3
|
<% } -%>
|
|
4
|
+
<% if (forms) { -%>
|
|
5
|
+
import { SelectInput } from "@canonical/react-ds-global-form";
|
|
6
|
+
<% } -%>
|
|
4
7
|
import { usePreferredTheme } from "@canonical/react-hooks";
|
|
5
8
|
import type { ChangeEvent, ReactElement } from "react";
|
|
6
9
|
|
|
10
|
+
<% if (forms) { -%>
|
|
11
|
+
/**
|
|
12
|
+
* Colour-scheme picker driving `usePreferredTheme`. Built on the form
|
|
13
|
+
* package's standalone `SelectInput`, so it wears the design system's input
|
|
14
|
+
* chrome.
|
|
15
|
+
*/
|
|
16
|
+
<% } else { -%>
|
|
17
|
+
/** Colour-scheme picker driving `usePreferredTheme`. */
|
|
18
|
+
<% } -%>
|
|
7
19
|
export default function ThemeSelector(): ReactElement {
|
|
8
20
|
const { value, source, set, reset } = usePreferredTheme();
|
|
9
21
|
<% if (intl) { -%>
|
|
@@ -23,24 +35,27 @@ export default function ThemeSelector(): ReactElement {
|
|
|
23
35
|
const selectValue = source === "system" ? "system" : value;
|
|
24
36
|
|
|
25
37
|
return (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
aria-label
|
|
38
|
+
<% if (forms) { -%>
|
|
39
|
+
<SelectInput
|
|
40
|
+
aria-label=<%- attr("theme.label", "Color theme") %>
|
|
41
|
+
onChange={handleChange}
|
|
42
|
+
options={[
|
|
43
|
+
{ value: "system", label: <%- str("theme.system", "System") %> },
|
|
44
|
+
{ value: "light", label: <%- str("theme.light", "Light") %> },
|
|
45
|
+
{ value: "dark", label: <%- str("theme.dark", "Dark") %> },
|
|
46
|
+
]}
|
|
47
|
+
value={selectValue}
|
|
48
|
+
/>
|
|
29
49
|
<% } else { -%>
|
|
30
|
-
|
|
31
|
-
|
|
50
|
+
<select
|
|
51
|
+
aria-label=<%- attr("theme.label", "Color theme") %>
|
|
32
52
|
onChange={handleChange}
|
|
33
53
|
value={selectValue}
|
|
34
54
|
>
|
|
35
|
-
|
|
36
|
-
<option value="
|
|
37
|
-
<option value="
|
|
38
|
-
<option value="dark">{t("theme.dark")}</option>
|
|
39
|
-
<% } else { -%>
|
|
40
|
-
<option value="system">System</option>
|
|
41
|
-
<option value="light">Light</option>
|
|
42
|
-
<option value="dark">Dark</option>
|
|
43
|
-
<% } -%>
|
|
55
|
+
<option value="system"><%- msg("theme.system", "System") %></option>
|
|
56
|
+
<option value="light"><%- msg("theme.light", "Light") %></option>
|
|
57
|
+
<option value="dark"><%- msg("theme.dark", "Dark") %></option>
|
|
44
58
|
</select>
|
|
59
|
+
<% } -%>
|
|
45
60
|
);
|
|
46
61
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
2
|
+
import { expect, within } from "storybook/test";
|
|
3
|
+
import withRouter from "../../../.storybook/decorators/withRouter.js";
|
|
4
|
+
import { stubRoutes } from "../../testing/fixtures.js";
|
|
5
|
+
import Component from "./TopBar.js";
|
|
6
|
+
|
|
7
|
+
const meta = {
|
|
8
|
+
title: "common/TopBar",
|
|
9
|
+
component: Component,
|
|
10
|
+
decorators: [withRouter({ routes: stubRoutes })],
|
|
11
|
+
parameters: { layout: "fullscreen" },
|
|
12
|
+
} satisfies Meta<typeof Component>;
|
|
13
|
+
|
|
14
|
+
export default meta;
|
|
15
|
+
|
|
16
|
+
type Story = StoryObj<typeof meta>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The bar every page opens with: the app's name, the demo navigation, and the
|
|
20
|
+
* theme<% if (intl) { %> and language<% } %> selector<% if (intl) { %>s<% } %> at the end.
|
|
21
|
+
*/
|
|
22
|
+
export const Default: Story = {
|
|
23
|
+
play: async ({ canvasElement }) => {
|
|
24
|
+
const canvas = within(canvasElement);
|
|
25
|
+
|
|
26
|
+
await expect(canvas.getByRole("banner")).toBeVisible();
|
|
27
|
+
await expect(canvas.getAllByRole("combobox")).toHaveLength(<%= intl ? 2 : 1 %>);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { screen, within } from "@testing-library/react";
|
|
2
|
+
<% if (intl) { -%>
|
|
3
|
+
import { afterEach, beforeAll, describe, expect, it } from "vitest";
|
|
4
|
+
<% } else { -%>
|
|
5
|
+
import { beforeAll, describe, expect, it } from "vitest";
|
|
6
|
+
<% } -%>
|
|
7
|
+
import { installMatchMedia } from "../../testing/browserStubs.js";
|
|
8
|
+
import renderWithProviders from "../../testing/renderWithProviders.js";
|
|
9
|
+
import TopBar from "./TopBar.js";
|
|
10
|
+
|
|
11
|
+
const APP_NAME = "<%= name %>";
|
|
12
|
+
|
|
13
|
+
beforeAll(() => {
|
|
14
|
+
installMatchMedia();
|
|
15
|
+
});
|
|
16
|
+
<% if (intl) { -%>
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
// The locale selector persists to a cookie and reflects <html lang dir>.
|
|
20
|
+
// biome-ignore lint/suspicious/noDocumentCookie: test cleanup
|
|
21
|
+
document.cookie = "locale=; max-age=0; path=/";
|
|
22
|
+
document.documentElement.removeAttribute("lang");
|
|
23
|
+
document.documentElement.removeAttribute("dir");
|
|
24
|
+
});
|
|
25
|
+
<% } -%>
|
|
26
|
+
|
|
27
|
+
describe("TopBar", () => {
|
|
28
|
+
it("renders the app's name as a link home", () => {
|
|
29
|
+
renderWithProviders(<TopBar />);
|
|
30
|
+
|
|
31
|
+
expect(screen.getByRole("link", { name: APP_NAME })).toHaveAttribute(
|
|
32
|
+
"href",
|
|
33
|
+
"/",
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
<% if (appShell) { -%>
|
|
38
|
+
it("holds the demo navigation, with a link to the dashboard", () => {
|
|
39
|
+
renderWithProviders(<TopBar />);
|
|
40
|
+
|
|
41
|
+
const navigation = screen.getByRole("navigation", { name: "Main" });
|
|
42
|
+
|
|
43
|
+
expect(
|
|
44
|
+
within(navigation).getByRole("link", { name: "Dashboard" }),
|
|
45
|
+
).toHaveAttribute("href", "/dashboard");
|
|
46
|
+
});
|
|
47
|
+
<% } else { -%>
|
|
48
|
+
it("holds the demo navigation", () => {
|
|
49
|
+
renderWithProviders(<TopBar />);
|
|
50
|
+
|
|
51
|
+
const navigation = screen.getByRole("navigation", { name: "Main" });
|
|
52
|
+
|
|
53
|
+
expect(
|
|
54
|
+
within(navigation).getByRole("link", { name: "Home" }),
|
|
55
|
+
).toHaveAttribute("href", "/");
|
|
56
|
+
});
|
|
57
|
+
<% } -%>
|
|
58
|
+
<% if (forms) { -%>
|
|
59
|
+
|
|
60
|
+
it("holds the theme selector, with the input chrome", () => {
|
|
61
|
+
renderWithProviders(<TopBar />);
|
|
62
|
+
|
|
63
|
+
expect(screen.getByRole("combobox", { name: "Color theme" })).toHaveClass(
|
|
64
|
+
"ds",
|
|
65
|
+
"input",
|
|
66
|
+
"select",
|
|
67
|
+
);
|
|
68
|
+
<% if (intl) { -%>
|
|
69
|
+
expect(screen.getByRole("combobox", { name: "Language" })).toHaveClass(
|
|
70
|
+
"ds",
|
|
71
|
+
"input",
|
|
72
|
+
"select",
|
|
73
|
+
);
|
|
74
|
+
<% } -%>
|
|
75
|
+
});
|
|
76
|
+
<% } else { -%>
|
|
77
|
+
|
|
78
|
+
it("holds the theme selector", () => {
|
|
79
|
+
renderWithProviders(<TopBar />);
|
|
80
|
+
|
|
81
|
+
expect(
|
|
82
|
+
screen.getByRole("combobox", { name: "Color theme" }),
|
|
83
|
+
).toBeInTheDocument();
|
|
84
|
+
<% if (intl) { -%>
|
|
85
|
+
expect(
|
|
86
|
+
screen.getByRole("combobox", { name: "Language" }),
|
|
87
|
+
).toBeInTheDocument();
|
|
88
|
+
<% } -%>
|
|
89
|
+
});
|
|
90
|
+
<% } -%>
|
|
91
|
+
<% if (intl) { -%>
|
|
92
|
+
|
|
93
|
+
it("labels each language in its own language", () => {
|
|
94
|
+
renderWithProviders(<TopBar />);
|
|
95
|
+
|
|
96
|
+
expect(screen.getByRole("option", { name: "français" })).toHaveAttribute(
|
|
97
|
+
"lang",
|
|
98
|
+
"fr",
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
<% } -%>
|
|
102
|
+
|
|
103
|
+
it("merges a consumer class with its own on the header", () => {
|
|
104
|
+
renderWithProviders(<TopBar className="custom" />);
|
|
105
|
+
|
|
106
|
+
expect(screen.getByRole("banner")).toHaveClass(
|
|
107
|
+
"ds",
|
|
108
|
+
"top-bar",
|
|
109
|
+
"surface",
|
|
110
|
+
"custom",
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
<% if (intl) { -%>
|
|
2
|
+
import { useTranslation } from "@canonical/i18n-react";
|
|
3
|
+
<% } -%>
|
|
4
|
+
import { Link } from "@canonical/router-react";
|
|
5
|
+
import type { ReactElement } from "react";
|
|
6
|
+
import Navigation from "../Navigation/index.js";
|
|
7
|
+
import type { TopBarProps } from "./types.js";
|
|
8
|
+
import "./styles.css";
|
|
9
|
+
|
|
10
|
+
const componentCssClassName = "ds top-bar";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The bar across the top of every page: the application's name, then the
|
|
14
|
+
* demo navigation — one link per area of the scaffold, followed by the
|
|
15
|
+
* theme selector<% if (intl) { %> and the language selector<% } %>. It is scaffolding for
|
|
16
|
+
* exploring what the scaffold ships; an application replaces the links with
|
|
17
|
+
* its own.
|
|
18
|
+
*
|
|
19
|
+
* It sits above every page layout. It is a `.surface`, so the selectors read
|
|
20
|
+
* the surface channels the design system's inputs expect.
|
|
21
|
+
*
|
|
22
|
+
* `import { TopBar } from "#lib/index.js";`
|
|
23
|
+
*/
|
|
24
|
+
export default function TopBar({
|
|
25
|
+
className,
|
|
26
|
+
...props
|
|
27
|
+
}: TopBarProps): ReactElement {
|
|
28
|
+
<% if (intl) { -%>
|
|
29
|
+
const { t } = useTranslation();
|
|
30
|
+
|
|
31
|
+
<% } -%>
|
|
32
|
+
return (
|
|
33
|
+
<header
|
|
34
|
+
{...props}
|
|
35
|
+
className={[componentCssClassName, "surface", className]
|
|
36
|
+
.filter(Boolean)
|
|
37
|
+
.join(" ")}
|
|
38
|
+
>
|
|
39
|
+
<Link className="name" to="home">
|
|
40
|
+
<%- msg("app.name", name) %>
|
|
41
|
+
</Link>
|
|
42
|
+
<Navigation />
|
|
43
|
+
</header>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
@layer app {
|
|
2
|
+
.ds.top-bar {
|
|
3
|
+
/* Exactly the height the page reserves for it, border included, with the
|
|
4
|
+
content centred in it; on a phone it grows with its wrapped rows. */
|
|
5
|
+
box-sizing: border-box;
|
|
6
|
+
block-size: var(--top-bar-block-size, auto);
|
|
7
|
+
display: flex;
|
|
8
|
+
flex-wrap: wrap;
|
|
9
|
+
align-items: center;
|
|
10
|
+
gap: var(--container-gap-default);
|
|
11
|
+
padding-inline: var(--grid-margin);
|
|
12
|
+
border-block-end: var(--dimension-stroke-thickness-medium) solid
|
|
13
|
+
var(--color-border);
|
|
14
|
+
|
|
15
|
+
& > .name {
|
|
16
|
+
font-weight: var(--typography-heading-4-font-weight);
|
|
17
|
+
color: inherit;
|
|
18
|
+
text-decoration: none;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
& nav {
|
|
22
|
+
display: flex;
|
|
23
|
+
flex: 1;
|
|
24
|
+
flex-wrap: wrap;
|
|
25
|
+
align-items: center;
|
|
26
|
+
gap: var(--container-gap-default);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* The theme and language selectors sit at the end of the bar at their
|
|
30
|
+
own width; the input chrome otherwise fills its container, as a form
|
|
31
|
+
field does. */
|
|
32
|
+
& nav > .ds.input {
|
|
33
|
+
width: auto;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
& nav > .ds.input:first-of-type {
|
|
37
|
+
margin-inline-start: auto;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/* On a phone the name takes its own row above the links. */
|
|
41
|
+
@media (width < 620px) {
|
|
42
|
+
block-size: auto;
|
|
43
|
+
padding-block: var(--space-100);
|
|
44
|
+
flex-direction: column;
|
|
45
|
+
align-items: stretch;
|
|
46
|
+
gap: var(--space-100);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ComponentProps } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Props for `TopBar`. The bar owns its content — the application's name and
|
|
5
|
+
* the demo navigation — so it takes only the native props of its root
|
|
6
|
+
* `<header>`, which reach the element unchanged.
|
|
7
|
+
*/
|
|
8
|
+
export type TopBarProps = Omit<ComponentProps<"header">, "children">;
|
|
@@ -1,10 +1,20 @@
|
|
|
1
|
+
<% if (appShell) { -%>
|
|
2
|
+
export { default as ApplicationShell } from "./ApplicationShell/index.js";
|
|
3
|
+
export { default as CanonicalLogo } from "./CanonicalLogo/index.js";
|
|
4
|
+
<% } -%>
|
|
1
5
|
<% if (relay && !spa) { -%>
|
|
2
6
|
export { default as ClientOnly } from "./ClientOnly/index.js";
|
|
3
7
|
<% } -%>
|
|
8
|
+
export { default as CodeMessage } from "./CodeMessage/index.js";
|
|
4
9
|
export { ExampleComponent } from "./ExampleComponent/index.js";
|
|
5
10
|
export { default as LazyComponent } from "./LazyComponent/index.js";
|
|
11
|
+
export { default as LoadingMessage } from "./LoadingMessage/index.js";
|
|
6
12
|
<% if (intl) { -%>
|
|
7
13
|
export { default as LocaleSelector } from "./LocaleSelector/index.js";
|
|
8
14
|
<% } -%>
|
|
9
15
|
export { default as Navigation } from "./Navigation/index.js";
|
|
16
|
+
<% if (appShell) { -%>
|
|
17
|
+
export { default as RouterLink } from "./RouterLink/index.js";
|
|
18
|
+
<% } -%>
|
|
10
19
|
export { default as ThemeSelector } from "./ThemeSelector/index.js";
|
|
20
|
+
export { default as TopBar } from "./TopBar/index.js";
|