@canonical/svelte-ds-app-launchpad 0.16.0-experimental.0 → 0.16.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.
Files changed (165) hide show
  1. package/LICENSE +166 -0
  2. package/dist/components/Badge/Badge.ssr.test.d.ts +1 -0
  3. package/dist/components/Badge/Badge.ssr.test.js +113 -0
  4. package/dist/components/Badge/Badge.stories.svelte +60 -0
  5. package/dist/components/Badge/Badge.stories.svelte.d.ts +19 -0
  6. package/dist/components/Badge/Badge.svelte +49 -0
  7. package/dist/components/Badge/Badge.svelte.d.ts +14 -0
  8. package/dist/components/Badge/Badge.svelte.test.d.ts +1 -0
  9. package/dist/components/Badge/Badge.svelte.test.js +120 -0
  10. package/dist/components/Badge/index.d.ts +2 -0
  11. package/dist/components/Badge/index.js +2 -0
  12. package/dist/components/Badge/styles.css +12 -0
  13. package/dist/components/Badge/types.d.ts +15 -0
  14. package/dist/components/Badge/types.js +1 -0
  15. package/dist/components/Button/Button.ssr.test.d.ts +1 -0
  16. package/dist/components/Button/Button.ssr.test.js +88 -0
  17. package/dist/components/Button/Button.stories.svelte +143 -0
  18. package/dist/components/Button/Button.stories.svelte.d.ts +19 -0
  19. package/dist/components/Button/Button.svelte +63 -0
  20. package/dist/components/Button/Button.svelte.d.ts +21 -0
  21. package/dist/components/Button/Button.svelte.test.d.ts +1 -0
  22. package/dist/components/Button/Button.svelte.test.js +150 -0
  23. package/dist/components/Button/common/Content/Content.svelte +30 -0
  24. package/dist/components/Button/common/Content/Content.svelte.d.ts +5 -0
  25. package/dist/components/Button/common/Content/index.d.ts +2 -0
  26. package/dist/components/Button/common/Content/index.js +1 -0
  27. package/dist/components/Button/common/Content/styles.css +60 -0
  28. package/dist/components/Button/common/Content/types.d.ts +8 -0
  29. package/dist/components/Button/common/Content/types.js +1 -0
  30. package/dist/components/Button/index.d.ts +2 -0
  31. package/dist/components/Button/index.js +2 -0
  32. package/dist/components/Button/styles.css +73 -0
  33. package/dist/components/Button/types.d.ts +10 -0
  34. package/dist/components/Button/types.js +1 -0
  35. package/dist/components/Checkbox/Checkbox.ssr.test.d.ts +1 -0
  36. package/dist/components/Checkbox/Checkbox.ssr.test.js +74 -0
  37. package/dist/components/Checkbox/Checkbox.stories.svelte +105 -0
  38. package/dist/components/Checkbox/Checkbox.stories.svelte.d.ts +19 -0
  39. package/dist/components/Checkbox/Checkbox.svelte +74 -0
  40. package/dist/components/Checkbox/Checkbox.svelte.d.ts +60 -0
  41. package/dist/components/Checkbox/Checkbox.svelte.test.d.ts +1 -0
  42. package/dist/components/Checkbox/Checkbox.svelte.test.js +116 -0
  43. package/dist/components/Checkbox/index.d.ts +2 -0
  44. package/dist/components/Checkbox/index.js +1 -0
  45. package/dist/components/Checkbox/styles.css +63 -0
  46. package/dist/components/Checkbox/types.d.ts +16 -0
  47. package/dist/components/Checkbox/types.js +1 -0
  48. package/dist/components/Chip/Chip.stories.svelte +9 -11
  49. package/dist/components/NumberInput/NumberInput.ssr.test.d.ts +1 -0
  50. package/dist/components/NumberInput/NumberInput.ssr.test.js +84 -0
  51. package/dist/components/NumberInput/NumberInput.stories.svelte +67 -0
  52. package/dist/components/NumberInput/NumberInput.stories.svelte.d.ts +4 -0
  53. package/dist/components/NumberInput/NumberInput.svelte +36 -0
  54. package/dist/components/NumberInput/NumberInput.svelte.d.ts +18 -0
  55. package/dist/components/NumberInput/NumberInput.svelte.test.d.ts +1 -0
  56. package/dist/components/NumberInput/NumberInput.svelte.test.js +96 -0
  57. package/dist/components/NumberInput/index.d.ts +2 -0
  58. package/dist/components/NumberInput/index.js +2 -0
  59. package/dist/components/NumberInput/styles.css +26 -0
  60. package/dist/components/NumberInput/types.d.ts +5 -0
  61. package/dist/components/NumberInput/types.js +1 -0
  62. package/dist/components/Radio/Radio.ssr.test.d.ts +1 -0
  63. package/dist/components/Radio/Radio.ssr.test.js +75 -0
  64. package/dist/components/Radio/Radio.stories.svelte +93 -0
  65. package/dist/components/Radio/Radio.stories.svelte.d.ts +19 -0
  66. package/dist/components/Radio/Radio.svelte +69 -0
  67. package/dist/components/Radio/Radio.svelte.d.ts +64 -0
  68. package/dist/components/Radio/Radio.svelte.test.d.ts +1 -0
  69. package/dist/components/Radio/Radio.svelte.test.js +101 -0
  70. package/dist/components/Radio/index.d.ts +2 -0
  71. package/dist/components/Radio/index.js +1 -0
  72. package/dist/components/Radio/styles.css +42 -0
  73. package/dist/components/Radio/types.d.ts +16 -0
  74. package/dist/components/Radio/types.js +1 -0
  75. package/dist/components/Spinner/Spinner.ssr.test.d.ts +1 -0
  76. package/dist/components/Spinner/Spinner.ssr.test.js +29 -0
  77. package/dist/components/Spinner/Spinner.stories.svelte +16 -0
  78. package/dist/components/Spinner/Spinner.stories.svelte.d.ts +19 -0
  79. package/dist/components/Spinner/Spinner.svelte +25 -0
  80. package/dist/components/Spinner/Spinner.svelte.d.ts +13 -0
  81. package/dist/components/Spinner/Spinner.svelte.test.d.ts +1 -0
  82. package/dist/components/Spinner/Spinner.svelte.test.js +29 -0
  83. package/dist/components/Spinner/index.d.ts +2 -0
  84. package/dist/components/Spinner/index.js +2 -0
  85. package/dist/components/Spinner/styles.css +14 -0
  86. package/dist/components/Spinner/types.d.ts +2 -0
  87. package/dist/components/Spinner/types.js +1 -0
  88. package/dist/components/Switch/Switch.ssr.test.d.ts +1 -0
  89. package/dist/components/Switch/Switch.ssr.test.js +99 -0
  90. package/dist/components/Switch/Switch.stories.svelte +93 -0
  91. package/dist/components/Switch/Switch.stories.svelte.d.ts +19 -0
  92. package/dist/components/Switch/Switch.svelte +89 -0
  93. package/dist/components/Switch/Switch.svelte.d.ts +65 -0
  94. package/dist/components/Switch/Switch.svelte.test.d.ts +1 -0
  95. package/dist/components/Switch/Switch.svelte.test.js +144 -0
  96. package/dist/components/Switch/index.d.ts +2 -0
  97. package/dist/components/Switch/index.js +1 -0
  98. package/dist/components/Switch/styles.css +41 -0
  99. package/dist/components/Switch/types.d.ts +16 -0
  100. package/dist/components/Switch/types.js +1 -0
  101. package/dist/components/TextInput/TextInput.ssr.test.d.ts +1 -0
  102. package/dist/components/TextInput/TextInput.ssr.test.js +85 -0
  103. package/dist/components/TextInput/TextInput.stories.svelte +96 -0
  104. package/dist/components/TextInput/TextInput.stories.svelte.d.ts +4 -0
  105. package/dist/components/TextInput/TextInput.svelte +21 -0
  106. package/dist/components/TextInput/TextInput.svelte.d.ts +5 -0
  107. package/dist/components/TextInput/TextInput.svelte.test.d.ts +1 -0
  108. package/dist/components/TextInput/TextInput.svelte.test.js +112 -0
  109. package/dist/components/TextInput/index.d.ts +2 -0
  110. package/dist/components/TextInput/index.js +2 -0
  111. package/dist/components/TextInput/styles.css +26 -0
  112. package/dist/components/TextInput/types.d.ts +5 -0
  113. package/dist/components/TextInput/types.js +1 -0
  114. package/dist/components/Textarea/Textarea.ssr.test.d.ts +1 -0
  115. package/dist/components/Textarea/Textarea.ssr.test.js +49 -0
  116. package/dist/components/Textarea/Textarea.stories.svelte +58 -0
  117. package/dist/components/Textarea/Textarea.stories.svelte.d.ts +19 -0
  118. package/dist/components/Textarea/Textarea.svelte +38 -0
  119. package/dist/components/Textarea/Textarea.svelte.d.ts +5 -0
  120. package/dist/components/Textarea/Textarea.svelte.test.d.ts +1 -0
  121. package/dist/components/Textarea/Textarea.svelte.test.js +90 -0
  122. package/dist/components/Textarea/index.d.ts +2 -0
  123. package/dist/components/Textarea/index.js +2 -0
  124. package/dist/components/Textarea/styles.css +62 -0
  125. package/dist/components/Textarea/types.d.ts +23 -0
  126. package/dist/components/Textarea/types.js +1 -0
  127. package/dist/components/Textarea/utils/calculateDynamicRows.d.ts +6 -0
  128. package/dist/components/Textarea/utils/calculateDynamicRows.js +36 -0
  129. package/dist/components/Textarea/utils/calculateDynamicRows.svelte.test.d.ts +1 -0
  130. package/dist/components/Textarea/utils/calculateDynamicRows.svelte.test.js +148 -0
  131. package/dist/components/Textarea/utils/index.d.ts +1 -0
  132. package/dist/components/Textarea/utils/index.js +1 -0
  133. package/dist/components/common/ButtonPrimitive/ButtonPrimitive.ssr.test.d.ts +1 -0
  134. package/dist/components/common/ButtonPrimitive/ButtonPrimitive.ssr.test.js +71 -0
  135. package/dist/components/common/ButtonPrimitive/ButtonPrimitive.svelte +43 -0
  136. package/dist/components/common/ButtonPrimitive/ButtonPrimitive.svelte.d.ts +5 -0
  137. package/dist/components/common/ButtonPrimitive/ButtonPrimitive.svelte.test.d.ts +1 -0
  138. package/dist/components/common/ButtonPrimitive/ButtonPrimitive.svelte.test.js +77 -0
  139. package/dist/components/common/ButtonPrimitive/index.d.ts +2 -0
  140. package/dist/components/common/ButtonPrimitive/index.js +2 -0
  141. package/dist/components/common/ButtonPrimitive/styles.css +47 -0
  142. package/dist/components/common/ButtonPrimitive/types.d.ts +5 -0
  143. package/dist/components/common/ButtonPrimitive/types.js +1 -0
  144. package/dist/components/common/InputPrimitive/InputPrimitive.ssr.test.d.ts +1 -0
  145. package/dist/components/common/InputPrimitive/InputPrimitive.ssr.test.js +95 -0
  146. package/dist/components/common/InputPrimitive/InputPrimitive.svelte +22 -0
  147. package/dist/components/common/InputPrimitive/InputPrimitive.svelte.d.ts +5 -0
  148. package/dist/components/common/InputPrimitive/InputPrimitive.svelte.test.d.ts +1 -0
  149. package/dist/components/common/InputPrimitive/InputPrimitive.svelte.test.js +85 -0
  150. package/dist/components/common/InputPrimitive/index.d.ts +2 -0
  151. package/dist/components/common/InputPrimitive/index.js +2 -0
  152. package/dist/components/common/InputPrimitive/styles.css +69 -0
  153. package/dist/components/common/InputPrimitive/types.d.ts +14 -0
  154. package/dist/components/common/InputPrimitive/types.js +1 -0
  155. package/dist/components/common/index.d.ts +2 -0
  156. package/dist/components/common/index.js +2 -0
  157. package/dist/components/index.d.ts +9 -0
  158. package/dist/components/index.js +9 -0
  159. package/dist/styles/index.css +3 -2
  160. package/dist/useIsMounted.svelte.d.ts +3 -0
  161. package/dist/useIsMounted.svelte.js +12 -0
  162. package/package.json +10 -9
  163. package/dist/components/Chip/__screenshots__/Chip.svelte.test.ts/Chip-component-Initial-Rendering-should-apply-custom-class-and-modifiers-1.png +0 -0
  164. package/dist/components/Chip/__screenshots__/Chip.svelte.test.ts/Chip-component-Initial-Rendering-should-not-render-a-button-when-dismiss-is-provided-1.png +0 -0
  165. package/dist/components/Chip/__screenshots__/Chip.svelte.test.ts/Chip-component-Initial-Rendering-should-not-render-a-button-when-readonly-is-provided-1.png +0 -0
@@ -0,0 +1,88 @@
1
+ import { render } from "@canonical/svelte-ssr-test";
2
+ import { createRawSnippet } from "svelte";
3
+ import { describe, expect, it } from "vitest";
4
+ import Component from "./Button.svelte";
5
+ describe("Button SSR", () => {
6
+ const baseProps = {
7
+ children: createRawSnippet(() => ({
8
+ render: () => `<span>Button</span>`,
9
+ })),
10
+ "data-testid": "button",
11
+ };
12
+ describe("basics", () => {
13
+ it("doesn't throw", () => {
14
+ expect(() => {
15
+ render(Component, { props: { ...baseProps } });
16
+ }).not.toThrow();
17
+ });
18
+ it("renders as a button by default", () => {
19
+ const page = render(Component, { props: { ...baseProps } });
20
+ expect(componentLocator(page)).toBeInstanceOf(page.window.HTMLButtonElement);
21
+ });
22
+ it("renders as an anchor when href is provided", () => {
23
+ const page = render(Component, {
24
+ props: { ...baseProps, href: "https://example.com" },
25
+ });
26
+ const link = page.getByRole("link");
27
+ expect(link).toBeInstanceOf(page.window.HTMLAnchorElement);
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
+ it("applies classes", () => {
41
+ const page = render(Component, {
42
+ props: { class: "test-class", ...baseProps },
43
+ });
44
+ expect(componentLocator(page).classList).toContain("test-class");
45
+ expect(componentLocator(page).classList).toContain("ds");
46
+ expect(componentLocator(page).classList).toContain("button");
47
+ });
48
+ it("applies style", () => {
49
+ const page = render(Component, {
50
+ props: { style: "color: orange;", ...baseProps },
51
+ });
52
+ expect(componentLocator(page).style.color).toBe("orange");
53
+ });
54
+ });
55
+ describe("modifiers", () => {
56
+ it("applies severity class", () => {
57
+ const page = render(Component, {
58
+ props: { ...baseProps, severity: "brand" },
59
+ });
60
+ expect(componentLocator(page).classList).toContain("brand");
61
+ });
62
+ it("applies density class", () => {
63
+ const page = render(Component, {
64
+ props: { ...baseProps, density: "dense" },
65
+ });
66
+ expect(componentLocator(page).classList).toContain("dense");
67
+ });
68
+ });
69
+ describe("disabled state", () => {
70
+ it("disables the button when disabled", () => {
71
+ const page = render(Component, {
72
+ props: { ...baseProps, disabled: true },
73
+ });
74
+ expect(componentLocator(page).hasAttribute("disabled")).toBe(true);
75
+ expect(componentLocator(page).classList).toContain("explicit-disabled");
76
+ });
77
+ it("disables the button when loading", () => {
78
+ const page = render(Component, {
79
+ props: { ...baseProps, loading: true },
80
+ });
81
+ expect(componentLocator(page).hasAttribute("disabled")).toBe(true);
82
+ expect(componentLocator(page).classList).toContain("loading");
83
+ });
84
+ });
85
+ });
86
+ function componentLocator(page) {
87
+ return page.getByTestId("button");
88
+ }
@@ -0,0 +1,143 @@
1
+ <script lang="ts" module>
2
+ import { defineMeta } from "@storybook/addon-svelte-csf";
3
+ import Button from "./Button.svelte";
4
+ import type { ButtonProps } from "./types";
5
+ import { fn } from "storybook/test";
6
+ import { MODIFIER_FAMILIES } from "../../modifier-families";
7
+ import { ArchiveIcon } from "@canonical/svelte-icons";
8
+
9
+ const BUTTON_SEVERITIES = [
10
+ "brand",
11
+ "base",
12
+ ...MODIFIER_FAMILIES.severity,
13
+ ] as const satisfies readonly NonNullable<ButtonProps["severity"]>[];
14
+
15
+ const { Story } = defineMeta({
16
+ title: "Components/Button",
17
+ component: Button,
18
+ tags: ["autodocs"],
19
+ });
20
+
21
+ let loading = $state(false);
22
+ const toggleLoading = () => {
23
+ loading = !loading;
24
+ setTimeout(() => {
25
+ loading = !loading;
26
+ }, 2000);
27
+ };
28
+ </script>
29
+
30
+ <Story
31
+ name="Default"
32
+ args={{
33
+ onclick: fn(),
34
+ }}
35
+ >
36
+ {#snippet template(args)}
37
+ <Button {...args}>Button</Button>
38
+ {/snippet}
39
+ </Story>
40
+
41
+ <Story name="Severities">
42
+ {#snippet template(args)}
43
+ <div class="row">
44
+ {#each BUTTON_SEVERITIES as severity (severity)}
45
+ <Button {...args} {severity} onclick={fn()}>
46
+ {severity}
47
+ </Button>
48
+ {/each}
49
+ </div>
50
+ {/snippet}
51
+ </Story>
52
+
53
+ <Story name="Densities">
54
+ {#snippet template(args)}
55
+ <div class="row">
56
+ {#each MODIFIER_FAMILIES.density as density (density)}
57
+ <Button {...args} {density} onclick={fn()}>
58
+ {density}
59
+ </Button>
60
+ {/each}
61
+ </div>
62
+ {/snippet}
63
+ </Story>
64
+
65
+ <Story name="With icons">
66
+ {#snippet template(args)}
67
+ <div class="row">
68
+ <Button {...args} onclick={fn()}>
69
+ {#snippet iconLeft()}
70
+ <ArchiveIcon />
71
+ {/snippet}
72
+ With left icon
73
+ </Button>
74
+ <Button {...args} onclick={fn()}>
75
+ With right icon
76
+ {#snippet iconRight()}
77
+ <ArchiveIcon />
78
+ {/snippet}
79
+ </Button>
80
+ <Button {...args} onclick={fn()}>
81
+ {#snippet iconLeft()}
82
+ <ArchiveIcon />
83
+ {/snippet}
84
+ Both icons
85
+ {#snippet iconRight()}
86
+ <ArchiveIcon />
87
+ {/snippet}
88
+ </Button>
89
+ </div>
90
+ {/snippet}
91
+ </Story>
92
+
93
+ <Story name="Icon only">
94
+ {#snippet template(args)}
95
+ <Button {...args} onclick={fn()}>
96
+ {#snippet iconLeft()}
97
+ <ArchiveIcon />
98
+ {/snippet}
99
+ </Button>
100
+ {/snippet}
101
+ </Story>
102
+
103
+ <Story name="Loading">
104
+ {#snippet template(args)}
105
+ <div class="row">
106
+ <Button {...args} {loading} onclick={toggleLoading}>Click to load</Button>
107
+ <br />
108
+ <br />
109
+ </div>
110
+ <p style="font-size: 12px; color: var(--lp-color-text-muted);">
111
+ Click the button to toggle the loading state.
112
+ </p>
113
+ {/snippet}
114
+ </Story>
115
+
116
+ <Story
117
+ name="Disabled"
118
+ args={{
119
+ disabled: true,
120
+ }}
121
+ >
122
+ Disabled button
123
+ </Story>
124
+
125
+ <Story
126
+ name="As link"
127
+ args={{
128
+ href: "https://ubuntu.com",
129
+ }}
130
+ >
131
+ Link button
132
+ </Story>
133
+
134
+ <Story name="Brand with icon">
135
+ {#snippet template(args)}
136
+ <Button {...args} severity="brand" onclick={fn()}>
137
+ {#snippet iconLeft()}
138
+ <ArchiveIcon />
139
+ {/snippet}
140
+ Brand action
141
+ </Button>
142
+ {/snippet}
143
+ </Story>
@@ -0,0 +1,19 @@
1
+ import Button from "./Button.svelte";
2
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
3
+ new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
4
+ $$bindings?: Bindings;
5
+ } & Exports;
6
+ (internal: unknown, props: {
7
+ $$events?: Events;
8
+ $$slots?: Slots;
9
+ }): Exports & {
10
+ $set?: any;
11
+ $on?: any;
12
+ };
13
+ z_$$bindings?: Bindings;
14
+ }
15
+ declare const Button: $$__sveltets_2_IsomorphicComponent<Record<string, never>, {
16
+ [evt: string]: CustomEvent<any>;
17
+ }, {}, {}, string>;
18
+ type Button = InstanceType<typeof Button>;
19
+ export default Button;
@@ -0,0 +1,63 @@
1
+ <script lang="ts">
2
+ import { Spinner } from "../Spinner/index.js";
3
+ import { ButtonPrimitive } from "../common/ButtonPrimitive/index.js";
4
+ import { Content } from "./common/Content";
5
+ import type { ButtonProps } from "./types";
6
+ import "./styles.css";
7
+
8
+ const componentCssClassName = "ds button";
9
+
10
+ let {
11
+ class: className,
12
+ ref = $bindable(),
13
+ severity,
14
+ density,
15
+ children,
16
+ iconLeft,
17
+ iconRight,
18
+ loading,
19
+ disabled,
20
+ ...rest
21
+ }: ButtonProps = $props();
22
+
23
+ const isDisabled = $derived(loading || disabled);
24
+ </script>
25
+
26
+ <ButtonPrimitive
27
+ bind:ref
28
+ class={[
29
+ componentCssClassName,
30
+ className,
31
+ severity,
32
+ density,
33
+ { loading, "explicit-disabled": disabled },
34
+ ]}
35
+ disabled={isDisabled}
36
+ {...rest}
37
+ >
38
+ <Content {iconLeft} {iconRight}>
39
+ {@render children?.()}
40
+ </Content>
41
+ {#if loading}
42
+ <span class="loader">
43
+ <Spinner />
44
+ </span>
45
+ {/if}
46
+ </ButtonPrimitive>
47
+
48
+ <!-- @component
49
+ `Button` is a styled button element.
50
+
51
+ ## Example Usage
52
+ ```svelte
53
+ <Button density="dense" severity="brand">
54
+ {#snippet iconLeft()}
55
+ <Check />
56
+ {/snippet}
57
+ Button Text
58
+ {#snippet iconRight()}
59
+ <ArrowRight />
60
+ {/snippet}
61
+ </Button>
62
+ ```
63
+ -->
@@ -0,0 +1,21 @@
1
+ import type { ButtonProps } from "./types";
2
+ import "./styles.css";
3
+ /**
4
+ * `Button` is a styled button element.
5
+ *
6
+ * ## Example Usage
7
+ * ```svelte
8
+ * <Button density="dense" severity="brand">
9
+ * {#snippet iconLeft()}
10
+ * <Check />
11
+ * {/snippet}
12
+ * Button Text
13
+ * {#snippet iconRight()}
14
+ * <ArrowRight />
15
+ * {/snippet}
16
+ * </Button>
17
+ * ```
18
+ */
19
+ declare const Button: import("svelte").Component<ButtonProps, {}, "ref">;
20
+ type Button = ReturnType<typeof Button>;
21
+ export default Button;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,150 @@
1
+ import { createRawSnippet } from "svelte";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import { render } from "vitest-browser-svelte";
4
+ import Component from "./Button.svelte";
5
+ describe("Button component", () => {
6
+ const baseProps = {
7
+ children: createRawSnippet(() => ({
8
+ render: () => `<span>Button</span>`,
9
+ })),
10
+ "data-testid": "button",
11
+ };
12
+ it("renders", async () => {
13
+ const page = render(Component, { ...baseProps });
14
+ await expect.element(componentLocator(page)).toBeInTheDocument();
15
+ await expect.element(page.getByText("Button")).toBeVisible();
16
+ });
17
+ describe("attributes", () => {
18
+ it.each([
19
+ ["id", "test-id"],
20
+ ["aria-label", "test-aria-label"],
21
+ ])("applies %s", async (attribute, expected) => {
22
+ const page = render(Component, {
23
+ ...baseProps,
24
+ [attribute]: expected,
25
+ });
26
+ await expect
27
+ .element(componentLocator(page))
28
+ .toHaveAttribute(attribute, expected);
29
+ });
30
+ it("applies classes", async () => {
31
+ const page = render(Component, {
32
+ ...baseProps,
33
+ class: "test-class",
34
+ });
35
+ await expect
36
+ .element(componentLocator(page))
37
+ .toHaveClass("test-class");
38
+ await expect.element(componentLocator(page)).toHaveClass("ds");
39
+ await expect.element(componentLocator(page)).toHaveClass("button");
40
+ });
41
+ it("applies style", async () => {
42
+ const page = render(Component, {
43
+ ...baseProps,
44
+ style: "color: orange;",
45
+ });
46
+ await expect
47
+ .element(componentLocator(page))
48
+ .toHaveStyle({ color: "orange" });
49
+ });
50
+ });
51
+ describe("rendering", () => {
52
+ it("renders as a button by default", async () => {
53
+ const page = render(Component, { ...baseProps });
54
+ await expect
55
+ .element(page.getByRole("button"))
56
+ .toBeInTheDocument();
57
+ });
58
+ it("renders as an anchor when href is provided", async () => {
59
+ const page = render(Component, {
60
+ ...baseProps,
61
+ href: "https://example.com",
62
+ });
63
+ await expect.element(page.getByRole("link")).toBeInTheDocument();
64
+ await expect
65
+ .element(page.getByRole("link"))
66
+ .toHaveAttribute("href", "https://example.com");
67
+ });
68
+ it("renders with icons", async () => {
69
+ const iconLeft = createRawSnippet(() => ({
70
+ render: () => `<span data-testid="icon-left">L</span>`,
71
+ }));
72
+ const iconRight = createRawSnippet(() => ({
73
+ render: () => `<span data-testid="icon-right">R</span>`,
74
+ }));
75
+ const page = render(Component, {
76
+ ...baseProps,
77
+ iconLeft,
78
+ iconRight,
79
+ });
80
+ await expect
81
+ .element(page.getByTestId("icon-left"))
82
+ .toBeInTheDocument();
83
+ await expect
84
+ .element(page.getByTestId("icon-right"))
85
+ .toBeInTheDocument();
86
+ });
87
+ });
88
+ describe("modifiers", () => {
89
+ it("applies severity class", async () => {
90
+ const page = render(Component, {
91
+ ...baseProps,
92
+ severity: "brand",
93
+ });
94
+ await expect
95
+ .element(componentLocator(page))
96
+ .toHaveClass("brand");
97
+ });
98
+ it("applies density class", async () => {
99
+ const page = render(Component, {
100
+ ...baseProps,
101
+ density: "dense",
102
+ });
103
+ await expect
104
+ .element(componentLocator(page))
105
+ .toHaveClass("dense");
106
+ });
107
+ });
108
+ describe("disabled state", () => {
109
+ it("disables the button when disabled", async () => {
110
+ const page = render(Component, {
111
+ ...baseProps,
112
+ disabled: true,
113
+ });
114
+ await expect.element(page.getByRole("button")).toBeDisabled();
115
+ await expect
116
+ .element(componentLocator(page))
117
+ .toHaveClass("explicit-disabled");
118
+ });
119
+ it("disables the button when loading", async () => {
120
+ const page = render(Component, {
121
+ ...baseProps,
122
+ loading: true,
123
+ });
124
+ await expect.element(page.getByRole("button")).toBeDisabled();
125
+ await expect
126
+ .element(componentLocator(page))
127
+ .toHaveClass("loading");
128
+ });
129
+ it("does not apply explicit-disabled class when loading", async () => {
130
+ const page = render(Component, {
131
+ ...baseProps,
132
+ loading: true,
133
+ });
134
+ await expect
135
+ .element(componentLocator(page))
136
+ .not.toHaveClass("explicit-disabled");
137
+ });
138
+ });
139
+ describe("interactions", () => {
140
+ it("handles click events", async () => {
141
+ const onclick = vi.fn();
142
+ const page = render(Component, { ...baseProps, onclick });
143
+ await page.getByRole("button").click();
144
+ expect(onclick).toHaveBeenCalled();
145
+ });
146
+ });
147
+ });
148
+ function componentLocator(page) {
149
+ return page.getByTestId("button");
150
+ }
@@ -0,0 +1,30 @@
1
+ <script lang="ts">
2
+ import type { ContentProps } from "./types";
3
+ import "./styles.css";
4
+
5
+ const componentCssClassName = "ds button-content";
6
+
7
+ let {
8
+ class: className,
9
+ children,
10
+ iconLeft,
11
+ iconRight,
12
+ ...rest
13
+ }: ContentProps = $props();
14
+ </script>
15
+
16
+ <div class={[componentCssClassName, className]} {...rest}>
17
+ {#if iconLeft}
18
+ <span class="icon left">
19
+ {@render iconLeft()}
20
+ </span>
21
+ {/if}
22
+ <span class="text">
23
+ {@render children?.()}
24
+ </span>
25
+ {#if iconRight}
26
+ <span class="icon right">
27
+ {@render iconRight()}
28
+ </span>
29
+ {/if}
30
+ </div>
@@ -0,0 +1,5 @@
1
+ import type { ContentProps } from "./types";
2
+ import "./styles.css";
3
+ declare const Content: import("svelte").Component<ContentProps, {}, "">;
4
+ type Content = ReturnType<typeof Content>;
5
+ export default Content;
@@ -0,0 +1,2 @@
1
+ export { default as Content } from "./Content.svelte";
2
+ export type { ContentProps } from "./types";
@@ -0,0 +1 @@
1
+ export { default as Content } from "./Content.svelte";
@@ -0,0 +1,60 @@
1
+ .ds.button-content {
2
+ display: inline-grid;
3
+ grid-auto-flow: column;
4
+ grid-auto-rows: minmax(1lh, auto);
5
+ align-items: center;
6
+
7
+ gap: var(--lp-dimension-spacing-inline-xs);
8
+ padding-block: var(
9
+ --lp-dimension-padding-block-context,
10
+ var(--lp-dimension-spacing-block-xxs)
11
+ );
12
+ padding-inline: var(
13
+ --lp-dimension-padding-inline-context,
14
+ var(--lp-dimension-spacing-inline-s)
15
+ );
16
+ --icon-padding-subtract: calc(
17
+ var(
18
+ --lp-dimension-padding-inline-context,
19
+ var(--lp-dimension-spacing-inline-s)
20
+ ) / 2
21
+ );
22
+
23
+ > .icon {
24
+ display: inline-flex;
25
+ }
26
+
27
+ /* icon inline center within the padding */
28
+ &:has(.text:not(:empty)) > .icon {
29
+ &.left {
30
+ margin-inline-start: calc(var(--icon-padding-subtract) * -1);
31
+ }
32
+ &.right {
33
+ margin-inline-end: calc(var(--icon-padding-subtract) * -1);
34
+ }
35
+ }
36
+
37
+ &:has(.text:empty) {
38
+ &:has(.icon.left) {
39
+ padding-inline-start: 0;
40
+ }
41
+ &:has(.icon.right) {
42
+ padding-inline-end: 0;
43
+ }
44
+
45
+ > .icon {
46
+ &.left {
47
+ margin-inline-start: var(--icon-padding-subtract);
48
+ margin-inline-end: calc(var(--icon-padding-subtract) * -1);
49
+ }
50
+ &.right {
51
+ margin-inline-end: var(--icon-padding-subtract);
52
+ margin-inline-start: calc(var(--icon-padding-subtract) * -1);
53
+ }
54
+ }
55
+ }
56
+
57
+ .text:empty {
58
+ display: none;
59
+ }
60
+ }
@@ -0,0 +1,8 @@
1
+ import type { Snippet } from "svelte";
2
+ import type { SvelteHTMLElements } from "svelte/elements";
3
+ type BaseProps = SvelteHTMLElements["span"];
4
+ export interface ContentProps extends BaseProps {
5
+ iconLeft?: Snippet;
6
+ iconRight?: Snippet;
7
+ }
8
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export { default as Button } from "./Button.svelte";
2
+ export type { ButtonProps } from "./types";
@@ -0,0 +1,2 @@
1
+ // Generated by @canonical/summon:component-svelte v0.1.0
2
+ export { default as Button } from "./Button.svelte";
@@ -0,0 +1,73 @@
1
+ .ds.button {
2
+ --color-background-button-active: var(
3
+ --lp-color-background-active-context,
4
+ var(--lp-color-background-neutral-active)
5
+ );
6
+ --color-background-button-hover: var(
7
+ --lp-color-background-hover-context,
8
+ var(--lp-color-background-neutral-hover)
9
+ );
10
+ --color-background-button: var(
11
+ --lp-color-background-context,
12
+ var(--lp-color-background-default)
13
+ );
14
+ --color-border-button: var(
15
+ --lp-color-border-context,
16
+ var(--lp-color-border-high-contrast)
17
+ );
18
+ --color-border-button-hover: var(
19
+ --lp-color-border-hover-context,
20
+ var(--color-border-button)
21
+ );
22
+ --color-border-button-active: var(
23
+ --lp-color-border-active-context,
24
+ var(--color-border-button)
25
+ );
26
+ --color-text-button: var(
27
+ --lp-color-text-context,
28
+ var(--lp-color-text-default)
29
+ );
30
+ --typography-button: var(
31
+ --lp-typography-context,
32
+ var(--lp-typography-paragraph-default)
33
+ );
34
+
35
+ padding-inline: 0;
36
+ padding-block: 0;
37
+ align-items: center;
38
+ display: inline-flex;
39
+ justify-content: center;
40
+
41
+ &:not(.explicit-disabled) {
42
+ --opacity-button-disabled: 1;
43
+ }
44
+
45
+ &.loading {
46
+ position: relative;
47
+
48
+ > .button-content {
49
+ visibility: hidden;
50
+ }
51
+
52
+ > .loader {
53
+ position: absolute;
54
+ inset: 0;
55
+ display: flex;
56
+ align-items: center;
57
+ justify-content: center;
58
+ }
59
+ }
60
+
61
+ /* Button-specific modifiers */
62
+ &.brand {
63
+ --lp-color-text-context: var(--lp-color-text-white);
64
+ --lp-color-border-context: var(--lp-color-brand-default);
65
+ --lp-color-background-context: var(--lp-color-brand-default);
66
+ --lp-color-background-hover-context: var(--lp-color-brand-hover);
67
+ --lp-color-background-active-context: var(--lp-color-brand-active);
68
+ }
69
+
70
+ &.base {
71
+ --lp-color-border-context: transparent;
72
+ }
73
+ }