@fragiola/primitives 0.0.0-stage → 0.0.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 (191) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -2
  3. package/dist/async-list-Bv6ff5Oj.js +81 -0
  4. package/dist/async-list-Bv6ff5Oj.js.map +1 -0
  5. package/dist/async-list.d.ts +2 -0
  6. package/dist/async-list.js +1 -0
  7. package/dist/calendar-keys-DGYpjLwv.d.ts +12 -0
  8. package/dist/calendar-system-B5U5HcPQ.d.ts +415 -0
  9. package/dist/calendar-systems-Daa6Q_ca.js +659 -0
  10. package/dist/calendar-systems-Daa6Q_ca.js.map +1 -0
  11. package/dist/calendar-systems.d.ts +3 -0
  12. package/dist/calendar-systems.js +1 -0
  13. package/dist/carousel-LRbWqbAx.js +375 -0
  14. package/dist/carousel-LRbWqbAx.js.map +1 -0
  15. package/dist/carousel.d.ts +2 -0
  16. package/dist/carousel.js +1 -0
  17. package/dist/cascade-B155Xvvv.js +64 -0
  18. package/dist/cascade-B155Xvvv.js.map +1 -0
  19. package/dist/cascade.d.ts +2 -0
  20. package/dist/cascade.js +1 -0
  21. package/dist/clipboard-Cy2cbGEW.js +33 -0
  22. package/dist/clipboard-Cy2cbGEW.js.map +1 -0
  23. package/dist/clipboard.d.ts +2 -0
  24. package/dist/clipboard.js +1 -0
  25. package/dist/collection-Dbk-PB5S.js +609 -0
  26. package/dist/collection-Dbk-PB5S.js.map +1 -0
  27. package/dist/collection.d.ts +2 -0
  28. package/dist/collection.js +3 -0
  29. package/dist/color-sURV5Irg.js +699 -0
  30. package/dist/color-sURV5Irg.js.map +1 -0
  31. package/dist/color.d.ts +2 -0
  32. package/dist/color.js +1 -0
  33. package/dist/command-DueCXi65.js +104 -0
  34. package/dist/command-DueCXi65.js.map +1 -0
  35. package/dist/command.d.ts +2 -0
  36. package/dist/command.js +1 -0
  37. package/dist/date-CrbFSJKQ.js +215 -0
  38. package/dist/date-CrbFSJKQ.js.map +1 -0
  39. package/dist/date.d.ts +4 -0
  40. package/dist/date.js +1 -0
  41. package/dist/direction-CTnVLpmd.d.ts +8 -0
  42. package/dist/direction-DJDPvnxJ.js +13 -0
  43. package/dist/direction-DJDPvnxJ.js.map +1 -0
  44. package/dist/direction.d.ts +2 -0
  45. package/dist/direction.js +1 -0
  46. package/dist/drag-session-CSr06B5v.js +41 -0
  47. package/dist/drag-session-CSr06B5v.js.map +1 -0
  48. package/dist/file-DSLtfzbi.js +263 -0
  49. package/dist/file-DSLtfzbi.js.map +1 -0
  50. package/dist/file.d.ts +2 -0
  51. package/dist/file.js +1 -0
  52. package/dist/focus-BI-VvM0f.js +75 -0
  53. package/dist/focus-BI-VvM0f.js.map +1 -0
  54. package/dist/focus.d.ts +2 -0
  55. package/dist/focus.js +3 -0
  56. package/dist/form-DovwKtqz.js +501 -0
  57. package/dist/form-DovwKtqz.js.map +1 -0
  58. package/dist/form.d.ts +2 -0
  59. package/dist/form.js +1 -0
  60. package/dist/hover-BakyPYnC.js +62 -0
  61. package/dist/hover-BakyPYnC.js.map +1 -0
  62. package/dist/hover.d.ts +2 -0
  63. package/dist/hover.js +1 -0
  64. package/dist/id-B0Y8iZrc.js +13 -0
  65. package/dist/id-B0Y8iZrc.js.map +1 -0
  66. package/dist/id.d.ts +2 -0
  67. package/dist/id.js +1 -0
  68. package/dist/image-D4X_yRYw.js +44 -0
  69. package/dist/image-D4X_yRYw.js.map +1 -0
  70. package/dist/image.d.ts +2 -0
  71. package/dist/image.js +1 -0
  72. package/dist/index-B0rwIZw9.d.ts +51 -0
  73. package/dist/index-B4wF1isn.d.ts +128 -0
  74. package/dist/index-BHiBRE01.d.ts +20 -0
  75. package/dist/index-BNlMm-0u.d.ts +17 -0
  76. package/dist/index-BapNrjY9.d.ts +48 -0
  77. package/dist/index-Bd-tVb7C.d.ts +6 -0
  78. package/dist/index-BjE8nDiX.d.ts +125 -0
  79. package/dist/index-BpbT-FsX.d.ts +117 -0
  80. package/dist/index-CBgGcTeX.d.ts +37 -0
  81. package/dist/index-CGThV_Md.d.ts +100 -0
  82. package/dist/index-CMig3TXL.d.ts +28 -0
  83. package/dist/index-CWFRduRd.d.ts +212 -0
  84. package/dist/index-CkrHWyU_.d.ts +33 -0
  85. package/dist/index-Cwu6f7y1.d.ts +18 -0
  86. package/dist/index-D10LDdR6.d.ts +139 -0
  87. package/dist/index-D146vm21.d.ts +104 -0
  88. package/dist/index-DGKhEvNr.d.ts +107 -0
  89. package/dist/index-DbAu4s16.d.ts +48 -0
  90. package/dist/index-DdaCj6IO.d.ts +54 -0
  91. package/dist/index-DeHcNdjC.d.ts +68 -0
  92. package/dist/index-DeR4ncrW.d.ts +93 -0
  93. package/dist/index-HIksgK0e.d.ts +70 -0
  94. package/dist/index-IZB0mlZl.d.ts +71 -0
  95. package/dist/index-IfDZj87U.d.ts +28 -0
  96. package/dist/index-SrTuVBms.d.ts +75 -0
  97. package/dist/index-T9UcYvkf.d.ts +339 -0
  98. package/dist/index-fRTojAoR.d.ts +31 -0
  99. package/dist/index-fuLUke9n.d.ts +6 -0
  100. package/dist/index-l63esAck.d.ts +104 -0
  101. package/dist/index-raSUg9U7.d.ts +38 -0
  102. package/dist/index-twgzPKvZ.d.ts +278 -0
  103. package/dist/index-wBpjRykh.d.ts +125 -0
  104. package/dist/index.d.ts +39 -0
  105. package/dist/index.js +42 -0
  106. package/dist/inert-DlGToy3q.js +80 -0
  107. package/dist/inert-DlGToy3q.js.map +1 -0
  108. package/dist/inert.d.ts +2 -0
  109. package/dist/inert.js +1 -0
  110. package/dist/layer-stack-BEtzaFC1.js +118 -0
  111. package/dist/layer-stack-BEtzaFC1.js.map +1 -0
  112. package/dist/layers-C-N4rrZX.js +24 -0
  113. package/dist/layers-C-N4rrZX.js.map +1 -0
  114. package/dist/layers.d.ts +2 -0
  115. package/dist/layers.js +3 -0
  116. package/dist/list-navigation-CbWv4DK5.js +40 -0
  117. package/dist/list-navigation-CbWv4DK5.js.map +1 -0
  118. package/dist/month-year-BG5imFUq.js +108 -0
  119. package/dist/month-year-BG5imFUq.js.map +1 -0
  120. package/dist/month-year.d.ts +3 -0
  121. package/dist/month-year.js +1 -0
  122. package/dist/number-BFUio97d.js +316 -0
  123. package/dist/number-BFUio97d.js.map +1 -0
  124. package/dist/number.d.ts +2 -0
  125. package/dist/number.js +1 -0
  126. package/dist/overlay-BwPyiM3T.js +78 -0
  127. package/dist/overlay-BwPyiM3T.js.map +1 -0
  128. package/dist/overlay.d.ts +2 -0
  129. package/dist/overlay.js +1 -0
  130. package/dist/pagination-BkVzaIGP.js +51 -0
  131. package/dist/pagination-BkVzaIGP.js.map +1 -0
  132. package/dist/pagination.d.ts +2 -0
  133. package/dist/pagination.js +1 -0
  134. package/dist/plain-date-idQH1P6X.d.ts +29 -0
  135. package/dist/plain-date-time-B8PGahhe.d.ts +99 -0
  136. package/dist/pointer-CuDtD9XA.js +189 -0
  137. package/dist/pointer-CuDtD9XA.js.map +1 -0
  138. package/dist/pointer.d.ts +2 -0
  139. package/dist/pointer.js +3 -0
  140. package/dist/positioning-DDUQ75mB.js +148 -0
  141. package/dist/positioning-DDUQ75mB.js.map +1 -0
  142. package/dist/positioning.d.ts +2 -0
  143. package/dist/positioning.js +1 -0
  144. package/dist/presence-BhKDOmII.js +86 -0
  145. package/dist/presence-BhKDOmII.js.map +1 -0
  146. package/dist/presence.d.ts +2 -0
  147. package/dist/presence.js +1 -0
  148. package/dist/range-o0_GBhyY.js +158 -0
  149. package/dist/range-o0_GBhyY.js.map +1 -0
  150. package/dist/range.d.ts +2 -0
  151. package/dist/range.js +1 -0
  152. package/dist/scroll-area-BYNhv36t.js +447 -0
  153. package/dist/scroll-area-BYNhv36t.js.map +1 -0
  154. package/dist/scroll-area.d.ts +2 -0
  155. package/dist/scroll-area.js +1 -0
  156. package/dist/scroll-lock-JzRNOA1O.js +42 -0
  157. package/dist/scroll-lock-JzRNOA1O.js.map +1 -0
  158. package/dist/scroll-lock.d.ts +2 -0
  159. package/dist/scroll-lock.js +1 -0
  160. package/dist/segments-eRu5G_Bd.js +587 -0
  161. package/dist/segments-eRu5G_Bd.js.map +1 -0
  162. package/dist/segments.d.ts +3 -0
  163. package/dist/segments.js +1 -0
  164. package/dist/selection-Cor67w_V.js +287 -0
  165. package/dist/selection-Cor67w_V.js.map +1 -0
  166. package/dist/selection.d.ts +2 -0
  167. package/dist/selection.js +1 -0
  168. package/dist/swipe.d.ts +2 -0
  169. package/dist/swipe.js +1 -0
  170. package/dist/tabbable-BPiLssIv.js +35 -0
  171. package/dist/tabbable-BPiLssIv.js.map +1 -0
  172. package/dist/time-Ct6oq9kt.js +208 -0
  173. package/dist/time-Ct6oq9kt.js.map +1 -0
  174. package/dist/time-zone-DegkzwST.js +305 -0
  175. package/dist/time-zone-DegkzwST.js.map +1 -0
  176. package/dist/time-zone.d.ts +2 -0
  177. package/dist/time-zone.js +1 -0
  178. package/dist/time.d.ts +2 -0
  179. package/dist/time.js +1 -0
  180. package/dist/toast-BjRpJalM.js +400 -0
  181. package/dist/toast-BjRpJalM.js.map +1 -0
  182. package/dist/toast.d.ts +2 -0
  183. package/dist/toast.js +1 -0
  184. package/dist/unit-grid-DJy8lb5D.d.ts +21 -0
  185. package/dist/virtual-BlX1RuHD.js +306 -0
  186. package/dist/virtual-BlX1RuHD.js.map +1 -0
  187. package/dist/virtual.d.ts +2 -0
  188. package/dist/virtual.js +1 -0
  189. package/dist/watch-swipe-BIucfr9O.js +301 -0
  190. package/dist/watch-swipe-BIucfr9O.js.map +1 -0
  191. package/package.json +234 -3
@@ -0,0 +1,263 @@
1
+ // src/file/accept-files.ts
2
+ /** The patterns of `accept`, trimmed and lowercased; `[]` takes every file. */
3
+ function acceptPatterns(accept) {
4
+ if (accept === void 0) return [];
5
+ return (typeof accept === "string" ? accept.split(",") : accept).map((pattern) => pattern.trim().toLowerCase()).filter((pattern) => pattern !== "");
6
+ }
7
+ /** `accept` as a file input's attribute (`undefined` for none). */
8
+ function acceptAttribute(accept) {
9
+ const patterns = accept === void 0 ? [] : (typeof accept === "string" ? accept.split(",") : accept).map((pattern) => pattern.trim()).filter((pattern) => pattern !== "");
10
+ return patterns.length === 0 ? void 0 : patterns.join(",");
11
+ }
12
+ /**
13
+ * Whether `file` matches `accept`, as a file input's `accept` reads it (spec/drop-zone.md, "API"):
14
+ * an extension (`.pdf`, case insensitive), a MIME type (`image/png`) or a wildcard (`image/*`).
15
+ * No `accept` (or an empty one) takes every file.
16
+ */
17
+ function acceptsFile(file, accept) {
18
+ const patterns = acceptPatterns(accept);
19
+ if (patterns.length === 0) return true;
20
+ const name = file.name.toLowerCase();
21
+ const type = file.type.toLowerCase();
22
+ return patterns.some((pattern) => {
23
+ if (pattern.startsWith(".")) return name.endsWith(pattern);
24
+ if (pattern.endsWith("/*")) return type.startsWith(pattern.slice(0, -1));
25
+ return type === pattern;
26
+ });
27
+ }
28
+ /**
29
+ * A DropZone's split of the files it was given (spec/drop-zone.md): the ones `accept` takes, and
30
+ * the others. When `multiple` is `false`, more than one file rejects them all.
31
+ */
32
+ function acceptFiles(files, accept, multiple) {
33
+ if (!multiple && files.length > 1) return {
34
+ accepted: [],
35
+ rejected: [...files]
36
+ };
37
+ const accepted = [];
38
+ const rejected = [];
39
+ for (const file of files) (acceptsFile(file, accept) ? accepted : rejected).push(file);
40
+ return {
41
+ accepted,
42
+ rejected
43
+ };
44
+ }
45
+ // src/file/assign-files.ts
46
+ /**
47
+ * Puts `files` into a file input (spec/file-upload.md, "Forms"): a `DataTransfer` of the input's
48
+ * window, built with them, gives its `files` list to `input.files`, so the native form submits
49
+ * them, `required` validates them and a `FormData` holds them. Verified in Chromium; Firefox
50
+ * (57+) and Safari (14.1+) accept the same assignment. Returns `false` where the window has no
51
+ * `DataTransfer` constructor (the input keeps its files).
52
+ */
53
+ function assignFiles(input, files) {
54
+ const view = input.ownerDocument.defaultView;
55
+ if (view === null || typeof view.DataTransfer !== "function") return false;
56
+ try {
57
+ const transfer = new view.DataTransfer();
58
+ for (const file of files) transfer.items.add(file);
59
+ input.files = transfer.files;
60
+ return true;
61
+ } catch {
62
+ return false;
63
+ }
64
+ }
65
+ // src/file/check-files.ts
66
+ /**
67
+ * Checks the files of a pick against `rules`, given the accepted files `value` (spec/file-upload.md):
68
+ * each file in order, every failing check a reason (`invalid-type`, `too-large`, `too-small`, the
69
+ * app's own, then `too-many` for a file over `maxFiles`). With `maxFiles` `1`, a single good file
70
+ * replaces the value and a pick of several rejects them all (`too-many`), as a native single file
71
+ * input does.
72
+ */
73
+ function checkFiles(value, files, rules) {
74
+ const maxFiles = Math.max(1, rules.maxFiles ?? 1);
75
+ const minSize = rules.minFileSize ?? 0;
76
+ const maxSize = rules.maxFileSize ?? Number.POSITIVE_INFINITY;
77
+ const single = maxFiles === 1;
78
+ const kept = single ? [] : [...value];
79
+ const accepted = [];
80
+ const rejected = [];
81
+ for (const file of files) {
82
+ const reasons = [];
83
+ if (!acceptsFile(file, rules.accept)) reasons.push("invalid-type");
84
+ if (file.size > maxSize) reasons.push("too-large");
85
+ if (file.size < minSize) reasons.push("too-small");
86
+ for (const reason of rules.validate?.(file, kept) ?? []) if (!reasons.includes(reason)) reasons.push(reason);
87
+ if (single && files.length > 1 || kept.length >= maxFiles) reasons.push("too-many");
88
+ if (reasons.length === 0) {
89
+ kept.push(file);
90
+ accepted.push(file);
91
+ } else rejected.push({
92
+ file,
93
+ reasons
94
+ });
95
+ }
96
+ return {
97
+ value: single && accepted.length === 0 ? [...value] : kept,
98
+ accepted,
99
+ rejected
100
+ };
101
+ }
102
+ // src/file/field-files.ts
103
+ /**
104
+ * A copy of `files` that a Field can compare (spec/file-upload.md, "Forms"): a Field detects a
105
+ * change of its control's value by its JSON text, and a `File` writes `{}`; the copy writes each
106
+ * file as `name:size:type:lastModified` (a non-enumerable `toJSON`), so a replaced file is a
107
+ * change. It is still the files, for the Field's `validate`.
108
+ */
109
+ function fieldFiles(files) {
110
+ const copy = [...files];
111
+ Object.defineProperty(copy, "toJSON", { value: () => files.map((file) => `${file.name}:${file.size}:${file.type}:${file.lastModified}`) });
112
+ return copy;
113
+ }
114
+ // src/file/format-file-size.ts
115
+ const UNITS = [
116
+ "byte",
117
+ "kilobyte",
118
+ "megabyte",
119
+ "gigabyte",
120
+ "terabyte"
121
+ ];
122
+ /**
123
+ * A file size in the locale's bytes (spec/file-upload.md, "State attributes and ARIA"):
124
+ * `Intl.NumberFormat`'s `unit` style, short, in decimal units (`Intl`'s are SI: 1 kB = 1000
125
+ * bytes), with one fraction digit above bytes: `en-US` writes `512 byte`, `1.5 kB`, `2 MB`.
126
+ * Locale data, not text of the library's own; `undefined` is the runtime's locale.
127
+ */
128
+ function formatFileSize(bytes, locale) {
129
+ let value = Math.max(0, bytes);
130
+ let unit = 0;
131
+ while (Math.round(value * 10) / 10 >= 1e3 && unit < UNITS.length - 1) {
132
+ value /= 1e3;
133
+ unit += 1;
134
+ }
135
+ return new Intl.NumberFormat(locale, {
136
+ style: "unit",
137
+ unit: UNITS[unit],
138
+ unitDisplay: "short",
139
+ maximumFractionDigits: unit === 0 ? 0 : 1
140
+ }).format(value);
141
+ }
142
+ // src/file/open-file-dialog.ts
143
+ /**
144
+ * Opens the browser's file dialog for `element`'s document (spec/drop-zone.md, "Purpose"): a file
145
+ * `input` created for this press, never put in the document, clicked in the press's user
146
+ * activation. `onFiles` receives the files picked, and is not called when the dialog closes with
147
+ * none.
148
+ */
149
+ function openFileDialog(element, options, onFiles) {
150
+ const input = element.ownerDocument.createElement("input");
151
+ input.type = "file";
152
+ const accept = acceptAttribute(options.accept);
153
+ if (accept !== void 0) input.accept = accept;
154
+ input.multiple = options.multiple ?? false;
155
+ if (options.capture !== void 0) input.setAttribute("capture", options.capture);
156
+ input.addEventListener("change", (event) => {
157
+ const files = [...input.files ?? []];
158
+ if (files.length > 0) onFiles(files, event);
159
+ }, { once: true });
160
+ input.click();
161
+ }
162
+ // src/file/watch-file-drop.ts
163
+ /** Whether a drag carries files (`dataTransfer.types` holds `"Files"`). */
164
+ function dragHasFiles(event) {
165
+ const types = event.dataTransfer?.types;
166
+ return types !== void 0 && [...types].includes("Files");
167
+ }
168
+ /**
169
+ * Makes `element` a target for files (spec/drop-zone.md, "Pointer", "Keyboard"): a drag of files
170
+ * over it (counted across its children's enters and leaves) is reported and shows the `copy`
171
+ * effect, its drop hands over the files, and a paste on it hands over the clipboard's files; a
172
+ * drag of anything else is left alone. While `disabled()`, a drag of files shows the `none` effect
173
+ * and its drop is canceled, so the browser does not open the file. A click, Enter (keydown) or Space (keyup)
174
+ * is a press (`onActivate`; neither scrolls the page, Enter does not repeat). Native listeners, so
175
+ * every framework sees the same events. Returns the cleanup.
176
+ */
177
+ function watchFileDrop(element, options) {
178
+ let depth = 0;
179
+ const end = (event) => {
180
+ if (depth > 0) {
181
+ depth = 0;
182
+ options.onDropTargetChange(false, event);
183
+ }
184
+ };
185
+ const onDragEnter = (event) => {
186
+ if (!dragHasFiles(event)) return;
187
+ event.preventDefault();
188
+ if (options.disabled()) return;
189
+ depth += 1;
190
+ if (depth === 1) options.onDropTargetChange(true, event);
191
+ };
192
+ const onDragOver = (event) => {
193
+ if (!dragHasFiles(event)) return;
194
+ event.preventDefault();
195
+ if (event.dataTransfer !== null) event.dataTransfer.dropEffect = options.disabled() ? "none" : "copy";
196
+ };
197
+ const onDragLeave = (event) => {
198
+ if (depth === 0) return;
199
+ depth -= 1;
200
+ if (depth === 0) options.onDropTargetChange(false, event);
201
+ };
202
+ const onDrop = (event) => {
203
+ if (!dragHasFiles(event)) return;
204
+ event.preventDefault();
205
+ end(event);
206
+ if (options.disabled()) return;
207
+ const files = [...event.dataTransfer?.files ?? []];
208
+ if (files.length > 0) options.onFiles(files, "drop", event);
209
+ };
210
+ const onPaste = (event) => {
211
+ if (options.disabled()) return;
212
+ const files = [...event.clipboardData?.files ?? []];
213
+ if (files.length > 0) {
214
+ event.preventDefault();
215
+ options.onFiles(files, "paste", event);
216
+ }
217
+ };
218
+ const onClick = (event) => {
219
+ if (event.target?.tagName === "INPUT") return;
220
+ if (!options.disabled()) options.onActivate?.(event);
221
+ };
222
+ let spacePressed = false;
223
+ const onKeyDown = (event) => {
224
+ if (event.target !== element || event.key !== "Enter" && event.key !== " ") return;
225
+ event.preventDefault();
226
+ if (event.key === " ") {
227
+ spacePressed = true;
228
+ return;
229
+ }
230
+ if (!event.repeat && !options.disabled()) options.onActivate?.(event);
231
+ };
232
+ const onKeyUp = (event) => {
233
+ if (event.key !== " " || !spacePressed) return;
234
+ spacePressed = false;
235
+ if (event.target === element && !options.disabled()) options.onActivate?.(event);
236
+ };
237
+ const onBlur = () => {
238
+ spacePressed = false;
239
+ };
240
+ element.addEventListener("dragenter", onDragEnter);
241
+ element.addEventListener("dragover", onDragOver);
242
+ element.addEventListener("dragleave", onDragLeave);
243
+ element.addEventListener("drop", onDrop);
244
+ element.addEventListener("paste", onPaste);
245
+ element.addEventListener("click", onClick);
246
+ element.addEventListener("keydown", onKeyDown);
247
+ element.addEventListener("keyup", onKeyUp);
248
+ element.addEventListener("blur", onBlur);
249
+ return () => {
250
+ element.removeEventListener("dragenter", onDragEnter);
251
+ element.removeEventListener("dragover", onDragOver);
252
+ element.removeEventListener("dragleave", onDragLeave);
253
+ element.removeEventListener("drop", onDrop);
254
+ element.removeEventListener("paste", onPaste);
255
+ element.removeEventListener("click", onClick);
256
+ element.removeEventListener("keydown", onKeyDown);
257
+ element.removeEventListener("keyup", onKeyUp);
258
+ element.removeEventListener("blur", onBlur);
259
+ };
260
+ }
261
+ export { fieldFiles as a, acceptAttribute as c, formatFileSize as i, acceptFiles as l, watchFileDrop as n, checkFiles as o, openFileDialog as r, assignFiles as s, dragHasFiles as t, acceptsFile as u };
262
+
263
+ //# sourceMappingURL=file-DSLtfzbi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-DSLtfzbi.js","names":[],"sources":["../src/file/accept-files.ts","../src/file/assign-files.ts","../src/file/check-files.ts","../src/file/field-files.ts","../src/file/format-file-size.ts","../src/file/open-file-dialog.ts","../src/file/watch-file-drop.ts"],"sourcesContent":["/** A file's name and type, all an `accept` pattern reads. */\nexport interface FileLike {\n readonly name: string;\n readonly type: string;\n}\n\n/** The `accept` of a file input: a comma-separated string, or its patterns (spec/drop-zone.md). */\nexport type FileAccept = string | readonly string[];\n\n/** The patterns of `accept`, trimmed and lowercased; `[]` takes every file. */\nfunction acceptPatterns(accept: FileAccept | undefined): string[] {\n if (accept === undefined) {\n return [];\n }\n const list = typeof accept === \"string\" ? accept.split(\",\") : accept;\n return list\n .map((pattern) => pattern.trim().toLowerCase())\n .filter((pattern) => pattern !== \"\");\n}\n\n/** `accept` as a file input's attribute (`undefined` for none). */\nexport function acceptAttribute(\n accept: FileAccept | undefined,\n): string | undefined {\n const patterns =\n accept === undefined\n ? []\n : (typeof accept === \"string\" ? accept.split(\",\") : accept)\n .map((pattern) => pattern.trim())\n .filter((pattern) => pattern !== \"\");\n return patterns.length === 0 ? undefined : patterns.join(\",\");\n}\n\n/**\n * Whether `file` matches `accept`, as a file input's `accept` reads it (spec/drop-zone.md, \"API\"):\n * an extension (`.pdf`, case insensitive), a MIME type (`image/png`) or a wildcard (`image/*`).\n * No `accept` (or an empty one) takes every file.\n */\nexport function acceptsFile(\n file: FileLike,\n accept: FileAccept | undefined,\n): boolean {\n const patterns = acceptPatterns(accept);\n if (patterns.length === 0) {\n return true;\n }\n const name = file.name.toLowerCase();\n const type = file.type.toLowerCase();\n return patterns.some((pattern) => {\n if (pattern.startsWith(\".\")) {\n return name.endsWith(pattern);\n }\n if (pattern.endsWith(\"/*\")) {\n return type.startsWith(pattern.slice(0, -1));\n }\n return type === pattern;\n });\n}\n\n/**\n * A DropZone's split of the files it was given (spec/drop-zone.md): the ones `accept` takes, and\n * the others. When `multiple` is `false`, more than one file rejects them all.\n */\nexport function acceptFiles<File extends FileLike>(\n files: readonly File[],\n accept: FileAccept | undefined,\n multiple: boolean,\n): { accepted: File[]; rejected: File[] } {\n if (!multiple && files.length > 1) {\n return { accepted: [], rejected: [...files] };\n }\n const accepted: File[] = [];\n const rejected: File[] = [];\n for (const file of files) {\n (acceptsFile(file, accept) ? accepted : rejected).push(file);\n }\n return { accepted, rejected };\n}\n","/**\n * Puts `files` into a file input (spec/file-upload.md, \"Forms\"): a `DataTransfer` of the input's\n * window, built with them, gives its `files` list to `input.files`, so the native form submits\n * them, `required` validates them and a `FormData` holds them. Verified in Chromium; Firefox\n * (57+) and Safari (14.1+) accept the same assignment. Returns `false` where the window has no\n * `DataTransfer` constructor (the input keeps its files).\n */\nexport function assignFiles(\n input: HTMLInputElement,\n files: readonly File[],\n): boolean {\n const view = input.ownerDocument.defaultView as\n | (Window & typeof globalThis)\n | null;\n if (view === null || typeof view.DataTransfer !== \"function\") {\n return false;\n }\n try {\n const transfer = new view.DataTransfer();\n for (const file of files) {\n transfer.items.add(file);\n }\n input.files = transfer.files;\n return true;\n } catch {\n return false;\n }\n}\n","import { acceptsFile, type FileAccept } from \"./accept-files.ts\";\n\n/** A file as the checks read it: its name, type and size in bytes. */\nexport interface CheckedFile {\n readonly name: string;\n readonly type: string;\n readonly size: number;\n}\n\n/**\n * Why a file was rejected (spec/file-upload.md, \"Purpose\"): its type (`accept`), its size, the\n * count (`maxFiles`), or a reason of the app's own `validate`.\n */\nexport type FileRejectionReason =\n | \"invalid-type\"\n | \"too-large\"\n | \"too-small\"\n | \"too-many\"\n | (string & Record<never, never>);\n\n/** A rejected file and every reason it failed. */\nexport interface FileRejection<File extends CheckedFile = CheckedFile> {\n file: File;\n reasons: readonly FileRejectionReason[];\n}\n\n/** A FileUpload's rules (spec/file-upload.md, \"API\"). */\nexport interface FileRules<File extends CheckedFile = CheckedFile> {\n accept?: FileAccept | undefined;\n /** At most this many accepted files (default `1`, which replaces the file on a new pick). */\n maxFiles?: number | undefined;\n /** Bytes, inclusive (default `0`). */\n minFileSize?: number | undefined;\n /** Bytes, inclusive (default `Infinity`). */\n maxFileSize?: number | undefined;\n /** The app's own reasons for a file (`null` or `[]` for none), given the files accepted so far. */\n validate?:\n | ((\n file: File,\n value: readonly File[],\n ) => readonly string[] | null | undefined)\n | undefined;\n}\n\n/** What a pick does to a FileUpload's files. */\nexport interface FileCheck<File extends CheckedFile = CheckedFile> {\n /** The accepted files after the pick: the value. */\n value: File[];\n /** The files of the pick that were accepted. */\n accepted: File[];\n /** The files of the pick that were rejected, with their reasons. */\n rejected: FileRejection<File>[];\n}\n\n/**\n * Checks the files of a pick against `rules`, given the accepted files `value` (spec/file-upload.md):\n * each file in order, every failing check a reason (`invalid-type`, `too-large`, `too-small`, the\n * app's own, then `too-many` for a file over `maxFiles`). With `maxFiles` `1`, a single good file\n * replaces the value and a pick of several rejects them all (`too-many`), as a native single file\n * input does.\n */\nexport function checkFiles<File extends CheckedFile>(\n value: readonly File[],\n files: readonly File[],\n rules: FileRules<File>,\n): FileCheck<File> {\n const maxFiles = Math.max(1, rules.maxFiles ?? 1);\n const minSize = rules.minFileSize ?? 0;\n const maxSize = rules.maxFileSize ?? Number.POSITIVE_INFINITY;\n const single = maxFiles === 1;\n const kept: File[] = single ? [] : [...value];\n const accepted: File[] = [];\n const rejected: FileRejection<File>[] = [];\n for (const file of files) {\n const reasons: string[] = [];\n if (!acceptsFile(file, rules.accept)) {\n reasons.push(\"invalid-type\");\n }\n if (file.size > maxSize) {\n reasons.push(\"too-large\");\n }\n if (file.size < minSize) {\n reasons.push(\"too-small\");\n }\n for (const reason of rules.validate?.(file, kept) ?? []) {\n if (!reasons.includes(reason)) {\n reasons.push(reason);\n }\n }\n if ((single && files.length > 1) || kept.length >= maxFiles) {\n reasons.push(\"too-many\");\n }\n if (reasons.length === 0) {\n kept.push(file);\n accepted.push(file);\n } else {\n rejected.push({ file, reasons });\n }\n }\n return {\n value: single && accepted.length === 0 ? [...value] : kept,\n accepted,\n rejected,\n };\n}\n","/**\n * A copy of `files` that a Field can compare (spec/file-upload.md, \"Forms\"): a Field detects a\n * change of its control's value by its JSON text, and a `File` writes `{}`; the copy writes each\n * file as `name:size:type:lastModified` (a non-enumerable `toJSON`), so a replaced file is a\n * change. It is still the files, for the Field's `validate`.\n */\nexport function fieldFiles(files: readonly File[]): File[] {\n const copy = [...files];\n Object.defineProperty(copy, \"toJSON\", {\n value: () =>\n files.map(\n (file) =>\n `${file.name}:${file.size}:${file.type}:${file.lastModified}`,\n ),\n });\n return copy;\n}\n","const UNITS = [\"byte\", \"kilobyte\", \"megabyte\", \"gigabyte\", \"terabyte\"] as const;\n\n/**\n * A file size in the locale's bytes (spec/file-upload.md, \"State attributes and ARIA\"):\n * `Intl.NumberFormat`'s `unit` style, short, in decimal units (`Intl`'s are SI: 1 kB = 1000\n * bytes), with one fraction digit above bytes: `en-US` writes `512 byte`, `1.5 kB`, `2 MB`.\n * Locale data, not text of the library's own; `undefined` is the runtime's locale.\n */\nexport function formatFileSize(\n bytes: number,\n locale?: string | readonly string[],\n): string {\n let value = Math.max(0, bytes);\n let unit = 0;\n // the unit is chosen after rounding to one fraction digit: 999 950 bytes is `1 MB`\n while (Math.round(value * 10) / 10 >= 1000 && unit < UNITS.length - 1) {\n value /= 1000;\n unit += 1;\n }\n return new Intl.NumberFormat(locale as string | string[] | undefined, {\n style: \"unit\",\n unit: UNITS[unit],\n unitDisplay: \"short\",\n maximumFractionDigits: unit === 0 ? 0 : 1,\n }).format(value);\n}\n","import { acceptAttribute, type FileAccept } from \"./accept-files.ts\";\n\nexport interface FileDialogOptions {\n accept?: FileAccept | undefined;\n multiple?: boolean | undefined;\n /** The camera a phone opens (`user`, `environment`), as a file input's `capture`. */\n capture?: \"user\" | \"environment\" | undefined;\n}\n\n/**\n * Opens the browser's file dialog for `element`'s document (spec/drop-zone.md, \"Purpose\"): a file\n * `input` created for this press, never put in the document, clicked in the press's user\n * activation. `onFiles` receives the files picked, and is not called when the dialog closes with\n * none.\n */\nexport function openFileDialog(\n element: Element,\n options: FileDialogOptions,\n onFiles: (files: File[], event: Event) => void,\n): void {\n const input = element.ownerDocument.createElement(\"input\");\n input.type = \"file\";\n const accept = acceptAttribute(options.accept);\n if (accept !== undefined) {\n input.accept = accept;\n }\n input.multiple = options.multiple ?? false;\n if (options.capture !== undefined) {\n input.setAttribute(\"capture\", options.capture);\n }\n input.addEventListener(\n \"change\",\n (event) => {\n const files = [...(input.files ?? [])];\n if (files.length > 0) {\n onFiles(files, event);\n }\n },\n { once: true },\n );\n input.click();\n}\n","/** Why files arrived on a drop target (spec/drop-zone.md). */\nexport type FileDropReason = \"drop\" | \"paste\";\n\nexport interface FileDropOptions {\n /** Read on every event: a disabled target takes nothing. */\n disabled: () => boolean;\n /** A drag of files entered the target (`true`), or left it, dropped or ended (`false`). */\n onDropTargetChange: (active: boolean, event: Event) => void;\n /** Files were dropped or pasted (never called with none). */\n onFiles: (files: File[], reason: FileDropReason, event: Event) => void;\n /** A press: a click, Enter or Space (the file dialog's opening). */\n onActivate?: ((event: Event) => void) | undefined;\n}\n\n/** Whether a drag carries files (`dataTransfer.types` holds `\"Files\"`). */\nexport function dragHasFiles(event: DragEvent): boolean {\n const types = event.dataTransfer?.types;\n return types !== undefined && [...types].includes(\"Files\");\n}\n\n/**\n * Makes `element` a target for files (spec/drop-zone.md, \"Pointer\", \"Keyboard\"): a drag of files\n * over it (counted across its children's enters and leaves) is reported and shows the `copy`\n * effect, its drop hands over the files, and a paste on it hands over the clipboard's files; a\n * drag of anything else is left alone. While `disabled()`, a drag of files shows the `none` effect\n * and its drop is canceled, so the browser does not open the file. A click, Enter (keydown) or Space (keyup)\n * is a press (`onActivate`; neither scrolls the page, Enter does not repeat). Native listeners, so\n * every framework sees the same events. Returns the cleanup.\n */\nexport function watchFileDrop(\n element: HTMLElement,\n options: FileDropOptions,\n): () => void {\n let depth = 0;\n const end = (event: Event) => {\n if (depth > 0) {\n depth = 0;\n options.onDropTargetChange(false, event);\n }\n };\n const onDragEnter = (event: DragEvent) => {\n if (!dragHasFiles(event)) {\n return;\n }\n event.preventDefault();\n if (options.disabled()) {\n return;\n }\n depth += 1;\n if (depth === 1) {\n options.onDropTargetChange(true, event);\n }\n };\n const onDragOver = (event: DragEvent) => {\n if (!dragHasFiles(event)) {\n return;\n }\n event.preventDefault();\n if (event.dataTransfer !== null) {\n event.dataTransfer.dropEffect = options.disabled()\n ? \"none\"\n : \"copy\";\n }\n };\n const onDragLeave = (event: DragEvent) => {\n if (depth === 0) {\n return;\n }\n depth -= 1;\n if (depth === 0) {\n options.onDropTargetChange(false, event);\n }\n };\n const onDrop = (event: DragEvent) => {\n if (!dragHasFiles(event)) {\n return;\n }\n event.preventDefault();\n end(event);\n if (options.disabled()) {\n return;\n }\n const files = [...(event.dataTransfer?.files ?? [])];\n if (files.length > 0) {\n options.onFiles(files, \"drop\", event);\n }\n };\n const onPaste = (event: ClipboardEvent) => {\n if (options.disabled()) {\n return;\n }\n const files = [...(event.clipboardData?.files ?? [])];\n if (files.length > 0) {\n event.preventDefault();\n options.onFiles(files, \"paste\", event);\n }\n };\n const onClick = (event: MouseEvent) => {\n // the click a file input inside the target gets when it is opened bubbles here: it is\n // no press (it would open the dialog again)\n if ((event.target as Element | null)?.tagName === \"INPUT\") {\n return;\n }\n if (!options.disabled()) {\n options.onActivate?.(event);\n }\n };\n // Enter presses on keydown, Space on keyup, as a native button (the APG button): moving the\n // focus away before Space is released cancels the press\n let spacePressed = false;\n const onKeyDown = (event: KeyboardEvent) => {\n if (\n event.target !== element ||\n (event.key !== \"Enter\" && event.key !== \" \")\n ) {\n return;\n }\n event.preventDefault();\n if (event.key === \" \") {\n spacePressed = true;\n return;\n }\n if (!event.repeat && !options.disabled()) {\n options.onActivate?.(event);\n }\n };\n const onKeyUp = (event: KeyboardEvent) => {\n if (event.key !== \" \" || !spacePressed) {\n return;\n }\n spacePressed = false;\n if (event.target === element && !options.disabled()) {\n options.onActivate?.(event);\n }\n };\n const onBlur = () => {\n spacePressed = false;\n };\n element.addEventListener(\"dragenter\", onDragEnter);\n element.addEventListener(\"dragover\", onDragOver);\n element.addEventListener(\"dragleave\", onDragLeave);\n element.addEventListener(\"drop\", onDrop);\n element.addEventListener(\"paste\", onPaste);\n element.addEventListener(\"click\", onClick);\n element.addEventListener(\"keydown\", onKeyDown);\n element.addEventListener(\"keyup\", onKeyUp);\n element.addEventListener(\"blur\", onBlur);\n return () => {\n element.removeEventListener(\"dragenter\", onDragEnter);\n element.removeEventListener(\"dragover\", onDragOver);\n element.removeEventListener(\"dragleave\", onDragLeave);\n element.removeEventListener(\"drop\", onDrop);\n element.removeEventListener(\"paste\", onPaste);\n element.removeEventListener(\"click\", onClick);\n element.removeEventListener(\"keydown\", onKeyDown);\n element.removeEventListener(\"keyup\", onKeyUp);\n element.removeEventListener(\"blur\", onBlur);\n };\n}\n"],"mappings":";;AAUA,SAAS,eAAe,QAA0C;CAC9D,IAAI,WAAW,KAAA,GACX,OAAO,CAAC;CAGZ,QADa,OAAO,WAAW,WAAW,OAAO,MAAM,GAAG,IAAI,OAAA,CAEzD,KAAK,YAAY,QAAQ,KAAK,CAAC,CAAC,YAAY,CAAC,CAAC,CAC9C,QAAQ,YAAY,YAAY,EAAE;AAC3C;;AAGA,SAAgB,gBACZ,QACkB;CAClB,MAAM,WACF,WAAW,KAAA,IACL,CAAC,KACA,OAAO,WAAW,WAAW,OAAO,MAAM,GAAG,IAAI,OAAA,CAC7C,KAAK,YAAY,QAAQ,KAAK,CAAC,CAAC,CAChC,QAAQ,YAAY,YAAY,EAAE;CACjD,OAAO,SAAS,WAAW,IAAI,KAAA,IAAY,SAAS,KAAK,GAAG;AAChE;;;;;;AAOA,SAAgB,YACZ,MACA,QACO;CACP,MAAM,WAAW,eAAe,MAAM;CACtC,IAAI,SAAS,WAAW,GACpB,OAAO;CAEX,MAAM,OAAO,KAAK,KAAK,YAAY;CACnC,MAAM,OAAO,KAAK,KAAK,YAAY;CACnC,OAAO,SAAS,MAAM,YAAY;EAC9B,IAAI,QAAQ,WAAW,GAAG,GACtB,OAAO,KAAK,SAAS,OAAO;EAEhC,IAAI,QAAQ,SAAS,IAAI,GACrB,OAAO,KAAK,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,QAAQ,oBAAoB,aAAa,WAAW;EACpD,QAAQ,oBAAoB,YAAY,UAAU;EAClD,QAAQ,oBAAoB,aAAa,WAAW;EACpD,QAAQ,oBAAoB,QAAQ,MAAM;EAC1C,QAAQ,oBAAoB,SAAS,OAAO;EAC5C,QAAQ,oBAAoB,SAAS,OAAO;EAC5C,QAAQ,oBAAoB,WAAW,SAAS;EAChD,QAAQ,oBAAoB,SAAS,OAAO;EAC5C,QAAQ,oBAAoB,QAAQ,MAAM;CAC9C;AACJ"}
package/dist/file.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { _ as FileLike, a as FileDialogOptions, b as acceptsFile, c as fieldFiles, d as FileRejection, f as FileRejectionReason, g as FileAccept, h as assignFiles, i as watchFileDrop, l as CheckedFile, m as checkFiles, n as FileDropReason, o as openFileDialog, p as FileRules, r as dragHasFiles, s as formatFileSize, t as FileDropOptions, u as FileCheck, v as acceptAttribute, y as acceptFiles } from "./index-B4wF1isn.js";
2
+ export { type CheckedFile, type FileAccept, type FileCheck, type FileDialogOptions, type FileDropOptions, type FileDropReason, type FileLike, type FileRejection, type FileRejectionReason, type FileRules, acceptAttribute, acceptFiles, acceptsFile, assignFiles, checkFiles, dragHasFiles, fieldFiles, formatFileSize, openFileDialog, watchFileDrop };
package/dist/file.js ADDED
@@ -0,0 +1 @@
1
+ export { c as acceptAttribute, l as acceptFiles, u as acceptsFile, s as assignFiles, o as checkFiles, t as dragHasFiles, a as fieldFiles, i as formatFileSize, r as openFileDialog, n as watchFileDrop } from "./file-DSLtfzbi.js";
@@ -0,0 +1,75 @@
1
+ import { t as getTabbables } from "./tabbable-BPiLssIv.js";
2
+ // src/focus/focus-trap.ts
3
+ /**
4
+ * Keeps Tab and Shift+Tab inside `container` (spec/dialog.md, "Focus"), wrapping at its ends, and
5
+ * pulls focus back when it escapes (to an element outside `container` and outside `allow()`, the
6
+ * child layers). Returns the cleanup.
7
+ */
8
+ function trapFocus(container, allow = () => [], options = {}) {
9
+ const doc = container.ownerDocument;
10
+ const inside = (target) => target instanceof Node && (container.contains(target) || allow().some((element) => element?.contains(target) === true));
11
+ const onKeyDown = (event) => {
12
+ if (event.key !== "Tab" || event.defaultPrevented) return;
13
+ if (!container.contains(doc.activeElement)) return;
14
+ const tabbables = getTabbables(container);
15
+ if (tabbables.length === 0) {
16
+ event.preventDefault();
17
+ container.focus();
18
+ return;
19
+ }
20
+ const first = tabbables[0];
21
+ const last = tabbables.at(-1);
22
+ const active = doc.activeElement;
23
+ if (event.shiftKey && (active === first || active === container)) {
24
+ event.preventDefault();
25
+ last.focus();
26
+ } else if (!event.shiftKey && active === last) {
27
+ event.preventDefault();
28
+ first.focus();
29
+ }
30
+ };
31
+ const onFocusIn = (event) => {
32
+ if (!inside(event.target)) (getTabbables(container)[0] ?? container).focus();
33
+ };
34
+ const onMouseDown = (event) => {
35
+ if (!inside(event.target)) event.preventDefault();
36
+ };
37
+ const hold = options.holdOutsidePress === true;
38
+ doc.addEventListener("keydown", onKeyDown);
39
+ doc.addEventListener("focusin", onFocusIn);
40
+ if (hold) doc.addEventListener("mousedown", onMouseDown, true);
41
+ return () => {
42
+ doc.removeEventListener("keydown", onKeyDown);
43
+ doc.removeEventListener("focusin", onFocusIn);
44
+ if (hold) doc.removeEventListener("mousedown", onMouseDown, true);
45
+ };
46
+ }
47
+ /** Focuses `initial`, else the first tabbable element of `container`, else `container`. */
48
+ function focusInitial(container, initial) {
49
+ (initial ?? getTabbables(container)[0] ?? container).focus({ preventScroll: true });
50
+ }
51
+ // src/focus/focus-visible.ts
52
+ /** Per document: whether its last input was a key (a document starts listening on first call). */
53
+ const keyboardInput = /* @__PURE__ */ new WeakMap();
54
+ /**
55
+ * Whether the last focus in `doc` came from the keyboard (for overlays that highlight on keyboard
56
+ * open only). Each document is tracked on its own (an iframe's, a test's), from its first call.
57
+ */
58
+ function isKeyboardModality(doc) {
59
+ let state = keyboardInput.get(doc);
60
+ if (state === void 0) {
61
+ const tracked = { keyboard: false };
62
+ state = tracked;
63
+ keyboardInput.set(doc, tracked);
64
+ doc.addEventListener("keydown", () => {
65
+ tracked.keyboard = true;
66
+ }, true);
67
+ doc.addEventListener("pointerdown", () => {
68
+ tracked.keyboard = false;
69
+ }, true);
70
+ }
71
+ return state.keyboard;
72
+ }
73
+ export { focusInitial as n, trapFocus as r, isKeyboardModality as t };
74
+
75
+ //# sourceMappingURL=focus-BI-VvM0f.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"focus-BI-VvM0f.js","names":[],"sources":["../src/focus/focus-trap.ts","../src/focus/focus-visible.ts"],"sourcesContent":["import { getTabbables } from \"./tabbable.ts\";\n\nexport interface FocusTrapOptions {\n /**\n * A press outside `container` and `allow()` keeps focus where it is: its default focus change\n * is prevented (an alert dialog, which an outside press never dismisses: spec/alert-dialog.md,\n * \"Focus\").\n */\n holdOutsidePress?: boolean;\n}\n\n/**\n * Keeps Tab and Shift+Tab inside `container` (spec/dialog.md, \"Focus\"), wrapping at its ends, and\n * pulls focus back when it escapes (to an element outside `container` and outside `allow()`, the\n * child layers). Returns the cleanup.\n */\nexport function trapFocus(\n container: HTMLElement,\n allow: () => readonly (Element | null | undefined)[] = () => [],\n options: FocusTrapOptions = {},\n): () => void {\n const doc = container.ownerDocument;\n const inside = (target: EventTarget | null) =>\n target instanceof Node &&\n (container.contains(target) ||\n allow().some((element) => element?.contains(target) === true));\n\n const onKeyDown = (event: KeyboardEvent) => {\n if (event.key !== \"Tab\" || event.defaultPrevented) {\n return;\n }\n // a child layer handles its own Tab\n if (!container.contains(doc.activeElement)) {\n return;\n }\n const tabbables = getTabbables(container);\n if (tabbables.length === 0) {\n event.preventDefault();\n container.focus();\n return;\n }\n const first = tabbables[0] as HTMLElement;\n const last = tabbables.at(-1) as HTMLElement;\n const active = doc.activeElement;\n if (event.shiftKey && (active === first || active === container)) {\n event.preventDefault();\n last.focus();\n } else if (!event.shiftKey && active === last) {\n event.preventDefault();\n first.focus();\n }\n };\n const onFocusIn = (event: FocusEvent) => {\n if (!inside(event.target)) {\n (getTabbables(container)[0] ?? container).focus();\n }\n };\n // the focus a press gives is the `mousedown`'s default (after a touch, its compatibility one)\n const onMouseDown = (event: MouseEvent) => {\n if (!inside(event.target)) {\n event.preventDefault();\n }\n };\n const hold = options.holdOutsidePress === true;\n doc.addEventListener(\"keydown\", onKeyDown);\n doc.addEventListener(\"focusin\", onFocusIn);\n if (hold) {\n doc.addEventListener(\"mousedown\", onMouseDown, true);\n }\n return () => {\n doc.removeEventListener(\"keydown\", onKeyDown);\n doc.removeEventListener(\"focusin\", onFocusIn);\n if (hold) {\n doc.removeEventListener(\"mousedown\", onMouseDown, true);\n }\n };\n}\n\n/** Focuses `initial`, else the first tabbable element of `container`, else `container`. */\nexport function focusInitial(\n container: HTMLElement,\n initial?: HTMLElement | null,\n): void {\n (initial ?? getTabbables(container)[0] ?? container).focus({\n preventScroll: true,\n });\n}\n","/** Per document: whether its last input was a key (a document starts listening on first call). */\nconst keyboardInput = /* @__PURE__ */ new WeakMap<\n Document,\n { keyboard: boolean }\n>();\n\n/**\n * Whether the last focus in `doc` came from the keyboard (for overlays that highlight on keyboard\n * open only). Each document is tracked on its own (an iframe's, a test's), from its first call.\n */\nexport function isKeyboardModality(doc: Document): boolean {\n let state = keyboardInput.get(doc);\n if (state === undefined) {\n const tracked = { keyboard: false };\n state = tracked;\n keyboardInput.set(doc, tracked);\n doc.addEventListener(\n \"keydown\",\n () => {\n tracked.keyboard = true;\n },\n true,\n );\n doc.addEventListener(\n \"pointerdown\",\n () => {\n tracked.keyboard = false;\n },\n true,\n );\n }\n return state.keyboard;\n}\n"],"mappings":";;;;;;;AAgBA,SAAgB,UACZ,WACA,cAA6D,CAAC,GAC9D,UAA4B,CAAC,GACnB;CACV,MAAM,MAAM,UAAU;CACtB,MAAM,UAAU,WACZ,kBAAkB,SACjB,UAAU,SAAS,MAAM,KACtB,MAAM,CAAC,CAAC,MAAM,YAAY,SAAS,SAAS,MAAM,MAAM,IAAI;CAEpE,MAAM,aAA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AU,KAAA,GAAW;EACrB,MAAM,UAAU,EAAE,UAAU,MAAM;EAClC,QAAQ;EACR,cAAc,IAAI,KAAK,OAAO;EAC9B,IAAI,iBACA,iBACM;GACF,QAAQ,WAAW;EACvB,GACA,IACJ;EACA,IAAI,iBACA,qBACM;GACF,QAAQ,WAAW;EACvB,GACA,IACJ;CACJ;CACA,OAAO,MAAM;AACjB"}
@@ -0,0 +1,2 @@
1
+ import { a as FocusTrapOptions, i as isKeyboardModality, n as nextTabbableAfter, o as focusInitial, r as previousTabbableBefore, s as trapFocus, t as getTabbables } from "./index-CMig3TXL.js";
2
+ export { type FocusTrapOptions, focusInitial, getTabbables, isKeyboardModality, nextTabbableAfter, previousTabbableBefore, trapFocus };
package/dist/focus.js ADDED
@@ -0,0 +1,3 @@
1
+ import { n as nextTabbableAfter, r as previousTabbableBefore, t as getTabbables } from "./tabbable-BPiLssIv.js";
2
+ import { n as focusInitial, r as trapFocus, t as isKeyboardModality } from "./focus-BI-VvM0f.js";
3
+ export { focusInitial, getTabbables, isKeyboardModality, nextTabbableAfter, previousTabbableBefore, trapFocus };