@mcpview/components 0.10.0 → 0.12.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 (50) hide show
  1. package/README.md +73 -9
  2. package/esm/preact-components.d.ts +1 -1
  3. package/esm/preact-components.d.ts.map +1 -1
  4. package/esm/preact-context.d.ts +10 -0
  5. package/esm/preact-context.d.ts.map +1 -0
  6. package/esm/preact-context.js +6 -0
  7. package/esm/src/context-basket-labels.d.ts +21 -0
  8. package/esm/src/context-basket-labels.d.ts.map +1 -0
  9. package/esm/src/context-basket-labels.js +40 -0
  10. package/esm/src/context-basket-styles.d.ts +8 -0
  11. package/esm/src/context-basket-styles.d.ts.map +1 -0
  12. package/esm/src/context-basket-styles.js +113 -0
  13. package/esm/src/preact/context-basket.d.ts +61 -0
  14. package/esm/src/preact/context-basket.d.ts.map +1 -0
  15. package/esm/src/preact/context-basket.js +201 -0
  16. package/esm/src/preact/mod.d.ts +1 -1
  17. package/esm/src/preact/mod.d.ts.map +1 -1
  18. package/esm/src/preact/plots.d.ts +15 -4
  19. package/esm/src/preact/plots.d.ts.map +1 -1
  20. package/esm/src/preact/plots.js +39 -25
  21. package/esm/src/preact/use-active-context.d.ts +15 -0
  22. package/esm/src/preact/use-active-context.d.ts.map +1 -0
  23. package/esm/src/preact/use-active-context.js +28 -0
  24. package/esm/src/theme.d.ts.map +1 -1
  25. package/esm/src/theme.js +92 -5
  26. package/package.json +10 -2
  27. package/script/preact-components.d.ts +1 -1
  28. package/script/preact-components.d.ts.map +1 -1
  29. package/script/preact-context.d.ts +10 -0
  30. package/script/preact-context.d.ts.map +1 -0
  31. package/script/preact-context.js +17 -0
  32. package/script/src/context-basket-labels.d.ts +21 -0
  33. package/script/src/context-basket-labels.d.ts.map +1 -0
  34. package/script/src/context-basket-labels.js +44 -0
  35. package/script/src/context-basket-styles.d.ts +8 -0
  36. package/script/src/context-basket-styles.d.ts.map +1 -0
  37. package/script/src/context-basket-styles.js +116 -0
  38. package/script/src/preact/context-basket.d.ts +61 -0
  39. package/script/src/preact/context-basket.d.ts.map +1 -0
  40. package/script/src/preact/context-basket.js +204 -0
  41. package/script/src/preact/mod.d.ts +1 -1
  42. package/script/src/preact/mod.d.ts.map +1 -1
  43. package/script/src/preact/plots.d.ts +15 -4
  44. package/script/src/preact/plots.d.ts.map +1 -1
  45. package/script/src/preact/plots.js +39 -25
  46. package/script/src/preact/use-active-context.d.ts +15 -0
  47. package/script/src/preact/use-active-context.d.ts.map +1 -0
  48. package/script/src/preact/use-active-context.js +31 -0
  49. package/script/src/theme.d.ts.map +1 -1
  50. package/script/src/theme.js +92 -5
package/README.md CHANGED
@@ -31,11 +31,11 @@ npm i @mcpview/components
31
31
  SysML renderer.
32
32
 
33
33
  The package root is renderer-neutral and carries no MCP Apps runtime: `@mcpview/view` and
34
- `@modelcontextprotocol/ext-apps` are reached only through the `/surface` entry, which owns the App
35
- lifecycle. The responsive layout decision and its hooks live in the `/layout` entry, which takes the
36
- host context as a value and loads no App runtime. The embedded webfonts are reached only through the
37
- `/fonts` entry. Preact is an optional peer and is loaded only through the Preact subpaths and
38
- `/layout`, whose hooks are Preact:
34
+ `@modelcontextprotocol/ext-apps` are reached through the `/surface` entry (also re-exported by
35
+ `/preact`), which owns the App lifecycle. The responsive layout decision and its hooks live in the
36
+ `/layout` entry, which takes the host context as a value and loads no App runtime. The embedded
37
+ webfonts are reached only through the `/fonts` entry. Preact is an optional peer and is loaded only
38
+ through the Preact subpaths and `/layout`, whose hooks are Preact:
39
39
 
40
40
  ```ts
41
41
  import { defineComponentRegistry, installMcpViewTheme } from "@mcpview/components";
@@ -104,7 +104,9 @@ Reusable structures:
104
104
  Numeric evidence:
105
105
 
106
106
  - `Sparkline` for the compact shape of one recorded series, readable at chip and row density.
107
- Requires at least two finite samples already ordered by the caller.
107
+ Requires at least two finite samples already ordered by the caller. `variant="inline"` (default)
108
+ keeps the compact mark; `variant="block"` shows the caller-supplied label and summary above a
109
+ full-width, `2.5rem`-high plot.
108
110
  - `SeriesChart` for a multi-series plot on a shared finite scale. Each series requires a `mark`
109
111
  field (`"line"` or `"bar"`): `"line"` connects samples with a polyline — an assertion that the
110
112
  quantity existed between them; `"bar"` draws each recorded sample as an isolated mark and claims
@@ -112,9 +114,21 @@ Numeric evidence:
112
114
  shared x scale (`undefined` when the pointer leaves); a series with no recorded point at that
113
115
  position is absent from the readout rather than filled. The kit never interpolates, aligns to a
114
116
  nearest neighbour, draws a continuity it has not measured, or invents a value — the provider
115
- declares both the mark shape and which sample a position corresponds to.
117
+ declares both the mark shape and which sample a position corresponds to. `legend?: boolean`
118
+ defaults to `true` and shows a legend for two or more series, using each caller-supplied series
119
+ label with a line or square swatch matching its mark. Set `legend={false}` to hide it. Optional
120
+ `yTicks?: readonly { value: number; label: string }[]` places caller-formatted labels on the y
121
+ scale with subtle horizontal grid lines; omitting `yTicks` keeps the plot free of grid lines.
122
+ Every tick value must be finite and within the inclusive `yMin`–`yMax` bounds. The CSS properties
123
+ `--mcp-view-series-chart-bar-width` (default `8px`) and `--mcp-view-series-chart-height` (default
124
+ `10rem`) control bar width and plot height.
116
125
  - `IntervalPlot` for caller-declared deviation intervals laid out against a shared zero line. The
117
- scale must contain zero; every bound and label is caller-supplied.
126
+ scale must contain zero; every bound and label is caller-supplied. Labels, interval tracks, and
127
+ readings share aligned columns across rows, including at narrow widths.
128
+
129
+ `Sparkline` and `SeriesChart` series also accept `"accent"` and `"brand"` tones. These plot-specific
130
+ choices use `--mcp-view-accent` and `--mcp-view-brand`, respectively, and do not extend
131
+ `PresentationTone` for other components.
118
132
 
119
133
  Semantic composition:
120
134
 
@@ -427,6 +441,10 @@ gone in 0.7.0; `viewerSession: { validate, toState, onInvalid }` is the one form
427
441
  The kit stays small and non-prescriptive, leaving assembly to the caller. When multiple viewers
428
442
  build the same assembly from the same primitives, this section is what they should have found first.
429
443
 
444
+ For a whole-view App, place a `PathBar` before one `SemanticElement density="card"`. Put numeric
445
+ evidence inside its `ElementBody` or an `ElementSection`: a bare plot directly in
446
+ `.mcp-view-surface-stack` touches the frame edge. The card body supplies the surrounding space.
447
+
430
448
  ### Named quantity section
431
449
 
432
450
  `Card` + `MetricGrid` carries one named group of measurements. Repeat the pattern for each domain
@@ -568,9 +586,55 @@ local checkout); the `/surface` entry is derived from the latter unless
568
586
  `MCP_VIEW_COMPONENTS_SURFACE_MODULE` names it. Run the generator from JSR:
569
587
 
570
588
  ```sh
571
- deno run -A jsr:@mcpview/components@0.10.0/scaffold result-viewer ./result-viewer
589
+ deno run -A jsr:@mcpview/components@0.12.0/scaffold result-viewer ./result-viewer
572
590
  ```
573
591
 
574
592
  The npm package contains only the runtime and presentation entry points. It does not export or ship
575
593
  `@mcpview/components/scaffold`; Node ESM imports and CommonJS requires of that subpath are expected
576
594
  to fail with `ERR_PACKAGE_PATH_NOT_EXPORTED` rather than loading a Deno-only module.
595
+
596
+ ## Active context
597
+
598
+ `@mcpview/components/preact/context` exports `ActiveContextChip`, `useActiveContext`,
599
+ `MCP_VIEW_CONTEXT_BASKET_CSS`, `MCP_VIEW_CONTEXT_SELECTION_CSS` and English/French labels through
600
+ `contextBasketLabels(locale)`. This narrow entry uses an existing `@mcpview/view/context` controller
601
+ and loads no App lifecycle, surface mounting or embedded fonts. Preact remains an optional peer.
602
+
603
+ Create the controller once outside the component tree, then call
604
+ `useActiveContext(controller, scopeKey)` in the view. The hook subscribes to acknowledged
605
+ selections, pending updates, failures and root-scoped actions. `selectionState(itemOrId)` exposes
606
+ `selected | adding | removing | none` for rows/cards immediately, including provisional pointer
607
+ intent; `isSelected(item)` retains its ACK-confirmed meaning. `ActiveContextChip` displays the
608
+ basket and calls the supplied removal, clear and optional retry callbacks. Labels, tool-resolvable
609
+ IDs, grouping and resource disclosure belong to the App; groups affect presentation rather than the
610
+ wire snapshot.
611
+
612
+ ```ts
613
+ import { createActiveContextController, createActiveContextPolicy } from "@mcpview/view/context";
614
+ import {
615
+ ActiveContextChip,
616
+ contextBasketLabels,
617
+ MCP_VIEW_CONTEXT_BASKET_CSS,
618
+ MCP_VIEW_CONTEXT_SELECTION_CSS,
619
+ useActiveContext,
620
+ } from "@mcpview/components/preact/context";
621
+ ```
622
+
623
+ Install the exported CSS once for the view; importing the entry does not inject styles. The chip
624
+ keeps confirmed selections visible while an update is pending or fails, exposes failures to
625
+ assistive technology and lets the App provide retry. Accepted Retry restores focus to a retained
626
+ basket control, or an external control if the basket disappears; rejected results preserve focus.
627
+ Use explicit buttons for selection/removal alongside optional gestures. Before sending a resource,
628
+ show its name, byte size and that it will be sent to the model.
629
+
630
+ For optional row styling, install `MCP_VIEW_CONTEXT_SELECTION_CSS` alongside the shared
631
+ `MCP_VIEW_THEME_CSS` from `@mcpview/components`. Apply class `mcp-view-context-selection` and
632
+ `data-context-state={context.selectionState(item)}` to the selection control. The styles use the
633
+ shared light/dark theme tokens; dashed borders distinguish adding/removing from confirmed selection.
634
+ Expose the pending state in accessible text or `aria-busy`, and derive desired `aria-pressed` from
635
+ `selected`/`adding`. The basket count still uses acknowledged selections. Connect `onPreview` to
636
+ `context.previewToggle(item)` for immediate row feedback during the arbiter's default 500 ms delay.
637
+
638
+ See the [context example](../view/examples/context/README.md) for a complete integration. The
639
+ controls are experimental. An ACK confirms host acceptance only; model consumption, production host
640
+ behavior and cross-iframe ownership require separate validation.
@@ -14,7 +14,7 @@ export type { BadgeGroupProps, BadgeProps, ButtonProps, CardProps, CodeBlockProp
14
14
  export { ArtifactRow, DrillHint, LimitGauge, PATH_BAR_DEFAULT_MAX_VISIBLE, PathBar, Slot3D, StaleBanner, TreeList, TypeBadge, } from "./src/preact/structural.js";
15
15
  export type { ArtifactFingerprint, ArtifactRowProps, ArtifactVerification, DrillHintDirection, DrillHintProps, LimitGaugeLimit, LimitGaugeProps, PathBarItem, PathBarProps, Slot3DProps, StaleBannerAction, StaleBannerProps, TreeListNode, TreeListProps, TypeBadgeProps, ViewKind, } from "./src/preact/structural.js";
16
16
  export { IntervalPlot, SeriesChart, Sparkline } from "./src/preact/plots.js";
17
- export type { IntervalPlotInterval, IntervalPlotProps, SeriesChartCursor, SeriesChartProps, SeriesChartReadout, SeriesChartSeries, SeriesChartSummaryItem, SeriesPoint, SparklineProps, } from "./src/preact/plots.js";
17
+ export type { IntervalPlotInterval, IntervalPlotProps, SeriesChartCursor, SeriesChartProps, SeriesChartReadout, SeriesChartSeries, SeriesChartSummaryItem, SeriesChartTone, SeriesPoint, SparklineProps, } from "./src/preact/plots.js";
18
18
  export { CollectionCard, ElementBody, ElementIdent, ElementLimit, ElementProvenance, ElementReading, ElementSection, ElementVerdict, SemanticElement, SemanticList, } from "./src/preact/semantic-element.js";
19
19
  export type { CollectionCardProps, ElementBodyProps, ElementIdentProps, ElementLimitProps, ElementProvenanceProps, ElementReadingProps, ElementSectionProps, ElementVerdictProps, SemanticElementDensity, SemanticElementProps, SemanticListProps, } from "./src/preact/semantic-element.js";
20
20
  export { Disclosure, FocusedView } from "./src/preact/focused-view.js";
@@ -1 +1 @@
1
- {"version":3,"file":"preact-components.d.ts","sourceRoot":"","sources":["../src/preact-components.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,qBAAqB,CAAC;AAG7B,OAAO,EACL,KAAK,EACL,UAAU,EACV,MAAM,EACN,IAAI,EACJ,SAAS,EACT,cAAc,EACd,SAAS,EACT,UAAU,EACV,UAAU,EACV,YAAY,EACZ,OAAO,EACP,MAAM,EACN,UAAU,EACV,WAAW,EACX,mBAAmB,EACnB,GAAG,EACH,QAAQ,EACR,KAAK,EACL,YAAY,EACZ,SAAS,EACT,OAAO,GACR,MAAM,4BAA4B,CAAC;AACpC,YAAY,EACV,eAAe,EACf,UAAU,EACV,WAAW,EACX,SAAS,EACT,cAAc,EACd,mBAAmB,EACnB,eAAe,EACf,cAAc,EACd,eAAe,EACf,eAAe,EACf,YAAY,EACZ,cAAc,EACd,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,UAAU,EACV,WAAW,EACX,gBAAgB,EACh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":"preact-components.d.ts","sourceRoot":"","sources":["../src/preact-components.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,qBAAqB,CAAC;AAG7B,OAAO,EACL,KAAK,EACL,UAAU,EACV,MAAM,EACN,IAAI,EACJ,SAAS,EACT,cAAc,EACd,SAAS,EACT,UAAU,EACV,UAAU,EACV,YAAY,EACZ,OAAO,EACP,MAAM,EACN,UAAU,EACV,WAAW,EACX,mBAAmB,EACnB,GAAG,EACH,QAAQ,EACR,KAAK,EACL,YAAY,EACZ,SAAS,EACT,OAAO,GACR,MAAM,4BAA4B,CAAC;AACpC,YAAY,EACV,eAAe,EACf,UAAU,EACV,WAAW,EACX,SAAS,EACT,cAAc,EACd,mBAAmB,EACnB,eAAe,EACf,cAAc,EACd,eAAe,EACf,eAAe,EACf,YAAY,EACZ,cAAc,EACd,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,UAAU,EACV,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,QAAQ,EACR,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,kBAAkB,EAClB,cAAc,EACd,YAAY,GACb,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EACL,WAAW,EACX,SAAS,EACT,UAAU,EACV,4BAA4B,EAC5B,OAAO,EACP,MAAM,EACN,WAAW,EACX,QAAQ,EACR,SAAS,GACV,MAAM,4BAA4B,CAAC;AACpC,YAAY,EACV,mBAAmB,EACnB,gBAAgB,EAChB,oBAAoB,EACpB,kBAAkB,EAClB,cAAc,EACd,eAAe,EACf,eAAe,EACf,WAAW,EACX,YAAY,EACZ,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,cAAc,EACd,QAAQ,GACT,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAC7E,YAAY,EACV,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,iBAAiB,EACjB,sBAAsB,EACtB,eAAe,EACf,WAAW,EACX,cAAc,GACf,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EACL,cAAc,EACd,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,iBAAiB,EACjB,cAAc,EACd,cAAc,EACd,cAAc,EACd,eAAe,EACf,YAAY,GACb,MAAM,kCAAkC,CAAC;AAC1C,YAAY,EACV,mBAAmB,EACnB,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,sBAAsB,EACtB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,sBAAsB,EACtB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AACvE,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC"}
@@ -0,0 +1,10 @@
1
+ /** Context controls without the MCP Apps lifecycle or surface runtime. @module */
2
+ import "./_dnt.polyfills.js";
3
+ export { ActiveContextChip } from "./src/preact/context-basket.js";
4
+ export type { ActiveContextChipProps, ContextBasketEntry, ContextBasketGroup, ContextBasketLabels, ContextBasketSelection, ContextBasketSlot, ContextMutation, } from "./src/preact/context-basket.js";
5
+ export { useActiveContext } from "./src/preact/use-active-context.js";
6
+ export type { ActiveContextHook } from "./src/preact/use-active-context.js";
7
+ export { CONTEXT_BASKET_MESSAGES_EN, CONTEXT_BASKET_MESSAGES_FR, contextBasketLabels, contextBasketMessages, } from "./src/context-basket-labels.js";
8
+ export type { ContextBasketMessages } from "./src/context-basket-labels.js";
9
+ export { MCP_VIEW_CONTEXT_BASKET_CSS, MCP_VIEW_CONTEXT_SELECTION_CSS, } from "./src/context-basket-styles.js";
10
+ //# sourceMappingURL=preact-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preact-context.d.ts","sourceRoot":"","sources":["../src/preact-context.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,OAAO,qBAAqB,CAAC;AAE7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,YAAY,EACV,sBAAsB,EACtB,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,sBAAsB,EACtB,iBAAiB,EACjB,eAAe,GAChB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AACtE,YAAY,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AAC5E,OAAO,EACL,0BAA0B,EAC1B,0BAA0B,EAC1B,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,gCAAgC,CAAC;AACxC,YAAY,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAC5E,OAAO,EACL,2BAA2B,EAC3B,8BAA8B,GAC/B,MAAM,gCAAgC,CAAC"}
@@ -0,0 +1,6 @@
1
+ /** Context controls without the MCP Apps lifecycle or surface runtime. @module */
2
+ import "./_dnt.polyfills.js";
3
+ export { ActiveContextChip } from "./src/preact/context-basket.js";
4
+ export { useActiveContext } from "./src/preact/use-active-context.js";
5
+ export { CONTEXT_BASKET_MESSAGES_EN, CONTEXT_BASKET_MESSAGES_FR, contextBasketLabels, contextBasketMessages, } from "./src/context-basket-labels.js";
6
+ export { MCP_VIEW_CONTEXT_BASKET_CSS, MCP_VIEW_CONTEXT_SELECTION_CSS, } from "./src/context-basket-styles.js";
@@ -0,0 +1,21 @@
1
+ import { type TranslatorSelector } from "./i18n.js";
2
+ import type { ContextBasketLabels } from "./preact/context-basket.js";
3
+ /** Interface text only; selection labels and group summaries remain caller data. */
4
+ export declare const CONTEXT_BASKET_MESSAGES_EN: {
5
+ readonly active: "Active context";
6
+ readonly items: "Context items";
7
+ readonly remove: "Remove {label} from context";
8
+ readonly clearAll: "Clear context";
9
+ readonly pending: "Updating context…";
10
+ readonly error: "Context update failed";
11
+ readonly retry: "Retry";
12
+ readonly evicted: "Removed oldest item: {label}";
13
+ };
14
+ export type ContextBasketMessages = {
15
+ readonly [Key in keyof typeof CONTEXT_BASKET_MESSAGES_EN]: string;
16
+ };
17
+ export declare const CONTEXT_BASKET_MESSAGES_FR: ContextBasketMessages;
18
+ export declare const contextBasketMessages: TranslatorSelector<keyof ContextBasketMessages>;
19
+ /** Optional EN/FR kit labels. Pass your own labels to customize any interface text. */
20
+ export declare function contextBasketLabels(locale?: string): ContextBasketLabels;
21
+ //# sourceMappingURL=context-basket-labels.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-basket-labels.d.ts","sourceRoot":"","sources":["../../src/src/context-basket-labels.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,kBAAkB,EAAE,MAAM,WAAW,CAAC;AACtE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAEtE,oFAAoF;AACpF,eAAO,MAAM,0BAA0B;;;;;;;;;CAS7B,CAAC;AAEX,MAAM,MAAM,qBAAqB,GAAG;IAClC,QAAQ,EAAE,GAAG,IAAI,MAAM,OAAO,0BAA0B,GAAG,MAAM;CAClE,CAAC;AAEF,eAAO,MAAM,0BAA0B,EAAE,qBASxC,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,kBAAkB,CAAC,MAAM,qBAAqB,CAI9E,CAAC;AAEL,uFAAuF;AACvF,wBAAgB,mBAAmB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,mBAAmB,CAYxE"}
@@ -0,0 +1,40 @@
1
+ import { createTranslator } from "./i18n.js";
2
+ /** Interface text only; selection labels and group summaries remain caller data. */
3
+ export const CONTEXT_BASKET_MESSAGES_EN = {
4
+ active: "Active context",
5
+ items: "Context items",
6
+ remove: "Remove {label} from context",
7
+ clearAll: "Clear context",
8
+ pending: "Updating context…",
9
+ error: "Context update failed",
10
+ retry: "Retry",
11
+ evicted: "Removed oldest item: {label}",
12
+ };
13
+ export const CONTEXT_BASKET_MESSAGES_FR = {
14
+ active: "Contexte actif",
15
+ items: "Éléments du contexte",
16
+ remove: "Retirer {label} du contexte",
17
+ clearAll: "Vider le contexte",
18
+ pending: "Mise à jour du contexte…",
19
+ error: "Échec de la mise à jour du contexte",
20
+ retry: "Réessayer",
21
+ evicted: "Élément le plus ancien retiré : {label}",
22
+ };
23
+ export const contextBasketMessages = createTranslator({
24
+ messages: CONTEXT_BASKET_MESSAGES_EN,
25
+ translations: { fr: CONTEXT_BASKET_MESSAGES_FR },
26
+ });
27
+ /** Optional EN/FR kit labels. Pass your own labels to customize any interface text. */
28
+ export function contextBasketLabels(locale) {
29
+ const t = contextBasketMessages(locale);
30
+ return {
31
+ active: t("active"),
32
+ items: t("items"),
33
+ clearAll: t("clearAll"),
34
+ pending: t("pending"),
35
+ error: t("error"),
36
+ retry: t("retry"),
37
+ remove: (label) => t("remove", { label }),
38
+ evicted: (label) => t("evicted", { label }),
39
+ };
40
+ }
@@ -0,0 +1,8 @@
1
+ /** Optional plain CSS for the controlled context basket; uses the shared theme tokens. */
2
+ export declare const MCP_VIEW_CONTEXT_BASKET_CSS: string;
3
+ /**
4
+ * Optional row/card selection treatment. Set data-context-state from
5
+ * useActiveContext().selectionState(item); shared light/dark theme tokens apply.
6
+ */
7
+ export declare const MCP_VIEW_CONTEXT_SELECTION_CSS: string;
8
+ //# sourceMappingURL=context-basket-styles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-basket-styles.d.ts","sourceRoot":"","sources":["../../src/src/context-basket-styles.ts"],"names":[],"mappings":"AAAA,0FAA0F;AAC1F,eAAO,MAAM,2BAA2B,EAAE,MAiFzC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,8BAA8B,EAAE,MAyB5C,CAAC"}
@@ -0,0 +1,113 @@
1
+ /** Optional plain CSS for the controlled context basket; uses the shared theme tokens. */
2
+ export const MCP_VIEW_CONTEXT_BASKET_CSS = String.raw `
3
+ .mcp-view-context-root {
4
+ position: relative; display: inline-flex; min-width: 0; max-width: 100%;
5
+ align-items: center; gap: .375rem; color: var(--mcp-view-muted);
6
+ font-family: var(--mcp-view-font-body); font-size: var(--mcp-view-size-chip);
7
+ }
8
+ .mcp-view-context-single, .mcp-view-context-trigger {
9
+ display: inline-flex; min-width: 0; align-items: center; gap: .25rem;
10
+ border: 1px solid color-mix(in srgb, var(--mcp-view-accent) 30%, transparent);
11
+ border-radius: var(--mcp-view-radius-control, 5px);
12
+ background: color-mix(in srgb, var(--mcp-view-accent) 8%, transparent);
13
+ }
14
+ .mcp-view-context-single { padding: .125rem .125rem .125rem .5rem; }
15
+ .mcp-view-context-activeLabel, .mcp-view-context-trigger { color: var(--mcp-view-accent-text); }
16
+ .mcp-view-context-activeLabel, .mcp-view-context-chevron { flex-shrink: 0; }
17
+ .mcp-view-context-singleLabel, .mcp-view-context-itemLabel { color: var(--mcp-view-text); }
18
+ .mcp-view-context-singleLabel, .mcp-view-context-itemLabel, .mcp-view-context-itemDetail,
19
+ .mcp-view-context-eviction, .mcp-view-context-pendingLabel {
20
+ overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
21
+ }
22
+ .mcp-view-context-trigger, .mcp-view-context-remove, .mcp-view-context-clearAll,
23
+ .mcp-view-context-retry {
24
+ font: inherit; cursor: pointer;
25
+ }
26
+ .mcp-view-context-trigger { padding: .25rem .5rem; }
27
+ .mcp-view-context-remove {
28
+ display: grid; width: 1.5rem; height: 1.5rem; flex-shrink: 0; place-items: center;
29
+ padding: 0; border: 0; border-radius: var(--mcp-view-radius-sm, 3px);
30
+ background: transparent; color: var(--mcp-view-quiet); font-size: var(--mcp-view-size-body); line-height: 1;
31
+ }
32
+ .mcp-view-context-trigger:hover, .mcp-view-context-remove:hover {
33
+ background: color-mix(in srgb, var(--mcp-view-accent) 12%, transparent);
34
+ }
35
+ .mcp-view-context-remove:hover { color: var(--mcp-view-text); }
36
+ .mcp-view-context-root button:focus-visible {
37
+ outline: 2px solid var(--mcp-view-accent); outline-offset: 1px;
38
+ }
39
+ .mcp-view-context-popover {
40
+ position: absolute; top: 100%; z-index: 30; margin-top: .375rem;
41
+ width: 14rem; max-width: calc(100vw - 24px); padding: .375rem;
42
+ border: 1px solid var(--mcp-view-border); border-radius: var(--mcp-view-radius, 8px);
43
+ background: var(--mcp-view-panel);
44
+ }
45
+ .mcp-view-context-popover[data-align="start"] { left: 0; }
46
+ .mcp-view-context-popover[data-align="end"] { right: 0; }
47
+ .mcp-view-context-list { display: flex; max-height: 16rem; flex-direction: column; overflow-y: auto; }
48
+ .mcp-view-context-item {
49
+ display: flex; min-width: 0; align-items: center; gap: .5rem;
50
+ border-radius: var(--mcp-view-radius-sm, 3px); padding: .25rem .375rem;
51
+ }
52
+ .mcp-view-context-item:hover { background: var(--mcp-view-subtle); }
53
+ .mcp-view-context-itemContent { min-width: 0; flex: 1; }
54
+ .mcp-view-context-itemLabel, .mcp-view-context-itemDetail { display: block; }
55
+ .mcp-view-context-itemDetail, .mcp-view-context-clearAll, .mcp-view-context-eviction {
56
+ font-size: var(--mcp-view-size-micro); color: var(--mcp-view-quiet);
57
+ }
58
+ .mcp-view-context-clearAll {
59
+ width: 100%; margin-top: .25rem; padding: .375rem .375rem 0;
60
+ border: 0; border-top: 1px solid var(--mcp-view-border-soft);
61
+ background: transparent; text-align: left;
62
+ }
63
+ .mcp-view-context-clearAll:hover, .mcp-view-context-error { color: var(--mcp-view-danger); }
64
+ .mcp-view-context-pending, .mcp-view-context-error { display: inline-flex; align-items: center; gap: .25rem; }
65
+ .mcp-view-context-pending { min-width: 0; }
66
+ .mcp-view-context-error { flex-shrink: 0; }
67
+ .mcp-view-context-spinner { flex-shrink: 0; animation: mcp-view-context-spin 1s linear infinite; }
68
+ .mcp-view-context-eviction { max-width: 9rem; }
69
+ .mcp-view-context-root[data-compact] .mcp-view-context-singleLabel,
70
+ .mcp-view-context-root[data-compact] .mcp-view-context-eviction { max-width: 6rem; }
71
+ .mcp-view-context-root[data-compact] .mcp-view-context-activeLabel,
72
+ .mcp-view-context-errorLabel[data-hidden] {
73
+ position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
74
+ overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
75
+ }
76
+ .mcp-view-context-retry {
77
+ border: 1px solid currentColor; border-radius: var(--mcp-view-radius-sm, 3px);
78
+ padding: .125rem .375rem; background: transparent; color: inherit;
79
+ }
80
+ .mcp-view-context-root button:disabled { cursor: default; opacity: .55; }
81
+ @keyframes mcp-view-context-spin { to { transform: rotate(360deg); } }
82
+ @media (prefers-reduced-motion: reduce) { .mcp-view-context-spinner { animation: none; } }
83
+ `;
84
+ /**
85
+ * Optional row/card selection treatment. Set data-context-state from
86
+ * useActiveContext().selectionState(item); shared light/dark theme tokens apply.
87
+ */
88
+ export const MCP_VIEW_CONTEXT_SELECTION_CSS = String.raw `
89
+ .mcp-view-context-selection {
90
+ border: 1px solid var(--mcp-view-border); border-radius: var(--mcp-view-radius-control, 5px);
91
+ background: var(--mcp-view-panel); color: var(--mcp-view-text);
92
+ }
93
+ .mcp-view-context-selection[data-context-state="selected"] {
94
+ border-color: var(--mcp-view-accent);
95
+ background: color-mix(in srgb, var(--mcp-view-accent) 12%, var(--mcp-view-panel));
96
+ box-shadow: inset 3px 0 var(--mcp-view-accent);
97
+ }
98
+ .mcp-view-context-selection[data-context-state="adding"] {
99
+ border-color: var(--mcp-view-accent); border-style: dashed;
100
+ background: color-mix(in srgb, var(--mcp-view-accent) 12%, var(--mcp-view-panel));
101
+ box-shadow: inset 3px 0 var(--mcp-view-accent);
102
+ }
103
+ .mcp-view-context-selection[data-context-state="removing"] {
104
+ border-color: var(--mcp-view-quiet); border-style: dashed;
105
+ background: var(--mcp-view-subtle); color: var(--mcp-view-muted);
106
+ }
107
+ .mcp-view-context-selection:focus-visible {
108
+ outline: 2px solid var(--mcp-view-accent); outline-offset: 2px;
109
+ }
110
+ .mcp-view-context-selection-status {
111
+ margin-inline-start: .5rem; font-size: var(--mcp-view-size-chip); color: var(--mcp-view-muted);
112
+ }
113
+ `;
@@ -0,0 +1,61 @@
1
+ /** @jsxImportSource preact */
2
+ import type { JSX } from "preact";
3
+ /** Minimum presentation identity; the owning controller retains the full payload. */
4
+ export interface ContextBasketSelection {
5
+ readonly scopeKey: string;
6
+ readonly item: {
7
+ readonly id: string;
8
+ readonly label: string;
9
+ readonly view?: string;
10
+ readonly value?: string;
11
+ };
12
+ }
13
+ /** A caller-projected group: identities, membership and summaries remain caller-owned. */
14
+ export interface ContextBasketGroup {
15
+ readonly scopeKey: string;
16
+ readonly id: string;
17
+ readonly label: string;
18
+ readonly summary?: string;
19
+ readonly view?: string;
20
+ readonly value?: string;
21
+ }
22
+ export type ContextBasketEntry<T extends ContextBasketSelection = ContextBasketSelection> = {
23
+ readonly kind: "single";
24
+ readonly selection: T;
25
+ } | {
26
+ readonly kind: "group";
27
+ readonly group: ContextBasketGroup;
28
+ };
29
+ /** Every visible message and accessible name follows the caller's locale. */
30
+ export interface ContextBasketLabels {
31
+ readonly active: string;
32
+ readonly items: string;
33
+ readonly remove: (label: string) => string;
34
+ readonly clearAll: string;
35
+ readonly pending: string;
36
+ readonly error: string;
37
+ readonly retry: string;
38
+ readonly evicted: (label: string) => string;
39
+ }
40
+ export type ContextBasketSlot = "root" | "single" | "activeLabel" | "singleLabel" | "remove" | "trigger" | "chevron" | "popover" | "list" | "item" | "itemContent" | "itemLabel" | "itemDetail" | "clearAll" | "pending" | "spinner" | "error" | "errorLabel" | "eviction" | "pendingLabel" | "retry";
41
+ export type ContextMutation = () => unknown | Promise<unknown>;
42
+ export interface ActiveContextChipProps<T extends ContextBasketSelection = ContextBasketSelection> {
43
+ readonly selections: readonly T[];
44
+ /** Overrides the visible list, e.g. one entry per group instead of its projected members. */
45
+ readonly entries?: readonly ContextBasketEntry<T>[];
46
+ readonly labels: ContextBasketLabels;
47
+ readonly failed?: boolean;
48
+ readonly pending?: boolean;
49
+ readonly evictedLabel?: string | null;
50
+ readonly onRemove?: (selection: T) => unknown | Promise<unknown>;
51
+ readonly onRemoveGroup?: (group: ContextBasketGroup) => unknown | Promise<unknown>;
52
+ readonly onClear: ContextMutation;
53
+ readonly onRetry?: ContextMutation;
54
+ readonly compact?: boolean;
55
+ readonly popoverAlign?: "start" | "end";
56
+ readonly className?: string;
57
+ readonly classes?: Partial<Record<ContextBasketSlot, string>>;
58
+ }
59
+ /** Controlled basket. Acknowledged selections and mutation results remain caller-owned. */
60
+ export declare function ActiveContextChip<T extends ContextBasketSelection>({ selections, entries, labels, failed, pending, evictedLabel, onRemove, onRemoveGroup, onClear, onRetry, compact, popoverAlign, className, classes, }: ActiveContextChipProps<T>): JSX.Element | null;
61
+ //# sourceMappingURL=context-basket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-basket.d.ts","sourceRoot":"","sources":["../../../src/src/preact/context-basket.tsx"],"names":[],"mappings":"AAAA,8BAA8B;AAE9B,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAGlC,qFAAqF;AACrF,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE;QACb,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;QACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;KACzB,CAAC;CACH;AAED,0FAA0F;AAC1F,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC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1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "preact/jsx-runtime";
2
+ import { useEffect, useId, useLayoutEffect, useRef, useState } from "preact/hooks";
3
+ const FOCUSABLE_SELECTOR = [
4
+ "a[href]",
5
+ "button:not([disabled])",
6
+ "input:not([disabled])",
7
+ "select:not([disabled])",
8
+ "textarea:not([disabled])",
9
+ '[tabindex]:not([tabindex="-1"])',
10
+ ].join(",");
11
+ function accepted(result) {
12
+ return result === "shared" || result === "cleared";
13
+ }
14
+ function outsideFocusable(wrapper) {
15
+ if (!wrapper)
16
+ return null;
17
+ const elements = Array.from(wrapper.ownerDocument.querySelectorAll(FOCUSABLE_SELECTOR));
18
+ const current = elements.indexOf(wrapper.ownerDocument.activeElement);
19
+ return elements.slice(current + 1).find((element) => !wrapper.contains(element)) ??
20
+ elements.slice(0, Math.max(0, current)).reverse().find((element) => !wrapper.contains(element)) ??
21
+ null;
22
+ }
23
+ function entryKey(entry) {
24
+ return entry.kind === "group"
25
+ ? JSON.stringify(["group", entry.group.scopeKey, entry.group.id])
26
+ : JSON.stringify(["single", entry.selection.scopeKey, entry.selection.item.id]);
27
+ }
28
+ function display(entry) {
29
+ return entry.kind === "group" ? entry.group : entry.selection.item;
30
+ }
31
+ /** Controlled basket. Acknowledged selections and mutation results remain caller-owned. */
32
+ export function ActiveContextChip({ selections, entries, labels, failed = false, pending = false, evictedLabel, onRemove, onRemoveGroup, onClear, onRetry, compact = false, popoverAlign = "end", className, classes, }) {
33
+ const slot = (name) => [`mcp-view-context-${name}`, classes?.[name]].filter(Boolean).join(" ");
34
+ const [open, setOpen] = useState(false);
35
+ const wrapperRef = useRef(null);
36
+ const triggerRef = useRef(null);
37
+ const singleRemoveRef = useRef(null);
38
+ const removeRefs = useRef(new Map());
39
+ const mountedRef = useRef(true);
40
+ const operationRef = useRef(0);
41
+ const popoverId = useId();
42
+ const visible = entries ??
43
+ selections.map((selection) => ({ kind: "single", selection }));
44
+ const visibleRef = useRef(visible);
45
+ visibleRef.current = visible;
46
+ const count = visible.length;
47
+ useLayoutEffect(() => {
48
+ mountedRef.current = true;
49
+ return () => {
50
+ mountedRef.current = false;
51
+ operationRef.current++;
52
+ };
53
+ }, []);
54
+ useEffect(() => {
55
+ if (count <= 1)
56
+ setOpen(false);
57
+ }, [count]);
58
+ useEffect(() => {
59
+ if (!open)
60
+ return;
61
+ const doc = wrapperRef.current.ownerDocument;
62
+ function pointer(event) {
63
+ if (!wrapperRef.current?.contains(event.target)) {
64
+ operationRef.current++;
65
+ setOpen(false);
66
+ }
67
+ }
68
+ function key(event) {
69
+ if (event.key !== "Escape")
70
+ return;
71
+ event.preventDefault();
72
+ operationRef.current++;
73
+ setOpen(false);
74
+ triggerRef.current?.focus();
75
+ }
76
+ doc.addEventListener("pointerdown", pointer);
77
+ doc.addEventListener("keydown", key);
78
+ return () => {
79
+ doc.removeEventListener("pointerdown", pointer);
80
+ doc.removeEventListener("keydown", key);
81
+ };
82
+ }, [open]);
83
+ // Run after the controlled render, and never steal focus back from a user who moved away.
84
+ function afterAccepted(operation, origin, callback) {
85
+ queueMicrotask(() => {
86
+ if (!mountedRef.current || operation !== operationRef.current)
87
+ return;
88
+ const current = origin?.ownerDocument.activeElement;
89
+ // A removed button naturally loses browser focus; a deliberate focus change wins.
90
+ if (origin && current !== origin && current !== origin.ownerDocument.body)
91
+ return;
92
+ callback();
93
+ });
94
+ }
95
+ async function remove(entry, index) {
96
+ const operation = ++operationRef.current;
97
+ const origin = wrapperRef.current?.ownerDocument.activeElement ?? null;
98
+ const external = outsideFocusable(wrapperRef.current);
99
+ let result;
100
+ try {
101
+ result = await (entry.kind === "group"
102
+ ? onRemoveGroup?.(entry.group)
103
+ : onRemove
104
+ ? onRemove(entry.selection)
105
+ : count === 1
106
+ ? onClear()
107
+ : undefined);
108
+ }
109
+ catch {
110
+ return;
111
+ }
112
+ if (!accepted(result))
113
+ return;
114
+ afterAccepted(operation, origin, () => {
115
+ const remaining = visibleRef.current;
116
+ if (!remaining.length) {
117
+ if (external?.isConnected)
118
+ external.focus({ preventScroll: true });
119
+ }
120
+ else if (remaining.length === 1) {
121
+ singleRemoveRef.current?.focus({ preventScroll: true });
122
+ }
123
+ else {
124
+ const neighbour = remaining[Math.min(index, remaining.length - 1)];
125
+ (removeRefs.current.get(entryKey(neighbour)) ?? triggerRef.current)?.focus({
126
+ preventScroll: true,
127
+ });
128
+ }
129
+ });
130
+ }
131
+ async function clear() {
132
+ const operation = ++operationRef.current;
133
+ const origin = wrapperRef.current?.ownerDocument.activeElement ?? null;
134
+ const external = outsideFocusable(wrapperRef.current);
135
+ let result;
136
+ try {
137
+ result = await onClear();
138
+ }
139
+ catch {
140
+ return;
141
+ }
142
+ if (!accepted(result))
143
+ return;
144
+ afterAccepted(operation, origin, () => {
145
+ setOpen(false);
146
+ if (external?.isConnected)
147
+ external.focus({ preventScroll: true });
148
+ });
149
+ }
150
+ async function retry() {
151
+ const operation = ++operationRef.current;
152
+ const origin = wrapperRef.current?.ownerDocument.activeElement ?? null;
153
+ const external = outsideFocusable(wrapperRef.current);
154
+ let result;
155
+ try {
156
+ result = await onRetry?.();
157
+ }
158
+ catch {
159
+ return;
160
+ }
161
+ if (!accepted(result))
162
+ return;
163
+ afterAccepted(operation, origin, () => {
164
+ // Retry may clear the basket, remove one item, or retain its confirmed list.
165
+ // Choose from the controlled render after ACK rather than the failed intent.
166
+ const retained = visibleRef.current.length === 1
167
+ ? singleRemoveRef.current
168
+ : triggerRef.current;
169
+ if (retained?.isConnected && !retained.disabled) {
170
+ retained.focus({ preventScroll: true });
171
+ }
172
+ else if (origin?.isConnected) {
173
+ origin.focus({ preventScroll: true });
174
+ }
175
+ else if (external?.isConnected) {
176
+ external.focus({ preventScroll: true });
177
+ }
178
+ });
179
+ }
180
+ function removeButton(entry, index, single = false) {
181
+ const item = display(entry);
182
+ return (_jsx("button", { type: "button", class: slot("remove"), ref: (element) => {
183
+ if (single)
184
+ singleRemoveRef.current = element;
185
+ if (element)
186
+ removeRefs.current.set(entryKey(entry), element);
187
+ else
188
+ removeRefs.current.delete(entryKey(entry));
189
+ }, disabled: pending || (entry.kind === "group" ? !onRemoveGroup : !onRemove && count > 1), "aria-label": labels.remove(item.label), title: labels.remove(item.label), onClick: () => void remove(entry, index), children: _jsx("span", { "aria-hidden": "true", children: "\u00D7" }) }));
190
+ }
191
+ if (count === 0 && !failed && !pending && !evictedLabel)
192
+ return null;
193
+ return (_jsxs("span", { ref: wrapperRef, "aria-busy": pending || undefined, class: [slot("root"), className].filter(Boolean).join(" "), "data-compact": compact || undefined, children: [count === 1 && (_jsxs("span", { class: slot("single"), "aria-live": "polite", "aria-atomic": "true", children: [_jsx("span", { class: slot("activeLabel"), children: labels.active }), _jsx("span", { class: slot("singleLabel"), title: display(visible[0]).label, children: display(visible[0]).label }), removeButton(visible[0], 0, true)] })), count > 1 && (_jsxs(_Fragment, { children: [_jsxs("button", { ref: triggerRef, type: "button", class: slot("trigger"), "aria-expanded": open, "aria-controls": open ? popoverId : undefined, "aria-haspopup": "dialog", onClick: () => {
194
+ operationRef.current++;
195
+ setOpen((value) => !value);
196
+ }, children: [_jsxs("span", { "aria-live": "polite", "aria-atomic": "true", children: [labels.active, " \u00B7 ", count] }), _jsx("span", { "aria-hidden": "true", class: slot("chevron"), children: open ? "▴" : "▾" })] }), open && (_jsxs("span", { id: popoverId, role: "dialog", "aria-label": labels.items, class: slot("popover"), "data-align": popoverAlign, children: [_jsx("span", { role: "list", class: slot("list"), children: visible.map((entry, index) => {
197
+ const item = display(entry);
198
+ const summary = entry.kind === "group" ? entry.group.summary : undefined;
199
+ return (_jsxs("span", { role: "listitem", class: slot("item"), children: [_jsxs("span", { class: slot("itemContent"), children: [_jsx("span", { class: slot("itemLabel"), children: item.label }), _jsx("span", { class: slot("itemDetail"), children: summary ?? [item.view, item.value].filter(Boolean).join(" · ") })] }), removeButton(entry, index)] }, entryKey(entry)));
200
+ }) }), _jsx("button", { type: "button", class: slot("clearAll"), disabled: pending, onClick: () => void clear(), children: labels.clearAll })] }))] })), pending && (_jsxs("span", { role: "status", "aria-live": "polite", class: slot("pending"), title: labels.pending, children: [_jsx("span", { "aria-hidden": "true", class: slot("spinner"), children: "\u25CC" }), _jsx("span", { class: slot("pendingLabel"), children: labels.pending })] })), failed && (_jsxs("span", { role: "status", class: slot("error"), title: labels.error, children: [_jsx("span", { "aria-hidden": "true", children: "!" }), _jsx("span", { class: slot("errorLabel"), "data-hidden": compact && count > 0 || undefined, children: labels.error }), onRetry && (_jsx("button", { type: "button", class: slot("retry"), disabled: pending, onClick: () => void retry(), children: labels.retry }))] })), evictedLabel && (_jsx("span", { role: "status", "aria-live": "polite", class: slot("eviction"), title: labels.evicted(evictedLabel), children: labels.evicted(evictedLabel) }))] }));
201
+ }