@canonical/svelte-ds-app-launchpad 0.29.0-experimental.0 → 0.29.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.
@@ -3,10 +3,12 @@
3
3
  <script lang="ts">
4
4
  import { useIsMounted } from "../../useFunctions/index.js";
5
5
  import { isEventTargetInElement } from "../../utils/index.js";
6
- import type { ModalMethods, ModalProps } from "./types.js";
6
+ import type { ModalProps } from "./types.js";
7
7
  import "./styles.css";
8
8
 
9
- const componentCssClassName = "ds modal";
9
+ const componentCssClassNameBase = "modal";
10
+ const componentCssClassName = `ds ${componentCssClassNameBase}`;
11
+ const componentCssClassNameNonModalBackdrop = `ds ${componentCssClassNameBase}-non-modal-backdrop`;
10
12
 
11
13
  let {
12
14
  id: idProp,
@@ -15,24 +17,16 @@
15
17
  children,
16
18
  closeOnOutsideClick = true,
17
19
  onclick,
20
+ ontoggle: ontoggleProp,
21
+ open = $bindable(),
18
22
  ...rest
19
23
  }: ModalProps = $props();
20
24
 
21
- let dialogRef = $state<HTMLDialogElement>();
22
-
23
25
  const fallbackId = $props.id();
24
26
  const id = $derived(idProp || fallbackId);
25
27
 
26
28
  const isMounted = useIsMounted();
27
29
 
28
- export const showModal: ModalMethods["showModal"] = () => {
29
- dialogRef?.showModal();
30
- };
31
-
32
- export const close: ModalMethods["close"] = () => {
33
- dialogRef?.close();
34
- };
35
-
36
30
  // Webkit doesn't support `closedby` attribute on dialog elements (https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/closedBy).
37
31
  // TODO(closedby): Remove this fallback when Webkit supports it.
38
32
  let contentWrapperRef = $state<HTMLElement>();
@@ -41,13 +35,6 @@
41
35
  isMounted.value && !("closedBy" in HTMLDialogElement.prototype),
42
36
  );
43
37
 
44
- // TODO(Invoker Commands API): Remove this when Invoker Commands API is widely supported (https://caniuse.com/wf-invoker-commands)
45
- const isInvokerCommandsFallbackNeeded = $derived(
46
- isMounted.value &&
47
- (!("commandForElement" in HTMLButtonElement.prototype) ||
48
- !("command" in HTMLButtonElement.prototype)),
49
- );
50
-
51
38
  const fallbackOnclick: typeof onclick = (e) => {
52
39
  onclick?.(e);
53
40
  if (
@@ -55,50 +42,81 @@
55
42
  contentWrapperRef &&
56
43
  !isEventTargetInElement(e.target, contentWrapperRef)
57
44
  ) {
58
- close();
45
+ open = false;
59
46
  }
60
47
  };
48
+
49
+ /** Capture the initial value of `open` for SSR paint. After hydration, the dialog's open attribute should never be set manually. */
50
+ const initialOpen = open;
51
+
52
+ /** Reflect the invoker commands, Escape, outside click changes back onto `open`. */
53
+ const ontoggle: typeof ontoggleProp = (e) => {
54
+ ontoggleProp?.(e);
55
+ const newOpen = e.newState === "open";
56
+ if (newOpen !== open) open = newOpen;
57
+ };
61
58
  </script>
62
59
 
63
60
  {@render trigger?.({
64
- onclick: isInvokerCommandsFallbackNeeded ? showModal : undefined,
65
61
  commandfor: id,
66
62
  command: "show-modal",
67
63
  "aria-haspopup": "dialog",
68
64
  })}
69
65
 
66
+ <!-- A non-modal dialog has no real `::backdrop` so we need to fake one. Can't be a pseudo-element, because a pseudo lives inside the dialog so `closeOnOutsideClick` wouldn't work. -->
67
+ {#if initialOpen}
68
+ <div class={componentCssClassNameNonModalBackdrop}></div>
69
+ {/if}
70
70
  <dialog
71
- bind:this={dialogRef}
72
71
  {id}
73
72
  class={[componentCssClassName, className]}
74
73
  closedby={closeOnOutsideClick ? "any" : "closerequest"}
75
74
  onclick={isClosedByFallbackNeeded ? fallbackOnclick : onclick}
75
+ {ontoggle}
76
+ open={initialOpen}
77
+ {@attach (dialogEl) => {
78
+ // Suppress the transition on mount so that when we upgrade to modal the open fade doesn't play.
79
+ dialogEl.classList.add("no-transition");
80
+
81
+ // Map `open` changes to `showModal`/`close`. First run upgrades the dialog to modal if `open` is true.
82
+ $effect(() => {
83
+ if (open) {
84
+ if (dialogEl.open && !dialogEl.matches(":modal")) {
85
+ // `open === true` during SSR case
86
+ // `showModal` throws when called on open non-modal dialog so we need to close it first.
87
+ dialogEl.close();
88
+ }
89
+ dialogEl.showModal();
90
+ } else {
91
+ dialogEl.close();
92
+ }
93
+
94
+ // Re-enable the transition after the first sync so that later changes animate.
95
+ dialogEl.classList.remove("no-transition");
96
+ });
97
+ }}
76
98
  {...rest}
77
99
  >
78
- <!-- TODO(closedby): Remove this wrapper when Webkit supports closedby -->
79
100
  <div style="display: contents;" bind:this={contentWrapperRef}>
80
- {@render children?.(id, close)}
101
+ {@render children?.(id, () => (open = false))}
81
102
  </div>
82
103
  </dialog>
83
104
 
84
105
  <!-- @component
85
- `Modal` provides a mechanism for displaying content overlaying the main application.
106
+ `Modal` provides a mechanism for displaying content overlaying the main application.
86
107
 
87
- Modal can be imperatively controlled by the following methods available on the component instance:
88
- - `showModal`: Shows the modal.
89
- - `close`: Closes the modal.
108
+ Modal is declaratively controlled by default through the [Invoker Commands API](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API) using `commandfor` and `command` attributes supplied via the `trigger` snippet. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog) for more information.
90
109
 
91
- Modal is declaratively controlled by default through the [Invoker Commands API](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API) using `commandfor` and `command` attributes supplied via the `trigger` snippet. Imperative methods remain available for cases where opening or closing must be orchestrated in code. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog) for more information.
110
+ For cases where opening or closing must be orchestrated in code, Modal can be controlled through the bindable `open` prop. With `bind:open` it stays in sync with the modal in both directions: setting it opens or closes the modal, and it updates to reflect changes made by invoker commands, `Escape`, or an outside click. Setting `open` during SSR renders the dialog open on page load without client-side JS, and it is upgraded to a true modal once hydrated.
92
111
 
93
112
  ## Example Usage
94
113
  ```svelte
95
114
  <script lang="ts">
96
- let modal = $state<ModalMethods>();
97
- // Imperative controls on the component instance
98
- $effect(() => modal?.showModal())
115
+ let open = $state(false);
99
116
  </script>
100
117
 
101
- <Modal bind:this={modal}>
118
+ <p>Modal is {open ? "open" : "closed"}</p>
119
+ <Modal bind:open>
102
120
  {#snippet trigger(triggerProps)}
103
121
  <button {...triggerProps}>
104
122
  Open Modal
@@ -130,5 +148,4 @@ Modal is declaratively controlled by default through the [Invoker Commands API](
130
148
  {/snippet}
131
149
  </Modal>
132
150
  ```
133
-
134
151
  -->
@@ -1,23 +1,20 @@
1
- import type { ModalMethods, ModalProps } from "./types.js";
1
+ import type { ModalProps } from "./types.js";
2
2
  import "./styles.css";
3
3
  /**
4
4
  * `Modal` provides a mechanism for displaying content overlaying the main application.
5
5
  *
6
- * Modal can be imperatively controlled by the following methods available on the component instance:
7
- * - `showModal`: Shows the modal.
8
- * - `close`: Closes the modal.
6
+ * Modal is declaratively controlled by default through the [Invoker Commands API](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API) using `commandfor` and `command` attributes supplied via the `trigger` snippet. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog) for more information.
9
7
  *
10
- * Modal is declaratively controlled by default through the [Invoker Commands API](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API) using `commandfor` and `command` attributes supplied via the `trigger` snippet. Imperative methods remain available for cases where opening or closing must be orchestrated in code. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog) for more information.
8
+ * For cases where opening or closing must be orchestrated in code, Modal can be controlled through the bindable `open` prop. With `bind:open` it stays in sync with the modal in both directions: setting it opens or closes the modal, and it updates to reflect changes made by invoker commands, `Escape`, or an outside click. Setting `open` during SSR renders the dialog open on page load without client-side JS, and it is upgraded to a true modal once hydrated.
11
9
  *
12
10
  * ## Example Usage
13
11
  * ```svelte
14
12
  * <script lang="ts">
15
- * let modal = $state<ModalMethods>();
16
- * // Imperative controls on the component instance
17
- * $effect(() => modal?.showModal())
13
+ * let open = $state(false);
18
14
  * </script>
19
15
  *
20
- * <Modal bind:this={modal}>
16
+ * <p>Modal is {open ? "open" : "closed"}</p>
17
+ * <Modal bind:open>
21
18
  * {#snippet trigger(triggerProps)}
22
19
  * <button {...triggerProps}>
23
20
  * Open Modal
@@ -50,9 +47,6 @@ import "./styles.css";
50
47
  * </Modal>
51
48
  * ```
52
49
  */
53
- declare const Modal: import("svelte").Component<ModalProps, {
54
- showModal: ModalMethods["showModal"];
55
- close: ModalMethods["close"];
56
- }, "">;
50
+ declare const Modal: import("svelte").Component<ModalProps, {}, "open">;
57
51
  type Modal = ReturnType<typeof Modal>;
58
52
  export default Modal;
@@ -4,6 +4,11 @@
4
4
 
5
5
  position: fixed;
6
6
  margin: auto;
7
+ inset: 0;
8
+ /* z-index on dialog is only relevant when opened as non-modal.
9
+ Modal dialogs are rendered on the top-layer anyways */
10
+ /* TODO: Add z-index tokens */
11
+ z-index: 10;
7
12
  width: min(100vw, var(--dimension-width-modal));
8
13
 
9
14
  border: var(--lp-dimension-stroke-thickness-default) solid
@@ -17,11 +22,21 @@
17
22
  transition-duration: var(--lp-transition-duration-fast);
18
23
  }
19
24
 
25
+ &.no-transition {
26
+ &,
27
+ &::backdrop {
28
+ transition: none;
29
+ }
30
+ }
31
+
20
32
  &::backdrop {
21
- background-color: var(--lp-color-background-overlay);
33
+ background-color: var(
34
+ --modal-backdrop-color,
35
+ var(--lp-color-background-overlay)
36
+ );
22
37
  }
23
38
 
24
- /*
39
+ /*
25
40
  [open] is a fallback for Safari that doesn't support the `:open`.
26
41
 
27
42
  TODO(:open): Remove when Safari supports it (https://developer.mozilla.org/en-US/docs/Web/CSS/:open)
@@ -30,10 +45,32 @@
30
45
  &,
31
46
  &::backdrop {
32
47
  opacity: 1;
48
+ }
49
+ }
33
50
 
34
- @starting-style {
51
+ /* Starting styles only for modal dialogs, so the SSR opened shows up immediately */
52
+ &:modal {
53
+ @starting-style {
54
+ &,
55
+ &::backdrop {
35
56
  opacity: 0;
36
57
  }
37
58
  }
38
59
  }
39
60
  }
61
+
62
+ .ds.modal-non-modal-backdrop {
63
+ position: fixed;
64
+ inset: 0;
65
+ background-color: var(
66
+ --modal-backdrop-color,
67
+ var(--lp-color-background-overlay)
68
+ );
69
+ display: none;
70
+ /* TODO: Add z-index tokens */
71
+ z-index: 10;
72
+
73
+ &:has(+ .ds.modal:is(:open, [open]):not(:modal)) {
74
+ display: block;
75
+ }
76
+ }
@@ -1,8 +1,7 @@
1
1
  import type { Snippet } from "svelte";
2
2
  import type { HTMLDialogAttributes } from "svelte/elements";
3
- type BaseProps = Omit<HTMLDialogAttributes, "open" | "children">;
3
+ type BaseProps = Omit<HTMLDialogAttributes, "children">;
4
4
  export type ModalTriggerProps = {
5
- onclick?: () => void;
6
5
  commandfor: string;
7
6
  command: "show-modal";
8
7
  "aria-haspopup": "dialog";
@@ -15,7 +14,6 @@ export interface ModalProps extends BaseProps {
15
14
  * - `triggerProps`: Props to spread on the button element. It contains:
16
15
  * - `commandfor`: The id of the dialog element. Setting it as `commandfor` associates the button with the dialog to open.
17
16
  * - `command`: Always set to `"show-modal"` to indicate that the button opens a modal.
18
- * - `onclick`: An onclick handler that calls `dialog.showModal()`. Present only as a fallback for browsers that don't support [Invoker Commands API](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API).
19
17
  * - `aria-haspopup`: Always set to `"dialog"` to indicate that the button opens a dialog.
20
18
  */
21
19
  trigger?: Snippet<[triggerProps: ModalTriggerProps]>;
@@ -33,9 +31,12 @@ export interface ModalProps extends BaseProps {
33
31
  * - `close`: A function to close the modal.
34
32
  */
35
33
  children?: Snippet<[commandfor: string, close: () => void]>;
36
- }
37
- export interface ModalMethods {
38
- showModal: () => void;
39
- close: () => void;
34
+ /**
35
+ * `open` serves two purposes:
36
+ * - As an SSR mechanism to render the modal already open without client-side JS.
37
+ * Note: a dialog displayed this way is non-modal, so the component styles emulate a modal and, once hydrated, upgrade it to a real one.
38
+ * - As a two-way bindable prop once hydrated: setting it maps to `showModal()` / `close()`, and it is updated back to reflect the state change triggered by other means (e.g., invoker commands, Escape press, outside click).
39
+ */
40
+ open?: BaseProps["open"];
40
41
  }
41
42
  export {};
@@ -2,27 +2,22 @@
2
2
 
3
3
  <script lang="ts">
4
4
  import { Modal } from "../Modal/index.js";
5
- import type { SidePanelMethods, SidePanelProps } from "./types.js";
5
+ import type { SidePanelProps } from "./types.js";
6
6
  import "./styles.css";
7
7
 
8
8
  const componentCssClassName = "ds side-panel";
9
9
 
10
- let { class: className, ...rest }: SidePanelProps = $props();
11
-
12
- let modalMethods = $state<SidePanelMethods>();
13
-
14
- export const showModal: SidePanelMethods["showModal"] = () => {
15
- modalMethods?.showModal();
16
- };
17
-
18
- export const close: SidePanelMethods["close"] = () => {
19
- modalMethods?.close();
20
- };
10
+ let {
11
+ class: className,
12
+ open = $bindable(),
13
+ ...rest
14
+ }: SidePanelProps = $props();
21
15
  </script>
22
16
 
23
17
  <Modal
24
- bind:this={modalMethods}
18
+ bind:open
25
19
  class={[componentCssClassName, className]}
20
+ --modal-backdrop-color="transparent"
26
21
  {...rest}
27
22
  />
28
23
 
@@ -1,13 +1,9 @@
1
- import type { SidePanelMethods } from "./types.js";
2
1
  import "./styles.css";
3
2
  /**
4
3
  * `SidePanel` displays a `Modal` with a side-docked layout, sliding in from the right edge of the viewport.
5
4
  *
6
5
  * For API details and usage patterns, refer to `Modal`.
7
6
  */
8
- declare const SidePanel: import("svelte").Component<import("../index.js").ModalProps, {
9
- showModal: SidePanelMethods["showModal"];
10
- close: SidePanelMethods["close"];
11
- }, "">;
7
+ declare const SidePanel: import("svelte").Component<import("../index.js").ModalProps, {}, "open">;
12
8
  type SidePanel = ReturnType<typeof SidePanel>;
13
9
  export default SidePanel;
@@ -21,7 +21,10 @@
21
21
  */
22
22
  &:is(:open, [open]) {
23
23
  transform: translateX(0);
24
+ }
24
25
 
26
+ /* Starting styles only for modal dialogs, so the SSR opened shows up immediately */
27
+ &:modal {
25
28
  @starting-style {
26
29
  transform: translateX(100%);
27
30
  }
@@ -1,3 +1,3 @@
1
1
  import type { ModalProps } from "../Modal/index.js";
2
2
  export type SidePanelProps = ModalProps;
3
- export type { ModalMethods as SidePanelMethods, ModalTriggerProps as SidePanelTriggerProps, } from "../Modal/index.js";
3
+ export type { ModalTriggerProps as SidePanelTriggerProps } from "../Modal/index.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@canonical/svelte-ds-app-launchpad",
3
3
  "description": "Launchpad-specific components for the Pragma design system.",
4
- "version": "0.29.0-experimental.0",
4
+ "version": "0.29.0",
5
5
  "type": "module",
6
6
  "module": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -63,13 +63,13 @@
63
63
  },
64
64
  "devDependencies": {
65
65
  "@biomejs/biome": "2.4.9",
66
- "@canonical/biome-config": "^0.28.0",
66
+ "@canonical/biome-config": "^0.29.0",
67
67
  "@canonical/design-tokens": "^0.6.0",
68
- "@canonical/storybook-config": "^0.29.0-experimental.0",
69
- "@canonical/storybook-helpers": "^0.29.0-experimental.0",
70
- "@canonical/svelte-ssr-test": "^0.28.0",
71
- "@canonical/typescript-config-svelte": "^0.28.0",
72
- "@canonical/webarchitect": "^0.29.0-experimental.0",
68
+ "@canonical/storybook-config": "^0.29.0",
69
+ "@canonical/storybook-helpers": "^0.29.0",
70
+ "@canonical/svelte-ssr-test": "^0.29.0",
71
+ "@canonical/typescript-config-svelte": "^0.29.0",
72
+ "@canonical/webarchitect": "^0.29.0",
73
73
  "@storybook/addon-docs": "^10.3.1",
74
74
  "@storybook/addon-svelte-csf": "^5.0.11",
75
75
  "@sveltejs/package": "^2.5.7",
@@ -77,7 +77,7 @@
77
77
  "@types/bun": "^1.3.10",
78
78
  "@vitest/browser": "^4.0.18",
79
79
  "@vitest/browser-playwright": "^4.0.18",
80
- "playwright": "^1.58.2",
80
+ "playwright": "^1.61.1",
81
81
  "storybook": "^10.3.1",
82
82
  "svelte-check": "^4.4.5",
83
83
  "typescript": "^5.9.3",
@@ -93,5 +93,5 @@
93
93
  "peerDependencies": {
94
94
  "svelte": "^5.47.1"
95
95
  },
96
- "gitHead": "eda67fea634a79b9f77910d3106219efc9b30136"
96
+ "gitHead": "535302c1e1e5dd8fa9bc2da4144de57ec4e63005"
97
97
  }