@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,1112 @@
1
+ import { base } from "./shared.js";
2
+ import { buttonControlCss } from "./button.js";
3
+ let carouselId = 0;
4
+ export function defineCarouselCard() {
5
+ if (customElements.get("spui-carousel-card"))
6
+ return;
7
+ class SpuiCarouselCard extends HTMLElement {
8
+ static observedAttributes = ["auto-size"];
9
+ get autoSize() { return this.hasAttribute("auto-size"); }
10
+ set autoSize(value) { this.toggleAttribute("auto-size", Boolean(value)); }
11
+ connectedCallback() {
12
+ if (!this.shadowRoot) {
13
+ const root = this.attachShadow({ mode: "open" });
14
+ const style = document.createElement("style");
15
+ style.textContent = `${base}:host{box-sizing:border-box;display:block;flex:0 0 var(--spui-carousel-slide-width,var(--spui-carousel-slide-size,100%));min-width:0;scroll-snap-align:var(--spui-carousel-align,start);scroll-snap-stop:always}:host([auto-size]){flex-basis:auto}:host([data-spui-carousel-space-start]){margin-inline-start:var(--spui-carousel-auto-space-start,0px)}:host([data-spui-carousel-space-end]){margin-inline-end:var(--spui-carousel-auto-space-end,0px)}:host([data-spui-carousel-motion]){grid-area:1/1;width:100%;flex:none;opacity:0;visibility:hidden;pointer-events:none;transition:opacity var(--spui-carousel-motion-duration,240ms) var(--spui-motion-easing-standard,ease),visibility 0s linear var(--spui-carousel-motion-duration,240ms)}:host([data-spui-carousel-motion][data-spui-carousel-active]){opacity:1;visibility:visible;pointer-events:auto;transition-delay:0s}@media(prefers-reduced-motion:reduce){:host([data-spui-carousel-motion]){transition:none}}slot{display:block}`;
16
+ root.append(style, document.createElement("slot"));
17
+ }
18
+ this.setAttribute("role", this.getAttribute("role") || "group");
19
+ this.setAttribute("aria-roledescription", this.getAttribute("aria-roledescription") || "slide");
20
+ }
21
+ }
22
+ customElements.define("spui-carousel-card", SpuiCarouselCard);
23
+ }
24
+ export function defineCarouselNav() {
25
+ if (customElements.get("spui-carousel-nav"))
26
+ return;
27
+ class SpuiCarouselNav extends HTMLElement {
28
+ connectedCallback() {
29
+ if (this.shadowRoot)
30
+ return;
31
+ const root = this.attachShadow({ mode: "open" });
32
+ const style = document.createElement("style");
33
+ style.textContent = `${base}:host{display:flex;align-items:center;justify-content:center;gap:4px;min-width:0}:host([appearance=brand]){--spui-carousel-indicator:var(--spui-ui-accent,#0078d4)}slot{display:contents}`;
34
+ const group = document.createElement("div");
35
+ group.setAttribute("role", "group");
36
+ group.setAttribute("aria-label", this.getAttribute("aria-label") || "Slide navigation");
37
+ const slot = document.createElement("slot");
38
+ group.append(slot);
39
+ root.append(style, group);
40
+ }
41
+ }
42
+ customElements.define("spui-carousel-nav", SpuiCarouselNav);
43
+ }
44
+ function defineCarouselNavControl(tag, image) {
45
+ if (customElements.get(tag))
46
+ return;
47
+ class SpuiCarouselNavControl extends HTMLElement {
48
+ static observedAttributes = ["index", "selected", "disabled", "label", "src", "alt"];
49
+ button;
50
+ picture;
51
+ get index() { const value = Number(this.getAttribute("index")); return Number.isInteger(value) && value >= 0 ? value : 0; }
52
+ set index(value) { this.setAttribute("index", String(Math.max(0, Math.floor(value)))); }
53
+ get selected() { return this.hasAttribute("selected"); }
54
+ set selected(value) { this.toggleAttribute("selected", Boolean(value)); }
55
+ get disabled() { return this.hasAttribute("disabled"); }
56
+ set disabled(value) { this.toggleAttribute("disabled", Boolean(value)); }
57
+ connectedCallback() {
58
+ if (!this.shadowRoot) {
59
+ const root = this.attachShadow({ mode: "open" });
60
+ const style = document.createElement("style");
61
+ style.textContent = `${base}:host{display:inline-flex;min-width:0}button{box-sizing:border-box;display:grid;place-items:center;min-width:44px;min-height:44px;padding:4px;border:1px solid var(--spui-ui-stroke,#d1d1d1);border-radius:var(--spui-control-radius,6px);background:var(--spui-ui-surface,#fff);color:var(--spui-ui-text,inherit);font:inherit;cursor:pointer}button[aria-current=step]{border-color:var(--spui-ui-accent,#0078d4);box-shadow:inset 0 0 0 1px var(--spui-ui-accent,#0078d4)}button:focus-visible{outline:2px solid var(--spui-focus-ring,var(--spui-ui-accent,#0078d4));outline-offset:2px}img{display:block;width:56px;height:40px;object-fit:cover;border-radius:calc(var(--spui-control-radius,6px) - 2px)}slot{display:contents}@media(forced-colors:active){button{border-color:ButtonText}button[aria-current=step]{outline:2px solid Highlight}}`;
62
+ this.button = document.createElement("button");
63
+ this.button.type = "button";
64
+ if (image) {
65
+ this.picture = document.createElement("img");
66
+ const imageSlot = document.createElement("slot");
67
+ imageSlot.name = "image";
68
+ imageSlot.append(this.picture);
69
+ this.button.append(imageSlot);
70
+ }
71
+ const content = document.createElement("slot");
72
+ this.button.append(content);
73
+ this.button.addEventListener("click", event => this.dispatchEvent(new CustomEvent("spui-carousel-nav-select", { bubbles: true, composed: true, detail: { index: this.index, event } })));
74
+ this.button.addEventListener("keydown", event => {
75
+ const nav = this.closest("spui-carousel-nav");
76
+ if (!nav)
77
+ return;
78
+ const buttons = [...nav.querySelectorAll("spui-carousel-nav-button,spui-carousel-nav-image-button")];
79
+ const current = buttons.indexOf(this);
80
+ const rtl = getComputedStyle(nav).direction === "rtl";
81
+ const next = event.key === "Home" ? 0 : event.key === "End" ? buttons.length - 1 : event.key === (rtl ? "ArrowLeft" : "ArrowRight") ? Math.min(buttons.length - 1, current + 1) : event.key === (rtl ? "ArrowRight" : "ArrowLeft") ? Math.max(0, current - 1) : -1;
82
+ if (next < 0)
83
+ return;
84
+ event.preventDefault();
85
+ buttons[next].shadowRoot?.querySelector("button")?.focus();
86
+ });
87
+ root.append(style, this.button);
88
+ }
89
+ this.sync();
90
+ }
91
+ attributeChangedCallback() { this.sync(); }
92
+ sync() {
93
+ if (!this.button)
94
+ return;
95
+ this.button.disabled = this.hasAttribute("disabled");
96
+ if (this.selected)
97
+ this.button.setAttribute("aria-current", "step");
98
+ else
99
+ this.button.removeAttribute("aria-current");
100
+ this.button.tabIndex = this.selected ? 0 : -1;
101
+ const label = this.getAttribute("label") || this.getAttribute("alt") || this.textContent?.trim() || `Slide ${this.index + 1}`;
102
+ this.button.setAttribute("aria-label", label);
103
+ if (this.picture) {
104
+ this.picture.src = this.getAttribute("src") || "";
105
+ this.picture.alt = this.getAttribute("alt") || "";
106
+ }
107
+ }
108
+ }
109
+ customElements.define(tag, SpuiCarouselNavControl);
110
+ }
111
+ export function defineCarouselNavButton() { defineCarouselNavControl("spui-carousel-nav-button", false); }
112
+ export function defineCarouselNavImageButton() { defineCarouselNavControl("spui-carousel-nav-image-button", true); }
113
+ export function defineCarouselButton() {
114
+ if (customElements.get("spui-carousel-button"))
115
+ return;
116
+ class SpuiCarouselButton extends HTMLElement {
117
+ static observedAttributes = ["nav-type", "disabled", "label"];
118
+ button;
119
+ get navType() { return this.getAttribute("nav-type") === "prev" ? "prev" : "next"; }
120
+ set navType(value) { this.setAttribute("nav-type", value); }
121
+ get disabled() { return this.hasAttribute("disabled"); }
122
+ set disabled(value) { this.toggleAttribute("disabled", Boolean(value)); }
123
+ connectedCallback() {
124
+ if (!this.shadowRoot) {
125
+ const root = this.attachShadow({ mode: "open" });
126
+ const style = document.createElement("style");
127
+ style.textContent = `${base}:host{display:inline-flex}button{box-sizing:border-box;min-width:44px;min-height:44px;padding:6px 10px;border:1px solid var(--spui-ui-stroke,#d1d1d1);border-radius:var(--spui-control-radius,6px);background:var(--spui-ui-surface,#fff);color:var(--spui-ui-text,inherit);font:inherit;cursor:pointer}button:focus-visible{outline:2px solid var(--spui-focus-ring,var(--spui-ui-accent,#0078d4));outline-offset:2px}button:disabled{color:var(--spui-ui-muted,#777);opacity:.55;cursor:not-allowed}@media(forced-colors:active){button{border-color:ButtonText}button:focus-visible{outline-color:Highlight}}`;
128
+ this.button = document.createElement("button");
129
+ this.button.type = "button";
130
+ const icon = document.createElement("slot");
131
+ icon.name = "icon";
132
+ this.button.append(icon, document.createElement("slot"));
133
+ this.button.addEventListener("click", event => this.dispatchEvent(new CustomEvent("spui-carousel-button-activate", { bubbles: true, composed: true, detail: { navType: this.navType, event } })));
134
+ root.append(style, this.button);
135
+ }
136
+ this.sync();
137
+ }
138
+ attributeChangedCallback() { this.sync(); }
139
+ sync() { if (!this.button)
140
+ return; this.button.disabled = this.disabled; this.button.setAttribute("aria-label", this.getAttribute("label") || this.textContent?.trim() || (this.navType === "prev" ? "Previous slide" : "Next slide")); }
141
+ }
142
+ customElements.define("spui-carousel-button", SpuiCarouselButton);
143
+ }
144
+ export function defineCarouselAutoplayButton() {
145
+ if (customElements.get("spui-carousel-autoplay-button"))
146
+ return;
147
+ class SpuiCarouselAutoplayButton extends HTMLElement {
148
+ static observedAttributes = ["selected", "checked", "default-checked", "controlled", "is-accessible", "appearance", "size", "shape", "disabled", "disabled-focusable", "icon-position", "play-label", "pause-label", "aria-label", "aria-describedby", "title", "tabindex"];
149
+ button;
150
+ get selected() { return this.hasAttribute("selected"); }
151
+ set selected(value) { this.toggleAttribute("selected", Boolean(value)); }
152
+ connectedCallback() {
153
+ if (!this.shadowRoot) {
154
+ const root = this.attachShadow({ mode: "open" });
155
+ const style = document.createElement("style");
156
+ style.textContent = `${buttonControlCss}:host{display:inline-block;vertical-align:middle}:host([selected]) button{border-color:var(--spui-toggle-pressed-border,var(--spui-ui-accent,var(--colorBrandBackground,#0078d4)));background:var(--spui-toggle-pressed-background,color-mix(in srgb,var(--spui-ui-accent,var(--colorBrandBackground,#0078d4)) 12%,var(--spui-ui-surface,var(--colorNeutralBackground1,#fff))))}:host([selected]) button:hover:not(:disabled){background:var(--spui-toggle-pressed-hover,color-mix(in srgb,var(--spui-ui-accent,var(--colorBrandBackground,#0078d4)) 18%,var(--spui-ui-surface,var(--colorNeutralBackground1,#fff))))}:host([is-accessible][selected]) button{border-color:var(--spui-ui-accent,var(--colorBrandBackground,#0078d4));background:var(--spui-ui-accent,var(--colorBrandBackground,#0078d4));color:var(--spui-ui-accent-text,#fff)}:host([is-accessible][selected]) button:hover:not(:disabled){background:var(--spui-ui-accent-hover,var(--colorBrandBackgroundHover,#106ebe))}:host([disabled-focusable]) button:disabled{opacity:1}@media(forced-colors:active){:host([selected]) button{outline:1px solid Highlight}}`;
157
+ this.button = document.createElement("button");
158
+ this.button.type = "button";
159
+ this.button.part.add("control");
160
+ const iconWrap = document.createElement("span");
161
+ iconWrap.className = "icon";
162
+ iconWrap.hidden = true;
163
+ iconWrap.setAttribute("aria-hidden", "true");
164
+ const icon = document.createElement("slot");
165
+ icon.name = "icon";
166
+ icon.addEventListener("slotchange", () => { iconWrap.hidden = !icon.assignedNodes({ flatten: true }).length; });
167
+ iconWrap.append(icon);
168
+ const content = document.createElement("span");
169
+ content.className = "content";
170
+ const label = document.createElement("span");
171
+ label.part.add("label");
172
+ content.append(label);
173
+ this.button.append(iconWrap, content);
174
+ this.button.addEventListener("click", event => {
175
+ if (this.hasAttribute("disabled")) {
176
+ event.preventDefault();
177
+ event.stopImmediatePropagation();
178
+ return;
179
+ }
180
+ const checked = !this.selected;
181
+ if (!this.hasAttribute("controlled"))
182
+ this.selected = checked;
183
+ this.dispatchEvent(new CustomEvent("spui-carousel-autoplay-toggle", { bubbles: true, composed: true, detail: { checked, event, type: "click" } }));
184
+ });
185
+ root.append(style, this.button);
186
+ }
187
+ if (!this.selected && !this.hasAttribute("controlled"))
188
+ this.selected = this.hasAttribute("default-checked") || this.hasAttribute("checked");
189
+ this.sync();
190
+ }
191
+ attributeChangedCallback(name, _oldValue, value) {
192
+ if (name === "checked" && this.isConnected && !this.hasAttribute("controlled"))
193
+ this.selected = value !== null;
194
+ if (this.isConnected)
195
+ this.sync();
196
+ }
197
+ sync() {
198
+ if (!this.button)
199
+ return;
200
+ const label = this.button.querySelector("[part=label]");
201
+ if (!label)
202
+ return;
203
+ const appearance = this.getAttribute("appearance") ?? "secondary";
204
+ this.button.className = appearance === "primary" ? "primary" : appearance === "outline" || appearance === "subtle" || appearance === "transparent" ? appearance : "secondary";
205
+ label.textContent = this.selected ? (this.getAttribute("pause-label") || "Pause") : (this.getAttribute("play-label") || "Play");
206
+ this.button.disabled = this.hasAttribute("disabled") && !this.hasAttribute("disabled-focusable");
207
+ if (this.hasAttribute("disabled-focusable") && this.hasAttribute("disabled"))
208
+ this.button.setAttribute("aria-disabled", "true");
209
+ else
210
+ this.button.removeAttribute("aria-disabled");
211
+ for (const name of ["aria-label", "aria-describedby", "title"]) {
212
+ const value = this.getAttribute(name);
213
+ if (name === "aria-label" && value === null)
214
+ this.button.setAttribute(name, label.textContent);
215
+ else if (value === null)
216
+ this.button.removeAttribute(name);
217
+ else
218
+ this.button.setAttribute(name, value);
219
+ }
220
+ if (this.hasAttribute("tabindex"))
221
+ this.button.setAttribute("tabindex", this.getAttribute("tabindex"));
222
+ else if (!this.hasAttribute("disabled-focusable"))
223
+ this.button.removeAttribute("tabindex");
224
+ this.button.setAttribute("aria-pressed", String(this.selected));
225
+ }
226
+ }
227
+ customElements.define("spui-carousel-autoplay-button", SpuiCarouselAutoplayButton);
228
+ }
229
+ export function defineCarouselNavContainer() {
230
+ if (customElements.get("spui-carousel-nav-container"))
231
+ return;
232
+ class SpuiCarouselNavContainer extends HTMLElement {
233
+ static observedAttributes = ["layout"];
234
+ connectedCallback() {
235
+ if (this.shadowRoot) {
236
+ this.sync();
237
+ return;
238
+ }
239
+ const root = this.attachShadow({ mode: "open" });
240
+ const style = document.createElement("style");
241
+ style.textContent = `${base}:host{display:block;min-width:0}.container{display:flex;align-items:center;justify-content:center;gap:var(--spui-carousel-gap,8px);min-width:0}.container[data-layout=inline-wide]{justify-content:space-between}.container[data-layout=overlay],.container[data-layout=overlay-wide]{padding:4px;border-radius:var(--spui-control-radius,8px);background:var(--spui-carousel-control-background,var(--spui-ui-surface,#fff));box-shadow:var(--spui-card-shadow,0 2px 8px #0002)}.container[data-layout=overlay-wide]{justify-content:space-between}.container[data-layout=overlay-expanded]{justify-content:space-between;background:transparent}.middle{display:flex;align-items:center;justify-content:center;gap:var(--spui-carousel-gap,8px);min-width:0}slot[name=nav]{display:contents}`;
242
+ const container = document.createElement("div");
243
+ container.className = "container";
244
+ container.setAttribute("role", "group");
245
+ const previous = document.createElement("slot");
246
+ previous.name = "previous";
247
+ const middle = document.createElement("div");
248
+ middle.className = "middle";
249
+ const navigation = document.createElement("slot");
250
+ navigation.name = "navigation";
251
+ const autoplay = document.createElement("slot");
252
+ autoplay.name = "autoplay";
253
+ middle.append(navigation, autoplay);
254
+ const next = document.createElement("slot");
255
+ next.name = "next";
256
+ const rest = document.createElement("slot");
257
+ container.append(previous, middle, next, rest);
258
+ root.append(style, container);
259
+ this.sync();
260
+ }
261
+ attributeChangedCallback() { this.sync(); }
262
+ sync() { const container = this.shadowRoot?.querySelector(".container"); if (container)
263
+ container.dataset.layout = this.getAttribute("layout") || "inline"; }
264
+ }
265
+ customElements.define("spui-carousel-nav-container", SpuiCarouselNavContainer);
266
+ }
267
+ export function defineCarouselSlider() {
268
+ if (customElements.get("spui-carousel-slider"))
269
+ return;
270
+ class SpuiCarouselSlider extends HTMLElement {
271
+ static observedAttributes = ["card-focus"];
272
+ get cardFocus() { return this.hasAttribute("card-focus"); }
273
+ set cardFocus(value) { this.toggleAttribute("card-focus", Boolean(value)); }
274
+ focusedIndex = 0;
275
+ observer;
276
+ originalTabIndex = new WeakMap();
277
+ keydown = (event) => {
278
+ if (!this.cardFocus || !(event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "Home" || event.key === "End"))
279
+ return;
280
+ const cards = [...this.querySelectorAll("spui-carousel-card")];
281
+ if (!cards.length)
282
+ return;
283
+ const currentCard = event.target.closest("spui-carousel-card");
284
+ const current = cards.indexOf(currentCard);
285
+ const rtl = getComputedStyle(this).direction === "rtl";
286
+ if (event.key === "Home")
287
+ this.focusedIndex = 0;
288
+ else if (event.key === "End")
289
+ this.focusedIndex = cards.length - 1;
290
+ else
291
+ this.focusedIndex = (Math.max(0, current >= 0 ? current : this.focusedIndex) + (event.key === "ArrowRight" ? (rtl ? -1 : 1) : (rtl ? 1 : -1)) + cards.length) % cards.length;
292
+ event.preventDefault();
293
+ event.stopPropagation();
294
+ this.dispatchEvent(new CustomEvent("spui-carousel-card-focus", { bubbles: true, composed: true, detail: { index: this.focusedIndex, event } }));
295
+ cards.forEach((card, index) => { if (this.cardFocus)
296
+ card.tabIndex = index === this.focusedIndex ? 0 : -1; });
297
+ cards[this.focusedIndex].focus();
298
+ };
299
+ connectedCallback() {
300
+ if (!this.shadowRoot) {
301
+ const root = this.attachShadow({ mode: "open" });
302
+ const style = document.createElement("style");
303
+ style.textContent = `${base}:host{box-sizing:border-box;display:flex;flex:0 0 100%;gap:var(--spui-carousel-gap,0px);min-width:0;width:100%;overflow-anchor:none}:host([data-spui-carousel-motion]){display:contents}slot{display:contents}`;
304
+ root.append(style, document.createElement("slot"));
305
+ }
306
+ this.setAttribute("role", this.getAttribute("role") || "group");
307
+ this.addEventListener("keydown", this.keydown);
308
+ this.observer?.disconnect();
309
+ this.observer = new MutationObserver(() => this.syncCardFocus());
310
+ this.observer.observe(this, { childList: true, subtree: true });
311
+ this.syncCardFocus();
312
+ }
313
+ disconnectedCallback() { this.removeEventListener("keydown", this.keydown); this.observer?.disconnect(); this.observer = undefined; this.syncCardFocus(true); }
314
+ attributeChangedCallback() { this.toggleAttribute("data-card-focus", this.cardFocus); this.syncCardFocus(); }
315
+ syncCardFocus(restore = false) {
316
+ const cards = [...this.querySelectorAll("spui-carousel-card")];
317
+ cards.forEach((card, index) => {
318
+ if (restore || !this.cardFocus) {
319
+ if (this.originalTabIndex.has(card)) {
320
+ const original = this.originalTabIndex.get(card);
321
+ if (original === null)
322
+ card.removeAttribute("tabindex");
323
+ else
324
+ card.setAttribute("tabindex", original);
325
+ this.originalTabIndex.delete(card);
326
+ }
327
+ return;
328
+ }
329
+ if (!this.originalTabIndex.has(card))
330
+ this.originalTabIndex.set(card, card.getAttribute("tabindex"));
331
+ card.tabIndex = index === this.focusedIndex ? 0 : -1;
332
+ });
333
+ }
334
+ }
335
+ customElements.define("spui-carousel-slider", SpuiCarouselSlider);
336
+ }
337
+ export function defineCarouselViewport() {
338
+ if (customElements.get("spui-carousel-viewport"))
339
+ return;
340
+ class SpuiCarouselViewport extends HTMLElement {
341
+ connectedCallback() {
342
+ if (!this.shadowRoot) {
343
+ const root = this.attachShadow({ mode: "open" });
344
+ const style = document.createElement("style");
345
+ style.textContent = `${base}:host{display:contents;max-width:100%;width:auto}slot{display:contents}`;
346
+ root.append(style, document.createElement("slot"));
347
+ }
348
+ this.setAttribute("role", this.getAttribute("role") || "presentation");
349
+ this.draggable = true;
350
+ }
351
+ }
352
+ customElements.define("spui-carousel-viewport", SpuiCarouselViewport);
353
+ }
354
+ export function defineCarousel() {
355
+ if (customElements.get("spui-carousel"))
356
+ return;
357
+ defineCarouselCard();
358
+ defineCarouselNav();
359
+ defineCarouselNavButton();
360
+ defineCarouselNavImageButton();
361
+ defineCarouselButton();
362
+ defineCarouselAutoplayButton();
363
+ defineCarouselNavContainer();
364
+ defineCarouselSlider();
365
+ defineCarouselViewport();
366
+ class SpuiCarousel extends HTMLElement {
367
+ static observedAttributes = ["aria-label", "aria-roledescription", "previous-label", "next-label", "slide-label", "slide-group-label", "navigation-label", "play-label", "pause-label", "slide-width", "group-size", "draggable", "whitespace", "align", "appearance", "motion", "motion-duration", "motion-physics-duration", "autoplay", "autoplay-interval", "wrap", "circular", "controlled", "selected-index", "default-selected-index", "active-index", "default-active-index"];
368
+ viewport;
369
+ slideSlot;
370
+ previous;
371
+ next;
372
+ controls;
373
+ indicators;
374
+ slides = [];
375
+ current = 0;
376
+ pageSize = 1;
377
+ pageGroups = [];
378
+ slidePages = [];
379
+ requestedTarget = null;
380
+ requestedSource;
381
+ pendingScrollSource;
382
+ interacted = false;
383
+ raf = 0;
384
+ scrollEndTimer = 0;
385
+ events;
386
+ autoplayTimer = 0;
387
+ slideAnimationFrame = 0;
388
+ cancelSlideAnimation;
389
+ autoplayButton;
390
+ liveRegion;
391
+ autoplayRunning = false;
392
+ autoplayExplicitlyStarted = false;
393
+ lastScrollEvent;
394
+ contentObserver;
395
+ resizeObserver;
396
+ announcement;
397
+ drag;
398
+ originalSlideState = new WeakMap();
399
+ originalSlideLayout = new WeakMap();
400
+ originalSlideColumn = new WeakMap();
401
+ connectedCallback() {
402
+ if (!this.shadowRoot)
403
+ this.render();
404
+ this.events?.abort();
405
+ this.events = new AbortController();
406
+ this.slideSlot.addEventListener("slotchange", this.onSlotChange, { signal: this.events.signal });
407
+ this.contentObserver?.disconnect();
408
+ this.contentObserver = new MutationObserver(this.onSlotChange);
409
+ this.contentObserver.observe(this, { childList: true, subtree: true });
410
+ if (typeof ResizeObserver !== "undefined") {
411
+ this.resizeObserver?.disconnect();
412
+ this.resizeObserver = new ResizeObserver(() => { if (this.updatePageSize())
413
+ this.syncSlides(false);
414
+ else
415
+ this.syncWhitespace(); });
416
+ this.resizeObserver.observe(this.viewport);
417
+ }
418
+ this.viewport.addEventListener("scroll", this.onScroll, { passive: true, signal: this.events.signal });
419
+ this.viewport.addEventListener("scrollend", this.onScrollEnd, { signal: this.events.signal });
420
+ this.viewport.addEventListener("keydown", this.onViewportKeyDown, { signal: this.events.signal });
421
+ this.viewport.addEventListener("pointerdown", this.onPointerDown, { signal: this.events.signal });
422
+ this.viewport.addEventListener("pointermove", this.onPointerMove, { signal: this.events.signal });
423
+ this.viewport.addEventListener("pointerup", this.onPointerUp, { signal: this.events.signal });
424
+ this.viewport.addEventListener("pointercancel", this.onPointerUp, { signal: this.events.signal });
425
+ this.indicators.addEventListener("keydown", this.onIndicatorKeyDown, { signal: this.events.signal });
426
+ this.addEventListener("spui-carousel-nav-select", this.onNavSelect, { signal: this.events.signal });
427
+ this.addEventListener("spui-carousel-button-activate", this.onButtonActivate, { signal: this.events.signal });
428
+ this.addEventListener("spui-carousel-autoplay-toggle", this.toggleAutoplay, { signal: this.events.signal });
429
+ this.addEventListener("spui-carousel-card-focus", this.onCardFocus, { signal: this.events.signal });
430
+ this.addEventListener("pointerenter", this.pauseAutoplay, { signal: this.events.signal });
431
+ this.addEventListener("pointerleave", this.resumeAutoplay, { signal: this.events.signal });
432
+ this.addEventListener("focusin", this.pauseForFocus, { signal: this.events.signal });
433
+ this.addEventListener("focusout", this.onFocusOut, { signal: this.events.signal });
434
+ document.addEventListener("visibilitychange", this.onVisibilityChange, { signal: this.events.signal });
435
+ matchMedia("(prefers-reduced-motion: reduce)").addEventListener("change", this.onMotionPreference, { signal: this.events.signal });
436
+ this.autoplayButton.addEventListener("click", this.toggleAutoplay, { signal: this.events.signal });
437
+ this.autoplayRunning = this.hasAttribute("autoplay") && !matchMedia("(prefers-reduced-motion: reduce)").matches;
438
+ this.syncSlideWidth();
439
+ this.syncPresentation();
440
+ this.syncLabels();
441
+ this.syncSlides();
442
+ this.scheduleAutoplay();
443
+ }
444
+ disconnectedCallback() {
445
+ this.events?.abort();
446
+ this.events = undefined;
447
+ this.contentObserver?.disconnect();
448
+ this.contentObserver = undefined;
449
+ this.resizeObserver?.disconnect();
450
+ this.resizeObserver = undefined;
451
+ this.stopSlideAnimation();
452
+ if (this.raf)
453
+ cancelAnimationFrame(this.raf);
454
+ this.raf = 0;
455
+ clearTimeout(this.scrollEndTimer);
456
+ this.scrollEndTimer = 0;
457
+ this.requestedTarget = null;
458
+ clearTimeout(this.autoplayTimer);
459
+ this.autoplayTimer = 0;
460
+ this.slides.forEach(slide => { this.restoreSlideState(slide); this.restoreSlideLayout(slide); });
461
+ }
462
+ attributeChangedCallback(name) {
463
+ if (!this.isConnected)
464
+ return;
465
+ if ((name === "selected-index" || name === "active-index") && this.hasAttribute("controlled"))
466
+ this.setIndex(Number(this.getAttribute(name)), false);
467
+ else if ((name === "default-selected-index" || name === "default-active-index") && !this.interacted)
468
+ this.setIndex(Number(this.getAttribute(name) ?? this.getAttribute("default-selected-index")), false);
469
+ this.syncLabels();
470
+ this.syncSlides();
471
+ if (name === "slide-width")
472
+ this.syncSlideWidth();
473
+ if (name === "motion") {
474
+ this.stopSlideAnimation();
475
+ this.requestedTarget = null;
476
+ this.requestedSource = undefined;
477
+ }
478
+ if (["align", "appearance", "motion", "motion-duration"].includes(name))
479
+ this.syncPresentation();
480
+ if (name === "autoplay")
481
+ this.autoplayRunning = this.hasAttribute("autoplay") && !matchMedia("(prefers-reduced-motion: reduce)").matches;
482
+ this.syncAutoplay();
483
+ }
484
+ get selectedIndex() { return this.current; }
485
+ set selectedIndex(value) { this.interacted = true; this.setIndex(value, false); }
486
+ get defaultSelectedIndex() { return this.current; }
487
+ set defaultSelectedIndex(value) { if (!this.interacted)
488
+ this.setIndex(value, false); }
489
+ get activeIndex() { return this.current; }
490
+ set activeIndex(value) { this.interacted = true; this.setIndex(value, false); }
491
+ get defaultActiveIndex() { return this.current; }
492
+ set defaultActiveIndex(value) { if (!this.interacted)
493
+ this.setIndex(value, false); }
494
+ get circular() { return this.hasAttribute("circular") || this.hasAttribute("wrap"); }
495
+ set circular(value) { this.toggleAttribute("circular", Boolean(value)); }
496
+ goTo(index) { this.requestIndex(index); }
497
+ nextSlide() { this.requestIndex(this.current + 1); }
498
+ previousSlide() { this.requestIndex(this.current - 1); }
499
+ get slideWidth() { return this.getAttribute("slide-width") || "100%"; }
500
+ set slideWidth(value) { this.setAttribute("slide-width", value); }
501
+ get groupSize() { const value = this.getAttribute("group-size") || "auto"; return value === "auto" ? value : Math.max(1, Math.floor(Number(value) || 1)); }
502
+ set groupSize(value) { this.setAttribute("group-size", String(value)); }
503
+ get draggable() { return this.hasAttribute("draggable"); }
504
+ set draggable(value) { this.toggleAttribute("draggable", Boolean(value)); }
505
+ get whitespace() { return this.hasAttribute("whitespace"); }
506
+ set whitespace(value) { this.toggleAttribute("whitespace", Boolean(value)); }
507
+ get align() { return this.getAttribute("align") === "start" || this.getAttribute("align") === "end" ? this.getAttribute("align") : "center"; }
508
+ set align(value) { this.setAttribute("align", value); }
509
+ get appearance() { return this.getAttribute("appearance") === "elevated" ? "elevated" : "flat"; }
510
+ set appearance(value) { this.setAttribute("appearance", value); }
511
+ get motion() {
512
+ if (this.getAttribute("motion") === "fade")
513
+ return "fade";
514
+ return this.hasAttribute("motion-physics-duration") ? { kind: "slide", duration: this.slideMotionDuration } : "slide";
515
+ }
516
+ set motion(value) {
517
+ if (typeof value === "string") {
518
+ this.setAttribute("motion", value);
519
+ this.removeAttribute("motion-physics-duration");
520
+ }
521
+ else {
522
+ this.setAttribute("motion", value.kind);
523
+ if (value.duration === undefined)
524
+ this.removeAttribute("motion-physics-duration");
525
+ else
526
+ this.setAttribute("motion-physics-duration", String(value.duration));
527
+ }
528
+ }
529
+ get motionDuration() { const raw = this.getAttribute("motion-duration"); const value = raw === null ? 240 : Number(raw); return Number.isFinite(value) ? Math.max(0, Math.min(2000, value)) : 240; }
530
+ set motionDuration(value) { this.setAttribute("motion-duration", String(value)); }
531
+ get slideMotionDuration() { const raw = this.getAttribute("motion-physics-duration"); const value = raw === null ? 25 : Number(raw); return Number.isFinite(value) ? Math.max(0, value) : 25; }
532
+ set slideMotionDuration(value) { this.setAttribute("motion-physics-duration", String(value)); }
533
+ syncSlideWidth() {
534
+ const value = this.getAttribute("slide-width")?.trim() || "100%";
535
+ this.style.setProperty("--spui-carousel-slide-size", CSS.supports("flex-basis", value) ? value : "100%");
536
+ }
537
+ updatePageSize() {
538
+ const previousGroups = this.pageGroups;
539
+ const activeSlide = previousGroups[this.current]?.[0] ?? 0;
540
+ const raw = this.getAttribute("group-size") || "auto";
541
+ const groups = [];
542
+ if (raw !== "auto") {
543
+ const requested = Number(raw);
544
+ const size = Number.isFinite(requested) && requested >= 1 ? Math.floor(requested) : 1;
545
+ for (let index = 0; index < this.slides.length; index += size)
546
+ groups.push(Array.from({ length: Math.min(size, this.slides.length - index) }, (_, offset) => index + offset));
547
+ }
548
+ else if (this.viewport && this.slides.length) {
549
+ const available = this.viewport.clientWidth;
550
+ const gap = Number.parseFloat(getComputedStyle(this.viewport).columnGap) || 0;
551
+ if (available > 0) {
552
+ let group = [];
553
+ let used = 0;
554
+ this.slides.forEach((slide, index) => {
555
+ const measured = slide.getBoundingClientRect().width;
556
+ const width = measured > 0 ? measured : available;
557
+ const nextUsed = used + (group.length ? gap : 0) + width;
558
+ if (group.length && nextUsed > available + 0.5) {
559
+ groups.push(group);
560
+ group = [];
561
+ used = 0;
562
+ }
563
+ used += (group.length ? gap : 0) + width;
564
+ group.push(index);
565
+ });
566
+ if (group.length)
567
+ groups.push(group);
568
+ }
569
+ else {
570
+ for (let index = 0; index < this.slides.length; index++)
571
+ groups.push([index]);
572
+ }
573
+ }
574
+ else {
575
+ for (let index = 0; index < this.slides.length; index++)
576
+ groups.push([index]);
577
+ }
578
+ const changed = groups.length !== previousGroups.length || groups.some((group, page) => group.length !== previousGroups[page]?.length || group.some((index, offset) => index !== previousGroups[page]?.[offset]));
579
+ if (changed) {
580
+ this.requestedTarget = null;
581
+ this.requestedSource = undefined;
582
+ this.pendingScrollSource = undefined;
583
+ this.stopSlideAnimation();
584
+ }
585
+ this.pageGroups = groups;
586
+ this.slidePages = Array(this.slides.length).fill(0);
587
+ groups.forEach((group, page) => group.forEach(index => { this.slidePages[index] = page; }));
588
+ this.pageSize = groups.reduce((largest, group) => Math.max(largest, group.length), 1);
589
+ this.current = Math.min(this.slidePages[activeSlide] ?? 0, Math.max(0, this.pageCount - 1));
590
+ this.style.setProperty("--spui-carousel-page-size", String(this.pageSize));
591
+ return changed;
592
+ }
593
+ get pageCount() { return this.pageGroups.length; }
594
+ pageForSlide(index) { return this.slidePages[index] ?? 0; }
595
+ firstSlideForPage(page) { return this.slides[this.pageGroups[page]?.[0] ?? 0]; }
596
+ columnForSlide(index) { return (this.pageGroups[this.pageForSlide(index)]?.indexOf(index) ?? 0) + 1; }
597
+ syncPresentation() {
598
+ this.style.setProperty("--spui-carousel-align", this.align);
599
+ this.style.setProperty("--spui-carousel-motion-duration", `${this.motionDuration}ms`);
600
+ this.querySelectorAll("spui-carousel-slider, spui-carousel-viewport").forEach(wrapper => wrapper.toggleAttribute("data-spui-carousel-motion", this.motion === "fade"));
601
+ this.syncWhitespace();
602
+ this.slides.forEach((slide, index) => this.applySlideState(slide, this.pageForSlide(index) === this.current, index));
603
+ }
604
+ syncWhitespace() {
605
+ const viewportWidth = this.viewport?.clientWidth || 0;
606
+ this.slides.forEach((slide, index) => {
607
+ const first = index === 0, last = index === this.slides.length - 1;
608
+ if (!this.whitespace || (!first && !last)) {
609
+ this.restoreSlideLayout(slide);
610
+ return;
611
+ }
612
+ if (!this.originalSlideLayout.has(slide))
613
+ this.originalSlideLayout.set(slide, { startAttr: slide.getAttribute("data-spui-carousel-space-start"), endAttr: slide.getAttribute("data-spui-carousel-space-end"), startValue: slide.style.getPropertyValue("--spui-carousel-auto-space-start"), endValue: slide.style.getPropertyValue("--spui-carousel-auto-space-end") });
614
+ const remaining = Math.max(0, viewportWidth - slide.getBoundingClientRect().width);
615
+ const start = first && (this.align === "center" || this.align === "end") ? (this.align === "center" ? remaining / 2 : remaining) : 0;
616
+ const end = last && (this.align === "center" || this.align === "start") ? (this.align === "center" ? remaining / 2 : remaining) : 0;
617
+ slide.toggleAttribute("data-spui-carousel-space-start", start > 0);
618
+ slide.toggleAttribute("data-spui-carousel-space-end", end > 0);
619
+ if (start > 0)
620
+ slide.style.setProperty("--spui-carousel-auto-space-start", `${start}px`);
621
+ else
622
+ slide.style.removeProperty("--spui-carousel-auto-space-start");
623
+ if (end > 0)
624
+ slide.style.setProperty("--spui-carousel-auto-space-end", `${end}px`);
625
+ else
626
+ slide.style.removeProperty("--spui-carousel-auto-space-end");
627
+ });
628
+ }
629
+ restoreSlideLayout(slide) {
630
+ const original = this.originalSlideLayout.get(slide);
631
+ if (!original)
632
+ return;
633
+ if (original.startAttr === null)
634
+ slide.removeAttribute("data-spui-carousel-space-start");
635
+ else
636
+ slide.setAttribute("data-spui-carousel-space-start", original.startAttr);
637
+ if (original.endAttr === null)
638
+ slide.removeAttribute("data-spui-carousel-space-end");
639
+ else
640
+ slide.setAttribute("data-spui-carousel-space-end", original.endAttr);
641
+ if (original.startValue)
642
+ slide.style.setProperty("--spui-carousel-auto-space-start", original.startValue);
643
+ else
644
+ slide.style.removeProperty("--spui-carousel-auto-space-start");
645
+ if (original.endValue)
646
+ slide.style.setProperty("--spui-carousel-auto-space-end", original.endValue);
647
+ else
648
+ slide.style.removeProperty("--spui-carousel-auto-space-end");
649
+ this.originalSlideLayout.delete(slide);
650
+ }
651
+ applySlideState(slide, active, index) {
652
+ if (!this.originalSlideState.has(slide))
653
+ this.originalSlideState.set(slide, { inert: slide.inert, ariaHidden: slide.getAttribute("aria-hidden") });
654
+ const original = this.originalSlideState.get(slide);
655
+ slide.toggleAttribute("data-spui-carousel-motion", this.motion === "fade");
656
+ if (this.motion !== "fade") {
657
+ this.restoreSlideState(slide);
658
+ return;
659
+ }
660
+ if (!this.originalSlideColumn.has(slide))
661
+ this.originalSlideColumn.set(slide, { value: slide.style.getPropertyValue("--spui-carousel-slide-column"), gridArea: slide.style.gridArea });
662
+ slide.style.setProperty("--spui-carousel-slide-column", String(this.columnForSlide(index)));
663
+ slide.style.gridArea = "1 / var(--spui-carousel-slide-column) / 2 / span 1";
664
+ slide.toggleAttribute("data-spui-carousel-active", active);
665
+ if (!active && (slide.contains(document.activeElement) || slide.shadowRoot?.activeElement))
666
+ this.viewport?.focus({ preventScroll: true });
667
+ slide.inert = original.inert || !active;
668
+ if (active && original.ariaHidden === null)
669
+ slide.removeAttribute("aria-hidden");
670
+ else
671
+ slide.setAttribute("aria-hidden", active ? original.ariaHidden : "true");
672
+ }
673
+ restoreSlideState(slide) {
674
+ const original = this.originalSlideState.get(slide);
675
+ if (!original)
676
+ return;
677
+ slide.removeAttribute("data-spui-carousel-active");
678
+ slide.removeAttribute("data-spui-carousel-motion");
679
+ slide.inert = original.inert;
680
+ const originalColumn = this.originalSlideColumn.get(slide);
681
+ if (originalColumn !== undefined) {
682
+ if (originalColumn.value)
683
+ slide.style.setProperty("--spui-carousel-slide-column", originalColumn.value);
684
+ else
685
+ slide.style.removeProperty("--spui-carousel-slide-column");
686
+ slide.style.gridArea = originalColumn.gridArea;
687
+ this.originalSlideColumn.delete(slide);
688
+ }
689
+ if (original.ariaHidden === null)
690
+ slide.removeAttribute("aria-hidden");
691
+ else
692
+ slide.setAttribute("aria-hidden", original.ariaHidden);
693
+ }
694
+ render() {
695
+ const shadow = this.attachShadow({ mode: "open" });
696
+ const style = document.createElement("style");
697
+ style.textContent = `${base}:host{position:relative;display:block;min-width:0}:host([appearance=elevated]){box-sizing:border-box;padding:var(--spui-carousel-padding,12px);border:1px solid var(--spui-ui-stroke,var(--spui-semantic-body-divider,#d1d1d1));border-radius:var(--spui-card-radius,var(--spui-control-radius,8px));background:var(--spui-ui-surface,var(--spui-base-surface,#fff));color:var(--spui-ui-text,inherit);box-shadow:var(--spui-card-shadow,0 2px 8px #0002)}:host([data-empty=true]){display:none}::slotted(spui-carousel-nav-container[layout=overlay]),::slotted(spui-carousel-nav-container[layout=overlay-wide]){position:absolute;z-index:2;inset-inline:12px;inset-block-end:12px}::slotted(spui-carousel-nav-container[layout=overlay-expanded]){position:absolute;z-index:2;inset-inline:12px;inset-block-start:50%;transform:translateY(-50%)}::slotted(spui-carousel-slider){flex:0 0 100%;width:100%}:host([data-dragging]) .viewport{cursor:grabbing;user-select:none;scroll-snap-type:none}:host([data-dragging]) ::slotted(*){pointer-events:none}.viewport{display:flex;gap:var(--spui-carousel-gap,0px);min-width:0;overflow-x:auto;overflow-y:hidden;overscroll-behavior-inline:contain;scroll-snap-type:x mandatory;scrollbar-width:thin;scrollbar-color:var(--spui-carousel-scrollbar,var(--spui-ui-stroke,#c8c8c8)) transparent;scroll-behavior:smooth}:host([motion=fade]) .viewport{display:grid;overflow:hidden;scroll-snap-type:none}:host([motion=fade]) .viewport slot{display:contents}.viewport:focus-visible{outline:2px solid var(--spui-focus-ring,var(--spui-ui-accent,#0078d4));outline-offset:2px}::slotted(*){box-sizing:border-box;flex:0 0 var(--spui-carousel-slide-width,var(--spui-carousel-slide-size,100%));min-width:0;scroll-snap-align:var(--spui-carousel-align,start);scroll-snap-stop:always}::slotted(spui-carousel-card[auto-size]){flex-basis:auto}::slotted([data-spui-carousel-space-start]){margin-inline-start:var(--spui-carousel-auto-space-start,0px)}::slotted([data-spui-carousel-space-end]){margin-inline-end:var(--spui-carousel-auto-space-end,0px)}:host([motion=fade]) ::slotted(*){grid-area:1/1;width:100%;flex:none;opacity:0;visibility:hidden;pointer-events:none;transition:opacity var(--spui-carousel-motion-duration,240ms) var(--spui-motion-easing-standard,ease),visibility 0s linear var(--spui-carousel-motion-duration,240ms)}:host([motion=fade]) ::slotted([data-spui-carousel-active]){opacity:1;visibility:visible;pointer-events:auto;transition-delay:0s}.controls{display:flex;align-items:center;justify-content:center;gap:4px;min-height:48px;margin-block-start:4px}.controls[hidden]{display:none}.arrow,.indicator,.autoplay{box-sizing:border-box;display:grid;place-items:center;flex:none;min-width:44px;min-height:44px;padding:0 10px;border:1px solid var(--spui-carousel-control-border,var(--spui-ui-stroke,#d1d1d1));border-radius:var(--spui-control-radius,6px);background:var(--spui-carousel-control-background,var(--spui-ui-surface,#fff));color:var(--spui-carousel-control-foreground,var(--spui-ui-text,inherit));font:inherit;cursor:pointer}.arrow:hover:not(:disabled),.indicator:hover,.autoplay:hover{background:var(--spui-ui-surface-hover,var(--spui-ui-canvas,#f5f5f5))}.arrow:focus-visible,.indicator:focus-visible,.autoplay:focus-visible{outline:2px solid var(--spui-focus-ring,var(--spui-ui-accent,#0078d4));outline-offset:2px}.arrow:disabled{color:var(--spui-ui-muted,#777);opacity:.55;cursor:not-allowed}.arrow svg{width:16px;height:16px;fill:none;stroke:currentColor;stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round}.indicators{display:flex;align-items:center;justify-content:center;gap:2px}.indicator{position:relative;border:0;background:transparent}.indicator:before{width:22px;height:8px;border-radius:999px;background:var(--spui-carousel-indicator,var(--spui-ui-stroke,#a0a0a0));content:"";transform:scaleX(.36);transition:transform var(--spui-motion-duration-normal,150ms) var(--spui-motion-easing-standard,cubic-bezier(.2,0,0,1)),background-color var(--spui-motion-duration-normal,150ms) var(--spui-motion-easing-standard,cubic-bezier(.2,0,0,1))}.indicator[aria-current=step]:before{transform:scaleX(1);background:var(--spui-ui-accent,#0078d4)}@media(prefers-reduced-motion:reduce){.viewport{scroll-behavior:auto}.indicator:before{transition:none}:host([motion=fade]) ::slotted(*){transition:none}}@media(forced-colors:active){:host([appearance=elevated]){border-color:CanvasText;background:Canvas;color:CanvasText;box-shadow:none}.viewport:focus-visible,.arrow:focus-visible,.indicator:focus-visible,.autoplay:focus-visible{outline-color:Highlight}.arrow,.autoplay{border-color:ButtonText}.indicator:before{background:ButtonText}.indicator[aria-current=step]:before{background:Highlight}}:host(:dir(rtl)) .arrow.previous svg,:host(:dir(rtl)) .arrow.next svg{transform:rotate(180deg)}`;
698
+ style.textContent += ":host([motion=fade]) .viewport{grid-template-columns:repeat(var(--spui-carousel-page-size,1),minmax(0,1fr));grid-template-rows:minmax(0,1fr)}";
699
+ this.viewport = document.createElement("div");
700
+ this.viewport.className = "viewport";
701
+ this.viewport.setAttribute("role", "group");
702
+ this.viewport.setAttribute("tabindex", "0");
703
+ this.viewport.setAttribute("aria-label", "Carousel slides");
704
+ this.viewport.id = `spui-carousel-${++carouselId}`;
705
+ this.slideSlot = document.createElement("slot");
706
+ this.viewport.append(this.slideSlot);
707
+ this.controls = document.createElement("div");
708
+ this.controls.className = "controls";
709
+ this.controls.part.add("controls");
710
+ this.previous = this.createArrow("previous", "M14 5 7 12l7 7");
711
+ this.indicators = document.createElement("div");
712
+ this.indicators.className = "indicators";
713
+ this.indicators.setAttribute("role", "group");
714
+ this.indicators.setAttribute("aria-label", "Slide navigation");
715
+ this.next = this.createArrow("next", "m10 5 7 7-7 7");
716
+ this.autoplayButton = document.createElement("button");
717
+ this.autoplayButton.type = "button";
718
+ this.autoplayButton.className = "autoplay";
719
+ this.autoplayButton.part.add("autoplay");
720
+ const navigation = document.createElement("slot");
721
+ navigation.name = "navigation";
722
+ navigation.part.add("navigation");
723
+ this.controls.append(this.previous, this.indicators, this.next, this.autoplayButton, navigation);
724
+ this.liveRegion = document.createElement("div");
725
+ this.liveRegion.className = "announcement";
726
+ this.liveRegion.setAttribute("aria-live", "polite");
727
+ this.liveRegion.setAttribute("aria-atomic", "true");
728
+ const visuallyHidden = document.createElement("style");
729
+ visuallyHidden.textContent = ".announcement{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0}";
730
+ shadow.append(style, this.viewport, this.controls, visuallyHidden, this.liveRegion);
731
+ }
732
+ createArrow(name, path) {
733
+ const button = document.createElement("button");
734
+ button.type = "button";
735
+ button.className = `arrow ${name}`;
736
+ button.part.add(`button-${name}`);
737
+ button.setAttribute("aria-controls", this.viewport.id);
738
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
739
+ svg.setAttribute("viewBox", "0 0 24 24");
740
+ svg.setAttribute("aria-hidden", "true");
741
+ const line = document.createElementNS("http://www.w3.org/2000/svg", "path");
742
+ line.setAttribute("d", path);
743
+ svg.append(line);
744
+ button.append(svg);
745
+ button.addEventListener("click", event => this.requestIndex(this.current + (name === "next" ? 1 : -1), { event, type: "click" }));
746
+ return button;
747
+ }
748
+ syncLabels() {
749
+ this.setAttribute("role", this.getAttribute("role") || "region");
750
+ if (!this.getAttribute("aria-roledescription"))
751
+ this.setAttribute("aria-roledescription", "carousel");
752
+ if (!this.getAttribute("aria-label"))
753
+ this.setAttribute("aria-label", "Carousel");
754
+ this.previous?.setAttribute("aria-label", this.getAttribute("previous-label") || "Previous slide");
755
+ this.next?.setAttribute("aria-label", this.getAttribute("next-label") || "Next slide");
756
+ this.viewport?.setAttribute("aria-label", this.getAttribute("slide-group-label") || "Carousel slides");
757
+ this.indicators?.setAttribute("aria-label", this.getAttribute("navigation-label") || "Slide navigation");
758
+ this.syncAutoplay();
759
+ }
760
+ syncAutoplay() {
761
+ if (!this.autoplayButton)
762
+ return;
763
+ this.autoplayButton.hidden = this.pageCount < 2;
764
+ this.autoplayButton.textContent = this.autoplayRunning ? (this.getAttribute("pause-label") || "Pause") : (this.getAttribute("play-label") || "Play");
765
+ this.autoplayButton.setAttribute("aria-label", this.autoplayButton.textContent);
766
+ this.autoplayButton.setAttribute("aria-pressed", String(this.autoplayRunning));
767
+ this.querySelectorAll("spui-carousel-autoplay-button").forEach(button => button.toggleAttribute("selected", this.autoplayRunning));
768
+ this.scheduleAutoplay();
769
+ }
770
+ scheduleAutoplay() {
771
+ clearTimeout(this.autoplayTimer);
772
+ this.autoplayTimer = 0;
773
+ if (!this.isConnected || !this.autoplayRunning || this.pageCount < 2 || ((this.matches(":hover") || this.contains(document.activeElement)) && !this.autoplayExplicitlyStarted) || document.hidden)
774
+ return;
775
+ const interval = Number(this.getAttribute("autoplay-interval") || 4000);
776
+ this.autoplayTimer = window.setTimeout(() => { this.requestIndex((this.current + 1) % this.pageCount, { event: new Event("autoplay"), type: "autoplay" }); this.scheduleAutoplay(); }, Number.isFinite(interval) ? Math.max(1000, interval) : 4000);
777
+ }
778
+ pauseAutoplay = () => { clearTimeout(this.autoplayTimer); this.autoplayTimer = 0; };
779
+ pauseForFocus = () => { this.autoplayExplicitlyStarted = false; this.pauseAutoplay(); };
780
+ resumeAutoplay = () => this.scheduleAutoplay();
781
+ onFocusOut = (event) => { if (!this.contains(event.relatedTarget))
782
+ this.scheduleAutoplay(); };
783
+ onVisibilityChange = () => this.scheduleAutoplay();
784
+ onMotionPreference = (event) => { if (event.matches) {
785
+ this.autoplayRunning = false;
786
+ this.autoplayExplicitlyStarted = false;
787
+ this.syncAutoplay();
788
+ } };
789
+ toggleAutoplay = () => { this.autoplayRunning = !this.autoplayRunning; this.autoplayExplicitlyStarted = this.autoplayRunning; this.syncAutoplay(); };
790
+ syncSlides(recalculateGroups = true) {
791
+ if (!this.slideSlot)
792
+ return;
793
+ const collectSlides = (element) => {
794
+ if (element.localName === "spui-carousel-slider" || element.localName === "spui-carousel-viewport")
795
+ return [...element.children].filter((child) => child instanceof HTMLElement).flatMap(collectSlides);
796
+ return [element];
797
+ };
798
+ const nextSlides = this.slideSlot.assignedElements({ flatten: true }).filter((element) => element instanceof HTMLElement).flatMap(collectSlides);
799
+ this.slides.filter(slide => !nextSlides.includes(slide)).forEach(slide => { this.resizeObserver?.unobserve(slide); this.restoreSlideState(slide); this.restoreSlideLayout(slide); });
800
+ this.slides = nextSlides;
801
+ if (recalculateGroups)
802
+ this.updatePageSize();
803
+ this.slides.forEach(slide => this.resizeObserver?.observe(slide));
804
+ this.syncWhitespace();
805
+ this.slides.forEach((slide, index) => {
806
+ slide.setAttribute("role", slide.getAttribute("role") || "group");
807
+ slide.setAttribute("aria-roledescription", slide.getAttribute("aria-roledescription") || "slide");
808
+ if (!slide.hasAttribute("aria-label")) {
809
+ slide.dataset.spuiCarouselGeneratedLabel = "";
810
+ slide.setAttribute("aria-label", this.formatSlideLabel(index));
811
+ }
812
+ else if (slide.dataset.spuiCarouselGeneratedLabel !== undefined)
813
+ delete slide.dataset.spuiCarouselGeneratedLabel;
814
+ });
815
+ this.setAttribute("data-empty", String(this.slides.length === 0));
816
+ this.indicators.replaceChildren(...Array.from({ length: this.pageCount }, (_, index) => {
817
+ const button = document.createElement("button");
818
+ button.type = "button";
819
+ button.className = "indicator";
820
+ button.setAttribute("aria-label", this.formatPageLabel(index));
821
+ button.addEventListener("click", event => this.requestIndex(index, { event, type: "click" }));
822
+ return button;
823
+ }));
824
+ this.current = Math.min(Math.max(this.current, 0), Math.max(0, this.pageCount - 1));
825
+ if (!this.interacted && this.pageCount && this.current === 0 && this.hasAttribute("default-selected-index"))
826
+ this.current = Math.min(Math.max(Number(this.getAttribute("default-selected-index")) || 0, 0), this.pageCount - 1);
827
+ this.syncState();
828
+ this.syncAutoplay();
829
+ if (this.slides[this.current] && this.motion !== "fade")
830
+ this.scrollToIndex(this.current, "instant");
831
+ }
832
+ formatSlideLabel(index) {
833
+ return (this.getAttribute("slide-label") || "Slide {index} of {count}").replaceAll("{index}", String(index + 1)).replaceAll("{count}", String(this.slides.length || this.slideSlot?.assignedElements({ flatten: true }).length || 0));
834
+ }
835
+ formatPageLabel(index) {
836
+ return (this.getAttribute("slide-label") || "Slide {index} of {count}").replaceAll("{index}", String(index + 1)).replaceAll("{count}", String(this.pageCount));
837
+ }
838
+ requestIndex(index, source) {
839
+ if (!this.pageCount)
840
+ return;
841
+ const changeSource = source ?? { event: new Event("programmatic"), type: "programmatic" };
842
+ let next = index;
843
+ if (this.circular)
844
+ next = (next + this.pageCount) % this.pageCount;
845
+ else
846
+ next = Math.min(Math.max(next, 0), this.pageCount - 1);
847
+ if (next === this.current && !this.circular)
848
+ return;
849
+ if (!this.hasAttribute("controlled")) {
850
+ this.interacted = true;
851
+ this.setIndex(next, true, changeSource);
852
+ }
853
+ else
854
+ this.dispatch(next, changeSource);
855
+ }
856
+ setIndex(index, emit, source) {
857
+ const next = Math.min(Math.max(Number.isFinite(index) ? Math.trunc(index) : 0, 0), Math.max(0, this.pageCount - 1));
858
+ const changed = next !== this.current;
859
+ this.current = next;
860
+ this.syncState();
861
+ if (changed && this.motion !== "fade") {
862
+ this.requestedTarget = next;
863
+ this.requestedSource = source;
864
+ this.scrollToIndex(next, "smooth");
865
+ }
866
+ else if (this.motion === "fade") {
867
+ this.requestedTarget = null;
868
+ this.requestedSource = undefined;
869
+ }
870
+ if (changed && emit)
871
+ this.dispatch(next, source);
872
+ }
873
+ dispatch(index, source) {
874
+ const groups = this.pageGroups.map(group => [...group]);
875
+ this.liveRegion.textContent = this.announcement?.(index, this.pageCount, groups) || this.formatPageLabel(index);
876
+ const changeSource = source ?? { event: new Event("programmatic"), type: "programmatic" };
877
+ this.dispatchEvent(new CustomEvent("spui-carousel-change", { bubbles: true, composed: true, detail: { value: index, index, type: "select", event: changeSource.event, eventType: changeSource.type } }));
878
+ }
879
+ scrollToIndex(index, behavior) {
880
+ const slide = this.firstSlideForPage(index);
881
+ if (!slide)
882
+ return;
883
+ const reduced = matchMedia("(prefers-reduced-motion: reduce)").matches;
884
+ if (behavior === "smooth" && !reduced && this.motion !== "fade" && this.slideMotionDuration > 0) {
885
+ this.animateSlideScroll(slide, this.slideMotionDuration);
886
+ return;
887
+ }
888
+ this.stopSlideAnimation();
889
+ slide.scrollIntoView({ behavior: "instant", block: "nearest", inline: this.align });
890
+ }
891
+ stopSlideAnimation() {
892
+ this.cancelSlideAnimation?.();
893
+ this.cancelSlideAnimation = undefined;
894
+ this.slideAnimationFrame = 0;
895
+ clearTimeout(this.scrollEndTimer);
896
+ this.scrollEndTimer = 0;
897
+ }
898
+ animateSlideScroll(slide, duration) {
899
+ this.stopSlideAnimation();
900
+ const viewport = this.viewport;
901
+ const originalSnap = viewport.style.scrollSnapType;
902
+ const originalBehavior = viewport.style.scrollBehavior;
903
+ const start = viewport.scrollLeft;
904
+ viewport.style.scrollSnapType = "none";
905
+ viewport.style.scrollBehavior = "auto";
906
+ slide.scrollIntoView({ behavior: "instant", block: "nearest", inline: this.align });
907
+ const target = viewport.scrollLeft;
908
+ viewport.scrollLeft = start;
909
+ let location = start;
910
+ let velocity = 0;
911
+ let active = true;
912
+ const restore = () => { viewport.style.scrollSnapType = originalSnap; viewport.style.scrollBehavior = originalBehavior; };
913
+ const cancel = () => {
914
+ if (!active)
915
+ return;
916
+ active = false;
917
+ if (this.slideAnimationFrame)
918
+ cancelAnimationFrame(this.slideAnimationFrame);
919
+ restore();
920
+ };
921
+ const finish = () => {
922
+ if (!active)
923
+ return;
924
+ active = false;
925
+ viewport.scrollLeft = target;
926
+ restore();
927
+ this.cancelSlideAnimation = undefined;
928
+ this.slideAnimationFrame = 0;
929
+ if (this.requestedTarget !== null)
930
+ this.onScrollEnd(this.lastScrollEvent);
931
+ };
932
+ this.cancelSlideAnimation = cancel;
933
+ const friction = 0.68;
934
+ const tick = () => {
935
+ if (!active)
936
+ return;
937
+ const displacement = target - location;
938
+ velocity = (velocity + displacement / duration) * friction;
939
+ location += velocity;
940
+ viewport.scrollLeft = location;
941
+ if (Math.abs(target - location) < 0.4 && Math.abs(velocity) < 0.4)
942
+ finish();
943
+ else
944
+ this.slideAnimationFrame = requestAnimationFrame(tick);
945
+ };
946
+ if (Math.abs(target - start) < 0.4)
947
+ finish();
948
+ else
949
+ this.slideAnimationFrame = requestAnimationFrame(tick);
950
+ }
951
+ syncState() {
952
+ const count = this.pageCount;
953
+ this.controls.hidden = count < 2;
954
+ this.previous.disabled = !this.circular && this.current <= 0;
955
+ this.next.disabled = !this.circular && this.current >= count - 1;
956
+ this.indicators.querySelectorAll(".indicator").forEach((button, index) => {
957
+ if (index === this.current)
958
+ button.setAttribute("aria-current", "step");
959
+ else
960
+ button.removeAttribute("aria-current");
961
+ button.tabIndex = index === this.current ? 0 : -1;
962
+ });
963
+ this.slides.forEach((slide, index) => { if (slide.dataset.spuiCarouselGeneratedLabel !== undefined)
964
+ slide.setAttribute("aria-label", this.formatPageLabel(this.pageForSlide(index))); });
965
+ this.slides.forEach((slide, index) => this.applySlideState(slide, this.pageForSlide(index) === this.current, index));
966
+ this.querySelectorAll("spui-carousel-nav-button,spui-carousel-nav-image-button").forEach(button => {
967
+ const buttonIndex = Number(button.getAttribute("index"));
968
+ button.toggleAttribute("selected", buttonIndex === this.current);
969
+ button.toggleAttribute("disabled", buttonIndex >= count);
970
+ });
971
+ this.querySelectorAll("spui-carousel-button").forEach(button => button.disabled = !this.circular && (button.navType === "prev" ? this.current <= 0 : this.current >= count - 1));
972
+ this.dataset.selectedIndex = String(this.current);
973
+ this.syncAutoplay();
974
+ }
975
+ onSlotChange = () => this.syncSlides();
976
+ onNavSelect = (event) => { const detail = event.detail; this.requestIndex(Number(detail?.index), { event: detail?.event ?? new MouseEvent("click"), type: "click" }); };
977
+ onCardFocus = (event) => { const detail = event.detail; this.requestIndex(this.pageForSlide(Number(detail?.index)), { event: detail?.event ?? new KeyboardEvent("keydown"), type: "keydown" }); };
978
+ onButtonActivate = (event) => { const detail = event.detail; this.requestIndex(this.current + (detail?.navType === "prev" ? -1 : 1), { event: detail?.event ?? new MouseEvent("click"), type: "click" }); };
979
+ onScroll = (event) => {
980
+ this.lastScrollEvent = event;
981
+ if (this.motion === "fade")
982
+ return;
983
+ if (this.drag?.moved)
984
+ return;
985
+ if (this.requestedTarget !== null) {
986
+ clearTimeout(this.scrollEndTimer);
987
+ this.scrollEndTimer = window.setTimeout(() => this.onScrollEnd(event), 150);
988
+ return;
989
+ }
990
+ if (this.raf)
991
+ return;
992
+ this.raf = requestAnimationFrame(() => {
993
+ this.raf = 0;
994
+ if (this.requestedTarget !== null)
995
+ return;
996
+ const nearest = this.nearestSlideIndex();
997
+ if (nearest !== this.current) {
998
+ const source = this.pendingScrollSource ?? { event, type: "scroll" };
999
+ this.pendingScrollSource = undefined;
1000
+ this.current = nearest;
1001
+ this.syncState();
1002
+ this.dispatch(nearest, source);
1003
+ }
1004
+ });
1005
+ };
1006
+ onPointerDown = (event) => {
1007
+ if (!this.draggable || event.pointerType !== "mouse" || event.button !== 0 || (event.target instanceof HTMLElement && event.target.closest("button,a,input,textarea,select,[contenteditable=true]")))
1008
+ return;
1009
+ if (this.cancelSlideAnimation) {
1010
+ this.stopSlideAnimation();
1011
+ this.requestedTarget = null;
1012
+ this.requestedSource = undefined;
1013
+ }
1014
+ this.drag = { pointerId: event.pointerId, startX: event.clientX, startY: event.clientY, startScrollLeft: this.viewport.scrollLeft, moved: false };
1015
+ this.viewport.setPointerCapture(event.pointerId);
1016
+ };
1017
+ onPointerMove = (event) => {
1018
+ const drag = this.drag;
1019
+ if (!drag || event.pointerId !== drag.pointerId)
1020
+ return;
1021
+ const dx = event.clientX - drag.startX, dy = event.clientY - drag.startY;
1022
+ if (!drag.moved && Math.abs(dx) < 5)
1023
+ return;
1024
+ if (Math.abs(dy) > Math.abs(dx))
1025
+ return;
1026
+ drag.moved = true;
1027
+ this.setAttribute("data-dragging", "");
1028
+ this.viewport.scrollLeft = drag.startScrollLeft - (getComputedStyle(this).direction === "rtl" ? -dx : dx);
1029
+ event.preventDefault();
1030
+ };
1031
+ onPointerUp = (event) => {
1032
+ if (!this.drag || event.pointerId !== this.drag.pointerId)
1033
+ return;
1034
+ if (this.viewport.hasPointerCapture(event.pointerId))
1035
+ this.viewport.releasePointerCapture(event.pointerId);
1036
+ const moved = this.drag.moved;
1037
+ this.drag = undefined;
1038
+ this.removeAttribute("data-dragging");
1039
+ if (moved && this.motion !== "fade") {
1040
+ const next = this.nearestSlideIndex();
1041
+ const source = { event, type: "drag" };
1042
+ if (next === this.current)
1043
+ this.pendingScrollSource = source;
1044
+ this.requestIndex(next, source);
1045
+ }
1046
+ };
1047
+ nearestSlideIndex() {
1048
+ const viewportBox = this.viewport.getBoundingClientRect();
1049
+ const rtl = getComputedStyle(this.viewport).direction === "rtl";
1050
+ const target = this.align === "center" ? viewportBox.left + viewportBox.width / 2 : this.align === "start" ? (rtl ? viewportBox.right : viewportBox.left) : (rtl ? viewportBox.left : viewportBox.right);
1051
+ const nearest = this.slides.reduce((best, slide, index) => {
1052
+ const rect = slide.getBoundingClientRect();
1053
+ const point = this.align === "center" ? rect.left + rect.width / 2 : this.align === "start" ? (rtl ? rect.right : rect.left) : (rtl ? rect.left : rect.right);
1054
+ const distance = Math.abs(point - target);
1055
+ return distance < best.distance ? { index, distance } : best;
1056
+ }, { index: 0, distance: Number.POSITIVE_INFINITY }).index;
1057
+ return this.pageForSlide(nearest);
1058
+ }
1059
+ onScrollEnd = (event) => {
1060
+ // The native scrollend event may belong to an interrupted smooth scroll.
1061
+ // The attraction animation commits its own target from finish().
1062
+ if (this.cancelSlideAnimation)
1063
+ return;
1064
+ if (this.motion === "fade") {
1065
+ this.requestedTarget = null;
1066
+ this.requestedSource = undefined;
1067
+ clearTimeout(this.scrollEndTimer);
1068
+ this.scrollEndTimer = 0;
1069
+ return;
1070
+ }
1071
+ if (this.requestedTarget === null) {
1072
+ this.pendingScrollSource = undefined;
1073
+ return;
1074
+ }
1075
+ clearTimeout(this.scrollEndTimer);
1076
+ this.scrollEndTimer = 0;
1077
+ const target = this.requestedTarget;
1078
+ this.requestedTarget = null;
1079
+ const nearest = target ?? this.nearestSlideIndex();
1080
+ const source = this.requestedSource ?? { event: event ?? this.lastScrollEvent ?? new Event("scroll"), type: "scroll" };
1081
+ this.requestedSource = undefined;
1082
+ if (nearest !== this.current) {
1083
+ this.current = nearest;
1084
+ this.syncState();
1085
+ this.dispatch(nearest, source);
1086
+ }
1087
+ };
1088
+ onViewportKeyDown = (event) => {
1089
+ if (event.target instanceof HTMLElement && event.target.closest("input,textarea,select,button,a,[contenteditable=true],[role=slider],[role=textbox]"))
1090
+ return;
1091
+ const rtl = getComputedStyle(this).direction === "rtl";
1092
+ const previous = rtl ? "ArrowRight" : "ArrowLeft";
1093
+ const next = rtl ? "ArrowLeft" : "ArrowRight";
1094
+ if (event.key !== previous && event.key !== next)
1095
+ return;
1096
+ event.preventDefault();
1097
+ this.requestIndex(this.current + (event.key === next ? 1 : -1), { event, type: "keydown" });
1098
+ };
1099
+ onIndicatorKeyDown = (event) => {
1100
+ const rtl = getComputedStyle(this).direction === "rtl";
1101
+ const previous = rtl ? "ArrowRight" : "ArrowLeft";
1102
+ const next = rtl ? "ArrowLeft" : "ArrowRight";
1103
+ const index = event.key === "Home" ? 0 : event.key === "End" ? this.pageCount - 1 : event.key === previous ? this.current - 1 : event.key === next ? this.current + 1 : -1;
1104
+ if (index < 0)
1105
+ return;
1106
+ event.preventDefault();
1107
+ this.requestIndex(index, { event, type: "keydown" });
1108
+ this.indicators.querySelectorAll(".indicator")[this.current]?.focus();
1109
+ };
1110
+ }
1111
+ customElements.define("spui-carousel", SpuiCarousel);
1112
+ }