@malloy-publisher/sdk 0.3.1 → 0.4.1

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 (136) hide show
  1. package/README.md +33 -9
  2. package/dist/{DocumentStorageProvider-tGvodI8f.es.js → DocumentStorage-BiFqO15i.es.js} +7126 -6943
  3. package/dist/DocumentStorage-CMyLifsz.cjs.js +134 -0
  4. package/dist/RenderedResult-CZzE8wv-.cjs.js +115 -0
  5. package/dist/{RenderedResult-mDE-4u8S.es.js → RenderedResult-CvrmYyr5.es.js} +64 -57
  6. package/dist/{ServerProvider-X2Ma0HRs.es.js → ServerProvider-BWOEKg0O.es.js} +1217 -1244
  7. package/dist/ServerProvider-Cc489A3f.cjs.js +1 -0
  8. package/dist/builder/index.cjs.js +52 -52
  9. package/dist/builder/index.es.js +3961 -3960
  10. package/dist/client/api.d.ts +100 -42
  11. package/dist/client/index.cjs.js +1 -1
  12. package/dist/client/index.es.js +1 -1
  13. package/dist/components/AppDialog.d.ts +30 -0
  14. package/dist/components/BackLink.d.ts +32 -0
  15. package/dist/components/Dashboard/DashboardBar.d.ts +21 -0
  16. package/dist/components/Dashboard/telemetry.d.ts +24 -7
  17. package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +12 -10
  18. package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +20 -2
  19. package/dist/components/DashboardBuilder/DashboardEditor.d.ts +21 -11
  20. package/dist/components/DashboardBuilder/newDashboard.d.ts +17 -0
  21. package/dist/components/DashboardBuilder/spliceDocument.d.ts +10 -1
  22. package/dist/components/DocumentStorage/DocumentStorage.d.ts +69 -5
  23. package/dist/components/DocumentStorage/index.d.ts +1 -0
  24. package/dist/components/ItemRow.d.ts +38 -0
  25. package/dist/components/Materializations/MaterializationDetailDialog.d.ts +1 -2
  26. package/dist/components/Materializations/MaterializationRunsList.d.ts +1 -5
  27. package/dist/components/Materializations/Materializations.d.ts +7 -3
  28. package/dist/components/Materializations/ScopeButton.d.ts +18 -0
  29. package/dist/components/Materializations/SectionLabel.d.ts +7 -4
  30. package/dist/components/Materializations/TriggerLabel.d.ts +14 -0
  31. package/dist/components/Materializations/index.d.ts +0 -1
  32. package/dist/components/Package/ContentTypeIcon.d.ts +3 -5
  33. package/dist/components/Package/NewDashboardDialog.d.ts +18 -0
  34. package/dist/components/Package/index.d.ts +0 -1
  35. package/dist/components/PackageSection.d.ts +23 -0
  36. package/dist/components/buttons.d.ts +53 -0
  37. package/dist/components/index.d.ts +5 -0
  38. package/dist/components/styles.d.ts +49 -40
  39. package/dist/hooks/useCrudMutation.d.ts +42 -0
  40. package/dist/index.cjs.js +22 -26
  41. package/dist/index.d.ts +1 -0
  42. package/dist/index.es.js +17431 -20676
  43. package/dist/telemetry/consoleEvents.d.ts +60 -0
  44. package/dist/utils/sha256.d.ts +2 -0
  45. package/package.json +1 -1
  46. package/src/components/AppDialog.tsx +115 -0
  47. package/src/components/BackLink.tsx +83 -0
  48. package/src/components/Connections/AddConnectionDialog.tsx +50 -70
  49. package/src/components/Connections/DeleteConnectionDialog.tsx +23 -50
  50. package/src/components/Connections/EditConnectionDialog.tsx +54 -73
  51. package/src/components/Dashboard/DashboardBar.tsx +88 -0
  52. package/src/components/Dashboard/telemetry.ts +24 -5
  53. package/src/components/DashboardBuilder/AddTileDialog.tsx +141 -143
  54. package/src/components/DashboardBuilder/BuilderToolbar.tsx +110 -218
  55. package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +5 -11
  56. package/src/components/DashboardBuilder/DashboardBuilder.tsx +202 -171
  57. package/src/components/DashboardBuilder/DashboardEditor.authoritative.spec.tsx +706 -0
  58. package/src/components/DashboardBuilder/DashboardEditor.package.spec.tsx +163 -0
  59. package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +2 -25
  60. package/src/components/DashboardBuilder/DashboardEditor.tsx +442 -94
  61. package/src/components/DashboardBuilder/DiffDialog.tsx +75 -82
  62. package/src/components/DashboardBuilder/DrillDialog.tsx +144 -156
  63. package/src/components/DashboardBuilder/FilterDialog.tsx +276 -295
  64. package/src/components/DashboardBuilder/FilterStrip.tsx +11 -10
  65. package/src/components/DashboardBuilder/newDashboard.spec.ts +40 -0
  66. package/src/components/DashboardBuilder/newDashboard.ts +48 -0
  67. package/src/components/DashboardBuilder/spliceDocument.spec.ts +53 -0
  68. package/src/components/DashboardBuilder/spliceDocument.ts +45 -8
  69. package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +56 -0
  70. package/src/components/DashboardBuilder/useDashboardEditor.ts +12 -4
  71. package/src/components/DocumentStorage/BrowserDocumentStorage.ts +7 -6
  72. package/src/components/DocumentStorage/DocumentStorage.ts +78 -5
  73. package/src/components/DocumentStorage/index.ts +1 -0
  74. package/src/components/Environment/About.tsx +14 -28
  75. package/src/components/Environment/AddPackageDialog.tsx +85 -154
  76. package/src/components/Environment/Connections.tsx +125 -205
  77. package/src/components/Environment/DeletePackageDialog.tsx +32 -77
  78. package/src/components/Environment/EditPackageDialog.tsx +46 -58
  79. package/src/components/Environment/Environment.tsx +34 -36
  80. package/src/components/Environment/Packages.tsx +63 -128
  81. package/src/components/Home/AddEnvironmentDialog.tsx +47 -70
  82. package/src/components/Home/DeleteEnvironmentDialog.tsx +32 -77
  83. package/src/components/Home/EditEnvironmentDialog.tsx +47 -61
  84. package/src/components/Home/Home.tsx +101 -214
  85. package/src/components/ItemRow.tsx +165 -0
  86. package/src/components/Materializations/CreateMaterializationDialog.tsx +34 -51
  87. package/src/components/Materializations/DeleteMaterializationDialog.tsx +31 -53
  88. package/src/components/Materializations/MaterializationDetailDialog.tsx +60 -69
  89. package/src/components/Materializations/MaterializationRunsList.tsx +29 -42
  90. package/src/components/Materializations/Materializations.tsx +137 -140
  91. package/src/components/Materializations/ScopeButton.tsx +85 -0
  92. package/src/components/Materializations/SectionLabel.tsx +7 -4
  93. package/src/components/Materializations/SetScheduleDialog.tsx +48 -89
  94. package/src/components/Materializations/TriggerLabel.tsx +36 -0
  95. package/src/components/Materializations/cron.spec.ts +113 -0
  96. package/src/components/Materializations/index.ts +0 -1
  97. package/src/components/Materializations/utils.spec.ts +182 -0
  98. package/src/components/Package/ContentTypeIcon.spec.ts +22 -37
  99. package/src/components/Package/ContentTypeIcon.tsx +10 -12
  100. package/src/components/Package/NewDashboardDialog.spec.tsx +129 -0
  101. package/src/components/Package/NewDashboardDialog.tsx +265 -0
  102. package/src/components/Package/Package.tsx +184 -238
  103. package/src/components/Package/index.ts +0 -1
  104. package/src/components/PackageSection.tsx +85 -0
  105. package/src/components/RenderedResult/RenderedResult.lifecycle.spec.tsx +198 -0
  106. package/src/components/RenderedResult/RenderedResult.tsx +22 -8
  107. package/src/components/ResultsDialog.tsx +8 -32
  108. package/src/components/buttons.tsx +114 -0
  109. package/src/components/designSystem.spec.tsx +316 -0
  110. package/src/components/given/paramCodec.spec.ts +1 -1
  111. package/src/components/index.ts +10 -0
  112. package/src/components/styles.ts +49 -85
  113. package/src/hooks/useCrudMutation.spec.tsx +119 -0
  114. package/src/hooks/useCrudMutation.tsx +85 -0
  115. package/src/index.ts +9 -0
  116. package/src/telemetry/consoleEvents.spec.ts +130 -0
  117. package/src/telemetry/consoleEvents.ts +124 -0
  118. package/src/theme/defaults.ts +34 -23
  119. package/src/theme/resolveTheme.spec.ts +9 -4
  120. package/src/theme/resolveTheme.ts +12 -8
  121. package/src/utils/sha256.ts +13 -0
  122. package/test/README.md +1 -1
  123. package/test/serverProvider.tsx +5 -2
  124. package/dist/DocumentStorageProvider-BeBD0uWc.cjs.js +0 -134
  125. package/dist/RenderedResult-CPi6uQWq.cjs.js +0 -115
  126. package/dist/ServerProvider-CH71GsoL.cjs.js +0 -1
  127. package/dist/components/Materializations/CreateEnvMaterializationDialog.d.ts +0 -14
  128. package/dist/components/Materializations/EnvironmentMaterializations.d.ts +0 -7
  129. package/dist/components/Materializations/ScheduleCard.d.ts +0 -20
  130. package/dist/components/Materializations/TriggerChip.d.ts +0 -10
  131. package/dist/components/Package/Models.d.ts +0 -6
  132. package/src/components/Materializations/CreateEnvMaterializationDialog.tsx +0 -179
  133. package/src/components/Materializations/EnvironmentMaterializations.tsx +0 -267
  134. package/src/components/Materializations/ScheduleCard.tsx +0 -231
  135. package/src/components/Materializations/TriggerChip.tsx +0 -44
  136. package/src/components/Package/Models.tsx +0 -71
@@ -0,0 +1,198 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { act, render, waitFor } from "@testing-library/react";
5
+ import { beforeEach, describe, expect, it, mock } from "bun:test";
6
+
7
+ /**
8
+ * The renderer's STAGE LIFECYCLE, which is the part of this component that has
9
+ * actually broken.
10
+ *
11
+ * `RenderedResult` never paints into its container directly. Each render builds
12
+ * a fresh "stage" div, appends it, renders the chart into it offscreen, and
13
+ * swaps it in only once the chart has painted — so a re-render never blanks the
14
+ * tile. Every defect this file guards is a bookkeeping error in that dance, and
15
+ * none of them is visible in a screenshot of a working chart:
16
+ *
17
+ * - The drag bug: a cleanup disposed the viz but left its stage node in the
18
+ * DOM, so the next render appended a second stage BELOW it, outside the
19
+ * container's clip — the tile drew empty while its rows sat in the DOM the
20
+ * whole time. Reproducing THAT one needs a real drag over a container that
21
+ * outlives the cleanup, so it is pinned in `dashboard-builder.spec.ts`; what
22
+ * is unit-testable here is that unmount takes both, and that a swap leaves
23
+ * exactly one stage behind.
24
+ * - Two charts at once: a promote that adds the new stage without removing
25
+ * what was already there.
26
+ * - A leaked viz: an async render that resolves after a newer one started, or
27
+ * after unmount, and paints anyway.
28
+ *
29
+ * The real renderer is a heavy async chunk that wants a browser; it is faked
30
+ * here down to the five methods this component calls, with `onReady` under the
31
+ * test's control so a render can be held mid-flight.
32
+ */
33
+
34
+ /** Every viz the component has created, in order. */
35
+ const vizzes: FakeViz[] = [];
36
+
37
+ class FakeViz {
38
+ /** The stage it was told to paint into. */
39
+ stage: HTMLElement | null = null;
40
+ removed = false;
41
+ private ready: (() => void) | null = null;
42
+ /** Fire the renderer's "I have painted" signal. */
43
+ paint() {
44
+ this.ready?.();
45
+ }
46
+ setResult() {}
47
+ render(element: HTMLElement) {
48
+ this.stage = element;
49
+ const painted = document.createElement("div");
50
+ painted.className = "malloy-render";
51
+ element.appendChild(painted);
52
+ }
53
+ remove() {
54
+ this.removed = true;
55
+ }
56
+ onReady(callback: () => void) {
57
+ this.ready = callback;
58
+ }
59
+ getMetadata() {
60
+ return null;
61
+ }
62
+ getDrillMetadata() {
63
+ return null;
64
+ }
65
+ }
66
+
67
+ mock.module("@malloydata/render", () => ({
68
+ MalloyRenderer: class {
69
+ createViz() {
70
+ const viz = new FakeViz();
71
+ vizzes.push(viz);
72
+ return viz;
73
+ }
74
+ },
75
+ }));
76
+
77
+ const RenderedResult = (await import("./RenderedResult")).default;
78
+
79
+ /** A minimal result payload; the fake renderer never reads it. */
80
+ const RESULT = JSON.stringify({ tag: "a" });
81
+ const OTHER = JSON.stringify({ tag: "b" });
82
+
83
+ /** The stage divs currently inside the component's container. */
84
+ const stagesIn = (container: HTMLElement): Element[] => {
85
+ const host = container.querySelector("[data-testid], div");
86
+ void host;
87
+ return Array.from(container.querySelectorAll(".malloy-render")).map(
88
+ (n) => n.parentElement as Element,
89
+ );
90
+ };
91
+
92
+ beforeEach(() => {
93
+ vizzes.length = 0;
94
+ });
95
+
96
+ describe("RenderedResult: the stage lifecycle", () => {
97
+ it("paints one chart into one stage", async () => {
98
+ const view = render(<RenderedResult result={RESULT} />);
99
+ await waitFor(() => expect(vizzes).toHaveLength(1));
100
+ act(() => vizzes[0].paint());
101
+ await waitFor(() =>
102
+ expect(stagesIn(view.container as HTMLElement)).toHaveLength(1),
103
+ );
104
+ expect(vizzes[0].removed).toBe(false);
105
+ });
106
+
107
+ it("disposes the viz as well as the node when it unmounts", async () => {
108
+ const view = render(<RenderedResult result={RESULT} />);
109
+ await waitFor(() => expect(vizzes).toHaveLength(1));
110
+ act(() => vizzes[0].paint());
111
+ await waitFor(() =>
112
+ expect(stagesIn(view.container as HTMLElement)).toHaveLength(1),
113
+ );
114
+ const stage = vizzes[0].stage!;
115
+ view.unmount();
116
+ expect(vizzes[0].removed).toBe(true);
117
+ expect(stage.isConnected).toBe(false);
118
+ });
119
+
120
+ it("keeps the old chart on screen until the new one has painted", async () => {
121
+ // The no-flicker rule: a re-render must never leave the tile empty.
122
+ const view = render(<RenderedResult result={RESULT} />);
123
+ await waitFor(() => expect(vizzes).toHaveLength(1));
124
+ act(() => vizzes[0].paint());
125
+ await waitFor(() =>
126
+ expect(stagesIn(view.container as HTMLElement)).toHaveLength(1),
127
+ );
128
+
129
+ view.rerender(<RenderedResult result={OTHER} />);
130
+ await waitFor(() => expect(vizzes).toHaveLength(2));
131
+
132
+ // Both are in the DOM, the outgoing one still painted, and the incoming
133
+ // one hidden and overlaid rather than stacked below it.
134
+ await waitFor(() =>
135
+ expect(stagesIn(view.container as HTMLElement)).toHaveLength(2),
136
+ );
137
+ expect(vizzes[0].removed).toBe(false);
138
+ const incoming = vizzes[1].stage as HTMLElement;
139
+ expect(incoming.style.position).toBe("absolute");
140
+ expect(incoming.style.visibility).toBe("hidden");
141
+ });
142
+
143
+ it("sweeps the container on promote, so exactly one stage survives", async () => {
144
+ const view = render(<RenderedResult result={RESULT} />);
145
+ await waitFor(() => expect(vizzes).toHaveLength(1));
146
+ act(() => vizzes[0].paint());
147
+ await waitFor(() =>
148
+ expect(stagesIn(view.container as HTMLElement)).toHaveLength(1),
149
+ );
150
+
151
+ view.rerender(<RenderedResult result={OTHER} />);
152
+ await waitFor(() => expect(vizzes).toHaveLength(2));
153
+ act(() => vizzes[1].paint());
154
+
155
+ await waitFor(() =>
156
+ expect(stagesIn(view.container as HTMLElement)).toHaveLength(1),
157
+ );
158
+ // The outgoing viz is disposed by the swap, not left to leak.
159
+ expect(vizzes[0].removed).toBe(true);
160
+ expect(vizzes[1].removed).toBe(false);
161
+ // And the survivor is revealed rather than left overlaid.
162
+ const live = vizzes[1].stage as HTMLElement;
163
+ expect(live.style.visibility).toBe("");
164
+ expect(live.style.position).toBe("");
165
+ });
166
+
167
+ it("ignores a stale render that paints after a newer one took over", async () => {
168
+ // A slow chart resolving late must not resurrect itself over the chart
169
+ // that replaced it, nor leave two stages behind.
170
+ const view = render(<RenderedResult result={RESULT} />);
171
+ await waitFor(() => expect(vizzes).toHaveLength(1));
172
+ act(() => vizzes[0].paint());
173
+ await waitFor(() =>
174
+ expect(stagesIn(view.container as HTMLElement)).toHaveLength(1),
175
+ );
176
+
177
+ view.rerender(<RenderedResult result={OTHER} />);
178
+ await waitFor(() => expect(vizzes).toHaveLength(2));
179
+ act(() => vizzes[1].paint());
180
+ await waitFor(() =>
181
+ expect(stagesIn(view.container as HTMLElement)).toHaveLength(1),
182
+ );
183
+
184
+ // The superseded render signals ready late. Nothing should move.
185
+ act(() => vizzes[0].paint());
186
+ expect(stagesIn(view.container as HTMLElement)).toHaveLength(1);
187
+ expect(vizzes[1].removed).toBe(false);
188
+ });
189
+
190
+ it("does not paint at all once it has unmounted", async () => {
191
+ const view = render(<RenderedResult result={RESULT} />);
192
+ await waitFor(() => expect(vizzes).toHaveLength(1));
193
+ view.unmount();
194
+ // A render resolving after unmount must not re-attach anything.
195
+ act(() => vizzes[0].paint());
196
+ expect(vizzes[0].removed).toBe(true);
197
+ });
198
+ });
@@ -418,10 +418,20 @@ function RenderedResultInner({
418
418
  // Dispose the last live viz on unmount only. Deliberately NOT done in the
419
419
  // render effect's cleanup: a re-run must keep the old chart until the new
420
420
  // one has painted, and the new render disposes it during the swap.
421
+ //
422
+ // The NODE goes with the viz, not just the viz. React destroys and
423
+ // re-creates a component's effects without discarding its DOM in several
424
+ // ordinary cases — a Suspense boundary revealing, development's
425
+ // double-invoked mount — and there the container this stage sits in is
426
+ // still on screen afterwards. Disposing the viz alone left an empty stage
427
+ // in it, which the next render then appended BELOW: the chart, pushed out
428
+ // of a clipped box, read as a tile that had lost its content.
421
429
  useEffect(() => {
422
430
  return () => {
423
431
  renderGenRef.current += 1;
424
- liveRef.current?.viz.remove();
432
+ const live = liveRef.current;
433
+ live?.viz.remove();
434
+ live?.node.remove();
425
435
  liveRef.current = null;
426
436
  };
427
437
  }, []);
@@ -545,7 +555,11 @@ function RenderedResultInner({
545
555
  // would repaint the old chart's chrome to the new theme before it is
546
556
  // swapped out. Scoping the vars to this stage keeps each chart stable.
547
557
  applyTableCssVars(stage, effectiveTheme, cardGeometry);
548
- if (previous) {
558
+ // Overlaid whenever the container is not empty, which is the
559
+ // outgoing chart and, defensively, anything an earlier render left
560
+ // behind. A stage appended as a SIBLING in the flow sits below what
561
+ // is already there and falls outside the container's clip.
562
+ if (element.childElementCount > 0) {
549
563
  element.style.position = "relative";
550
564
  stage.style.position = "absolute";
551
565
  stage.style.inset = "0";
@@ -589,12 +603,12 @@ function RenderedResultInner({
589
603
  clearTimeout(readyFallback);
590
604
  readyFallback = null;
591
605
  }
592
- // The new chart has painted; drop the outgoing one now.
593
- if (previous) {
594
- previous.viz.remove();
595
- if (previous.node.parentNode === element) {
596
- element.removeChild(previous.node);
597
- }
606
+ // The new chart has painted; drop the outgoing one now, and with
607
+ // it anything else still in the container, so the promoted stage
608
+ // is the only thing in it.
609
+ previous?.viz.remove();
610
+ for (const node of Array.from(element.children)) {
611
+ if (node !== stageNode) element.removeChild(node);
598
612
  }
599
613
  stageNode.style.position = "";
600
614
  stageNode.style.inset = "";
@@ -1,12 +1,12 @@
1
1
  // Copyright (c) Credible Data Inc.
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
- import CloseIcon from "@mui/icons-material/Close";
5
- import { Dialog, DialogContent, DialogTitle, IconButton } from "@mui/material";
4
+ import { Box } from "@mui/material";
6
5
  import { LogMessage } from "../client";
7
6
  import type { DrillBinding } from "./drill";
8
7
  import ResultContainer from "./RenderedResult/ResultContainer";
9
8
  import { RESULTS_DIALOG_MAX_HEIGHT } from "./RenderedResult/resultSizing";
9
+ import { AppDialog } from "./AppDialog";
10
10
 
11
11
  interface ResultsDialogProps {
12
12
  open: boolean;
@@ -27,38 +27,14 @@ export default function ResultsDialog({
27
27
  drill,
28
28
  }: ResultsDialogProps) {
29
29
  return (
30
- <Dialog
30
+ <AppDialog
31
31
  open={open}
32
32
  onClose={onClose}
33
+ title={title}
33
34
  maxWidth={false}
34
- fullWidth
35
- sx={{
36
- "& .MuiDialog-paper": {
37
- width: "95vw",
38
- height: "95vh",
39
- maxWidth: "none",
40
- },
41
- }}
35
+ showClose
42
36
  >
43
- <DialogTitle
44
- sx={{
45
- display: "flex",
46
- justifyContent: "space-between",
47
- alignItems: "center",
48
- }}
49
- >
50
- {title}
51
- <IconButton onClick={onClose} sx={{ color: "text.secondary" }}>
52
- <CloseIcon />
53
- </IconButton>
54
- </DialogTitle>
55
- <DialogContent
56
- sx={{
57
- height: "calc(95vh - 120px)",
58
- overflow: "auto",
59
- padding: "0 16px",
60
- }}
61
- >
37
+ <Box sx={{ height: "70vh", overflow: "auto" }}>
62
38
  <ResultContainer
63
39
  result={result}
64
40
  maxHeight={RESULTS_DIALOG_MAX_HEIGHT}
@@ -66,7 +42,7 @@ export default function ResultsDialog({
66
42
  renderLogs={renderLogs}
67
43
  drill={drill}
68
44
  />
69
- </DialogContent>
70
- </Dialog>
45
+ </Box>
46
+ </AppDialog>
71
47
  );
72
48
  }
@@ -0,0 +1,114 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import AddIcon from "@mui/icons-material/Add";
5
+ import { Button, Tooltip } from "@mui/material";
6
+ import * as React from "react";
7
+
8
+ /**
9
+ * The Console's buttons, by the job they do. Four roles, and every button on
10
+ * every page is one of them:
11
+ *
12
+ * 1. **Primary** — the one thing a surface is for: adding a package, a
13
+ * dashboard, a materialization; the confirm in a dialog. A filled dark pill
14
+ * at the default size. {@link AddButton} is the section-heading form of it.
15
+ * 2. **Secondary** — a control beside the primary one, changing something that
16
+ * already exists: Scope, Schedule, Filter. An outlined pill, the same size
17
+ * as the primary, so a heading row reads as one set of controls rather than
18
+ * a big button and some small ones. {@link SecondaryButton}.
19
+ * 3. **Quiet** — a dense bar of equals, where an outline per item would draw a
20
+ * grid: the dashboard builder's toolbar, a dialog's Cancel. A small text
21
+ * button, written inline.
22
+ * 4. **Icon** — a row's overflow menu or a compact toggle. A small icon
23
+ * button, written inline.
24
+ *
25
+ * The roles are exhaustive on purpose. A button that is none of them is a new
26
+ * family of control on a page that already has four, so add it here — with the
27
+ * job it does — rather than inline at its one call site.
28
+ */
29
+
30
+ /**
31
+ * The control that adds one thing to a section: a filled pill with a plus,
32
+ * right of the section's heading.
33
+ *
34
+ * The LABEL names the thing alone — "Package", "Connection", "Dashboard" —
35
+ * because the plus already says add; the verb went back into the accessible
36
+ * name, where a control called "Package" would otherwise say nothing about
37
+ * what it does.
38
+ */
39
+ export function AddButton({
40
+ label,
41
+ onClick,
42
+ disabled,
43
+ }: {
44
+ /** The thing being added, as it should read on the button: "Package". */
45
+ label: string;
46
+ onClick: () => void;
47
+ disabled?: boolean;
48
+ }) {
49
+ return (
50
+ <Button
51
+ variant="contained"
52
+ color="primary"
53
+ // Always the plus. The glyph is half of what makes this role
54
+ // recognisable across pages, and a per-caller override is how a
55
+ // consistent control becomes an inconsistent one.
56
+ startIcon={<AddIcon />}
57
+ onClick={onClick}
58
+ disabled={disabled}
59
+ aria-label={`Add ${label.toLowerCase()}`}
60
+ sx={{ whiteSpace: "nowrap" }}
61
+ >
62
+ {label}
63
+ </Button>
64
+ );
65
+ }
66
+
67
+ /**
68
+ * A secondary control: outlined, and the same size as the primary it sits
69
+ * beside. Takes its own icon and, when it is disabled, the reason — a control
70
+ * that greys out without saying why is a control a reader has to guess at.
71
+ */
72
+ export function SecondaryButton({
73
+ label,
74
+ icon,
75
+ onClick,
76
+ disabled,
77
+ disabledReason,
78
+ ariaLabel,
79
+ ariaHasPopup,
80
+ }: {
81
+ label: string;
82
+ icon?: React.ReactNode;
83
+ onClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
84
+ disabled?: boolean;
85
+ /** Shown on hover when disabled. */
86
+ disabledReason?: string;
87
+ /** Overrides the accessible name, which is otherwise the label. */
88
+ ariaLabel?: string;
89
+ ariaHasPopup?: "menu" | "dialog";
90
+ }) {
91
+ const button = (
92
+ // A disabled button fires no pointer events, so the tooltip needs a
93
+ // wrapper that does.
94
+ <span>
95
+ <Button
96
+ variant="outlined"
97
+ startIcon={icon}
98
+ onClick={onClick}
99
+ disabled={disabled}
100
+ aria-label={ariaLabel}
101
+ aria-haspopup={ariaHasPopup}
102
+ // A pill that wraps to two lines stops reading as a pill.
103
+ sx={{ whiteSpace: "nowrap" }}
104
+ >
105
+ {label}
106
+ </Button>
107
+ </span>
108
+ );
109
+ return disabled && disabledReason ? (
110
+ <Tooltip title={disabledReason}>{button}</Tooltip>
111
+ ) : (
112
+ button
113
+ );
114
+ }