dssoca 0.2.0 → 0.8.2

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 (51) hide show
  1. package/README.md +54 -18
  2. package/dist/components/Accordion.svelte +281 -0
  3. package/dist/components/Accordion.svelte.d.ts +47 -0
  4. package/dist/components/Badge.svelte +144 -7
  5. package/dist/components/Badge.svelte.d.ts +31 -2
  6. package/dist/components/BottomNav.svelte +216 -0
  7. package/dist/components/BottomNav.svelte.d.ts +30 -0
  8. package/dist/components/Button.svelte +187 -6
  9. package/dist/components/Button.svelte.d.ts +24 -4
  10. package/dist/components/Card.svelte +203 -8
  11. package/dist/components/Card.svelte.d.ts +21 -1
  12. package/dist/components/EmptyState.svelte +123 -36
  13. package/dist/components/EmptyState.svelte.d.ts +17 -1
  14. package/dist/components/Icon.svelte +191 -12
  15. package/dist/components/Icon.svelte.d.ts +69 -2
  16. package/dist/components/Image.svelte +430 -0
  17. package/dist/components/Image.svelte.d.ts +49 -0
  18. package/dist/components/Input.svelte +206 -18
  19. package/dist/components/Input.svelte.d.ts +29 -0
  20. package/dist/components/Link.svelte +148 -0
  21. package/dist/components/Link.svelte.d.ts +22 -0
  22. package/dist/components/LogStream.svelte +403 -21
  23. package/dist/components/LogStream.svelte.d.ts +37 -6
  24. package/dist/components/Menu.svelte +355 -0
  25. package/dist/components/Menu.svelte.d.ts +46 -0
  26. package/dist/components/MetricTile.svelte +161 -7
  27. package/dist/components/MetricTile.svelte.d.ts +23 -0
  28. package/dist/components/SegmentedControl.svelte +208 -0
  29. package/dist/components/SegmentedControl.svelte.d.ts +30 -0
  30. package/dist/components/ServiceCard.svelte +270 -9
  31. package/dist/components/ServiceCard.svelte.d.ts +16 -1
  32. package/dist/components/Sidebar.svelte +391 -25
  33. package/dist/components/Sidebar.svelte.d.ts +20 -6
  34. package/dist/components/Sparkline.svelte +213 -6
  35. package/dist/components/Sparkline.svelte.d.ts +24 -0
  36. package/dist/components/Toaster.svelte +273 -60
  37. package/dist/components/Toaster.svelte.d.ts +9 -16
  38. package/dist/components/Topbar.svelte +273 -17
  39. package/dist/components/Topbar.svelte.d.ts +23 -0
  40. package/dist/config.d.ts +32 -14
  41. package/dist/config.js +43 -17
  42. package/dist/dssoca.config.d.ts +50 -0
  43. package/dist/dssoca.config.js +45 -0
  44. package/dist/index.d.ts +12 -5
  45. package/dist/index.js +14 -4
  46. package/dist/theme.css +587 -382
  47. package/dist/toast.svelte.d.ts +49 -5
  48. package/dist/toast.svelte.js +165 -9
  49. package/package.json +17 -3
  50. package/dist/components/PassocaMark.svelte +0 -15
  51. package/dist/components/PassocaMark.svelte.d.ts +0 -7
package/README.md CHANGED
@@ -1,16 +1,21 @@
1
1
  # dssoca
2
2
 
3
+ [![npm](https://img.shields.io/npm/v/dssoca.svg)](https://www.npmjs.com/package/dssoca)
4
+ [![CI](https://github.com/httpassoca/dssoca/actions/workflows/ci.yml/badge.svg)](https://github.com/httpassoca/dssoca/actions/workflows/ci.yml)
5
+ [![license](https://img.shields.io/npm/l/dssoca.svg)](./LICENSE)
6
+
3
7
  A Svelte 5 design system — **signal green on near-black, monospace-forward, zero border-radius**
4
8
  everywhere. Configured along two orthogonal axes:
5
9
 
6
- | Axis | Attribute | Values | Default |
7
- |-------------|----------------|---------------------|---------|
8
- | **Color** | `data-theme` | `dark` · `light` | `dark` |
9
- | **Density** | `data-density` | `comfy` · `compact` | `comfy` |
10
+ | Axis | Attribute | Values | Default |
11
+ |------------|----------------------|--------------------|---------|
12
+ | **Color** | `data-theme` | `dark` · `light` | `dark` |
13
+ | **Size** | `data-size-variant` | `sm` · `md` · `lg` | `md` |
10
14
 
11
- Components are thin wrappers over a token-driven `theme.css`; flip an axis on any ancestor
12
- (usually `<html>`) and everything below rescales/recolors. See [`DESIGN.md`](./DESIGN.md) for the
13
- full rationale and [`docs/tokens.md`](./docs/tokens.md) for the token reference.
15
+ Each component styles itself in a scoped block and consumes token-driven CSS custom properties;
16
+ flip an axis on any ancestor (usually `<html>`) and everything below rescales/recolors. Components
17
+ also take a per-instance `size` prop. See [`DESIGN.md`](./DESIGN.md) for the full rationale and
18
+ [`docs/tokens.md`](./docs/tokens.md) for the token reference.
14
19
 
15
20
  ## Install
16
21
 
@@ -44,11 +49,14 @@ Import the stylesheet once (global tokens + base styles), then use components:
44
49
  ```ts
45
50
  import { applyDesignConfig, designAttributes } from 'dssoca'
46
51
 
47
- // flip one or both axes; merges over current config; writes to <html>
48
- applyDesignConfig({ density: 'compact' }) // dense dashboard
49
- applyDesignConfig({ theme: 'light' }) // keeps density
52
+ // flip axes; merges over current config; writes to <html>
53
+ applyDesignConfig({ sizeVariant: 'sm' }) // global: everything small
54
+ applyDesignConfig({ theme: 'light' }) // keeps size
55
+ applyDesignConfig({ componentsSize: { Button: 'lg' } }) // per-component default
50
56
  ```
51
57
 
58
+ Per instance, just set the `size` prop: `<Button size="lg">`.
59
+
52
60
  SSR / no-flash — spread the attributes directly in markup instead of mutating the DOM:
53
61
 
54
62
  ```svelte
@@ -56,34 +64,62 @@ SSR / no-flash — spread the attributes directly in markup instead of mutating
56
64
  import { designAttributes } from 'dssoca'
57
65
  </script>
58
66
 
59
- <html {...designAttributes({ density: 'compact' })}>
67
+ <html {...designAttributes({ sizeVariant: 'sm' })}>
60
68
  ```
61
69
 
62
70
  ## What's in the box
63
71
 
64
- **Components:** `PassocaMark`, `Icon`, `Badge`, `Button`, `Input`, `Card`, `Sparkline`,
72
+ **Components:** `Icon`, `Badge`, `Button`, `Input`, `Card`, `Sparkline`,
65
73
  `ServiceCard`, `MetricTile`, `Topbar`, `Sidebar`, `LogStream`, `Toaster`, `EmptyState`.
66
74
 
67
75
  **Toasts:** `toast` (`.success` / `.error` / `.info`) + the `toasts` store — render once with
68
76
  `<Toaster />`.
69
77
 
70
- **Config:** `applyDesignConfig`, `designAttributes`, `getDesignConfig`, `defaultDesignConfig`, and
71
- the `ColorTheme` / `Density` / `DesignConfig` types.
78
+ **Config:** `dssocaConfig` (the manifest), `applyDesignConfig`, `designAttributes`,
79
+ `getDesignConfig`, `resolveComponentSize`, `defaultDesignConfig`, and the `ColorTheme` / `Size` /
80
+ `DesignConfig` / `ComponentsSize` types.
81
+
82
+ ## Accessibility
83
+
84
+ Targets **WCAG 2.2 AA**: native/keyboard-operable controls (Enter + Space), labelled
85
+ inputs (auto-associated ids, `aria-invalid` / `aria-describedby`), live-region toasts
86
+ (`role="status"`, `role="alert"` for errors), `aria-current` on active nav/tabs, decorative
87
+ icons/graphics `aria-hidden`, AA-contrast text tokens, and `prefers-reduced-motion` support
88
+ (motion-duration tokens collapse; the toast transition is guarded). Components are checked with
89
+ `vitest-axe` in unit tests and `@storybook/addon-a11y` in Storybook.
72
90
 
73
91
  ## House rules
74
92
 
75
93
  - **Zero border-radius.** Every radius token is `0` — never override.
76
- - New chrome reads density tokens (`--ss-*`), not hardcoded px, so it rescales with `data-density`.
94
+ - New chrome reads size tokens (`--ss-*`), not hardcoded px, so it rescales with `data-size-variant`.
95
+
96
+ ## Documentation
97
+
98
+ A custom documentation site lives in [`documentation/`](./documentation) (SvelteKit + mdsvex). It
99
+ dogfoods dssoca for its own chrome and covers the Introduction, Installation, Theming & config, a
100
+ live Tokens gallery, and a page per component that embeds the component's live Storybook story.
101
+
102
+ ```sh
103
+ pnpm docs:dev # docs site (run `pnpm storybook` alongside for the live demos)
104
+ pnpm docs:build # static build of the docs site
105
+ ```
106
+
107
+ The docs app is local-only and is **not** part of the published npm package.
77
108
 
78
109
  ## Develop
79
110
 
80
111
  ```sh
81
112
  pnpm install
82
- pnpm dev # showcase app with live theme + density toggles
83
- pnpm test # Vitest component/unit suite
84
- pnpm pack # build dist/ (svelte-package) + validate (publint)
113
+ pnpm dev # library SvelteKit app (stub landing)
114
+ pnpm storybook # interactive component explorer (:6006)
115
+ pnpm docs:dev # documentation site
116
+ pnpm test # Vitest component/unit suite
117
+ pnpm pack # build dist/ (svelte-package) + validate (publint)
85
118
  ```
86
119
 
120
+ See [`CONTRIBUTING.md`](./CONTRIBUTING.md) for the full workflow and [`CHANGELOG.md`](./CHANGELOG.md)
121
+ for release history.
122
+
87
123
  ## License
88
124
 
89
125
  [MIT](./LICENSE) © Rafael Passoca
@@ -0,0 +1,281 @@
1
+ <script module lang="ts">
2
+ /** One collapsible section of the Accordion. */
3
+ export interface AccordionItem {
4
+ /** Stable identifier — used for open state, `value`, and ARIA wiring. */
5
+ id: string
6
+ /** Header text (also the accessible name of the toggle button). */
7
+ label: string
8
+ /** Optional secondary text shown faint after the label. */
9
+ hint?: string
10
+ /** Disable this section — the header is not togglable. */
11
+ disabled?: boolean
12
+ }
13
+
14
+ /** Controlled `value`: a single open id (single mode) or a list (`multiple`). */
15
+ export type AccordionValue = string | string[] | undefined
16
+ </script>
17
+
18
+ <script lang="ts">
19
+ import { untrack, type Snippet } from 'svelte'
20
+ import { resolveComponentSize, type Size } from '../config.js'
21
+
22
+ interface Props {
23
+ /** The collapsible sections, top to bottom. */
24
+ items: AccordionItem[]
25
+ /**
26
+ * Content renderer for each section's panel; receives the item.
27
+ * {#snippet panel(item)} … {/snippet}
28
+ */
29
+ panel: Snippet<[AccordionItem]>
30
+ /** Optional header renderer; overrides the default label + hint markup. */
31
+ header?: Snippet<[AccordionItem]>
32
+ /** Allow several sections open at once. Default: single-open. */
33
+ multiple?: boolean
34
+ /**
35
+ * Controlled open state (bindable). In single mode a `string` id (or
36
+ * `undefined`); in `multiple` mode a `string[]` of open ids. When omitted,
37
+ * the component manages its own (uncontrolled) state.
38
+ */
39
+ value?: AccordionValue
40
+ /** Open these sections initially when uncontrolled. */
41
+ defaultValue?: AccordionValue
42
+ /** Fired with the new open value whenever a section toggles. */
43
+ onChange?: (value: AccordionValue) => void
44
+ /** Token size (sm|md|lg); inherits the global size when unset. */
45
+ size?: Size
46
+ /** Heading level wrapping each header button (for the document outline). */
47
+ headingLevel?: 1 | 2 | 3 | 4 | 5 | 6
48
+ /** Base id used to namespace the generated header/panel ids. */
49
+ idBase?: string
50
+ }
51
+
52
+ let {
53
+ items,
54
+ panel,
55
+ header,
56
+ multiple = false,
57
+ value = $bindable(),
58
+ defaultValue,
59
+ onChange,
60
+ size,
61
+ headingLevel = 3,
62
+ idBase = `ss-acc-${Math.random().toString(36).slice(2, 8)}`,
63
+ }: Props = $props()
64
+
65
+ // --- open-state model ----------------------------------------------------
66
+ // Normalise whatever `value`/`defaultValue` shape we were handed into a Set.
67
+ function toSet(v: AccordionValue): Set<string> {
68
+ if (v == null) return new Set()
69
+ return new Set(Array.isArray(v) ? v : [v])
70
+ }
71
+
72
+ // Uncontrolled fallback store, seeded once from defaultValue. `untrack`
73
+ // marks the read as an intentional one-time snapshot (not a live binding),
74
+ // silencing the "referenced locally" reactivity warning.
75
+ let internal = $state<Set<string>>(untrack(() => toSet(defaultValue)))
76
+
77
+ // Controlled when the consumer passes `value` (incl. bound); else internal.
78
+ const controlled = $derived(value !== undefined)
79
+ const open = $derived(controlled ? toSet(value) : internal)
80
+
81
+ const isOpen = (item: AccordionItem): boolean => open.has(item.id)
82
+
83
+ /** Project a Set back to the public value shape for `value`/`onChange`. */
84
+ function project(next: Set<string>): AccordionValue {
85
+ if (multiple) return [...next]
86
+ return next.size ? [...next][0] : undefined
87
+ }
88
+
89
+ function toggle(item: AccordionItem) {
90
+ if (item.disabled) return
91
+ const next = new Set(open)
92
+ if (next.has(item.id)) {
93
+ next.delete(item.id)
94
+ } else {
95
+ if (!multiple) next.clear() // single-open: opening one closes others
96
+ next.add(item.id)
97
+ }
98
+ if (controlled) {
99
+ value = project(next) // keep the bound value in sync
100
+ } else {
101
+ internal = next
102
+ }
103
+ onChange?.(project(next))
104
+ }
105
+
106
+ // --- roving keyboard nav across headers (WAI-ARIA Accordion pattern) -----
107
+ let headerEls = $state<Array<HTMLButtonElement | null>>([])
108
+
109
+ function focusHeader(index: number) {
110
+ const n = items.length
111
+ if (n === 0) return
112
+ const i = ((index % n) + n) % n // wrap
113
+ headerEls[i]?.focus()
114
+ }
115
+
116
+ function onHeaderKeydown(e: KeyboardEvent, index: number) {
117
+ switch (e.key) {
118
+ case 'ArrowDown':
119
+ e.preventDefault(); focusHeader(index + 1); break
120
+ case 'ArrowUp':
121
+ e.preventDefault(); focusHeader(index - 1); break
122
+ case 'Home':
123
+ e.preventDefault(); focusHeader(0); break
124
+ case 'End':
125
+ e.preventDefault(); focusHeader(items.length - 1); break
126
+ // Enter / Space activate the native <button>; no extra handling needed.
127
+ }
128
+ }
129
+
130
+ const headerId = (item: AccordionItem) => `${idBase}-h-${item.id}`
131
+ const panelId = (item: AccordionItem) => `${idBase}-p-${item.id}`
132
+ </script>
133
+
134
+ <div class="ss-accordion" data-size-variant={resolveComponentSize('Accordion', size)}>
135
+ {#each items as item, i (item.id)}
136
+ {@const expanded = isOpen(item)}
137
+ <div class="item" class:open={expanded} class:disabled={item.disabled}>
138
+ <div role="heading" aria-level={headingLevel} class="heading">
139
+ <button
140
+ bind:this={headerEls[i]}
141
+ type="button"
142
+ class="head"
143
+ id={headerId(item)}
144
+ aria-expanded={expanded}
145
+ aria-controls={panelId(item)}
146
+ aria-disabled={item.disabled || undefined}
147
+ onclick={() => toggle(item)}
148
+ onkeydown={(e) => onHeaderKeydown(e, i)}
149
+ >
150
+ {#if header}
151
+ {@render header(item)}
152
+ {:else}
153
+ <span class="title">{item.label}</span>
154
+ {#if item.hint}<span class="hint">{item.hint}</span>{/if}
155
+ {/if}
156
+ <span class="chevron" aria-hidden="true"></span>
157
+ </button>
158
+ </div>
159
+ <div
160
+ class="panel"
161
+ id={panelId(item)}
162
+ role="region"
163
+ aria-labelledby={headerId(item)}
164
+ hidden={!expanded}
165
+ >
166
+ <div class="panel-inner">
167
+ {@render panel(item)}
168
+ </div>
169
+ </div>
170
+ </div>
171
+ {/each}
172
+ </div>
173
+
174
+ <style>.ss-accordion {
175
+ --ss-acc-head-py: var(--ss-row-py);
176
+ --ss-acc-head-px: var(--ss-row-px);
177
+ --ss-acc-body-py: var(--ss-s-3);
178
+ --ss-acc-body-px: var(--ss-row-px);
179
+ --ss-acc-chevron: 9px;
180
+ --ss-acc-gap: var(--ss-s-3);
181
+ }
182
+ .ss-accordion[data-size-variant=sm] {
183
+ --ss-acc-head-py: var(--ss-s-1);
184
+ --ss-acc-head-px: var(--ss-s-2);
185
+ --ss-acc-body-py: var(--ss-s-2);
186
+ --ss-acc-body-px: var(--ss-s-2);
187
+ --ss-acc-chevron: 7px;
188
+ --ss-acc-gap: var(--ss-s-2);
189
+ }
190
+ .ss-accordion[data-size-variant=lg] {
191
+ --ss-acc-head-py: var(--ss-s-3);
192
+ --ss-acc-head-px: var(--ss-s-4);
193
+ --ss-acc-body-py: var(--ss-s-4);
194
+ --ss-acc-body-px: var(--ss-s-4);
195
+ --ss-acc-chevron: 11px;
196
+ --ss-acc-gap: var(--ss-s-4);
197
+ }
198
+ .ss-accordion {
199
+ display: flex;
200
+ flex-direction: column;
201
+ border: 1px solid var(--ss-line);
202
+ background: var(--ss-bg);
203
+ }
204
+ .ss-accordion .item + .item {
205
+ border-top: 1px solid var(--ss-line);
206
+ }
207
+ .ss-accordion .heading {
208
+ margin: 0;
209
+ font: inherit;
210
+ }
211
+ .ss-accordion .head {
212
+ display: flex;
213
+ align-items: center;
214
+ gap: var(--ss-acc-gap);
215
+ width: 100%;
216
+ box-sizing: border-box;
217
+ padding: var(--ss-acc-head-py) var(--ss-acc-head-px);
218
+ background: none;
219
+ border: 0;
220
+ text-align: left;
221
+ cursor: pointer;
222
+ color: var(--ss-fg);
223
+ font: 500 var(--ss-ui-md) var(--ss-font-mono);
224
+ transition: background var(--ss-dur-fast) var(--ss-ease), color var(--ss-dur-fast) var(--ss-ease);
225
+ }
226
+ .ss-accordion .head:hover:not([aria-disabled=true]) {
227
+ background: var(--ss-hover);
228
+ }
229
+ .ss-accordion .head:focus-visible {
230
+ outline: 2px solid var(--ss-primary);
231
+ outline-offset: -2px;
232
+ }
233
+ .ss-accordion .head[aria-disabled=true] {
234
+ cursor: not-allowed;
235
+ color: var(--ss-fg-faint);
236
+ }
237
+ .ss-accordion .head .title {
238
+ flex: 1 1 auto;
239
+ min-width: 0;
240
+ }
241
+ .ss-accordion .head .hint {
242
+ flex: 0 0 auto;
243
+ color: var(--ss-fg-faint);
244
+ font-size: var(--ss-ui-sm);
245
+ }
246
+ .ss-accordion .head .chevron {
247
+ flex: 0 0 auto;
248
+ width: var(--ss-acc-chevron);
249
+ height: var(--ss-acc-chevron);
250
+ border-right: 1.5px solid currentColor;
251
+ border-bottom: 1.5px solid currentColor;
252
+ transform: rotate(45deg);
253
+ transform-origin: center;
254
+ transition: transform var(--ss-dur) var(--ss-ease);
255
+ color: var(--ss-fg-muted);
256
+ }
257
+ .ss-accordion .item.open .head .chevron {
258
+ transform: rotate(225deg);
259
+ }
260
+ .ss-accordion .panel {
261
+ display: grid;
262
+ grid-template-rows: 0fr;
263
+ transition: grid-template-rows var(--ss-dur) var(--ss-ease);
264
+ }
265
+ .ss-accordion .panel[hidden] {
266
+ display: grid;
267
+ grid-template-rows: 0fr;
268
+ }
269
+ .ss-accordion .panel:not([hidden]) {
270
+ grid-template-rows: 1fr;
271
+ }
272
+ .ss-accordion .panel-inner {
273
+ overflow: hidden;
274
+ min-height: 0;
275
+ }
276
+ .ss-accordion .panel:not([hidden]) .panel-inner {
277
+ padding: var(--ss-acc-body-py) var(--ss-acc-body-px);
278
+ color: var(--ss-fg-muted);
279
+ font-size: var(--ss-ui-md);
280
+ line-height: var(--ss-leading);
281
+ }</style>
@@ -0,0 +1,47 @@
1
+ /** One collapsible section of the Accordion. */
2
+ export interface AccordionItem {
3
+ /** Stable identifier — used for open state, `value`, and ARIA wiring. */
4
+ id: string;
5
+ /** Header text (also the accessible name of the toggle button). */
6
+ label: string;
7
+ /** Optional secondary text shown faint after the label. */
8
+ hint?: string;
9
+ /** Disable this section — the header is not togglable. */
10
+ disabled?: boolean;
11
+ }
12
+ /** Controlled `value`: a single open id (single mode) or a list (`multiple`). */
13
+ export type AccordionValue = string | string[] | undefined;
14
+ import { type Snippet } from 'svelte';
15
+ import { type Size } from '../config.js';
16
+ interface Props {
17
+ /** The collapsible sections, top to bottom. */
18
+ items: AccordionItem[];
19
+ /**
20
+ * Content renderer for each section's panel; receives the item.
21
+ * {#snippet panel(item)} … {/snippet}
22
+ */
23
+ panel: Snippet<[AccordionItem]>;
24
+ /** Optional header renderer; overrides the default label + hint markup. */
25
+ header?: Snippet<[AccordionItem]>;
26
+ /** Allow several sections open at once. Default: single-open. */
27
+ multiple?: boolean;
28
+ /**
29
+ * Controlled open state (bindable). In single mode a `string` id (or
30
+ * `undefined`); in `multiple` mode a `string[]` of open ids. When omitted,
31
+ * the component manages its own (uncontrolled) state.
32
+ */
33
+ value?: AccordionValue;
34
+ /** Open these sections initially when uncontrolled. */
35
+ defaultValue?: AccordionValue;
36
+ /** Fired with the new open value whenever a section toggles. */
37
+ onChange?: (value: AccordionValue) => void;
38
+ /** Token size (sm|md|lg); inherits the global size when unset. */
39
+ size?: Size;
40
+ /** Heading level wrapping each header button (for the document outline). */
41
+ headingLevel?: 1 | 2 | 3 | 4 | 5 | 6;
42
+ /** Base id used to namespace the generated header/panel ids. */
43
+ idBase?: string;
44
+ }
45
+ declare const Accordion: import("svelte").Component<Props, {}, "value">;
46
+ type Accordion = ReturnType<typeof Accordion>;
47
+ export default Accordion;
@@ -1,16 +1,153 @@
1
1
  <script lang="ts">
2
2
  import type { Snippet } from 'svelte'
3
+ import { resolveComponentSize, type Size } from '../config.js'
4
+
5
+ type Tone = 'up' | 'deg' | 'down' | 'maint' | 'info' | 'neutral'
3
6
 
4
7
  interface Props {
5
- tone?: 'up' | 'deg' | 'down' | 'info'
6
- children: Snippet
8
+ /** Semantic tone; `neutral` is the safe baseline for non-status labels. */
9
+ tone?: Tone
10
+ /** Token size (sm|md|lg); inherits the global size when unset. */
11
+ size?: Size
12
+ /** Render the leading status dot. Off by default — opt in per badge. */
13
+ dot?: boolean
14
+ /**
15
+ * Optional numeric badge. When set, renders the count (clamped to `max`).
16
+ * A `count` of 0 renders nothing.
17
+ */
18
+ count?: number
19
+ /** Cap for `count`; values above render as `${max}+`. Defaults to 99. */
20
+ max?: number
21
+ /**
22
+ * When set, treats the badge as a live status region
23
+ * (`role="status"` + `aria-live="polite"`) so AT announces changes.
24
+ * Static category tags stay plain spans.
25
+ */
26
+ live?: boolean
27
+ /**
28
+ * Accessible label. Required for dot-only / label-less badges so the
29
+ * status isn't conveyed by colour alone (WCAG 1.4.1). Also names the
30
+ * dismiss button's target.
31
+ */
32
+ label?: string
33
+ /** When provided, renders a trailing, keyboard-focusable dismiss button. */
34
+ ondismiss?: () => void
35
+ /** Badge text. Optional: omit for a dot-only or count-only badge. */
36
+ children?: Snippet
7
37
  }
8
- let { tone = 'info', children }: Props = $props()
9
38
 
10
- const hasDot = $derived(['up', 'deg', 'down', 'info'].includes(tone))
39
+ let {
40
+ tone = 'info',
41
+ size,
42
+ dot = false,
43
+ count,
44
+ max = 99,
45
+ live = false,
46
+ label,
47
+ ondismiss,
48
+ children,
49
+ }: Props = $props()
50
+
51
+ // Numeric badge: clamp to max, hide entirely when zero.
52
+ const showCount = $derived(count !== undefined && count !== 0)
53
+ const countText = $derived(
54
+ count === undefined ? '' : count > max ? `${max}+` : `${count}`,
55
+ )
56
+
57
+ const hasLabel = $derived(children !== undefined)
58
+ // Dot-only / count-only badges have no visible text → carry an aria-label so
59
+ // the status isn't conveyed by colour alone (WCAG 1.4.1).
60
+ const ariaLabel = $derived(!hasLabel ? label : undefined)
61
+ // `live` wins (role=status). Otherwise a label-less badge needs a role for
62
+ // its aria-label to be valid (a bare span + aria-label is prohibited); use
63
+ // `img` so AT announces it as a single named status indicator.
64
+ const role = $derived(live ? 'status' : !hasLabel ? 'img' : undefined)
11
65
  </script>
12
66
 
13
- <span class="ss-badge {tone}">
14
- {#if hasDot}<span class="dot"></span>{/if}
15
- {@render children()}
67
+ <span
68
+ class="ss-badge {tone}"
69
+ class:label-less={!hasLabel}
70
+ data-size-variant={resolveComponentSize('Badge', size)}
71
+ {role}
72
+ aria-live={live ? 'polite' : undefined}
73
+ aria-label={ariaLabel}
74
+ >
75
+ {#if dot}<span class="dot" aria-hidden="true"></span>{/if}
76
+ {#if hasLabel}{@render children?.()}{/if}
77
+ {#if showCount}<span class="count">{countText}</span>{/if}
78
+ {#if ondismiss}
79
+ <button
80
+ class="x"
81
+ type="button"
82
+ aria-label={label ? `Remove ${label}` : 'Remove'}
83
+ onclick={ondismiss}
84
+ >×</button>
85
+ {/if}
16
86
  </span>
87
+
88
+ <style>.ss-badge {
89
+ font-family: var(--ss-font-mono);
90
+ font-size: var(--ss-ui-xs);
91
+ padding: var(--ss-badge-py) var(--ss-badge-px);
92
+ letter-spacing: 0.05em;
93
+ display: inline-flex;
94
+ align-items: center;
95
+ gap: var(--ss-badge-gap);
96
+ text-transform: lowercase;
97
+ border: 1px solid var(--ss-line);
98
+ }
99
+ .ss-badge.up {
100
+ background: var(--ss-badge-up-bg);
101
+ color: var(--ss-primary);
102
+ border-color: var(--ss-badge-up-border);
103
+ }
104
+ .ss-badge.deg {
105
+ background: var(--ss-badge-deg-bg);
106
+ color: var(--ss-yellow);
107
+ border-color: var(--ss-badge-deg-border);
108
+ }
109
+ .ss-badge.down {
110
+ background: var(--ss-badge-down-bg);
111
+ color: var(--ss-red);
112
+ border-color: var(--ss-badge-down-border);
113
+ }
114
+ .ss-badge.maint {
115
+ background: var(--ss-badge-maint-bg);
116
+ color: var(--ss-blue);
117
+ border-color: var(--ss-badge-maint-border);
118
+ }
119
+ .ss-badge.info {
120
+ background: var(--ss-badge-info-bg);
121
+ color: var(--ss-cyan);
122
+ border-color: var(--ss-badge-info-border);
123
+ }
124
+ .ss-badge.neutral {
125
+ background: var(--ss-badge-neutral-bg);
126
+ color: var(--ss-fg-muted);
127
+ border-color: var(--ss-badge-neutral-border);
128
+ }
129
+ .ss-badge .dot {
130
+ width: var(--ss-badge-dot);
131
+ height: var(--ss-badge-dot);
132
+ background: currentColor;
133
+ flex: none;
134
+ }
135
+ .ss-badge .count {
136
+ text-transform: none;
137
+ font-variant-numeric: tabular-nums;
138
+ }
139
+ .ss-badge .x {
140
+ flex: none;
141
+ background: none;
142
+ border: none;
143
+ color: currentColor;
144
+ cursor: pointer;
145
+ font-size: var(--ss-size-sm);
146
+ line-height: 1;
147
+ padding: 0;
148
+ margin-inline-start: calc(var(--ss-badge-gap) * -0.25);
149
+ opacity: 0.8;
150
+ }
151
+ .ss-badge .x:hover, .ss-badge .x:focus-visible {
152
+ opacity: 1;
153
+ }</style>
@@ -1,7 +1,36 @@
1
1
  import type { Snippet } from 'svelte';
2
+ import { type Size } from '../config.js';
3
+ type Tone = 'up' | 'deg' | 'down' | 'maint' | 'info' | 'neutral';
2
4
  interface Props {
3
- tone?: 'up' | 'deg' | 'down' | 'info';
4
- children: Snippet;
5
+ /** Semantic tone; `neutral` is the safe baseline for non-status labels. */
6
+ tone?: Tone;
7
+ /** Token size (sm|md|lg); inherits the global size when unset. */
8
+ size?: Size;
9
+ /** Render the leading status dot. Off by default — opt in per badge. */
10
+ dot?: boolean;
11
+ /**
12
+ * Optional numeric badge. When set, renders the count (clamped to `max`).
13
+ * A `count` of 0 renders nothing.
14
+ */
15
+ count?: number;
16
+ /** Cap for `count`; values above render as `${max}+`. Defaults to 99. */
17
+ max?: number;
18
+ /**
19
+ * When set, treats the badge as a live status region
20
+ * (`role="status"` + `aria-live="polite"`) so AT announces changes.
21
+ * Static category tags stay plain spans.
22
+ */
23
+ live?: boolean;
24
+ /**
25
+ * Accessible label. Required for dot-only / label-less badges so the
26
+ * status isn't conveyed by colour alone (WCAG 1.4.1). Also names the
27
+ * dismiss button's target.
28
+ */
29
+ label?: string;
30
+ /** When provided, renders a trailing, keyboard-focusable dismiss button. */
31
+ ondismiss?: () => void;
32
+ /** Badge text. Optional: omit for a dot-only or count-only badge. */
33
+ children?: Snippet;
5
34
  }
6
35
  declare const Badge: import("svelte").Component<Props, {}, "">;
7
36
  type Badge = ReturnType<typeof Badge>;