@w5-ui/svelte 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@w5-ui/svelte",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Svelte 5 components for the Dash UI design system - mirrors the @w5-ui/react API 1:1. Part of Webber's design systems.",
5
5
  "keywords": [
6
6
  "svelte",
@@ -45,10 +45,10 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@w5-ui/assets": "^0.2.1",
48
- "@w5-ui/tokens": "^0.2.1"
48
+ "@w5-ui/tokens": "^0.5.0"
49
49
  },
50
50
  "devDependencies": {
51
- "svelte": "^5.55.8",
51
+ "svelte": "^5.55.9",
52
52
  "typescript": "^5.5.0"
53
53
  },
54
54
  "files": [
@@ -1,8 +1,23 @@
1
1
  <script lang="ts" module>
2
+ export type ContextMenuPillVariant = 'success' | 'warn' | 'danger' | 'info' | 'neutral';
3
+ /** Inline badge rendered where the label's `{pill}` placeholder sits. */
4
+ export interface ContextMenuPill {
5
+ text: string;
6
+ variant?: ContextMenuPillVariant;
7
+ }
2
8
  export interface ContextMenuItem {
3
9
  id: string;
10
+ /** Item copy. May contain a single `{pill}` placeholder slot which is
11
+ * replaced by an inline {@link ContextMenuPill} when `pill` is set. */
4
12
  label: string;
13
+ /** Inline badge substituted for the `{pill}` slot in `label`. */
14
+ pill?: ContextMenuPill;
15
+ /** Red tone (destructive). Mutually exclusive with `warning`. */
5
16
  danger?: boolean;
17
+ /** Yellow tone (caution / heads-up). Mutually exclusive with `danger`. */
18
+ warning?: boolean;
19
+ /** Green tone (active / positive state). Mutually exclusive with `danger` + `warning`. */
20
+ success?: boolean;
6
21
  disabled?: boolean;
7
22
  }
8
23
  export type ContextMenuEntry = ContextMenuItem | { separator: true };
@@ -10,6 +25,22 @@
10
25
 
11
26
  <script lang="ts">
12
27
  import { onDestroy, tick } from 'svelte';
28
+ import Pill from './Pill.svelte';
29
+
30
+ const PILL_SLOT = '{pill}';
31
+
32
+ /** Split a label around its first `{pill}` slot so the pill can be
33
+ * rendered inline between the surrounding copy. Returns null when the
34
+ * item has no pill or no slot, signalling a plain-text label. */
35
+ function pillParts(entry: ContextMenuItem): { before: string; after: string } | null {
36
+ if (entry.pill === undefined) return null;
37
+ const idx = entry.label.indexOf(PILL_SLOT);
38
+ if (idx === -1) return null;
39
+ return {
40
+ before: entry.label.slice(0, idx),
41
+ after: entry.label.slice(idx + PILL_SLOT.length),
42
+ };
43
+ }
13
44
 
14
45
  interface Props {
15
46
  items?: ContextMenuEntry[];
@@ -110,10 +141,16 @@
110
141
  aria-disabled={entry.disabled || undefined}
111
142
  data-active={actionIndex(entry) === activeIdx ? 'true' : undefined}
112
143
  data-danger={entry.danger || undefined}
113
- class="flex cursor-pointer items-center whitespace-nowrap rounded-[5px] px-3 py-1.5 text-13 text-text-2 outline-none hover:bg-row-active hover:text-text-1 data-[active=true]:bg-row-active data-[active=true]:text-text-1 data-[danger=true]:text-status-danger data-[danger=true]:hover:bg-status-danger/10 aria-disabled:cursor-not-allowed aria-disabled:opacity-40"
144
+ data-warning={entry.warning || undefined}
145
+ data-success={entry.success || undefined}
146
+ class="flex cursor-pointer items-center whitespace-nowrap rounded-[5px] px-3 py-1.5 text-13 text-text-2 outline-none hover:bg-row-active hover:text-text-1 data-[active=true]:bg-row-active data-[active=true]:text-text-1 data-[danger=true]:text-status-danger data-[danger=true]:hover:bg-status-danger/10 data-[warning=true]:text-status-warning data-[warning=true]:hover:bg-status-warning/10 data-[success=true]:text-status-success data-[success=true]:hover:bg-status-success/10 aria-disabled:cursor-not-allowed aria-disabled:opacity-40"
114
147
  onmouseenter={() => (activeIdx = actionIndex(entry))}
115
148
  onmousedown={(e) => { e.preventDefault(); (() => { if (!entry.disabled) activate(entry.id); })(); }}
116
- >{entry.label}</li>
149
+ >{#if pillParts(entry)}{@const parts = pillParts(entry)}{parts?.before}<Pill
150
+ variant={entry.pill?.variant ?? 'neutral'}
151
+ showDot={false}
152
+ class="mx-0.5 !px-1.5 !py-0 text-[10px] font-semibold leading-[1]"
153
+ >{entry.pill?.text}</Pill>{parts?.after}{:else}{entry.label}{/if}</li>
117
154
  {/if}
118
155
  {/each}
119
156
  </ul>
@@ -1,3 +1,7 @@
1
+ <script module lang="ts">
2
+ export type ModalSize = 'sm' | 'md' | 'lg' | 'xl' | '2xl';
3
+ </script>
4
+
1
5
  <script lang="ts">
2
6
  import { tick } from 'svelte';
3
7
  import IconButton from './IconButton.svelte';
@@ -5,6 +9,9 @@
5
9
  interface Props {
6
10
  open?: boolean;
7
11
  title: string;
12
+ /** Panel width. `md` (520px) is the default; larger sizes suit
13
+ * data-dense or editor-style dialogs. Always capped at 90vw. */
14
+ size?: ModalSize;
8
15
  children?: import('svelte').Snippet;
9
16
  footer?: import('svelte').Snippet;
10
17
  }
@@ -12,9 +19,21 @@
12
19
  let {
13
20
  open = $bindable(false),
14
21
  title,
22
+ size = 'md',
15
23
  children,
16
24
  footer
17
25
  }: Props = $props();
26
+
27
+ // Pre-composed width utilities so Tailwind's static scanner keeps
28
+ // each class in the build. The React sibling expresses the same
29
+ // scale via `.modal--*` rules in dashboard.css.
30
+ const SIZE: Record<ModalSize, string> = {
31
+ sm: 'w-[400px]',
32
+ md: 'w-[520px]',
33
+ lg: 'w-[720px]',
34
+ xl: 'w-[960px]',
35
+ '2xl': 'w-[1200px]'
36
+ };
18
37
  const titleId = `modal-title-${Math.random().toString(36).slice(2, 9)}`;
19
38
  const FOCUSABLE =
20
39
  'a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])';
@@ -90,7 +109,7 @@
90
109
  >
91
110
  <div
92
111
  bind:this={modalEl}
93
- class="w-[520px] max-w-[90vw] overflow-hidden rounded-xl border border-border-2 bg-bg-1 shadow-modal"
112
+ class="{SIZE[size]} max-w-[90vw] overflow-hidden rounded-xl border border-border-2 bg-bg-1 shadow-modal"
94
113
  role="dialog"
95
114
  aria-modal="true"
96
115
  aria-labelledby={titleId}
@@ -6,6 +6,7 @@
6
6
  import { onMount, onDestroy, tick } from 'svelte';
7
7
  import Button from './Button.svelte';
8
8
  import { portal } from '../actions/portal.ts';
9
+ import { computePanelPosition } from './popover-position.ts';
9
10
 
10
11
  interface Props {
11
12
  label: string;
@@ -67,17 +68,28 @@
67
68
  function recomputePosition() {
68
69
  if (!triggerEl || !panelEl) return;
69
70
  const tr = triggerEl.getBoundingClientRect();
70
- const margin = 8;
71
- const offset = 6; // gap between trigger bottom and panel top
72
- const panelWidth = panelEl.offsetWidth;
73
- let left: number;
74
- if (placement === 'bottom-start') left = tr.left;
75
- else if (placement === 'bottom-end') left = tr.right - panelWidth;
76
- else left = tr.left + tr.width / 2 - panelWidth / 2; // 'bottom'
77
- // Clamp into the visible viewport with the configured gutter.
78
- left = Math.max(margin, Math.min(left, window.innerWidth - panelWidth - margin));
71
+ // Delegate the actual math to a pure helper so the auto-flip
72
+ // rule (panel goes above the trigger when below would overflow
73
+ // AND above has more room) is unit-testable without mounting
74
+ // the component. See `popover-position.ts`.
75
+ const { top, left } = computePanelPosition({
76
+ triggerRect: {
77
+ top: tr.top,
78
+ bottom: tr.bottom,
79
+ left: tr.left,
80
+ right: tr.right,
81
+ width: tr.width,
82
+ },
83
+ panelWidth: panelEl.offsetWidth,
84
+ panelHeight: panelEl.offsetHeight,
85
+ viewportWidth: window.innerWidth,
86
+ viewportHeight: window.innerHeight,
87
+ placement,
88
+ margin: 8,
89
+ offset: 6,
90
+ });
91
+ panelTop = top;
79
92
  panelLeft = left;
80
- panelTop = tr.bottom + offset;
81
93
  }
82
94
 
83
95
  async function toggle() {
@@ -107,6 +107,28 @@
107
107
  let logoRingClass = $derived(
108
108
  `inline-flex h-6 w-6 items-center justify-center overflow-hidden rounded-full ring-2 ring-inset ${LOGO_RING[status]}`,
109
109
  );
110
+
111
+ // Status-coloured glow drawn behind the site logo. Replaces the old
112
+ // "N down" headline pill as the at-a-glance health signal: green when
113
+ // healthy, amber when degraded, red when something is down. Rendered
114
+ // as a blurred radial puddle behind the logo so the colour bleeds
115
+ // softly past the rim rather than reading as a hard second ring.
116
+ // Neutral (no data yet) gets no glow so a cold-start chrome stays calm.
117
+ const GLOW_TOKEN: Record<TopbarStatus, string | null> = {
118
+ ok: '--status-success',
119
+ warn: '--status-warning',
120
+ danger: '--status-danger',
121
+ neutral: null,
122
+ };
123
+ let logoGlowStyle = $derived.by(() => {
124
+ const token = GLOW_TOKEN[status];
125
+ if (token === null) return 'opacity: 0;';
126
+ const colour = `var(${token})`;
127
+ return (
128
+ `background: radial-gradient(closest-side, ${colour}, transparent);` +
129
+ ' opacity: 0.85; filter: blur(5px);'
130
+ );
131
+ });
110
132
  </script>
111
133
 
112
134
  <!--
@@ -128,8 +150,16 @@
128
150
  title={`${siteName} — status: ${status}`}
129
151
  >
130
152
  {#if siteLogo !== undefined}
131
- <span class={logoRingClass} aria-hidden="true">
132
- <img src={resolveLogo(siteLogo)} alt="" width="24" height="24" class="h-full w-full object-cover" />
153
+ <span class="relative inline-flex h-6 w-6 items-center justify-center">
154
+ <span
155
+ data-testid="site-logo-glow"
156
+ aria-hidden="true"
157
+ class="pointer-events-none absolute -inset-1.5 rounded-full transition-opacity duration-300"
158
+ style={logoGlowStyle}
159
+ ></span>
160
+ <span class={`relative ${logoRingClass}`} aria-hidden="true">
161
+ <img src={resolveLogo(siteLogo)} alt="" width="24" height="24" class="h-full w-full object-cover" />
162
+ </span>
133
163
  </span>
134
164
  {:else}
135
165
  <span class={ringWrapClass}><span class={ringDotClass}></span></span>
@@ -144,8 +174,16 @@
144
174
  title={`${siteName} — status: ${status}`}
145
175
  >
146
176
  {#if siteLogo !== undefined}
147
- <span class={logoRingClass} aria-hidden="true">
148
- <img src={resolveLogo(siteLogo)} alt="" width="24" height="24" class="h-full w-full object-cover" />
177
+ <span class="relative inline-flex h-6 w-6 items-center justify-center">
178
+ <span
179
+ data-testid="site-logo-glow"
180
+ aria-hidden="true"
181
+ class="pointer-events-none absolute -inset-1.5 rounded-full transition-opacity duration-300"
182
+ style={logoGlowStyle}
183
+ ></span>
184
+ <span class={`relative ${logoRingClass}`} aria-hidden="true">
185
+ <img src={resolveLogo(siteLogo)} alt="" width="24" height="24" class="h-full w-full object-cover" />
186
+ </span>
149
187
  </span>
150
188
  {:else}
151
189
  <span class={ringWrapClass}><span class={ringDotClass}></span></span>
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Pure positioning helper for `Popover.svelte`.
3
+ *
4
+ * Computes the viewport-coord `{ top, left }` for the panel given
5
+ * the trigger geometry, panel size, viewport size, and placement.
6
+ * Lives in its own module so the layout policy (which side wins on
7
+ * tight viewports, how flips clamp to the margin, etc.) is unit-
8
+ * testable without spinning up the Svelte component + jsdom.
9
+ *
10
+ * Auto-flip behaviour: the documented placements only mention
11
+ * `bottom-*`, but when the trigger sits near the bottom of the
12
+ * viewport (e.g. a pill on a session footer) the panel would open
13
+ * off-screen. We treat the placement's `bottom` half as a HINT
14
+ * rather than a hard rule \u2014 if the panel doesn't fit below AND
15
+ * there's more room above than below, we flip the vertical anchor
16
+ * to open upward. Horizontal alignment (`-start` / `-end` /
17
+ * centred) is preserved across the flip.
18
+ */
19
+
20
+ export interface TriggerRect {
21
+ top: number
22
+ bottom: number
23
+ left: number
24
+ right: number
25
+ width: number
26
+ }
27
+
28
+ export type PopoverPlacement = 'bottom-start' | 'bottom-end' | 'bottom'
29
+
30
+ export interface ComputePanelPositionInput {
31
+ triggerRect: TriggerRect
32
+ panelWidth: number
33
+ panelHeight: number
34
+ viewportWidth: number
35
+ viewportHeight: number
36
+ placement: PopoverPlacement
37
+ /** Gutter between the panel and the viewport edge on every side. */
38
+ margin: number
39
+ /** Gap between the trigger edge and the panel edge along the axis. */
40
+ offset: number
41
+ }
42
+
43
+ export interface PanelPosition {
44
+ top: number
45
+ left: number
46
+ }
47
+
48
+ export const computePanelPosition = (input: ComputePanelPositionInput): PanelPosition => {
49
+ const {
50
+ triggerRect,
51
+ panelWidth,
52
+ panelHeight,
53
+ viewportWidth,
54
+ viewportHeight,
55
+ placement,
56
+ margin,
57
+ offset,
58
+ } = input
59
+
60
+ // Horizontal: align per placement, then clamp into the viewport so
61
+ // edge-anchored triggers don't push the panel off-screen.
62
+ let left: number
63
+ if (placement === 'bottom-start') {
64
+ left = triggerRect.left
65
+ } else if (placement === 'bottom-end') {
66
+ left = triggerRect.right - panelWidth
67
+ } else {
68
+ left = triggerRect.left + triggerRect.width / 2 - panelWidth / 2
69
+ }
70
+ left = Math.max(margin, Math.min(left, viewportWidth - panelWidth - margin))
71
+
72
+ // Vertical: prefer below the trigger (documented behaviour). Flip
73
+ // above when:
74
+ // 1. the panel does NOT fit below (panel height + offset would
75
+ // cross the bottom margin), AND
76
+ // 2. above has more room than below.
77
+ // When neither side fully fits, we still prefer the side with more
78
+ // breathing room \u2014 the panel then gets clamped to the viewport
79
+ // margin so its content scrolls instead of clipping at the edge.
80
+ const spaceBelow = viewportHeight - triggerRect.bottom - margin
81
+ const spaceAbove = triggerRect.top - margin
82
+ const fitsBelow = panelHeight + offset <= spaceBelow
83
+
84
+ let top: number
85
+ if (fitsBelow || spaceBelow >= spaceAbove) {
86
+ top = triggerRect.bottom + offset
87
+ } else {
88
+ top = Math.max(margin, triggerRect.top - offset - panelHeight)
89
+ }
90
+
91
+ return { top, left }
92
+ }
package/src/lib/index.ts CHANGED
@@ -20,6 +20,7 @@ export { default as TabPanel } from './components/TabPanel.svelte'
20
20
  export { default as Toggle } from './components/Toggle.svelte'
21
21
  export { default as RowToggle } from './components/RowToggle.svelte'
22
22
  export { default as Modal } from './components/Modal.svelte'
23
+ export type { ModalSize } from './components/Modal.svelte'
23
24
  export { default as Sparkline } from './components/Sparkline.svelte'
24
25
  export { default as SparklineMatrix } from './components/SparklineMatrix.svelte'
25
26
  export type { SparklineMatrixRow } from './components/SparklineMatrix.svelte'