@canonical/summon-component 0.14.0 → 0.15.0-experimental.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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [0.15.0-experimental.0](https://github.com/canonical/pragma/compare/v0.14.0...v0.15.0-experimental.0) (2026-02-17)
7
+
8
+
9
+ ### Features
10
+
11
+ * **svelte-generator:** update Svelte component templates ([#422](https://github.com/canonical/pragma/issues/422)) ([f1fb13f](https://github.com/canonical/pragma/commit/f1fb13fa08463b844e611ae5cd0f94a06b13ff30))
12
+
13
+
14
+
15
+
16
+
6
17
  # [0.14.0](https://github.com/canonical/pragma/compare/v0.13.0...v0.14.0) (2026-02-16)
7
18
 
8
19
  **Note:** Version bump only for package @canonical/summon-component
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@canonical/summon-component",
3
3
  "description": "Component generators for Summon - React and Svelte component scaffolding",
4
- "version": "0.14.0",
4
+ "version": "0.15.0-experimental.0",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
7
7
  "author": {
@@ -41,5 +41,5 @@
41
41
  "peerDependencies": {
42
42
  "@canonical/summon": "^0.1.0"
43
43
  },
44
- "gitHead": "3bcc701893d91c5b7d36454e0c00fb3b490f55cc"
44
+ "gitHead": "92f45eb133ed113718640012a6c1095f0e2c0d8b"
45
45
  }
@@ -264,6 +264,7 @@ describe("component/svelte generator", () => {
264
264
  expect(paths).toContain(
265
265
  "src/lib/components/Button/Button.stories.svelte",
266
266
  );
267
+ expect(paths).toContain("src/lib/components/Button/styles.css");
267
268
  });
268
269
 
269
270
  it("creates typescript stories when useTsStories is true", () => {
@@ -306,5 +307,26 @@ describe("component/svelte generator", () => {
306
307
  expect(content).toContain("$props()");
307
308
  expect(content).toContain("@render children");
308
309
  });
310
+
311
+ it("imports styles.css when withStyles is enabled", () => {
312
+ const task = generator.generate({
313
+ componentPath: "src/lib/components/Banner",
314
+ withStyles: true,
315
+ withStories: false,
316
+ useTsStories: false,
317
+ withSsrTests: false,
318
+ });
319
+
320
+ const result = dryRunWithTemplates(task);
321
+ const mainFile = result.effects.find(
322
+ (e) =>
323
+ e._tag === "WriteFile" &&
324
+ (e as { path: string }).path.endsWith("Banner.svelte"),
325
+ );
326
+
327
+ const content = (mainFile as { content: string }).content;
328
+ expect(content).toContain('import "./styles.css";');
329
+ expect(content).not.toContain("<style>");
330
+ });
309
331
  });
310
332
  });
@@ -9,6 +9,7 @@
9
9
  * - `ComponentName.ssr.test.ts` - SSR tests (optional)
10
10
  * - `ComponentName.stories.svelte` - Svelte CSF stories (optional)
11
11
  * - `ComponentName.stories.ts` - TypeScript stories (optional, alternative to Svelte CSF)
12
+ * - `styles.css` - CSS styles (optional)
12
13
  *
13
14
  * @example
14
15
  * ```bash
@@ -57,6 +58,7 @@ const svelteTemplates = {
57
58
  ssrTest: path.join(templatesDir, "svelte", "ssr.test.ts.ejs"),
58
59
  storiesSvelte: path.join(templatesDir, "svelte", "stories.svelte.ejs"),
59
60
  storiesTs: path.join(templatesDir, "svelte", "stories.ts.ejs"),
61
+ styles: path.join(templatesDir, "shared", "styles.css.ejs"),
60
62
  };
61
63
 
62
64
  // =============================================================================
@@ -75,16 +77,16 @@ export const generator = {
75
77
  meta: {
76
78
  name: "component/svelte",
77
79
  description:
78
- "Generate a Svelte 5 component with TypeScript, tests, and stories",
80
+ "Generate a Svelte 5 component with TypeScript, tests, stories, and styles",
79
81
  version: "0.1.0",
80
- help: `Generate a Svelte 5 component with TypeScript, tests, and stories.
82
+ help: `Generate a Svelte 5 component with TypeScript, tests, stories, and styles.
81
83
 
82
84
  FEATURES:
83
85
  - Svelte 5 with runes ($props)
84
86
  - TypeScript types and props interface
85
87
  - Unit tests with @testing-library/svelte
86
88
  - Storybook integration (Svelte CSF or TypeScript)
87
- - Optional scoped styles
89
+ - CSS styles (optional)
88
90
  - SSR tests (optional)
89
91
  - Auto-export from parent index.ts
90
92
 
@@ -187,6 +189,16 @@ For example, 'src/lib/components/Button' creates a 'Button' component.`,
187
189
  }),
188
190
  ),
189
191
 
192
+ when(answers.withStyles, debug("Creating styles file")),
193
+ when(
194
+ answers.withStyles,
195
+ template({
196
+ source: svelteTemplates.styles,
197
+ dest: path.join(componentDir, "styles.css"),
198
+ vars: ctx,
199
+ }),
200
+ ),
201
+
190
202
  debug("Updating parent index.ts with export"),
191
203
  appendExportToParentIndex(parentDir, componentName),
192
204
 
@@ -2,24 +2,22 @@
2
2
 
3
3
  <script lang="ts">
4
4
  import type { <%= name %>Props } from "./types";
5
+ <% if (withStyles) { -%>
6
+ import "./styles.css";
7
+ <% } -%>
8
+
9
+ const componentCssClassName = "ds <%= kebabName %>";
5
10
 
6
11
  let {
7
- class: className = "",
12
+ class: className,
8
13
  children,
9
- ...props
14
+ ...rest
10
15
  }: <%= name %>Props = $props();
11
16
  </script>
12
17
 
13
18
  <div
14
- class="<%= kebabName %>{className ? ` ${className}` : ''}"
15
- {...props}
19
+ class={[componentCssClassName, className]}
20
+ {...rest}
16
21
  >
17
22
  {@render children?.()}
18
23
  </div>
19
- <% if (withStyles) { %>
20
- <style>
21
- .<%= kebabName %> {
22
- /* Component styles */
23
- }
24
- </style>
25
- <% } -%>
@@ -1,12 +1,86 @@
1
1
  // Generated by <%= generatorName %> v<%= version %>
2
2
 
3
+ import { render } from "@canonical/svelte-ssr-test";
4
+ import type { RenderResult } from "@canonical/svelte-ssr-test";
5
+ import { createRawSnippet } from "svelte";
6
+ import type { ComponentProps } from "svelte";
3
7
  import { describe, expect, it } from "vitest";
4
- import { render } from "svelte/server";
5
- import <%= name %> from "./<%= name %>.svelte";
8
+ import Component from "./<%= name %>.svelte";
6
9
 
7
10
  describe("<%= name %> SSR", () => {
8
- it("renders on server without errors", () => {
9
- const { body } = render(<%= name %>);
10
- expect(body).toContain("<%= kebabName %>");
11
+ const baseProps = {
12
+ children: createRawSnippet(() => ({
13
+ render: () => `<span><%= name %></span>`,
14
+ })),
15
+ } satisfies ComponentProps<typeof Component>;
16
+
17
+ describe("basics", () => {
18
+ it("doesn't throw", () => {
19
+ expect(() => {
20
+ render(Component, { props: { ...baseProps } });
21
+ }).not.toThrow();
22
+ });
23
+
24
+ it("renders", () => {
25
+ const page = render(Component, { props: { ...baseProps } });
26
+ expect(componentLocator(page)).toBeInstanceOf(page.window.HTMLDivElement);
27
+ });
28
+ });
29
+
30
+ describe("attributes", () => {
31
+ it.each([
32
+ ["id", "test-id"],
33
+ ["aria-label", "test-aria-label"],
34
+ ])("applies %s", (attribute, expected) => {
35
+ const page = render(Component, {
36
+ props: { ...baseProps, [attribute]: expected },
37
+ });
38
+ expect(componentLocator(page).getAttribute(attribute)).toBe(expected);
39
+ });
40
+
41
+ it("applies classes", () => {
42
+ const page = render(Component, {
43
+ props: { class: "test-class", ...baseProps },
44
+ });
45
+ expect(componentLocator(page).classList).toContain("test-class");
46
+ <% if (withStyles) { _%>
47
+ expect(componentLocator(page).classList).toContain("ds");
48
+ expect(componentLocator(page).classList).toContain("<%= kebabName %>");<% } %>
49
+ });
50
+
51
+ it("applies style", () => {
52
+ const page = render(Component, {
53
+ props: { style: "color: orange;", ...baseProps },
54
+ });
55
+ expect(componentLocator(page).style.color).toBe("orange");
56
+ });
11
57
  });
12
58
  });
59
+
60
+ // Note: Prefer role/semantics-oriented ways of selecting elements (e.g., by role, label, etc.) not only for component roots but for all elements to enhance accessibility and maintainability.
61
+ // To select the component's root element, use one of the available [Queries](https://testing-library.com/docs/queries/about/#priority).
62
+ function componentLocator(page: RenderResult): HTMLElement {
63
+ // 1. SEMANTIC ROLES (PREFERRED) - These the most closely resemble how assistive technologies interact with your component
64
+ // return page.getByRole("link", { name: "Learn more" });
65
+
66
+ // 2. LABELS - To test the association between labels and form controls
67
+ // return page.getByLabelText("Email address");
68
+
69
+ // 3. PLACEHOLDER TEXT - Useful for input fields with placeholders
70
+ // return page.getByPlaceholderText("Enter your email");
71
+
72
+ // 4. TEXT CONTENT - Good for testing the presence of unique texts and selecting non-interactive elements
73
+ // return page.getByText("<%= name %>");
74
+
75
+ // 5. DISPLAY VALUE - For selecting form elements by their current value
76
+ // return page.getByDisplayValue("test value");
77
+
78
+ // 6. ALT TEXT - For images and visual elements
79
+ // return page.getByAltText("Company logo");
80
+
81
+ // 7. TITLE ATTRIBUTE - For elements with title tooltips
82
+ // return page.getByTitle("Close dialog");
83
+
84
+ // 8. TEST IDS (LAST RESORT) - When semantic options aren't available
85
+ // return page.getByTestId("<%= kebabName %>");
86
+ }
@@ -12,9 +12,16 @@
12
12
  </script>
13
13
 
14
14
  <Story name="Default">
15
- <<%= name %>>Hello, <%= name %>!</<%= name %>>
15
+ Hello, <%= name %>!
16
16
  </Story>
17
17
 
18
- <Story name="WithCustomClass">
19
- <<%= name %> class="custom-class">Custom styled</<%= name %>>
18
+ <Story
19
+ name="WithCustomClass"
20
+ args={{
21
+ class: "custom-class",
22
+ id: "custom-id",
23
+ style: "background-color: tomato;",
24
+ }}
25
+ >
26
+ Custom styled
20
27
  </Story>
@@ -1,18 +1,80 @@
1
1
  // Generated by <%= generatorName %> v<%= version %>
2
2
 
3
+ import type { Locator } from "@vitest/browser/context";
4
+ import { createRawSnippet } from "svelte";
5
+ import type { ComponentProps } from "svelte";
3
6
  import { describe, expect, it } from "vitest";
4
- import { render } from "@testing-library/svelte";
5
- import <%= name %> from "./<%= name %>.svelte";
7
+ import { render } from "vitest-browser-svelte";
8
+ import type { RenderResult } from "vitest-browser-svelte";
9
+ import Component from "./<%= name %>.svelte";
6
10
 
7
- describe("<%= name %>", () => {
8
- it("renders", () => {
9
- render(<%= name %>);
10
- // Add assertions based on your component
11
+ describe("<%= name %> component", () => {
12
+ const baseProps = {
13
+ children: createRawSnippet(() => ({
14
+ render: () => `<span><%= name %></span>`,
15
+ })),
16
+ } satisfies ComponentProps<typeof Component>;
17
+
18
+ it("renders", async () => {
19
+ const page = render(Component, { ...baseProps });
20
+ await expect.element(componentLocator(page)).toBeInTheDocument();
21
+ await expect.element(page.getByText("<%= name %>")).toBeVisible();
11
22
  });
12
23
 
13
- it("applies custom class", () => {
14
- render(<%= name %>, { props: { class: "custom-class" } });
15
- const element = document.querySelector(".<%= kebabName %>");
16
- expect(element).toHaveClass("custom-class");
24
+ describe("attributes", () => {
25
+ it.each([
26
+ ["id", "test-id"],
27
+ ["aria-label", "test-aria-label"],
28
+ ])("applies %s", async (attribute, expected) => {
29
+ const page = render(Component, { ...baseProps, [attribute]: expected });
30
+ await expect
31
+ .element(componentLocator(page))
32
+ .toHaveAttribute(attribute, expected);
33
+ });
34
+
35
+ it("applies classes", async () => {
36
+ const page = render(Component, { ...baseProps, class: "test-class" });
37
+ await expect.element(componentLocator(page)).toHaveClass("test-class");
38
+ <% if (withStyles) { _%>
39
+ await expect.element(componentLocator(page)).toHaveClass("ds");
40
+ await expect
41
+ .element(componentLocator(page))
42
+ .toHaveClass("<%= kebabName %>");<% } %>
43
+ });
44
+
45
+ it("applies style", async () => {
46
+ const page = render(Component, {
47
+ ...baseProps,
48
+ style: "color: orange;",
49
+ });
50
+ await expect
51
+ .element(componentLocator(page))
52
+ .toHaveStyle({ color: "orange" });
53
+ });
17
54
  });
18
55
  });
56
+
57
+ // Note: Prefer role/semantics-oriented ways of selecting elements (e.g., by role, label, etc.) not only for component roots but for all elements to enhance accessibility and maintainability.
58
+ // To select the component's root element, use one of the available [Locators](https://vitest.dev/guide/browser/locators.html).
59
+ function componentLocator(page: RenderResult<typeof Component>): Locator {
60
+ // 1. SEMANTIC ROLES (PREFERRED) - These the most closely resemble how assistive technologies interact with your component
61
+ // return page.getByRole("link", { name: "Learn more" });
62
+
63
+ // 2. LABELS - To test the association between labels and form controls
64
+ // return page.getByLabel("Email address");
65
+
66
+ // 3. PLACEHOLDER TEXT - Useful for input fields with placeholders
67
+ // return page.getByPlaceholder("Enter your email");
68
+
69
+ // 4. TEXT CONTENT - Good for testing the presence of unique texts and selecting non-interactive elements
70
+ // return page.getByText("<%= name %>");
71
+
72
+ // 5. ALT TEXT - For images and visual elements
73
+ // return page.getByAltText("Company logo");
74
+
75
+ // 6. TITLE ATTRIBUTE - For elements with title tooltips
76
+ // return page.getByTitle("Close dialog");
77
+
78
+ // 7. TEST IDS (LAST RESORT) - When semantic options aren't available
79
+ // return page.getByTestId("<%= kebabName %>");
80
+ }
@@ -1,9 +1,36 @@
1
1
  // Generated by <%= generatorName %> v<%= version %>
2
2
 
3
- import type { Snippet } from "svelte";
4
- import type { HTMLAttributes } from "svelte/elements";
3
+ import type { SvelteHTMLElements } from "svelte/elements";
5
4
 
6
- export interface <%= name %>Props extends HTMLAttributes<HTMLDivElement> {
5
+ /*
6
+ For components that render specific HTML elements with extended prop sets,
7
+ make sure to change `"div"` to the appropriate HTML element type.
8
+ */
9
+ type BaseProps = SvelteHTMLElements["div"];
10
+
11
+ export interface <%= name %>Props extends BaseProps {
7
12
  /** Content to render inside the component */
8
13
  children?: Snippet;
14
+ // Define additional props specific to <%= name %> here
9
15
  }
16
+
17
+ /*
18
+ ...or consider using appropriate interfaces directly exported from `svelte/elements`.
19
+
20
+ For example:
21
+ - `HTMLButtonAttributes` (provides `disabled`, `type`, etc.)
22
+ - `HTMLAnchorAttributes` (provides `href`, `target`, etc.)
23
+ - `HTMLInputAttributes` (provides `type`, `checked`, `value`, etc.)
24
+ - `HTMLFormAttributes` (provides `action`, `method`, etc.)
25
+
26
+ ```typescript
27
+ import type { HTMLButtonAttributes } from "svelte/elements";
28
+
29
+ export interface MyButtonProps extends HTMLButtonAttributes {
30
+ // Your custom props here
31
+ }
32
+ ```
33
+
34
+ See Svelte documentation on typing wrapper components:
35
+ https://svelte.dev/docs/svelte/typescript#Typing-wrapper-components
36
+ */