@spfx-fast/sharepoint-ui 0.0.0-stage → 0.4.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 (158) hide show
  1. package/AGENTS.md +211 -0
  2. package/LICENSE +21 -0
  3. package/README.md +897 -2
  4. package/components.json +5810 -0
  5. package/dist/appearance.d.ts +6 -0
  6. package/dist/appearance.js +217 -0
  7. package/dist/elements/accordion.d.ts +9 -0
  8. package/dist/elements/accordion.js +338 -0
  9. package/dist/elements/alert.d.ts +2 -0
  10. package/dist/elements/alert.js +62 -0
  11. package/dist/elements/avatar-group-primitives.d.ts +3 -0
  12. package/dist/elements/avatar-group-primitives.js +189 -0
  13. package/dist/elements/avatar-group.d.ts +2 -0
  14. package/dist/elements/avatar-group.js +110 -0
  15. package/dist/elements/avatar.d.ts +4 -0
  16. package/dist/elements/avatar.js +251 -0
  17. package/dist/elements/badge.d.ts +8 -0
  18. package/dist/elements/badge.js +182 -0
  19. package/dist/elements/breadcrumb.d.ts +4 -0
  20. package/dist/elements/breadcrumb.js +233 -0
  21. package/dist/elements/button-group.d.ts +1 -0
  22. package/dist/elements/button-group.js +20 -0
  23. package/dist/elements/button-variants.d.ts +6 -0
  24. package/dist/elements/button-variants.js +339 -0
  25. package/dist/elements/button.d.ts +5 -0
  26. package/dist/elements/button.js +233 -0
  27. package/dist/elements/card-primitives.d.ts +3 -0
  28. package/dist/elements/card-primitives.js +83 -0
  29. package/dist/elements/card.d.ts +5 -0
  30. package/dist/elements/card.js +213 -0
  31. package/dist/elements/carousel.d.ts +92 -0
  32. package/dist/elements/carousel.js +1112 -0
  33. package/dist/elements/color-picker.d.ts +24 -0
  34. package/dist/elements/color-picker.js +1004 -0
  35. package/dist/elements/combobox.d.ts +1 -0
  36. package/dist/elements/combobox.js +1012 -0
  37. package/dist/elements/data-grid-model.d.ts +117 -0
  38. package/dist/elements/data-grid-model.js +294 -0
  39. package/dist/elements/data-grid.d.ts +29 -0
  40. package/dist/elements/data-grid.js +1567 -0
  41. package/dist/elements/date-picker.d.ts +1 -0
  42. package/dist/elements/date-picker.js +486 -0
  43. package/dist/elements/dialog.d.ts +23 -0
  44. package/dist/elements/dialog.js +409 -0
  45. package/dist/elements/divider.d.ts +2 -0
  46. package/dist/elements/divider.js +66 -0
  47. package/dist/elements/drawer.d.ts +3 -0
  48. package/dist/elements/drawer.js +308 -0
  49. package/dist/elements/field-composition.d.ts +26 -0
  50. package/dist/elements/field-composition.js +133 -0
  51. package/dist/elements/field.d.ts +1 -0
  52. package/dist/elements/field.js +67 -0
  53. package/dist/elements/file-upload.d.ts +6 -0
  54. package/dist/elements/file-upload.js +211 -0
  55. package/dist/elements/floating-position.d.ts +26 -0
  56. package/dist/elements/floating-position.js +39 -0
  57. package/dist/elements/image.d.ts +28 -0
  58. package/dist/elements/image.js +38 -0
  59. package/dist/elements/info.d.ts +9 -0
  60. package/dist/elements/info.js +102 -0
  61. package/dist/elements/input.d.ts +5 -0
  62. package/dist/elements/input.js +255 -0
  63. package/dist/elements/label.d.ts +15 -0
  64. package/dist/elements/label.js +56 -0
  65. package/dist/elements/link.d.ts +1 -0
  66. package/dist/elements/link.js +92 -0
  67. package/dist/elements/list.d.ts +19 -0
  68. package/dist/elements/list.js +297 -0
  69. package/dist/elements/menu.d.ts +33 -0
  70. package/dist/elements/menu.js +1103 -0
  71. package/dist/elements/message-bar.d.ts +10 -0
  72. package/dist/elements/message-bar.js +299 -0
  73. package/dist/elements/nav.d.ts +32 -0
  74. package/dist/elements/nav.js +791 -0
  75. package/dist/elements/overflow.d.ts +9 -0
  76. package/dist/elements/overflow.js +218 -0
  77. package/dist/elements/persona.d.ts +3 -0
  78. package/dist/elements/persona.js +124 -0
  79. package/dist/elements/popover.d.ts +77 -0
  80. package/dist/elements/popover.js +840 -0
  81. package/dist/elements/progress-bar.d.ts +4 -0
  82. package/dist/elements/progress-bar.js +82 -0
  83. package/dist/elements/radio-group.d.ts +1 -0
  84. package/dist/elements/radio-group.js +303 -0
  85. package/dist/elements/rating-display.d.ts +2 -0
  86. package/dist/elements/rating-display.js +125 -0
  87. package/dist/elements/rating.d.ts +2 -0
  88. package/dist/elements/rating.js +206 -0
  89. package/dist/elements/rich-text-editor.d.ts +18 -0
  90. package/dist/elements/rich-text-editor.js +508 -0
  91. package/dist/elements/search.d.ts +3 -0
  92. package/dist/elements/search.js +207 -0
  93. package/dist/elements/select.d.ts +6 -0
  94. package/dist/elements/select.js +291 -0
  95. package/dist/elements/shared.d.ts +19 -0
  96. package/dist/elements/shared.js +396 -0
  97. package/dist/elements/skeleton.d.ts +6 -0
  98. package/dist/elements/skeleton.js +91 -0
  99. package/dist/elements/slider.d.ts +1 -0
  100. package/dist/elements/slider.js +248 -0
  101. package/dist/elements/spin-button.d.ts +7 -0
  102. package/dist/elements/spin-button.js +499 -0
  103. package/dist/elements/spinner.d.ts +4 -0
  104. package/dist/elements/spinner.js +124 -0
  105. package/dist/elements/swatch-picker.d.ts +5 -0
  106. package/dist/elements/swatch-picker.js +363 -0
  107. package/dist/elements/table.d.ts +21 -0
  108. package/dist/elements/table.js +261 -0
  109. package/dist/elements/tabs.d.ts +1 -0
  110. package/dist/elements/tabs.js +276 -0
  111. package/dist/elements/tag-picker.d.ts +29 -0
  112. package/dist/elements/tag-picker.js +697 -0
  113. package/dist/elements/tags.d.ts +21 -0
  114. package/dist/elements/tags.js +421 -0
  115. package/dist/elements/teaching-popover.d.ts +32 -0
  116. package/dist/elements/teaching-popover.js +412 -0
  117. package/dist/elements/text.d.ts +5 -0
  118. package/dist/elements/text.js +69 -0
  119. package/dist/elements/textarea.d.ts +1 -0
  120. package/dist/elements/textarea.js +182 -0
  121. package/dist/elements/toast.d.ts +25 -0
  122. package/dist/elements/toast.js +337 -0
  123. package/dist/elements/toaster.d.ts +6 -0
  124. package/dist/elements/toaster.js +356 -0
  125. package/dist/elements/toggle.d.ts +2 -0
  126. package/dist/elements/toggle.js +246 -0
  127. package/dist/elements/toolbar.d.ts +12 -0
  128. package/dist/elements/toolbar.js +381 -0
  129. package/dist/elements/tooltip.d.ts +76 -0
  130. package/dist/elements/tooltip.js +510 -0
  131. package/dist/elements/tree-model.d.ts +19 -0
  132. package/dist/elements/tree-model.js +34 -0
  133. package/dist/elements/tree.d.ts +5 -0
  134. package/dist/elements/tree.js +273 -0
  135. package/dist/elements/view-card.d.ts +2 -0
  136. package/dist/elements/view-card.js +78 -0
  137. package/dist/elements/virtual-list.d.ts +41 -0
  138. package/dist/elements/virtual-list.js +369 -0
  139. package/dist/elements.d.ts +72 -0
  140. package/dist/elements.js +207 -0
  141. package/dist/index.d.ts +76 -0
  142. package/dist/index.js +66 -0
  143. package/dist/react-theme-styles.d.ts +2 -0
  144. package/dist/react-theme-styles.js +75 -0
  145. package/dist/react.d.ts +4540 -0
  146. package/dist/react.js +6799 -0
  147. package/dist/theme.d.ts +169 -0
  148. package/dist/theme.js +167 -0
  149. package/dist/virtualizer-hooks.d.ts +80 -0
  150. package/dist/virtualizer-hooks.js +253 -0
  151. package/package.json +87 -4
  152. package/scripts/generate-token-index.mjs +43 -0
  153. package/scripts/sync-component-catalog-signatures.mjs +55 -0
  154. package/scripts/validate-component-catalog.mjs +44 -0
  155. package/skills/sharepoint-ui/SKILL.md +12 -0
  156. package/skills/sharepoint-ui/references/choosing-components.md +20 -0
  157. package/skills/sharepoint-ui/references/sharepoint-patterns.md +17 -0
  158. package/tokens.json +3777 -0
@@ -0,0 +1,182 @@
1
+ import { base, rememberNativeFormControlDefaults, stopHostAccessibleNameSync, syncHostAccessibleName } from "./shared.js";
2
+ export function defineTextArea() {
3
+ if (customElements.get("spui-textarea"))
4
+ return;
5
+ class SpuiTextArea extends HTMLElement {
6
+ static formAssociated = true;
7
+ static observedAttributes = ["label", "value", "default-value", "rows", "size", "appearance", "resize", "disabled", "required", "read-only", "placeholder", "aria-label", "aria-labelledby", "aria-description", "aria-describedby", "aria-invalid", "name", "autocomplete", "dirname", "form", "maxlength", "minlength", "cols", "wrap"];
8
+ internals;
9
+ currentValue = "";
10
+ dirty = false;
11
+ formDisabled = false;
12
+ boundTextarea;
13
+ slotTextareaDefaults;
14
+ constructor() { super(); try {
15
+ this.internals = this.attachInternals();
16
+ }
17
+ catch { /* The native textarea still works when form-associated custom elements are unavailable. */ } }
18
+ get value() { return this.input()?.value ?? this.currentValue; }
19
+ set value(value) { this.currentValue = String(value ?? ""); this.dirty = true; const control = this.input(); if (control)
20
+ control.value = this.currentValue; this.syncForm(control); }
21
+ get defaultValue() { return this.getAttribute("default-value") ?? this.getAttribute("value") ?? this.input()?.defaultValue ?? ""; }
22
+ set defaultValue(value) { this.setAttribute("default-value", String(value ?? "")); }
23
+ get name() { return this.getAttribute("name") ?? ""; }
24
+ set name(value) { this.setAttribute("name", value); }
25
+ get disabled() { return this.hasAttribute("disabled"); }
26
+ set disabled(value) { this.toggleAttribute("disabled", Boolean(value)); }
27
+ get required() { return this.hasAttribute("required"); }
28
+ set required(value) { this.toggleAttribute("required", Boolean(value)); }
29
+ get readOnly() { return this.hasAttribute("read-only"); }
30
+ set readOnly(value) { this.toggleAttribute("read-only", Boolean(value)); }
31
+ get form() { return this.internals?.form ?? null; }
32
+ get validity() { return this.internals?.validity ?? this.input()?.validity; }
33
+ get validationMessage() { return this.internals?.validationMessage ?? this.input()?.validationMessage ?? ""; }
34
+ get willValidate() { return this.internals?.willValidate ?? Boolean(this.input() && !this.input().disabled && !this.input().readOnly); }
35
+ connectedCallback() {
36
+ if (this.shadowRoot)
37
+ return;
38
+ const shadow = this.attachShadow({ mode: "open" });
39
+ const id = `textarea-${Math.random().toString(36).slice(2)}`;
40
+ const label = document.createElement("label");
41
+ label.htmlFor = id;
42
+ label.textContent = this.getAttribute("label") ?? "";
43
+ label.part.add("label");
44
+ const input = document.createElement("textarea");
45
+ input.className = "textarea-fallback";
46
+ input.id = id;
47
+ input.defaultValue = this.getAttribute("default-value") ?? this.getAttribute("value") ?? "";
48
+ input.value = this.getAttribute("value") ?? input.defaultValue;
49
+ input.rows = Number(this.getAttribute("rows") ?? 3);
50
+ input.disabled = this.hasAttribute("disabled");
51
+ input.required = this.hasAttribute("required");
52
+ input.readOnly = this.hasAttribute("read-only");
53
+ input.placeholder = this.getAttribute("placeholder") ?? "";
54
+ input.part.add("control");
55
+ for (const name of ["aria-label", "aria-labelledby", "aria-description", "aria-describedby", "aria-invalid", "name", "autocomplete", "dirname", "form", "maxlength", "minlength", "cols", "wrap"]) {
56
+ const value = this.getAttribute(name);
57
+ if (value !== null)
58
+ input.setAttribute(name, value);
59
+ }
60
+ const style = document.createElement("style");
61
+ style.textContent = `${base}:host{display:grid;gap:var(--spui-control-gap,6px)}label{font-size:14px;font-weight:600;color:var(--spui-ui-muted,var(--spui-semantic-body-text,inherit))}textarea{width:100%;min-height:72px;box-sizing:border-box;padding:var(--spui-control-padding-block,10px) var(--spui-control-padding-inline,12px);border:1px solid var(--spui-control-border,var(--spui-ui-stroke,var(--spui-semantic-input-border,var(--colorNeutralStroke1,#8a8886))));border-radius:var(--spui-control-radius,4px);background:var(--spui-control-background,var(--spui-ui-surface,var(--spui-semantic-input-background,var(--colorNeutralBackground1,#fff))));box-shadow:var(--spui-control-shadow,none);color:var(--spui-ui-text,var(--spui-semantic-body-text,inherit));font:inherit;resize:none}:host([size=small]) textarea{min-height:64px;padding-block:8px;font-size:var(--spui-text-font-size-small,12px)}:host([size=medium]) textarea{min-height:72px}:host([size=large]) textarea{min-height:88px;font-size:var(--spui-text-font-size-large,16px)}:host([appearance=filled-darker]),:host([appearance=filled-darker-shadow]){--spui-control-background:var(--spui-input-filled-darker,var(--spui-ui-surface-hover,var(--colorNeutralBackground3,#f0f0f0)));--spui-control-border:transparent}:host([appearance=filled-lighter]),:host([appearance=filled-lighter-shadow]){--spui-control-background:var(--spui-input-filled-lighter,var(--spui-ui-canvas,var(--colorNeutralBackground2,#fafafa)));--spui-control-border:transparent}:host([appearance=filled-darker-shadow]) textarea,:host([appearance=filled-lighter-shadow]) textarea{box-shadow:var(--spui-input-filled-shadow,var(--shadow2,0 1px 2px rgb(0 0 0 / 20%)))}:host([resize=vertical]) textarea{resize:vertical}:host([resize=horizontal]) textarea{resize:horizontal}:host([resize=both]) textarea{resize:both}textarea:focus-visible{outline:2px solid var(--spui-focus-ring,var(--spui-ui-accent,#0078d4));outline-offset:1px}textarea:disabled{opacity:.55}@media(forced-colors:active){textarea{border-color:FieldText}textarea:focus-visible{outline-color:Highlight}}`;
62
+ style.textContent += `textarea[hidden]{display:none}slot[name=textarea]{display:contents}slot[name=textarea]::slotted(textarea){width:100%;min-height:72px;box-sizing:border-box;padding:var(--spui-control-padding-block,10px) var(--spui-control-padding-inline,12px);border:1px solid var(--spui-control-border,var(--spui-ui-stroke,var(--spui-semantic-input-border,var(--colorNeutralStroke1,#8a8886))));border-radius:var(--spui-control-radius,4px);background:var(--spui-control-background,var(--spui-ui-surface,var(--spui-semantic-input-background,var(--colorNeutralBackground1,#fff))));box-shadow:var(--spui-control-shadow,none);color:var(--spui-ui-text,var(--spui-semantic-body-text,inherit));font:inherit;resize:none}:host([size=small]) slot[name=textarea]::slotted(textarea){min-height:64px;padding-block:8px;font-size:var(--spui-text-font-size-small,12px)}:host([size=large]) slot[name=textarea]::slotted(textarea){min-height:88px;font-size:var(--spui-text-font-size-large,16px)}:host([resize=vertical]) slot[name=textarea]::slotted(textarea){resize:vertical}:host([resize=horizontal]) slot[name=textarea]::slotted(textarea){resize:horizontal}:host([resize=both]) slot[name=textarea]::slotted(textarea){resize:both}slot[name=textarea]::slotted(textarea:focus-visible){outline:2px solid var(--spui-focus-ring,var(--spui-ui-accent,#0078d4));outline-offset:1px}`;
63
+ style.textContent += `:host([appearance=filled-darker-shadow]) slot[name=textarea]::slotted(textarea),:host([appearance=filled-lighter-shadow]) slot[name=textarea]::slotted(textarea){box-shadow:var(--spui-input-filled-shadow,var(--shadow2,0 1px 2px rgb(0 0 0 / 20%)))}`;
64
+ const controlSlot = document.createElement("slot");
65
+ controlSlot.name = "textarea";
66
+ controlSlot.part.add("textarea");
67
+ controlSlot.append(input);
68
+ controlSlot.addEventListener("slotchange", () => this.syncTextareaSlot());
69
+ shadow.append(style, label, controlSlot);
70
+ if (!this.dirty) {
71
+ const assigned = this.input();
72
+ this.currentValue = this.hasAttribute("value") || this.hasAttribute("default-value") ? this.defaultValue : assigned && assigned !== input ? assigned.value : this.defaultValue;
73
+ input.value = this.currentValue;
74
+ }
75
+ this.syncTextareaSlot();
76
+ }
77
+ disconnectedCallback() { stopHostAccessibleNameSync(this); }
78
+ attributeChangedCallback(name) {
79
+ const input = this.input();
80
+ if (!input)
81
+ return;
82
+ if (name === "value" || name === "default-value") {
83
+ input.defaultValue = this.defaultValue;
84
+ if (!this.dirty) {
85
+ this.currentValue = this.defaultValue;
86
+ input.value = this.currentValue;
87
+ }
88
+ }
89
+ if (name === "rows")
90
+ input.rows = Number(this.getAttribute(name) ?? 3);
91
+ if (name === "disabled")
92
+ input.disabled = this.hasAttribute(name) || this.formDisabled;
93
+ if (name === "required")
94
+ input.required = this.hasAttribute(name);
95
+ if (name === "read-only")
96
+ input.readOnly = this.hasAttribute(name);
97
+ if (name === "placeholder")
98
+ input.placeholder = this.getAttribute(name) ?? "";
99
+ if (name === "label") {
100
+ const label = this.shadowRoot?.querySelector("label");
101
+ if (label)
102
+ label.textContent = this.getAttribute(name) ?? "";
103
+ }
104
+ if (["aria-label", "aria-labelledby", "aria-description", "aria-describedby", "aria-invalid", "name", "autocomplete", "dirname", "form", "maxlength", "minlength", "cols", "wrap"].includes(name)) {
105
+ const value = this.getAttribute(name);
106
+ value === null ? input.removeAttribute(name) : input.setAttribute(name, value);
107
+ }
108
+ this.syncTextareaSlot();
109
+ }
110
+ formDisabledCallback(disabled) { this.formDisabled = disabled; this.syncTextareaSlot(); }
111
+ formResetCallback() { this.dirty = false; const input = this.input(); this.currentValue = this.defaultValue; if (input) {
112
+ input.value = this.currentValue;
113
+ this.syncForm(input);
114
+ } }
115
+ formStateRestoreCallback(state) { if (typeof state === "string")
116
+ this.value = state; }
117
+ focus(options) { this.input()?.focus(options); }
118
+ select() { this.input()?.select(); }
119
+ checkValidity() { return this.internals?.checkValidity() ?? this.input()?.checkValidity() ?? true; }
120
+ reportValidity() { return this.internals?.reportValidity() ?? this.input()?.reportValidity() ?? true; }
121
+ input() {
122
+ const assigned = this.querySelector('textarea[slot="textarea"]');
123
+ if (assigned)
124
+ return assigned;
125
+ const slot = this.shadowRoot?.querySelector('slot[name="textarea"]');
126
+ return slot?.assignedElements().find((element) => element instanceof HTMLTextAreaElement) ?? this.shadowRoot?.querySelector(".textarea-fallback") ?? null;
127
+ }
128
+ syncTextareaSlot() {
129
+ const input = this.input();
130
+ if (!input)
131
+ return;
132
+ const fallback = this.shadowRoot?.querySelector(".textarea-fallback");
133
+ if (this.boundTextarea !== input) {
134
+ this.boundTextarea?.removeEventListener("input", this.handleTextareaInput);
135
+ input.addEventListener("input", this.handleTextareaInput);
136
+ this.boundTextarea = input;
137
+ }
138
+ if (input !== fallback)
139
+ this.slotTextareaDefaults = rememberNativeFormControlDefaults(this.slotTextareaDefaults, input);
140
+ if (fallback) {
141
+ const assigned = input !== fallback;
142
+ fallback.hidden = assigned;
143
+ fallback.setAttribute("aria-hidden", String(assigned));
144
+ }
145
+ if (input !== fallback) {
146
+ for (const name of ["autocomplete", "dirname", "form", "maxlength", "minlength", "cols", "wrap"])
147
+ if (!input.hasAttribute(name) && this.hasAttribute(name))
148
+ input.setAttribute(name, this.getAttribute(name));
149
+ if (!input.hasAttribute("aria-label") && !input.hasAttribute("aria-labelledby")) {
150
+ const label = this.getAttribute("label");
151
+ if (label)
152
+ input.setAttribute("aria-label", label);
153
+ }
154
+ if (this.hasAttribute("name"))
155
+ input.removeAttribute("name");
156
+ if (!this.dirty && (this.hasAttribute("value") || this.hasAttribute("default-value")))
157
+ input.value = this.currentValue;
158
+ }
159
+ const defaults = input === fallback ? undefined : this.slotTextareaDefaults;
160
+ input.disabled = this.disabled || this.formDisabled || Boolean(defaults?.disabled);
161
+ input.required = this.required || Boolean(defaults?.required);
162
+ input.readOnly = this.readOnly || Boolean(defaults?.readOnly);
163
+ syncHostAccessibleName(this, input);
164
+ this.syncForm(input);
165
+ }
166
+ handleTextareaInput = (event) => { const input = this.input(); if (!input)
167
+ return; this.currentValue = input.value; this.dirty = true; this.syncForm(input); this.dispatchEvent(new CustomEvent("spui-input", { bubbles: true, composed: true, detail: { value: input.value, sourceEvent: event } })); };
168
+ syncForm(input) {
169
+ if (!input)
170
+ return;
171
+ this.currentValue = input.value;
172
+ this.internals?.setFormValue(input.disabled || this.formDisabled ? null : input.value, input.value);
173
+ const validity = input.validity;
174
+ const flags = { badInput: validity.badInput, patternMismatch: validity.patternMismatch, rangeOverflow: validity.rangeOverflow, rangeUnderflow: validity.rangeUnderflow, stepMismatch: validity.stepMismatch, tooLong: validity.tooLong, tooShort: validity.tooShort, typeMismatch: validity.typeMismatch, valueMissing: validity.valueMissing, customError: false };
175
+ try {
176
+ this.internals?.setValidity(validity.valid ? {} : flags, input.validationMessage, input);
177
+ }
178
+ catch { /* Native validity remains available inside the shadow root. */ }
179
+ }
180
+ }
181
+ customElements.define("spui-textarea", SpuiTextArea);
182
+ }
@@ -0,0 +1,25 @@
1
+ export type ToastIntent = "info" | "success" | "warning" | "error";
2
+ export type ToastPoliteness = "polite" | "assertive";
3
+ export type ToastAppearance = "brand" | "inverted";
4
+ export type ToastContent = string | Node;
5
+ export type ToastOptions = {
6
+ id?: string;
7
+ /** Lower values are promoted first when a limited Toaster has room. Equal priorities keep dispatch order. */
8
+ priority?: number;
9
+ title?: ToastContent;
10
+ body?: ToastContent;
11
+ actionLabel?: string;
12
+ onAction?: (id: string) => void;
13
+ intent?: ToastIntent;
14
+ appearance?: ToastAppearance;
15
+ politeness?: ToastPoliteness;
16
+ /** Milliseconds before automatic dismissal. Use 0 or -1 to keep the toast until dismissed. */
17
+ timeout?: number;
18
+ dismissLabel?: string;
19
+ pauseOnHover?: boolean;
20
+ pauseOnWindowBlur?: boolean;
21
+ };
22
+ export declare function defineToastBody(): void;
23
+ export declare function defineToastFooter(): void;
24
+ export declare function defineToastTitle(): void;
25
+ export declare function defineToast(): void;
@@ -0,0 +1,337 @@
1
+ import { base } from "./shared.js";
2
+ function toastIntentIcon(document, intent) {
3
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
4
+ svg.setAttribute("viewBox", "0 0 20 20");
5
+ svg.setAttribute("aria-hidden", "true");
6
+ svg.setAttribute("focusable", "false");
7
+ const paths = {
8
+ info: "M10 1.5a8.5 8.5 0 1 0 0 17 8.5 8.5 0 0 0 0-17ZM9 8h2v6H9zm0-3h2v2H9z",
9
+ success: "M10 1.5a8.5 8.5 0 1 0 0 17 8.5 8.5 0 0 0 0-17Zm-1 12.2-3.2-3.2 1.4-1.4L9 10.9l4.8-4.8 1.4 1.4Z",
10
+ warning: "M10 1.5 19 18H1L10 1.5Zm-1 6v5h2v-5zm0 7v2h2v-2z",
11
+ error: "M10 1.5a8.5 8.5 0 1 0 0 17 8.5 8.5 0 0 0 0-17Zm3.5 11.6-1.4 1.4L10 11.4l-2.1 2.1-1.4-1.4L8.6 10 6.5 7.9l1.4-1.4L10 8.6l2.1-2.1 1.4 1.4L11.4 10z",
12
+ };
13
+ const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
14
+ path.setAttribute("d", paths[intent]);
15
+ svg.append(path);
16
+ return svg;
17
+ }
18
+ function defineToastSimplePart(tag, markup, css) {
19
+ if (customElements.get(tag))
20
+ return;
21
+ class SpuiToastPart extends HTMLElement {
22
+ connectedCallback() {
23
+ if (this.shadowRoot)
24
+ return;
25
+ const shadow = this.attachShadow({ mode: "open" });
26
+ const root = document.createElement("div");
27
+ root.part.add("root");
28
+ root.innerHTML = markup;
29
+ const style = document.createElement("style");
30
+ style.textContent = `${base}:host{display:block;min-width:0;color:inherit;font:inherit}${css}`;
31
+ shadow.append(style, root);
32
+ }
33
+ }
34
+ customElements.define(tag, SpuiToastPart);
35
+ }
36
+ export function defineToastBody() {
37
+ defineToastSimplePart("spui-toast-body", '<slot></slot><slot name="subtitle"></slot>', ":host{grid-column:2;line-height:1.45;overflow-wrap:anywhere}slot[name=subtitle]{display:block;margin-block-start:4px;color:var(--spui-ui-muted,var(--spui-semantic-body-text,#616161));font-size:.9em}");
38
+ }
39
+ export function defineToastFooter() {
40
+ defineToastSimplePart("spui-toast-footer", "<slot></slot>", ":host{grid-column:2;display:flex;align-items:center;flex-wrap:wrap;gap:var(--spui-control-gap,8px);padding-block-start:8px}");
41
+ }
42
+ export function defineToastTitle() {
43
+ if (customElements.get("spui-toast-title"))
44
+ return;
45
+ class SpuiToastTitle extends HTMLElement {
46
+ static observedAttributes = ["intent"];
47
+ mediaSlot;
48
+ fallback;
49
+ connectedCallback() {
50
+ if (!this.shadowRoot) {
51
+ const shadow = this.attachShadow({ mode: "open" });
52
+ const media = document.createElement("span");
53
+ media.className = "media";
54
+ media.part.add("media");
55
+ this.fallback = document.createElement("span");
56
+ this.fallback.className = "fallback";
57
+ this.fallback.setAttribute("aria-hidden", "true");
58
+ media.append(this.fallback);
59
+ this.mediaSlot = document.createElement("slot");
60
+ this.mediaSlot.name = "media";
61
+ this.mediaSlot.addEventListener("slotchange", () => this.syncMedia());
62
+ media.append(this.mediaSlot);
63
+ const text = document.createElement("span");
64
+ text.className = "text";
65
+ text.part.add("text");
66
+ text.append(document.createElement("slot"));
67
+ const action = document.createElement("slot");
68
+ action.name = "action";
69
+ action.part.add("action");
70
+ const style = document.createElement("style");
71
+ style.textContent = `${base}:host{display:grid;grid-template-columns:auto minmax(0,1fr) auto;align-items:center;gap:8px;min-width:0;color:inherit;font:inherit;font-weight:var(--fontWeightSemibold,600)}.media{display:grid;place-items:center;inline-size:20px;block-size:20px;color:var(--spui-toast-intent-color,var(--spui-ui-accent,#0078d4))}.media:empty{display:none}.media svg{inline-size:20px;block-size:20px;fill:currentColor}.fallback[hidden]{display:none}.text{min-width:0;overflow-wrap:anywhere}slot[name=action]{display:block;margin-inline-start:auto}`;
72
+ shadow.append(style, media, text, action);
73
+ }
74
+ this.syncMedia();
75
+ }
76
+ attributeChangedCallback() { if (this.isConnected)
77
+ this.syncMedia(); }
78
+ refresh() { this.syncMedia(); }
79
+ syncMedia() {
80
+ if (!this.fallback || !this.mediaSlot)
81
+ return;
82
+ const assigned = this.mediaSlot.assignedNodes({ flatten: true }).length > 0;
83
+ this.fallback.replaceChildren();
84
+ const intent = (this.getAttribute("intent") ?? this.closest("spui-toast")?.getAttribute("intent") ?? "info");
85
+ if (!assigned)
86
+ this.fallback.append(toastIntentIcon(this.ownerDocument, ["info", "success", "warning", "error"].includes(intent) ? intent : "info"));
87
+ this.fallback.hidden = assigned;
88
+ }
89
+ }
90
+ customElements.define("spui-toast-title", SpuiToastTitle);
91
+ }
92
+ let generatedId = 0;
93
+ const makeId = () => globalThis.crypto?.randomUUID?.() ?? `spui-toast-${Date.now()}-${++generatedId}`;
94
+ export function defineToast() {
95
+ if (customElements.get("spui-toast"))
96
+ return;
97
+ defineToastTitle();
98
+ defineToastBody();
99
+ defineToastFooter();
100
+ class SpuiToast extends HTMLElement {
101
+ static observedAttributes = ["toast-id", "intent", "appearance", "politeness", "timeout", "dismiss-label", "priority"];
102
+ timer;
103
+ startedAt = 0;
104
+ remaining = 3000;
105
+ pauseReasons = new Set();
106
+ announcementScheduled = false;
107
+ closeButton;
108
+ titleSlot;
109
+ contentObserver;
110
+ onWindowBlur = () => { if (this.hasAttribute("pause-on-window-blur") && this.getAttribute("pause-on-window-blur") !== "false")
111
+ this.pauseFor("window"); };
112
+ onWindowFocus = () => this.resumeFor("window");
113
+ onVisibility = () => {
114
+ if (document.hidden && this.hasAttribute("pause-on-window-blur") && this.getAttribute("pause-on-window-blur") !== "false")
115
+ this.pauseFor("visibility");
116
+ else
117
+ this.resumeFor("visibility");
118
+ };
119
+ connectedCallback() {
120
+ if (!this.shadowRoot)
121
+ this.render();
122
+ const toaster = this.parentElement;
123
+ if (toaster?.localName === "spui-toaster")
124
+ toaster.registerToast?.(this);
125
+ this.syncSemantics();
126
+ this.resetTimer();
127
+ this.ownerDocument.defaultView?.addEventListener("blur", this.onWindowBlur);
128
+ this.ownerDocument.defaultView?.addEventListener("focus", this.onWindowFocus);
129
+ this.ownerDocument.addEventListener("visibilitychange", this.onVisibility);
130
+ this.addEventListener("mouseenter", this.onMouseEnter);
131
+ this.addEventListener("mouseleave", this.onMouseLeave);
132
+ this.addEventListener("focusin", this.onFocusIn);
133
+ this.addEventListener("focusout", this.onFocusOut);
134
+ this.addEventListener("slotchange", this.onSlotChange, true);
135
+ this.syncTitle();
136
+ this.scheduleAnnouncement();
137
+ const Observer = this.ownerDocument.defaultView?.MutationObserver;
138
+ if (Observer) {
139
+ this.contentObserver = new Observer(this.scheduleAnnouncement);
140
+ this.contentObserver.observe(this, { childList: true, characterData: true, subtree: true });
141
+ }
142
+ }
143
+ disconnectedCallback() {
144
+ this.clearTimer();
145
+ this.pauseReasons.clear();
146
+ this.contentObserver?.disconnect();
147
+ this.contentObserver = undefined;
148
+ this.ownerDocument.defaultView?.removeEventListener("blur", this.onWindowBlur);
149
+ this.ownerDocument.defaultView?.removeEventListener("focus", this.onWindowFocus);
150
+ this.ownerDocument.removeEventListener("visibilitychange", this.onVisibility);
151
+ this.removeEventListener("mouseenter", this.onMouseEnter);
152
+ this.removeEventListener("mouseleave", this.onMouseLeave);
153
+ this.removeEventListener("focusin", this.onFocusIn);
154
+ this.removeEventListener("focusout", this.onFocusOut);
155
+ this.removeEventListener("slotchange", this.onSlotChange, true);
156
+ }
157
+ attributeChangedCallback(name) {
158
+ if (!this.isConnected)
159
+ return;
160
+ if (name === "timeout")
161
+ this.resetTimer();
162
+ if (name === "priority") {
163
+ const toaster = this.parentElement;
164
+ if (toaster?.localName === "spui-toaster")
165
+ toaster.refreshQueue?.();
166
+ }
167
+ if (name === "intent" || name === "politeness" || name === "dismiss-label")
168
+ this.syncSemantics();
169
+ if (name === "intent" || name === "politeness")
170
+ this.scheduleAnnouncement();
171
+ }
172
+ get toastId() { return this.getAttribute("toast-id") ?? ""; }
173
+ configure(options) {
174
+ const id = options.id || this.toastId || makeId();
175
+ this.hidden = false;
176
+ this.setAttribute("toast-id", id);
177
+ if (Number.isFinite(options.priority))
178
+ this.setAttribute("priority", String(options.priority));
179
+ else
180
+ this.removeAttribute("priority");
181
+ if (options.intent)
182
+ this.setAttribute("intent", options.intent);
183
+ else
184
+ this.removeAttribute("intent");
185
+ if (options.appearance)
186
+ this.setAttribute("appearance", options.appearance);
187
+ else
188
+ this.removeAttribute("appearance");
189
+ if (options.politeness)
190
+ this.setAttribute("politeness", options.politeness);
191
+ else
192
+ this.removeAttribute("politeness");
193
+ this.setAttribute("timeout", String(options.timeout ?? 3000));
194
+ this.setAttribute("dismiss-label", options.dismissLabel ?? "Dismiss");
195
+ this.setAttribute("pause-on-hover", options.pauseOnHover ? "" : "false");
196
+ this.setAttribute("pause-on-window-blur", options.pauseOnWindowBlur ? "" : "false");
197
+ this.setPart("title", options.title);
198
+ this.setPart("body", options.body);
199
+ this.querySelector("[data-spui-toast-generated-action]")?.remove();
200
+ if (options.actionLabel) {
201
+ const button = this.ownerDocument.createElement("button");
202
+ button.slot = "action";
203
+ button.type = "button";
204
+ button.dataset.spuiToastGeneratedAction = "";
205
+ button.textContent = options.actionLabel;
206
+ button.addEventListener("click", () => { options.onAction?.(id); this.dismiss("action"); });
207
+ this.append(button);
208
+ }
209
+ this.syncSemantics();
210
+ this.resetTimer();
211
+ this.syncTitle();
212
+ this.scheduleAnnouncement();
213
+ return id;
214
+ }
215
+ dismiss(reason = "dismiss") {
216
+ if (this.hidden)
217
+ return;
218
+ this.clearTimer();
219
+ const active = this.ownerDocument.activeElement;
220
+ const hadFocus = active === this || this.contains(active) || Boolean(this.shadowRoot?.activeElement);
221
+ this.hidden = true;
222
+ this.dispatchEvent(new CustomEvent("spui-dismiss", { detail: { id: this.toastId, reason, hadFocus }, bubbles: true, composed: true }));
223
+ }
224
+ pause(reason = "imperative") { this.pauseFor(reason); }
225
+ play(reason = "imperative") { this.resumeFor(reason); if (reason === "queue")
226
+ this.scheduleAnnouncement(); }
227
+ setPart(slot, value) {
228
+ this.querySelector(`[data-spui-toast-part="${slot}"]`)?.remove();
229
+ if (value === undefined)
230
+ return;
231
+ const wrapper = this.ownerDocument.createElement(slot === "body" ? "spui-toast-body" : "spui-toast-title");
232
+ wrapper.dataset.spuiToastPart = slot;
233
+ if (slot === "title")
234
+ wrapper.slot = "title";
235
+ if (typeof value === "string")
236
+ wrapper.textContent = value;
237
+ else
238
+ wrapper.append(value);
239
+ this.append(wrapper);
240
+ }
241
+ onMouseEnter = () => { if (this.hasAttribute("pause-on-hover") && this.getAttribute("pause-on-hover") !== "false")
242
+ this.pauseFor("hover"); };
243
+ onMouseLeave = () => this.resumeFor("hover");
244
+ onFocusIn = () => this.pauseFor("focus");
245
+ onFocusOut = (event) => { if (!this.contains(event.relatedTarget))
246
+ this.resumeFor("focus"); };
247
+ onSlotChange = () => { this.syncTitle(); this.scheduleAnnouncement(); };
248
+ syncTitle() {
249
+ if (!this.titleSlot)
250
+ return;
251
+ const hasTitle = this.titleSlot.assignedNodes({ flatten: true }).some(node => node.nodeType === Node.ELEMENT_NODE || Boolean(node.textContent?.trim()));
252
+ this.toggleAttribute("data-has-title", hasTitle);
253
+ }
254
+ syncSemantics() {
255
+ const intent = this.getAttribute("intent") ?? "info";
256
+ const politeness = this.getAttribute("politeness") ?? (intent === "info" ? "polite" : "assertive");
257
+ this.setAttribute("role", "group");
258
+ this.removeAttribute("aria-live");
259
+ this.removeAttribute("aria-atomic");
260
+ if (this.closeButton)
261
+ this.closeButton.setAttribute("aria-label", this.getAttribute("dismiss-label") ?? "Dismiss");
262
+ for (const title of this.querySelectorAll("spui-toast-title"))
263
+ title.refresh?.();
264
+ }
265
+ scheduleAnnouncement() {
266
+ if (this.announcementScheduled)
267
+ return;
268
+ this.announcementScheduled = true;
269
+ queueMicrotask(() => {
270
+ this.announcementScheduled = false;
271
+ if (!this.isConnected || this.hidden)
272
+ return;
273
+ const message = this.textContent?.replace(/\s+/g, " ").trim();
274
+ if (!message)
275
+ return;
276
+ const intent = this.getAttribute("intent") ?? "info";
277
+ const requested = this.getAttribute("politeness");
278
+ const politeness = requested === "polite" || requested === "assertive" ? requested : intent === "info" ? "polite" : "assertive";
279
+ this.dispatchEvent(new CustomEvent("spui-toast-announce", { detail: { id: this.toastId, message, politeness }, bubbles: true, composed: true }));
280
+ });
281
+ }
282
+ clearTimer() { if (this.timer !== undefined)
283
+ this.ownerDocument.defaultView?.clearTimeout(this.timer); this.timer = undefined; }
284
+ resetTimer() {
285
+ this.clearTimer();
286
+ const timeout = Number(this.getAttribute("timeout") ?? 3000);
287
+ this.remaining = timeout === 0 || timeout === -1 ? 0 : Number.isFinite(timeout) && timeout > 0 ? timeout : 3000;
288
+ this.startTimer();
289
+ }
290
+ pauseFor(reason) {
291
+ this.pauseReasons.add(reason);
292
+ if (this.timer === undefined)
293
+ return;
294
+ this.remaining = Math.max(0, this.remaining - (performance.now() - this.startedAt));
295
+ this.clearTimer();
296
+ }
297
+ resumeFor(reason) {
298
+ this.pauseReasons.delete(reason);
299
+ this.startTimer();
300
+ }
301
+ startTimer() {
302
+ if (!this.isConnected || this.hidden || this.timer !== undefined || this.remaining === 0 || this.pauseReasons.size)
303
+ return;
304
+ this.startedAt = performance.now();
305
+ this.timer = this.ownerDocument.defaultView.setTimeout(() => this.dismiss("timeout"), this.remaining);
306
+ }
307
+ render() {
308
+ const shadow = this.attachShadow({ mode: "open" });
309
+ const header = this.ownerDocument.createElement("div");
310
+ header.className = "header";
311
+ this.titleSlot = this.ownerDocument.createElement("slot");
312
+ this.titleSlot.name = "title";
313
+ this.titleSlot.part.add("title");
314
+ header.append(this.titleSlot);
315
+ this.closeButton = this.ownerDocument.createElement("button");
316
+ this.closeButton.type = "button";
317
+ this.closeButton.className = "close";
318
+ this.closeButton.setAttribute("aria-label", this.getAttribute("dismiss-label") ?? "Dismiss");
319
+ this.closeButton.part.add("dismiss");
320
+ const mark = this.ownerDocument.createElement("span");
321
+ mark.className = "close-mark";
322
+ mark.setAttribute("aria-hidden", "true");
323
+ this.closeButton.append(mark);
324
+ this.closeButton.addEventListener("click", () => this.dismiss("close"));
325
+ header.append(this.closeButton);
326
+ const body = this.ownerDocument.createElement("slot");
327
+ body.part.add("body");
328
+ const action = this.ownerDocument.createElement("slot");
329
+ action.name = "action";
330
+ action.part.add("action");
331
+ const style = this.ownerDocument.createElement("style");
332
+ style.textContent = `${base}:host{display:block;box-sizing:border-box;inline-size:min(100%,420px);padding:12px 14px;border:1px solid var(--spui-ui-stroke,var(--spui-semantic-body-divider,#d1d1d1));border-radius:var(--spui-toast-radius,var(--spui-control-radius,8px));background:var(--spui-toast-background,var(--spui-ui-surface,#fff));color:var(--spui-ui-text,var(--spui-semantic-body-text,#242424));box-shadow:var(--spui-toast-shadow,0 8px 24px #0002);font:inherit;line-height:1.45;pointer-events:auto;animation:spui-toast-enter 180ms ease-out both}:host([hidden]){display:none}:host([intent=success]){--spui-toast-background:var(--spui-ui-success-surface,var(--spui-semantic-success-background,#dff6dd))}:host([intent=warning]){--spui-toast-background:var(--spui-ui-warning-surface,var(--spui-semantic-warning-background,#fff4ce))}:host([intent=error]){--spui-toast-background:var(--spui-ui-danger-surface,var(--spui-semantic-error-background,#fde7e9))}:host([appearance=brand]){background:var(--spui-toast-brand-background,var(--spui-ui-accent,#78716c));color:var(--spui-toast-brand-color,var(--spui-ui-accent-text,#fff))}:host([appearance=inverted]){background:var(--spui-toast-inverted-background,color-mix(in srgb,var(--spui-ui-text,#242424) 92%,var(--spui-ui-canvas,#f5f5f5) 8%));color:var(--spui-toast-inverted-color,color-mix(in srgb,var(--spui-ui-surface,#fff) 92%,var(--spui-ui-text,#242424) 8%))}.header{display:flex;align-items:start;justify-content:space-between;gap:8px;font-weight:var(--fontWeightSemibold,600)}slot[name=title]{display:none}:host([data-has-title]) slot[name=title]{display:block}slot:not([name]){display:block;margin-block-start:4px}slot[name=action]{display:block;margin-block-start:10px}::slotted(button[slot=action]){font:inherit}button.close{flex:none;display:grid;place-items:center;inline-size:32px;block-size:32px;margin-block-start:-6px;margin-inline-end:-8px;margin-block-end:-4px;margin-inline-start:0;padding:0;border:0;border-radius:var(--spui-control-radius,6px);background:transparent;color:inherit;cursor:pointer}button.close:hover{background:var(--spui-ui-surface-hover,#0001)}button.close:focus-visible,::slotted(button:focus-visible){outline:2px solid var(--spui-focus-ring,#005a9e);outline-offset:2px}.close-mark{position:relative;inline-size:14px;block-size:14px}.close-mark:before,.close-mark:after{content:"";position:absolute;inset-block-start:6px;inset-inline-start:0;inline-size:14px;block-size:2px;border-radius:2px;background:currentColor;transform:rotate(45deg)}.close-mark:after{transform:rotate(-45deg)}@keyframes spui-toast-enter{from{opacity:0;transform:translateY(5px)}to{opacity:1;transform:translateY(0)}}@media(prefers-reduced-motion:reduce){:host{animation:none}}@media(forced-colors:active){:host{border-color:CanvasText;color:CanvasText;background:Canvas;box-shadow:none}.close-mark:before,.close-mark:after{background:ButtonText}}`;
333
+ shadow.replaceChildren(style, header, body, action);
334
+ }
335
+ }
336
+ customElements.define("spui-toast", SpuiToast);
337
+ }
@@ -0,0 +1,6 @@
1
+ export type ToastPosition = "top-start" | "top-end" | "bottom-start" | "bottom-end";
2
+ export type ToastAnnounceOptions = {
3
+ politeness: "polite" | "assertive";
4
+ };
5
+ export type ToastAnnounce = (message: string, options: ToastAnnounceOptions) => void;
6
+ export declare function defineToaster(): void;