@gridkitjs/forms-dom 0.0.0-dev-20261206092422 → 0.0.0-dev-20261206150419

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.
package/dist/index.js CHANGED
@@ -1,3 +1,14 @@
1
+ // src/abortSignals.ts
2
+ var abortSignals = () => {
3
+ const controller = new AbortController();
4
+ return {
5
+ signal: controller.signal,
6
+ abort: () => {
7
+ controller.abort();
8
+ }
9
+ };
10
+ };
11
+
1
12
  // src/announcer.ts
2
13
  var NO_BREAK_SPACE = String.fromCodePoint(160);
3
14
  function createLiveAnnouncer(container = document.body, options = {}) {
@@ -108,6 +119,160 @@ function attachFocusGroup(root, initialOptions) {
108
119
  };
109
120
  }
110
121
 
122
+ // src/writingDirection.ts
123
+ function writingDirection(element) {
124
+ return getComputedStyle(element).direction === "rtl" ? "rtl" : "ltr";
125
+ }
126
+
127
+ // src/form/attachRovingFocus.ts
128
+ function attachRovingFocus(root, initialOptions) {
129
+ let options = initialOptions;
130
+ let current = null;
131
+ const events = new AbortController();
132
+ const { signal } = events;
133
+ function stops() {
134
+ return Array.from(root.querySelectorAll(options.items)).filter(
135
+ (item) => !item.matches(":disabled")
136
+ );
137
+ }
138
+ function stopOf(target2) {
139
+ if (!(target2 instanceof Element)) {
140
+ return null;
141
+ }
142
+ const item = target2.closest(options.items);
143
+ return item !== null && root.contains(item) ? item : null;
144
+ }
145
+ function setTabStops() {
146
+ const all = stops();
147
+ if (current !== null && !all.includes(current)) {
148
+ current = null;
149
+ }
150
+ const stop = options.tabStop === false ? null : current ?? all[0];
151
+ for (const item of all) {
152
+ item.tabIndex = item === stop ? 0 : -1;
153
+ }
154
+ }
155
+ function focusItem(item) {
156
+ current = item;
157
+ setTabStops();
158
+ item.focus();
159
+ }
160
+ function stepOf(event) {
161
+ const { orientation } = options;
162
+ const across = orientation !== "vertical";
163
+ const down = orientation !== "horizontal";
164
+ const rtl = writingDirection(root) === "rtl";
165
+ switch (event.key) {
166
+ case "ArrowLeft":
167
+ return across ? rtl ? "next" : "previous" : null;
168
+ case "ArrowRight":
169
+ return across ? rtl ? "previous" : "next" : null;
170
+ case "ArrowUp":
171
+ return down ? "previous" : null;
172
+ case "ArrowDown":
173
+ return down ? "next" : null;
174
+ case "Home":
175
+ return "first";
176
+ case "End":
177
+ return "last";
178
+ default:
179
+ return null;
180
+ }
181
+ }
182
+ function target(all, from, step) {
183
+ switch (step) {
184
+ case "first":
185
+ return all[0];
186
+ case "last":
187
+ return all.at(-1);
188
+ case "previous":
189
+ return from > 0 ? all[from - 1] : options.wrap ? all.at(-1) : void 0;
190
+ case "next":
191
+ return from < all.length - 1 ? all[from + 1] : options.wrap ? all[0] : void 0;
192
+ }
193
+ }
194
+ function onKeyDown(event) {
195
+ const item = stopOf(event.target);
196
+ if (item === null || event.defaultPrevented) {
197
+ return;
198
+ }
199
+ const modified = event.ctrlKey || event.metaKey || event.altKey;
200
+ const step = modified ? null : stepOf(event);
201
+ const all = stops();
202
+ const next = step === null ? void 0 : target(all, all.indexOf(item), step);
203
+ if (next === void 0) {
204
+ options.onKey?.(item, event);
205
+ return;
206
+ }
207
+ event.preventDefault();
208
+ if (next !== item) {
209
+ focusItem(next);
210
+ options.onMove?.(next, event);
211
+ }
212
+ }
213
+ function onFocusIn(event) {
214
+ const item = stopOf(event.target);
215
+ if (item !== null && item !== current) {
216
+ current = item;
217
+ setTabStops();
218
+ }
219
+ }
220
+ const observer = new MutationObserver(setTabStops);
221
+ observer.observe(root, { childList: true, subtree: true });
222
+ root.addEventListener("keydown", onKeyDown, { signal });
223
+ root.addEventListener("focusin", onFocusIn, { signal });
224
+ setTabStops();
225
+ return {
226
+ setOptions(next) {
227
+ options = next;
228
+ setTabStops();
229
+ },
230
+ focusItem,
231
+ detach() {
232
+ events.abort();
233
+ observer.disconnect();
234
+ current = null;
235
+ }
236
+ };
237
+ }
238
+
239
+ // src/form/attachValidityCarrier.ts
240
+ function firstErrorText(messages) {
241
+ return messages.find((message) => message.severity === "error")?.text ?? "";
242
+ }
243
+ function attachValidityCarrier(carrier, control, controller) {
244
+ let customValidity = "";
245
+ const events = new AbortController();
246
+ function render() {
247
+ const validity = firstErrorText(controller.getMessages());
248
+ if (validity !== customValidity) {
249
+ customValidity = validity;
250
+ carrier.setCustomValidity(validity);
251
+ }
252
+ }
253
+ carrier.addEventListener(
254
+ "focus",
255
+ () => {
256
+ control.focus();
257
+ },
258
+ { signal: events.signal }
259
+ );
260
+ const unsubscribe = controller.subscribe(render);
261
+ render();
262
+ let attached = true;
263
+ return {
264
+ detach() {
265
+ if (!attached) {
266
+ return;
267
+ }
268
+ attached = false;
269
+ events.abort();
270
+ unsubscribe();
271
+ carrier.setCustomValidity("");
272
+ }
273
+ };
274
+ }
275
+
111
276
  // src/form/formControls.ts
112
277
  var ENTER_MOVES_FROM = /* @__PURE__ */ new Set([
113
278
  "text",
@@ -213,6 +378,82 @@ function attachFitCount(root, { max, onCount }) {
213
378
  };
214
379
  }
215
380
 
381
+ // src/layout/attachOverflowCount.ts
382
+ function pixels2(value) {
383
+ const parsed = Number.parseFloat(value);
384
+ return Number.isNaN(parsed) ? 0 : parsed;
385
+ }
386
+ function attachOverflowCount(container, { items, reserve, onCount }) {
387
+ let count = null;
388
+ const watched = /* @__PURE__ */ new Set();
389
+ function measure() {
390
+ const style = getComputedStyle(container);
391
+ const gap = pixels2(style.columnGap);
392
+ const room = container.clientWidth - pixels2(style.paddingInlineStart) - pixels2(style.paddingInlineEnd);
393
+ const widths = Array.from(
394
+ container.querySelectorAll(items),
395
+ (item) => item.getBoundingClientRect().width
396
+ );
397
+ const all = widths.reduce(
398
+ (sum, width, index) => sum + width + (index > 0 ? gap : 0),
399
+ 0
400
+ );
401
+ let next = widths.length;
402
+ if (all > room) {
403
+ const kept = reserve === null ? null : container.querySelector(reserve);
404
+ const free = room - (kept === null ? 0 : kept.getBoundingClientRect().width + gap);
405
+ let used = 0;
406
+ next = 0;
407
+ for (const width of widths) {
408
+ const after = used + (next > 0 ? gap : 0) + width;
409
+ if (after > free) {
410
+ break;
411
+ }
412
+ used = after;
413
+ next += 1;
414
+ }
415
+ }
416
+ if (next !== count) {
417
+ count = next;
418
+ onCount(next);
419
+ }
420
+ }
421
+ const sizes = new ResizeObserver(measure);
422
+ function watch() {
423
+ const now = /* @__PURE__ */ new Set([
424
+ container,
425
+ ...container.querySelectorAll(items),
426
+ ...reserve === null ? [] : container.querySelectorAll(reserve)
427
+ ]);
428
+ for (const element of watched) {
429
+ if (!now.has(element)) {
430
+ sizes.unobserve(element);
431
+ watched.delete(element);
432
+ }
433
+ }
434
+ for (const element of now) {
435
+ if (!watched.has(element)) {
436
+ sizes.observe(element);
437
+ watched.add(element);
438
+ }
439
+ }
440
+ }
441
+ const changes = new MutationObserver(() => {
442
+ watch();
443
+ measure();
444
+ });
445
+ changes.observe(container, { childList: true, subtree: true });
446
+ watch();
447
+ measure();
448
+ return {
449
+ detach: () => {
450
+ sizes.disconnect();
451
+ changes.disconnect();
452
+ watched.clear();
453
+ }
454
+ };
455
+ }
456
+
216
457
  // src/listbox/attachChecklist.ts
217
458
  var ITEM = "[data-gridkit-checklist-item]";
218
459
  var INDEX = "data-gridkit-checklist-index";
@@ -376,6 +617,755 @@ function attachChecklist(list, options) {
376
617
  };
377
618
  }
378
619
 
620
+ // src/listbox/attachCombobox.ts
621
+ var OPEN_KEYS = /* @__PURE__ */ new Set(["ArrowDown", "ArrowUp", "F4"]);
622
+ var SELECT_ONLY_OPEN_KEYS = /* @__PURE__ */ new Set(["Enter", " ", "Home", "End"]);
623
+ function hasFocus(control) {
624
+ const root = control.getRootNode();
625
+ return (root instanceof Document || root instanceof ShadowRoot) && root.activeElement === control;
626
+ }
627
+ function isPrintable(event) {
628
+ return !event.ctrlKey && !event.metaKey && !event.altKey && Array.from(event.key).length === 1;
629
+ }
630
+ function caretAtStart(control) {
631
+ return (control instanceof HTMLInputElement || control instanceof HTMLTextAreaElement) && control.selectionStart === 0 && control.selectionEnd === 0;
632
+ }
633
+ function textOf(control) {
634
+ return control instanceof HTMLInputElement || control instanceof HTMLTextAreaElement ? control.value : "";
635
+ }
636
+ function attachCombobox(control, controller, initialOptions) {
637
+ let options = initialOptions;
638
+ let attached = true;
639
+ let pendingKeys = [];
640
+ let unsubscribe = null;
641
+ let panelEvents = null;
642
+ const events = new AbortController();
643
+ const { signal } = events;
644
+ const isOpen = () => options.listbox !== null;
645
+ const drivesField = () => options.drivesField ?? !options.editable;
646
+ function writeAria() {
647
+ if (!attached) {
648
+ return;
649
+ }
650
+ const { listbox } = options;
651
+ control.setAttribute("aria-expanded", String(listbox !== null));
652
+ const listId = listbox?.element.id ?? "";
653
+ if (listId === "") {
654
+ control.removeAttribute("aria-controls");
655
+ } else {
656
+ control.setAttribute("aria-controls", listId);
657
+ }
658
+ const activeId = listbox?.activeId() ?? null;
659
+ if (activeId === null) {
660
+ control.removeAttribute("aria-activedescendant");
661
+ } else {
662
+ control.setAttribute("aria-activedescendant", activeId);
663
+ }
664
+ }
665
+ function connect(next, previous) {
666
+ if (next.listbox !== previous?.listbox) {
667
+ unsubscribe?.();
668
+ unsubscribe = next.listbox?.subscribe(writeAria) ?? null;
669
+ }
670
+ if (next.panel !== previous?.panel) {
671
+ panelEvents?.abort();
672
+ panelEvents = null;
673
+ if (next.panel !== null) {
674
+ panelEvents = new AbortController();
675
+ next.panel.addEventListener(
676
+ "mousedown",
677
+ (event) => {
678
+ event.preventDefault();
679
+ },
680
+ { signal: panelEvents.signal }
681
+ );
682
+ }
683
+ }
684
+ }
685
+ function requestOpen(reason, event) {
686
+ if (!options.readOnly) {
687
+ options.onOpenRequest(reason, event);
688
+ }
689
+ }
690
+ function forwardPendingKeys() {
691
+ const { listbox } = options;
692
+ if (listbox === null) {
693
+ return;
694
+ }
695
+ const keys = pendingKeys;
696
+ pendingKeys = [];
697
+ for (const key of keys) {
698
+ listbox.handleKey(key);
699
+ }
700
+ }
701
+ function onChipKey(event) {
702
+ const { onChipKey: handler } = options;
703
+ if (handler === void 0 || event.ctrlKey || event.metaKey || event.altKey) {
704
+ return false;
705
+ }
706
+ const previousKey = writingDirection(control) === "rtl" ? "ArrowRight" : "ArrowLeft";
707
+ if (event.key === previousKey && !event.shiftKey && (!options.editable || caretAtStart(control))) {
708
+ handler("previous", event);
709
+ return true;
710
+ }
711
+ if (event.key === "Backspace" && textOf(control) === "") {
712
+ handler("remove-last", event);
713
+ return true;
714
+ }
715
+ return false;
716
+ }
717
+ function onClosedKey(event) {
718
+ const { editable } = options;
719
+ if (event.key === "Escape") {
720
+ return drivesField() && controller.revert(event);
721
+ }
722
+ if (event.ctrlKey || event.metaKey) {
723
+ return false;
724
+ }
725
+ if (OPEN_KEYS.has(event.key) && (!event.altKey || event.key === "ArrowDown") || !editable && !event.altKey && SELECT_ONLY_OPEN_KEYS.has(event.key)) {
726
+ requestOpen("keyboard", event);
727
+ return true;
728
+ }
729
+ if (editable && drivesField() && event.key === "Enter" && !event.altKey && !event.shiftKey) {
730
+ controller.commit("enter", event);
731
+ return true;
732
+ }
733
+ if (!editable && isPrintable(event) && !options.readOnly) {
734
+ pendingKeys.push(event);
735
+ requestOpen("typing", event);
736
+ forwardPendingKeys();
737
+ return true;
738
+ }
739
+ return onChipKey(event);
740
+ }
741
+ function textBoxKeeps(event) {
742
+ return event.key === "Home" || event.key === "End" || event.key === " " || isPrintable(event) || (event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "a" || event.shiftKey && (event.key === "ArrowDown" || event.key === "ArrowUp");
743
+ }
744
+ function onOpenKey(event, listbox) {
745
+ switch (event.key) {
746
+ case "Escape":
747
+ options.onCloseRequest("escape", event);
748
+ return true;
749
+ case "F4":
750
+ options.onCloseRequest("button", event);
751
+ return true;
752
+ }
753
+ if (event.altKey) {
754
+ if (event.key === "ArrowUp") {
755
+ options.onCloseRequest("button", event);
756
+ return true;
757
+ }
758
+ return false;
759
+ }
760
+ if (options.editable && textBoxKeeps(event)) {
761
+ if (event.key === "Home" || event.key === "End") {
762
+ listbox.activate(null, "keyboard");
763
+ }
764
+ return false;
765
+ }
766
+ if (listbox.handleKey(event)) {
767
+ return true;
768
+ }
769
+ return onChipKey(event);
770
+ }
771
+ function onTab(event) {
772
+ if (event.ctrlKey || event.altKey || event.metaKey) {
773
+ return;
774
+ }
775
+ const { listbox } = options;
776
+ if (listbox !== null && options.tabChooses && !options.multiple && !event.shiftKey) {
777
+ listbox.chooseActive(event);
778
+ }
779
+ if (isOpen()) {
780
+ options.onCloseRequest("blur", event);
781
+ }
782
+ if (drivesField() && controller.commit("tab", event).kind === "refused") {
783
+ event.preventDefault();
784
+ }
785
+ }
786
+ function onKeyDown(event) {
787
+ if (event.defaultPrevented || event.isComposing) {
788
+ return;
789
+ }
790
+ if (event.key === "Tab") {
791
+ onTab(event);
792
+ return;
793
+ }
794
+ const { listbox } = options;
795
+ const taken = listbox === null ? onClosedKey(event) : onOpenKey(event, listbox);
796
+ if (taken) {
797
+ event.preventDefault();
798
+ }
799
+ }
800
+ function onInput(event) {
801
+ if (options.editable && !isOpen()) {
802
+ requestOpen("typing", event);
803
+ }
804
+ }
805
+ function onClick(event) {
806
+ if (!isOpen()) {
807
+ requestOpen("button", event);
808
+ } else if (!options.editable) {
809
+ options.onCloseRequest("button", event);
810
+ }
811
+ }
812
+ function onFocus(event) {
813
+ if (drivesField()) {
814
+ controller.focus(event);
815
+ }
816
+ }
817
+ function onBlur(event) {
818
+ pendingKeys = [];
819
+ if (isOpen()) {
820
+ options.onCloseRequest("blur", event);
821
+ }
822
+ const to = event.relatedTarget;
823
+ const staying = to instanceof Element && (options.isInside?.(to) ?? false);
824
+ if (drivesField() && !staying) {
825
+ controller.blur(event);
826
+ }
827
+ }
828
+ control.addEventListener("keydown", onKeyDown, { signal, capture: true });
829
+ control.addEventListener("input", onInput, { signal });
830
+ control.addEventListener("click", onClick, { signal });
831
+ control.addEventListener("focus", onFocus, { signal });
832
+ control.addEventListener("blur", onBlur, { signal });
833
+ connect(options, null);
834
+ writeAria();
835
+ if (drivesField() && hasFocus(control)) {
836
+ controller.focus(null);
837
+ }
838
+ return {
839
+ setOptions(next) {
840
+ if (!attached) {
841
+ return;
842
+ }
843
+ const previous = options;
844
+ options = next;
845
+ connect(next, previous);
846
+ if (previous.listbox !== null && next.listbox === null) {
847
+ pendingKeys = [];
848
+ }
849
+ writeAria();
850
+ forwardPendingKeys();
851
+ },
852
+ detach() {
853
+ if (!attached) {
854
+ return;
855
+ }
856
+ attached = false;
857
+ events.abort();
858
+ panelEvents?.abort();
859
+ unsubscribe?.();
860
+ pendingKeys = [];
861
+ control.removeAttribute("aria-controls");
862
+ control.removeAttribute("aria-activedescendant");
863
+ }
864
+ };
865
+ }
866
+
867
+ // src/listbox/attachListbox.ts
868
+ import {
869
+ isLandingRow,
870
+ moveActive,
871
+ typeaheadRow
872
+ } from "@gridkitjs/forms-core";
873
+ var ROW_ATTRIBUTE = "data-gridkit-forms-row";
874
+ var ROW = `[${ROW_ATTRIBUTE}]`;
875
+ var TYPEAHEAD_RESET = 500;
876
+ var NEAR_END = 5;
877
+ var INACTIVE = { kind: "inactive" };
878
+ function rowIndex(row) {
879
+ return Number(row.getAttribute(ROW_ATTRIBUTE));
880
+ }
881
+ function nearestLanding(rows, from, disabledOptions) {
882
+ for (let index = Math.max(from, 0); index < rows.length; index++) {
883
+ const row = rows[index];
884
+ if (row !== void 0 && isLandingRow(row, disabledOptions)) {
885
+ return index;
886
+ }
887
+ }
888
+ for (let index = Math.min(from, rows.length) - 1; index >= 0; index--) {
889
+ const row = rows[index];
890
+ if (row !== void 0 && isLandingRow(row, disabledOptions)) {
891
+ return index;
892
+ }
893
+ }
894
+ return null;
895
+ }
896
+ function isPrintable2(event) {
897
+ return !event.ctrlKey && !event.metaKey && !event.altKey && Array.from(event.key).length === 1;
898
+ }
899
+ function attachListbox(list, initialOptions) {
900
+ let options = initialOptions;
901
+ let rows = [];
902
+ let state = INACTIVE;
903
+ let anchorKey = null;
904
+ let typed = "";
905
+ let typeaheadTimer;
906
+ let nearEndFor = -1;
907
+ let pointerPressed = false;
908
+ const listeners = /* @__PURE__ */ new Set();
909
+ const events = new AbortController();
910
+ const { signal } = events;
911
+ const disabledOptions = () => options.disabledOptions ?? "skip";
912
+ const roving = () => options.focus === "roving";
913
+ function activeIndex() {
914
+ return state.kind === "active" || state.kind === "awaiting" ? state.index : null;
915
+ }
916
+ function rowElement(index) {
917
+ return list.querySelector(
918
+ `[${ROW_ATTRIBUTE}="${String(index)}"]`
919
+ );
920
+ }
921
+ function rowOf(target) {
922
+ const row = target instanceof Element ? target.closest(ROW) : null;
923
+ return row !== null && list.contains(row) ? row : null;
924
+ }
925
+ function notify() {
926
+ for (const listener of [...listeners]) {
927
+ listener();
928
+ }
929
+ }
930
+ function setTabStop() {
931
+ if (!roving()) {
932
+ return;
933
+ }
934
+ const mounted = Array.from(list.querySelectorAll(ROW));
935
+ const active = activeIndex();
936
+ const stop = mounted.find((row) => rowIndex(row) === active) ?? mounted.find((row) => {
937
+ const described = rows[rowIndex(row)];
938
+ return described !== void 0 && isLandingRow(described, disabledOptions());
939
+ });
940
+ for (const row of mounted) {
941
+ row.tabIndex = row === stop ? 0 : -1;
942
+ }
943
+ }
944
+ function checkNearEnd() {
945
+ if (options.onNearEnd === void 0 || nearEndFor === rows.length) {
946
+ return;
947
+ }
948
+ const threshold = rows.length - NEAR_END;
949
+ const active = activeIndex();
950
+ const last = Array.from(list.querySelectorAll(ROW)).at(-1);
951
+ if (active !== null && active >= threshold || last !== void 0 && rowIndex(last) >= threshold) {
952
+ nearEndFor = rows.length;
953
+ options.onNearEnd();
954
+ }
955
+ }
956
+ function focusActiveRow() {
957
+ if (!roving() || state.kind !== "active" && state.kind !== "awaiting") {
958
+ return;
959
+ }
960
+ const row = rowElement(state.index);
961
+ if (row === null) {
962
+ state = { kind: "awaiting", index: state.index };
963
+ } else {
964
+ state = { kind: "active", index: state.index };
965
+ row.focus({ preventScroll: true });
966
+ }
967
+ }
968
+ function setActive(index, by, align) {
969
+ if (state.kind === "detached") {
970
+ return;
971
+ }
972
+ const previous = activeIndex();
973
+ if (index === null || rows[index] === void 0) {
974
+ state = INACTIVE;
975
+ } else {
976
+ state = { kind: "active", index };
977
+ if (align !== null) {
978
+ options.scrollToIndex(index, align);
979
+ }
980
+ focusActiveRow();
981
+ }
982
+ const next = activeIndex();
983
+ if (next !== previous) {
984
+ options.onActiveChange(next, by);
985
+ }
986
+ setTabStop();
987
+ notify();
988
+ checkNearEnd();
989
+ }
990
+ function anchorFor(fallback) {
991
+ const index = anchorKey === null ? -1 : rows.findIndex((row) => row.key === anchorKey);
992
+ if (index !== -1) {
993
+ return index;
994
+ }
995
+ anchorKey = rows[fallback]?.key ?? null;
996
+ return fallback;
997
+ }
998
+ function choose(index, choice, event) {
999
+ const row = rows[index];
1000
+ if (row === void 0 || row.kind === "header") {
1001
+ return false;
1002
+ }
1003
+ if (choice.kind !== "all" && row.disabled) {
1004
+ return false;
1005
+ }
1006
+ if (choice.kind === "one" || choice.kind === "toggle") {
1007
+ anchorKey = row.key;
1008
+ }
1009
+ options.onChoose(index, choice, event);
1010
+ return true;
1011
+ }
1012
+ function chooseAlone(index, event) {
1013
+ choose(index, { kind: options.multiple ? "toggle" : "one" }, event);
1014
+ }
1015
+ function pageSize() {
1016
+ const top = list.getBoundingClientRect().top + list.clientTop;
1017
+ const bottom = top + list.clientHeight;
1018
+ const whole = Array.from(list.querySelectorAll(ROW)).filter((row) => {
1019
+ const box = row.getBoundingClientRect();
1020
+ return box.top >= top && box.bottom <= bottom;
1021
+ }).length;
1022
+ return Math.max(1, whole);
1023
+ }
1024
+ function move(step, event) {
1025
+ const from = activeIndex();
1026
+ const relative = step.kind !== "first" && step.kind !== "last";
1027
+ if (relative && from !== null && !roving() && rowElement(from) === null) {
1028
+ options.scrollToIndex(from, "nearest");
1029
+ return true;
1030
+ }
1031
+ const to = moveActive(rows, from, step, disabledOptions());
1032
+ if (to === null) {
1033
+ return true;
1034
+ }
1035
+ const ranging = event.shiftKey && options.multiple;
1036
+ const anchor = ranging ? anchorFor(from ?? to) : null;
1037
+ setActive(to, "keyboard", "nearest");
1038
+ if (to === from) {
1039
+ return true;
1040
+ }
1041
+ if (anchor !== null) {
1042
+ choose(to, { kind: "range", anchor }, event);
1043
+ } else if (options.selectionFollowsFocus === true && !options.multiple) {
1044
+ choose(to, { kind: "one" }, event);
1045
+ }
1046
+ return true;
1047
+ }
1048
+ function typeahead(event) {
1049
+ if (!options.typeahead || !isPrintable2(event)) {
1050
+ return false;
1051
+ }
1052
+ if (event.key === " " && typed === "") {
1053
+ return false;
1054
+ }
1055
+ typed += event.key;
1056
+ clearTimeout(typeaheadTimer);
1057
+ typeaheadTimer = setTimeout(() => {
1058
+ typed = "";
1059
+ }, TYPEAHEAD_RESET);
1060
+ const from = activeIndex();
1061
+ const to = typeaheadRow(
1062
+ rows,
1063
+ from,
1064
+ typed,
1065
+ options.locale,
1066
+ disabledOptions()
1067
+ );
1068
+ if (to !== null && to !== from) {
1069
+ setActive(to, "keyboard", "nearest");
1070
+ if (options.selectionFollowsFocus === true && !options.multiple) {
1071
+ choose(to, { kind: "one" }, event);
1072
+ }
1073
+ }
1074
+ return true;
1075
+ }
1076
+ function space(event) {
1077
+ const active = activeIndex();
1078
+ if (active === null) {
1079
+ return false;
1080
+ }
1081
+ if (event.repeat) {
1082
+ return true;
1083
+ }
1084
+ if (!roving()) {
1085
+ chooseAlone(active, event);
1086
+ } else if (options.multiple && event.shiftKey) {
1087
+ choose(active, { kind: "range", anchor: anchorFor(active) }, event);
1088
+ } else {
1089
+ chooseAlone(active, event);
1090
+ }
1091
+ return true;
1092
+ }
1093
+ function onKey(event) {
1094
+ if (state.kind === "detached" || event.isComposing || event.altKey) {
1095
+ return false;
1096
+ }
1097
+ const modified = event.ctrlKey || event.metaKey;
1098
+ if (typeahead(event)) {
1099
+ return true;
1100
+ }
1101
+ switch (event.key) {
1102
+ case "ArrowDown":
1103
+ return !modified && move({ kind: "next" }, event);
1104
+ case "ArrowUp":
1105
+ return !modified && move({ kind: "previous" }, event);
1106
+ case "PageDown":
1107
+ return !modified && move({ kind: "pageDown", rows: pageSize() }, event);
1108
+ case "PageUp":
1109
+ return !modified && move({ kind: "pageUp", rows: pageSize() }, event);
1110
+ // Ctrl+Shift+Home and End range as Shift+Home and End do.
1111
+ case "Home":
1112
+ return (!modified || event.shiftKey) && move({ kind: "first" }, event);
1113
+ case "End":
1114
+ return (!modified || event.shiftKey) && move({ kind: "last" }, event);
1115
+ case "Enter": {
1116
+ const active = activeIndex();
1117
+ if (roving() || active === null) {
1118
+ return false;
1119
+ }
1120
+ chooseAlone(active, event);
1121
+ return true;
1122
+ }
1123
+ case " ":
1124
+ return !modified && space(event);
1125
+ case "a":
1126
+ case "A": {
1127
+ if (!modified || !options.multiple) {
1128
+ return false;
1129
+ }
1130
+ const target = activeIndex() ?? nearestLanding(rows, 0, disabledOptions());
1131
+ return target !== null && choose(target, { kind: "all" }, event);
1132
+ }
1133
+ default:
1134
+ return false;
1135
+ }
1136
+ }
1137
+ function onKeyDown(event) {
1138
+ if (roving() && onKey(event)) {
1139
+ event.preventDefault();
1140
+ }
1141
+ }
1142
+ function onFocusIn(event) {
1143
+ const row = rowOf(event.target);
1144
+ if (!roving() || row === null || state.kind === "detached") {
1145
+ return;
1146
+ }
1147
+ const index = rowIndex(row);
1148
+ const previous = activeIndex();
1149
+ state = { kind: "active", index };
1150
+ if (index !== previous) {
1151
+ options.onActiveChange(index, pointerPressed ? "pointer" : "keyboard");
1152
+ }
1153
+ setTabStop();
1154
+ notify();
1155
+ checkNearEnd();
1156
+ }
1157
+ function onPointerMove(event) {
1158
+ if (roving() || event.pointerType !== "mouse") {
1159
+ return;
1160
+ }
1161
+ const row = rowOf(event.target);
1162
+ if (row === null) {
1163
+ return;
1164
+ }
1165
+ const index = rowIndex(row);
1166
+ const described = rows[index];
1167
+ if (index !== activeIndex() && described !== void 0 && isLandingRow(described, disabledOptions())) {
1168
+ setActive(index, "pointer", null);
1169
+ }
1170
+ }
1171
+ function onClick(event) {
1172
+ const row = rowOf(event.target);
1173
+ const index = row === null ? -1 : rowIndex(row);
1174
+ const described = rows[index];
1175
+ if (row === null || state.kind === "detached" || described === void 0 || !isLandingRow(described, disabledOptions())) {
1176
+ return;
1177
+ }
1178
+ setActive(index, "pointer", null);
1179
+ if (roving() && options.multiple && event.shiftKey) {
1180
+ choose(index, { kind: "range", anchor: anchorFor(index) }, event);
1181
+ } else if (roving() && (event.ctrlKey || event.metaKey)) {
1182
+ choose(index, { kind: "toggle" }, event);
1183
+ } else {
1184
+ chooseAlone(index, event);
1185
+ }
1186
+ }
1187
+ const observer = new MutationObserver(() => {
1188
+ setTabStop();
1189
+ if (state.kind === "awaiting") {
1190
+ const row = rowElement(state.index);
1191
+ const focused = document.activeElement;
1192
+ if (row !== null) {
1193
+ state = { kind: "active", index: state.index };
1194
+ if (focused === null || focused === document.body || list.contains(focused)) {
1195
+ row.focus({ preventScroll: true });
1196
+ }
1197
+ }
1198
+ }
1199
+ notify();
1200
+ checkNearEnd();
1201
+ });
1202
+ observer.observe(list, {
1203
+ childList: true,
1204
+ subtree: true,
1205
+ attributeFilter: [ROW_ATTRIBUTE]
1206
+ });
1207
+ list.tabIndex = -1;
1208
+ list.addEventListener("keydown", onKeyDown, { signal });
1209
+ list.addEventListener("focusin", onFocusIn, { signal });
1210
+ list.addEventListener("pointermove", onPointerMove, { signal });
1211
+ list.addEventListener("click", onClick, { signal });
1212
+ list.addEventListener(
1213
+ "pointerdown",
1214
+ () => {
1215
+ pointerPressed = true;
1216
+ },
1217
+ { signal }
1218
+ );
1219
+ list.ownerDocument.addEventListener(
1220
+ "pointerup",
1221
+ () => {
1222
+ pointerPressed = false;
1223
+ },
1224
+ { signal, capture: true }
1225
+ );
1226
+ return {
1227
+ element: list,
1228
+ setRows(next) {
1229
+ if (state.kind === "detached") {
1230
+ return;
1231
+ }
1232
+ const previous = activeIndex();
1233
+ const previousKey = previous === null ? void 0 : rows[previous]?.key;
1234
+ rows = next;
1235
+ if (state.kind === "active" || state.kind === "awaiting") {
1236
+ const kept = previousKey === void 0 ? -1 : next.findIndex((row) => row.key === previousKey);
1237
+ const index = kept !== -1 ? kept : nearestLanding(
1238
+ next,
1239
+ Math.min(state.index, next.length - 1),
1240
+ disabledOptions()
1241
+ );
1242
+ state = index === null ? INACTIVE : { kind: state.kind, index };
1243
+ if (index !== previous) {
1244
+ options.onActiveChange(index, "list");
1245
+ }
1246
+ }
1247
+ setTabStop();
1248
+ notify();
1249
+ checkNearEnd();
1250
+ },
1251
+ setOptions(next) {
1252
+ options = next;
1253
+ setTabStop();
1254
+ },
1255
+ activate(index, by) {
1256
+ setActive(
1257
+ index,
1258
+ by,
1259
+ by === "pointer" ? null : by === "open" ? "center" : "nearest"
1260
+ );
1261
+ },
1262
+ handleKey(event) {
1263
+ if (roving() || !onKey(event)) {
1264
+ return false;
1265
+ }
1266
+ event.preventDefault();
1267
+ return true;
1268
+ },
1269
+ chooseActive(nativeEvent) {
1270
+ const active = activeIndex();
1271
+ return state.kind !== "detached" && active !== null && choose(active, { kind: "one" }, nativeEvent);
1272
+ },
1273
+ getActive: activeIndex,
1274
+ activeId() {
1275
+ const active = activeIndex();
1276
+ const row = active === null ? null : rowElement(active);
1277
+ return row === null || row.id === "" ? null : row.id;
1278
+ },
1279
+ subscribe(listener) {
1280
+ listeners.add(listener);
1281
+ return () => {
1282
+ listeners.delete(listener);
1283
+ };
1284
+ },
1285
+ detach() {
1286
+ if (state.kind === "detached") {
1287
+ return;
1288
+ }
1289
+ state = { kind: "detached" };
1290
+ observer.disconnect();
1291
+ events.abort();
1292
+ clearTimeout(typeaheadTimer);
1293
+ listeners.clear();
1294
+ }
1295
+ };
1296
+ }
1297
+
1298
+ // src/scheduleTimeout.ts
1299
+ var scheduleTimeout = (callback, delay) => {
1300
+ const timer = setTimeout(callback, delay);
1301
+ return () => {
1302
+ clearTimeout(timer);
1303
+ };
1304
+ };
1305
+
1306
+ // src/listbox/choiceAnnouncer.ts
1307
+ var DEFAULT_SETTLE_DELAY = 500;
1308
+ function createChoiceAnnouncer(container, initialOptions) {
1309
+ let options = initialOptions;
1310
+ const region = createLiveAnnouncer(container, {
1311
+ className: options.className
1312
+ });
1313
+ let state = { kind: "quiet", last: null };
1314
+ function settle(last) {
1315
+ if (state.kind === "settling") {
1316
+ state.cancel();
1317
+ }
1318
+ state = { kind: "quiet", last };
1319
+ }
1320
+ return {
1321
+ results(query, count) {
1322
+ if (state.kind === "destroyed") {
1323
+ return;
1324
+ }
1325
+ const key = JSON.stringify([query, count]);
1326
+ const { last } = state;
1327
+ settle(last);
1328
+ if (key === last) {
1329
+ return;
1330
+ }
1331
+ const cancel = scheduleTimeout(() => {
1332
+ state = { kind: "quiet", last: key };
1333
+ const { messages } = options;
1334
+ region.announce(
1335
+ count === 0 ? messages.noMatches(query) : messages.resultCount(count)
1336
+ );
1337
+ }, options.settleDelay ?? DEFAULT_SETTLE_DELAY);
1338
+ state = { kind: "settling", last, cancel };
1339
+ },
1340
+ selected(count) {
1341
+ if (state.kind !== "destroyed") {
1342
+ region.announce(options.messages.selectedCount(count));
1343
+ }
1344
+ },
1345
+ announce(text) {
1346
+ if (state.kind !== "destroyed") {
1347
+ region.announce(text);
1348
+ }
1349
+ },
1350
+ cancel() {
1351
+ if (state.kind !== "destroyed") {
1352
+ settle(null);
1353
+ }
1354
+ },
1355
+ setOptions(next) {
1356
+ options = next;
1357
+ },
1358
+ destroy() {
1359
+ if (state.kind === "destroyed") {
1360
+ return;
1361
+ }
1362
+ settle(null);
1363
+ state = { kind: "destroyed" };
1364
+ region.destroy();
1365
+ }
1366
+ };
1367
+ }
1368
+
379
1369
  // src/overlay/overlayStack.ts
380
1370
  var STACK_KEY = /* @__PURE__ */ Symbol.for("@gridkitjs/overlay-stack");
381
1371
  function createStack() {
@@ -522,11 +1512,6 @@ function placeLeftToRight(anchor, panel, viewport, options) {
522
1512
  };
523
1513
  }
524
1514
 
525
- // src/writingDirection.ts
526
- function writingDirection(element) {
527
- return getComputedStyle(element).direction === "rtl" ? "rtl" : "ltr";
528
- }
529
-
530
1515
  // src/overlay/attachOverlay.ts
531
1516
  var FOCUSABLE = "a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]";
532
1517
  function tabbables(panel) {
@@ -849,14 +1834,6 @@ function createRowVirtualizer(scrollElement, options) {
849
1834
  };
850
1835
  }
851
1836
 
852
- // src/scheduleTimeout.ts
853
- var scheduleTimeout = (callback, delay) => {
854
- const timer = setTimeout(callback, delay);
855
- return () => {
856
- clearTimeout(timer);
857
- };
858
- };
859
-
860
1837
  // src/step/attachHoldRepeat.ts
861
1838
  var IDLE = { kind: "idle" };
862
1839
  var DETACHED2 = { kind: "detached" };
@@ -980,7 +1957,7 @@ function keyStep(key, edges) {
980
1957
  return null;
981
1958
  }
982
1959
  }
983
- function hasFocus(control) {
1960
+ function hasFocus2(control) {
984
1961
  const root = control.getRootNode();
985
1962
  return (root instanceof Document || root instanceof ShadowRoot) && root.activeElement === control;
986
1963
  }
@@ -1008,7 +1985,7 @@ function attachStepKeys(control, initialOptions) {
1008
1985
  control.addEventListener(
1009
1986
  "wheel",
1010
1987
  (event) => {
1011
- if (options.wheel !== true || !hasFocus(control)) {
1988
+ if (options.wheel !== true || !hasFocus2(control)) {
1012
1989
  wheelTravel = 0;
1013
1990
  return;
1014
1991
  }
@@ -1041,7 +2018,7 @@ function attachStepKeys(control, initialOptions) {
1041
2018
  }
1042
2019
 
1043
2020
  // src/text/attachAutoGrow.ts
1044
- function pixels2(value) {
2021
+ function pixels3(value) {
1045
2022
  const parsed = Number.parseFloat(value);
1046
2023
  return Number.isNaN(parsed) ? 0 : parsed;
1047
2024
  }
@@ -1057,9 +2034,9 @@ function attachAutoGrow(textarea, initialOptions) {
1057
2034
  return;
1058
2035
  }
1059
2036
  const style = getComputedStyle(textarea);
1060
- const lineHeight = style.lineHeight === "normal" ? pixels2(style.fontSize) * NORMAL_LINE_HEIGHT : pixels2(style.lineHeight);
1061
- const padding = pixels2(style.paddingTop) + pixels2(style.paddingBottom);
1062
- const border = pixels2(style.borderTopWidth) + pixels2(style.borderBottomWidth);
2037
+ const lineHeight = style.lineHeight === "normal" ? pixels3(style.fontSize) * NORMAL_LINE_HEIGHT : pixels3(style.lineHeight);
2038
+ const padding = pixels3(style.paddingTop) + pixels3(style.paddingBottom);
2039
+ const border = pixels3(style.borderTopWidth) + pixels3(style.borderBottomWidth);
1063
2040
  const min = Math.max(1, options.minRows) * lineHeight;
1064
2041
  const max = Math.max(options.minRows, options.maxRows) * lineHeight;
1065
2042
  textarea.style.height = "0px";
@@ -1196,7 +2173,7 @@ function sourceOf(inputType) {
1196
2173
  }
1197
2174
  return inputType.startsWith("history") ? "undo" : "typing";
1198
2175
  }
1199
- function hasFocus2(control) {
2176
+ function hasFocus3(control) {
1200
2177
  const root = control.getRootNode();
1201
2178
  return (root instanceof Document || root instanceof ShadowRoot) && root.activeElement === control;
1202
2179
  }
@@ -1211,9 +2188,6 @@ function snapshot(control) {
1211
2188
  function caretFromEnd(previousText, caret, nextText) {
1212
2189
  return Math.max(0, nextText.length - (previousText.length - caret));
1213
2190
  }
1214
- function errorText(messages) {
1215
- return messages.find((message) => message.severity === "error")?.text ?? "";
1216
- }
1217
2191
  function attachTextInput(control, controller, initialOptions = {}) {
1218
2192
  let options = initialOptions;
1219
2193
  let state = READY2;
@@ -1244,7 +2218,7 @@ function attachTextInput(control, controller, initialOptions = {}) {
1244
2218
  };
1245
2219
  }
1246
2220
  function writeText(text) {
1247
- if (!hasFocus2(control)) {
2221
+ if (!hasFocus3(control)) {
1248
2222
  control.value = text;
1249
2223
  return;
1250
2224
  }
@@ -1280,7 +2254,7 @@ function attachTextInput(control, controller, initialOptions = {}) {
1280
2254
  if (control.value !== text) {
1281
2255
  writeText(text);
1282
2256
  }
1283
- const validity = errorText(controller.getMessages());
2257
+ const validity = firstErrorText(controller.getMessages());
1284
2258
  if (validity !== customValidity) {
1285
2259
  customValidity = validity;
1286
2260
  control.setCustomValidity(validity);
@@ -1537,7 +2511,7 @@ function attachTextInput(control, controller, initialOptions = {}) {
1537
2511
  function limitCaret() {
1538
2512
  const { lastCaret } = options;
1539
2513
  const { selectionStart, selectionEnd } = control;
1540
- if (lastCaret === void 0 || selectionStart === null || selectionStart !== selectionEnd || !hasFocus2(control)) {
2514
+ if (lastCaret === void 0 || selectionStart === null || selectionStart !== selectionEnd || !hasFocus3(control)) {
1541
2515
  return;
1542
2516
  }
1543
2517
  const last = lastCaret(control.value);
@@ -1557,7 +2531,7 @@ function attachTextInput(control, controller, initialOptions = {}) {
1557
2531
  element.addEventListener(
1558
2532
  "pointerdown",
1559
2533
  () => {
1560
- pointerFocus = !hasFocus2(control);
2534
+ pointerFocus = !hasFocus3(control);
1561
2535
  },
1562
2536
  { signal }
1563
2537
  );
@@ -1585,7 +2559,7 @@ function attachTextInput(control, controller, initialOptions = {}) {
1585
2559
  element.addEventListener("keydown", onKeyDown, { signal });
1586
2560
  control.ownerDocument.addEventListener("reset", onReset, { signal });
1587
2561
  const unsubscribe = controller.subscribe(render);
1588
- if (hasFocus2(control) && !inGroup()) {
2562
+ if (hasFocus3(control) && !inGroup()) {
1589
2563
  controller.focus(null);
1590
2564
  }
1591
2565
  render();
@@ -1593,6 +2567,7 @@ function attachTextInput(control, controller, initialOptions = {}) {
1593
2567
  setOptions: (next) => {
1594
2568
  options = next;
1595
2569
  },
2570
+ refresh: render,
1596
2571
  detach: () => {
1597
2572
  if (state.kind === "detached") {
1598
2573
  return;
@@ -1606,16 +2581,11 @@ function attachTextInput(control, controller, initialOptions = {}) {
1606
2581
  }
1607
2582
 
1608
2583
  // src/toggle/attachToggle.ts
1609
- import {
1610
- nextCheckedValue
1611
- } from "@gridkitjs/forms-core";
1612
- function hasFocus3(control) {
2584
+ import { nextCheckedValue } from "@gridkitjs/forms-core";
2585
+ function hasFocus4(control) {
1613
2586
  const root = control.getRootNode();
1614
2587
  return (root instanceof Document || root instanceof ShadowRoot) && root.activeElement === control;
1615
2588
  }
1616
- function errorText2(messages) {
1617
- return messages.find((message) => message.severity === "error")?.text ?? "";
1618
- }
1619
2589
  function attachToggle(control, controller, initialOptions = {}) {
1620
2590
  let options = initialOptions;
1621
2591
  let attached = true;
@@ -1630,7 +2600,7 @@ function attachToggle(control, controller, initialOptions = {}) {
1630
2600
  const { value } = controller.getState();
1631
2601
  control.checked = value === true;
1632
2602
  control.indeterminate = value === null;
1633
- const validity = errorText2(controller.getMessages());
2603
+ const validity = firstErrorText(controller.getMessages());
1634
2604
  if (validity !== customValidity) {
1635
2605
  customValidity = validity;
1636
2606
  control.setCustomValidity(validity);
@@ -1686,7 +2656,7 @@ function attachToggle(control, controller, initialOptions = {}) {
1686
2656
  );
1687
2657
  control.ownerDocument.addEventListener("reset", onReset, { signal });
1688
2658
  const unsubscribe = controller.subscribe(render);
1689
- if (hasFocus3(control)) {
2659
+ if (hasFocus4(control)) {
1690
2660
  controller.focus(null);
1691
2661
  }
1692
2662
  render();
@@ -2303,18 +3273,25 @@ function attachSegments(group, controller, initialOptions) {
2303
3273
  };
2304
3274
  }
2305
3275
  export {
3276
+ abortSignals,
2306
3277
  attachAutoGrow,
2307
3278
  attachChecklist,
3279
+ attachCombobox,
2308
3280
  attachEnterNavigation,
2309
3281
  attachFitCount,
2310
3282
  attachFocusGroup,
2311
3283
  attachHoldRepeat,
3284
+ attachListbox,
3285
+ attachOverflowCount,
2312
3286
  attachOverlay,
3287
+ attachRovingFocus,
2313
3288
  attachSegments,
2314
3289
  attachStepKeys,
2315
3290
  attachTextInput,
2316
3291
  attachToggle,
3292
+ attachValidityCarrier,
2317
3293
  compareDocumentOrder,
3294
+ createChoiceAnnouncer,
2318
3295
  createLiveAnnouncer,
2319
3296
  createRowVirtualizer,
2320
3297
  findFirstInvalidControl,