@fragiola/primitives 0.0.0-stage → 0.1.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 (192) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/LICENSE +21 -0
  3. package/README.md +50 -2
  4. package/dist/async-list-Bv6ff5Oj.js +81 -0
  5. package/dist/async-list-Bv6ff5Oj.js.map +1 -0
  6. package/dist/async-list.d.ts +2 -0
  7. package/dist/async-list.js +1 -0
  8. package/dist/calendar-keys-DGYpjLwv.d.ts +12 -0
  9. package/dist/calendar-system-B5U5HcPQ.d.ts +415 -0
  10. package/dist/calendar-systems-Daa6Q_ca.js +659 -0
  11. package/dist/calendar-systems-Daa6Q_ca.js.map +1 -0
  12. package/dist/calendar-systems.d.ts +3 -0
  13. package/dist/calendar-systems.js +1 -0
  14. package/dist/carousel-LRbWqbAx.js +375 -0
  15. package/dist/carousel-LRbWqbAx.js.map +1 -0
  16. package/dist/carousel.d.ts +2 -0
  17. package/dist/carousel.js +1 -0
  18. package/dist/cascade-B155Xvvv.js +64 -0
  19. package/dist/cascade-B155Xvvv.js.map +1 -0
  20. package/dist/cascade.d.ts +2 -0
  21. package/dist/cascade.js +1 -0
  22. package/dist/clipboard-Cy2cbGEW.js +33 -0
  23. package/dist/clipboard-Cy2cbGEW.js.map +1 -0
  24. package/dist/clipboard.d.ts +2 -0
  25. package/dist/clipboard.js +1 -0
  26. package/dist/collection-Dbk-PB5S.js +609 -0
  27. package/dist/collection-Dbk-PB5S.js.map +1 -0
  28. package/dist/collection.d.ts +2 -0
  29. package/dist/collection.js +3 -0
  30. package/dist/color-sURV5Irg.js +699 -0
  31. package/dist/color-sURV5Irg.js.map +1 -0
  32. package/dist/color.d.ts +2 -0
  33. package/dist/color.js +1 -0
  34. package/dist/command-DueCXi65.js +104 -0
  35. package/dist/command-DueCXi65.js.map +1 -0
  36. package/dist/command.d.ts +2 -0
  37. package/dist/command.js +1 -0
  38. package/dist/date-CrbFSJKQ.js +215 -0
  39. package/dist/date-CrbFSJKQ.js.map +1 -0
  40. package/dist/date.d.ts +4 -0
  41. package/dist/date.js +1 -0
  42. package/dist/direction-CTnVLpmd.d.ts +8 -0
  43. package/dist/direction-DJDPvnxJ.js +13 -0
  44. package/dist/direction-DJDPvnxJ.js.map +1 -0
  45. package/dist/direction.d.ts +2 -0
  46. package/dist/direction.js +1 -0
  47. package/dist/drag-session-CSr06B5v.js +41 -0
  48. package/dist/drag-session-CSr06B5v.js.map +1 -0
  49. package/dist/file-DSLtfzbi.js +263 -0
  50. package/dist/file-DSLtfzbi.js.map +1 -0
  51. package/dist/file.d.ts +2 -0
  52. package/dist/file.js +1 -0
  53. package/dist/focus-BI-VvM0f.js +75 -0
  54. package/dist/focus-BI-VvM0f.js.map +1 -0
  55. package/dist/focus.d.ts +2 -0
  56. package/dist/focus.js +3 -0
  57. package/dist/form-DovwKtqz.js +501 -0
  58. package/dist/form-DovwKtqz.js.map +1 -0
  59. package/dist/form.d.ts +2 -0
  60. package/dist/form.js +1 -0
  61. package/dist/hover-BakyPYnC.js +62 -0
  62. package/dist/hover-BakyPYnC.js.map +1 -0
  63. package/dist/hover.d.ts +2 -0
  64. package/dist/hover.js +1 -0
  65. package/dist/id-B0Y8iZrc.js +13 -0
  66. package/dist/id-B0Y8iZrc.js.map +1 -0
  67. package/dist/id.d.ts +2 -0
  68. package/dist/id.js +1 -0
  69. package/dist/image-D4X_yRYw.js +44 -0
  70. package/dist/image-D4X_yRYw.js.map +1 -0
  71. package/dist/image.d.ts +2 -0
  72. package/dist/image.js +1 -0
  73. package/dist/index-B0rwIZw9.d.ts +51 -0
  74. package/dist/index-B4wF1isn.d.ts +128 -0
  75. package/dist/index-BHiBRE01.d.ts +20 -0
  76. package/dist/index-BNlMm-0u.d.ts +17 -0
  77. package/dist/index-BapNrjY9.d.ts +48 -0
  78. package/dist/index-Bd-tVb7C.d.ts +6 -0
  79. package/dist/index-BjE8nDiX.d.ts +125 -0
  80. package/dist/index-BpbT-FsX.d.ts +117 -0
  81. package/dist/index-CBgGcTeX.d.ts +37 -0
  82. package/dist/index-CGThV_Md.d.ts +100 -0
  83. package/dist/index-CMig3TXL.d.ts +28 -0
  84. package/dist/index-CWFRduRd.d.ts +212 -0
  85. package/dist/index-CkrHWyU_.d.ts +33 -0
  86. package/dist/index-Cwu6f7y1.d.ts +18 -0
  87. package/dist/index-D10LDdR6.d.ts +139 -0
  88. package/dist/index-D146vm21.d.ts +104 -0
  89. package/dist/index-DGKhEvNr.d.ts +107 -0
  90. package/dist/index-DbAu4s16.d.ts +48 -0
  91. package/dist/index-DdaCj6IO.d.ts +54 -0
  92. package/dist/index-DeHcNdjC.d.ts +68 -0
  93. package/dist/index-DeR4ncrW.d.ts +93 -0
  94. package/dist/index-HIksgK0e.d.ts +70 -0
  95. package/dist/index-IZB0mlZl.d.ts +71 -0
  96. package/dist/index-IfDZj87U.d.ts +28 -0
  97. package/dist/index-SrTuVBms.d.ts +75 -0
  98. package/dist/index-T9UcYvkf.d.ts +339 -0
  99. package/dist/index-fRTojAoR.d.ts +31 -0
  100. package/dist/index-fuLUke9n.d.ts +6 -0
  101. package/dist/index-l63esAck.d.ts +104 -0
  102. package/dist/index-raSUg9U7.d.ts +38 -0
  103. package/dist/index-twgzPKvZ.d.ts +278 -0
  104. package/dist/index-wBpjRykh.d.ts +125 -0
  105. package/dist/index.d.ts +39 -0
  106. package/dist/index.js +42 -0
  107. package/dist/inert-DlGToy3q.js +80 -0
  108. package/dist/inert-DlGToy3q.js.map +1 -0
  109. package/dist/inert.d.ts +2 -0
  110. package/dist/inert.js +1 -0
  111. package/dist/layer-stack-BEtzaFC1.js +118 -0
  112. package/dist/layer-stack-BEtzaFC1.js.map +1 -0
  113. package/dist/layers-C-N4rrZX.js +24 -0
  114. package/dist/layers-C-N4rrZX.js.map +1 -0
  115. package/dist/layers.d.ts +2 -0
  116. package/dist/layers.js +3 -0
  117. package/dist/list-navigation-CbWv4DK5.js +40 -0
  118. package/dist/list-navigation-CbWv4DK5.js.map +1 -0
  119. package/dist/month-year-BG5imFUq.js +108 -0
  120. package/dist/month-year-BG5imFUq.js.map +1 -0
  121. package/dist/month-year.d.ts +3 -0
  122. package/dist/month-year.js +1 -0
  123. package/dist/number-BFUio97d.js +316 -0
  124. package/dist/number-BFUio97d.js.map +1 -0
  125. package/dist/number.d.ts +2 -0
  126. package/dist/number.js +1 -0
  127. package/dist/overlay-BwPyiM3T.js +78 -0
  128. package/dist/overlay-BwPyiM3T.js.map +1 -0
  129. package/dist/overlay.d.ts +2 -0
  130. package/dist/overlay.js +1 -0
  131. package/dist/pagination-BkVzaIGP.js +51 -0
  132. package/dist/pagination-BkVzaIGP.js.map +1 -0
  133. package/dist/pagination.d.ts +2 -0
  134. package/dist/pagination.js +1 -0
  135. package/dist/plain-date-idQH1P6X.d.ts +29 -0
  136. package/dist/plain-date-time-B8PGahhe.d.ts +99 -0
  137. package/dist/pointer-CuDtD9XA.js +189 -0
  138. package/dist/pointer-CuDtD9XA.js.map +1 -0
  139. package/dist/pointer.d.ts +2 -0
  140. package/dist/pointer.js +3 -0
  141. package/dist/positioning-DDUQ75mB.js +148 -0
  142. package/dist/positioning-DDUQ75mB.js.map +1 -0
  143. package/dist/positioning.d.ts +2 -0
  144. package/dist/positioning.js +1 -0
  145. package/dist/presence-BhKDOmII.js +86 -0
  146. package/dist/presence-BhKDOmII.js.map +1 -0
  147. package/dist/presence.d.ts +2 -0
  148. package/dist/presence.js +1 -0
  149. package/dist/range-o0_GBhyY.js +158 -0
  150. package/dist/range-o0_GBhyY.js.map +1 -0
  151. package/dist/range.d.ts +2 -0
  152. package/dist/range.js +1 -0
  153. package/dist/scroll-area-BYNhv36t.js +447 -0
  154. package/dist/scroll-area-BYNhv36t.js.map +1 -0
  155. package/dist/scroll-area.d.ts +2 -0
  156. package/dist/scroll-area.js +1 -0
  157. package/dist/scroll-lock-JzRNOA1O.js +42 -0
  158. package/dist/scroll-lock-JzRNOA1O.js.map +1 -0
  159. package/dist/scroll-lock.d.ts +2 -0
  160. package/dist/scroll-lock.js +1 -0
  161. package/dist/segments-eRu5G_Bd.js +587 -0
  162. package/dist/segments-eRu5G_Bd.js.map +1 -0
  163. package/dist/segments.d.ts +3 -0
  164. package/dist/segments.js +1 -0
  165. package/dist/selection-Cor67w_V.js +287 -0
  166. package/dist/selection-Cor67w_V.js.map +1 -0
  167. package/dist/selection.d.ts +2 -0
  168. package/dist/selection.js +1 -0
  169. package/dist/swipe.d.ts +2 -0
  170. package/dist/swipe.js +1 -0
  171. package/dist/tabbable-BPiLssIv.js +35 -0
  172. package/dist/tabbable-BPiLssIv.js.map +1 -0
  173. package/dist/time-Ct6oq9kt.js +208 -0
  174. package/dist/time-Ct6oq9kt.js.map +1 -0
  175. package/dist/time-zone-DegkzwST.js +305 -0
  176. package/dist/time-zone-DegkzwST.js.map +1 -0
  177. package/dist/time-zone.d.ts +2 -0
  178. package/dist/time-zone.js +1 -0
  179. package/dist/time.d.ts +2 -0
  180. package/dist/time.js +1 -0
  181. package/dist/toast-BjRpJalM.js +400 -0
  182. package/dist/toast-BjRpJalM.js.map +1 -0
  183. package/dist/toast.d.ts +2 -0
  184. package/dist/toast.js +1 -0
  185. package/dist/unit-grid-DJy8lb5D.d.ts +21 -0
  186. package/dist/virtual-BlX1RuHD.js +306 -0
  187. package/dist/virtual-BlX1RuHD.js.map +1 -0
  188. package/dist/virtual.d.ts +2 -0
  189. package/dist/virtual.js +1 -0
  190. package/dist/watch-swipe-BIucfr9O.js +301 -0
  191. package/dist/watch-swipe-BIucfr9O.js.map +1 -0
  192. package/package.json +234 -3
@@ -0,0 +1,587 @@
1
+ // src/segments/segment-format.ts
2
+ const EDITABLE = /* @__PURE__ */ new Set([
3
+ "year",
4
+ "month",
5
+ "day",
6
+ "hour",
7
+ "minute",
8
+ "second",
9
+ "dayPeriod"
10
+ ]);
11
+ /** A UTC `Date` of the fields, years 0–99 included (`Date.UTC` reads them as 19xx). */
12
+ function utcDate(year, month, day, hour = 0, minute = 0, second = 0) {
13
+ const date = /* @__PURE__ */ new Date(0);
14
+ date.setUTCFullYear(year, month - 1, day);
15
+ date.setUTCHours(hour, minute, second, 0);
16
+ return date;
17
+ }
18
+ function formatOptions(kind, granularity, hourCycle) {
19
+ const options = { timeZone: "UTC" };
20
+ if (kind === "date") {
21
+ options.year = "numeric";
22
+ options.month = "numeric";
23
+ options.day = "numeric";
24
+ }
25
+ if (granularity !== "day") {
26
+ options.hour = "numeric";
27
+ if (granularity !== "hour") options.minute = "2-digit";
28
+ if (granularity === "second") options.second = "2-digit";
29
+ if (hourCycle === 12) options.hour12 = true;
30
+ else if (hourCycle === 24) options.hourCycle = "h23";
31
+ else if (hourCycle !== void 0) options.hourCycle = hourCycle;
32
+ }
33
+ return options;
34
+ }
35
+ /**
36
+ * A literal with its spaces as one kind: engines write the space before a day period as U+0020,
37
+ * U+00A0 or U+202F depending on their ICU (Node and Chromium differ), so a server render and its
38
+ * hydration would disagree; the bidirectional marks stay.
39
+ */
40
+ function normalizeLiteral(text) {
41
+ return text.replace(/[\u00a0\u2007\u202f]/g, " ");
42
+ }
43
+ function partType(type) {
44
+ return EDITABLE.has(type) || type === "era" || type === "timeZoneName" ? type : "literal";
45
+ }
46
+ /** The ten digits of a locale's numbering system. */
47
+ function localeDigits(locale, numberingSystem) {
48
+ const format = new Intl.NumberFormat(locale, {
49
+ numberingSystem,
50
+ useGrouping: false
51
+ });
52
+ return Array.from({ length: 10 }, (_, digit) => format.format(digit));
53
+ }
54
+ function segmentNames(locale) {
55
+ const Names = Intl.DisplayNames;
56
+ if (Names === void 0) return {};
57
+ try {
58
+ const names = new Names(locale, { type: "dateTimeField" });
59
+ const result = {};
60
+ for (const type of [
61
+ "era",
62
+ ...EDITABLE,
63
+ "timeZoneName"
64
+ ]) {
65
+ const name = names.of(type);
66
+ if (name !== void 0) result[type] = name;
67
+ }
68
+ return result;
69
+ } catch {
70
+ return {};
71
+ }
72
+ }
73
+ /**
74
+ * The format of a date or time field in a locale (spec/date-field.md, "Segments"): the parts of
75
+ * `Intl.DateTimeFormat#formatToParts` for the field's segments, in the locale's order, with its
76
+ * literals, hour cycle and digits.
77
+ */
78
+ function segmentFormat(options) {
79
+ const { locale, kind, calendar, timeZone } = options;
80
+ const granularity = options.granularity === void 0 || kind === "time" && options.granularity === "day" ? kind === "time" ? "minute" : "day" : options.granularity;
81
+ const intlLocale = calendar?.locale(locale) ?? locale;
82
+ const intl = formatOptions(kind, granularity, options.hourCycle);
83
+ const formatter = new Intl.DateTimeFormat(intlLocale, timeZone?.segmentOptions(options, intl) ?? intl);
84
+ const resolved = formatter.resolvedOptions();
85
+ const parts = [];
86
+ for (const part of formatter.formatToParts(utcDate(2026, 11, 23, 14, 35, 45))) {
87
+ const type = partType(part.type);
88
+ const previous = parts[parts.length - 1];
89
+ if (type === "literal" && previous?.type === "literal") {
90
+ parts[parts.length - 1] = {
91
+ type,
92
+ literal: previous.literal + normalizeLiteral(part.value)
93
+ };
94
+ continue;
95
+ }
96
+ parts.push({
97
+ type,
98
+ literal: type === "literal" ? normalizeLiteral(part.value) : type === "era" ? part.value : ""
99
+ });
100
+ }
101
+ const types = parts.map((part) => part.type).filter((type) => EDITABLE.has(type));
102
+ const hasHour = types.includes("hour");
103
+ const period = (hour) => formatter.formatToParts(utcDate(2026, 11, 23, hour)).find((part) => part.type === "dayPeriod")?.value ?? "";
104
+ const format = {
105
+ ...options,
106
+ granularity,
107
+ parts,
108
+ types,
109
+ hourCycle: hasHour ? resolved.hourCycle ?? "h23" : null,
110
+ digits: localeDigits(locale, resolved.numberingSystem),
111
+ dayPeriods: types.includes("dayPeriod") ? [period(0), period(12)] : ["", ""],
112
+ names: segmentNames(locale),
113
+ formatter,
114
+ monthNames: new Intl.DateTimeFormat(intlLocale, {
115
+ month: "long",
116
+ timeZone: "UTC"
117
+ })
118
+ };
119
+ return calendar?.segmentFormat(format) ?? format;
120
+ }
121
+ // src/segments/segment-values.ts
122
+ /** The amount Page Up and Page Down step a segment by (spec/date-field.md, "Keyboard"). */
123
+ const SEGMENT_PAGE_STEPS = {
124
+ era: 1,
125
+ year: 5,
126
+ month: 2,
127
+ day: 7,
128
+ hour: 2,
129
+ minute: 15,
130
+ second: 15,
131
+ dayPeriod: 1
132
+ };
133
+ /** The days of a month; a month with no year set counts as in a leap year (February has 29). */
134
+ function monthDays(year, month) {
135
+ const date = /* @__PURE__ */ new Date(0);
136
+ date.setUTCFullYear(year ?? 2e3, month, 0);
137
+ return date.getUTCDate();
138
+ }
139
+ function hourRange(cycle) {
140
+ switch (cycle) {
141
+ case "h11": return {
142
+ min: 0,
143
+ max: 11
144
+ };
145
+ case "h12": return {
146
+ min: 1,
147
+ max: 12
148
+ };
149
+ case "h24": return {
150
+ min: 1,
151
+ max: 24
152
+ };
153
+ default: return {
154
+ min: 0,
155
+ max: 23
156
+ };
157
+ }
158
+ }
159
+ /** The range of a segment, given what else is set (a day's maximum follows its month and year). */
160
+ function segmentRange(format, values, type) {
161
+ const system = format.calendar?.segmentRange(format, values, type);
162
+ if (system) return system;
163
+ switch (type) {
164
+ case "year": return {
165
+ min: 1,
166
+ max: 9999,
167
+ digits: 4
168
+ };
169
+ case "month": return {
170
+ min: 1,
171
+ max: 12,
172
+ digits: 2
173
+ };
174
+ case "day": return {
175
+ min: 1,
176
+ max: values.month === void 0 ? 31 : monthDays(values.year, values.month),
177
+ digits: 2
178
+ };
179
+ case "hour": return {
180
+ ...hourRange(format.hourCycle),
181
+ digits: 2
182
+ };
183
+ case "minute":
184
+ case "second": return {
185
+ min: 0,
186
+ max: 59,
187
+ digits: 2
188
+ };
189
+ case "era":
190
+ case "dayPeriod": return {
191
+ min: 0,
192
+ max: 1,
193
+ digits: 0
194
+ };
195
+ }
196
+ }
197
+ /** The half of the day the values are in: the set day period, else the hour's, else `fallback`. */
198
+ function dayPeriodOf(values, fallback = 0) {
199
+ if (values.dayPeriod !== void 0) return values.dayPeriod;
200
+ return values.hour === void 0 ? fallback : values.hour >= 12 ? 1 : 0;
201
+ }
202
+ /** The hour as its cycle shows it (`h12`: 0 → 12, 13 → 1). */
203
+ function displayHour(hour, cycle) {
204
+ switch (cycle) {
205
+ case "h11": return hour % 12;
206
+ case "h12": return hour % 12 || 12;
207
+ case "h24": return hour || 24;
208
+ default: return hour;
209
+ }
210
+ }
211
+ /** The 0–23 hour of a shown hour in its cycle and half of the day. */
212
+ function hourOf(shown, cycle, period) {
213
+ switch (cycle) {
214
+ case "h11":
215
+ case "h12": return shown % 12 + (period === 1 ? 12 : 0);
216
+ case "h24": return shown % 24;
217
+ default: return shown;
218
+ }
219
+ }
220
+ /** A segment's value in the unit it shows; `undefined` while it is a placeholder. */
221
+ function shownValue(format, values, type) {
222
+ if (type === "hour") return values.hour === void 0 ? void 0 : displayHour(values.hour, format.hourCycle);
223
+ return values[type];
224
+ }
225
+ /**
226
+ * Sets a segment to a value in the unit it shows (an hour in its cycle, kept in its half of the
227
+ * day; a day period moves a set hour into its half), or clears it with `undefined`. A day past
228
+ * its month's end is clamped into it.
229
+ */
230
+ function setSegment(format, values, type, shown, seed = {}) {
231
+ const next = { ...values };
232
+ if (shown === void 0) {
233
+ delete next[type];
234
+ return next;
235
+ }
236
+ if (type === "hour") {
237
+ const period = format.types.includes("dayPeriod") ? dayPeriodOf(values, dayPeriodOf(seed)) : shown >= 12 ? 1 : 0;
238
+ next.hour = hourOf(shown, format.hourCycle, period);
239
+ return next;
240
+ }
241
+ if (type === "dayPeriod") {
242
+ next.dayPeriod = shown;
243
+ if (next.hour !== void 0) next.hour = next.hour % 12 + (shown === 1 ? 12 : 0);
244
+ return next;
245
+ }
246
+ next[type] = shown;
247
+ if (type === "month" || type === "year" || type === "era") for (const field of ["month", "day"]) {
248
+ const { max } = segmentRange(format, next, field);
249
+ if ((next[field] ?? 0) > max) next[field] = max;
250
+ }
251
+ return next;
252
+ }
253
+ /**
254
+ * Steps a segment by `amount`, wrapping within its range and carrying nothing into the others (an
255
+ * hour of a 12-hour cycle stays in its half of the day; the day period toggles). A placeholder
256
+ * takes the seed's value instead (the field's `placeholderValue`), with no step.
257
+ */
258
+ function stepSegment(format, values, type, amount, seed = {}) {
259
+ const current = shownValue(format, values, type);
260
+ if (current === void 0) {
261
+ if (type === "dayPeriod") return setSegment(format, values, type, dayPeriodOf(values, dayPeriodOf(seed)));
262
+ const seeded = type === "hour" ? displayHour(seed.hour ?? 0, format.hourCycle) : seed[type];
263
+ const range = segmentRange(format, values, type);
264
+ return setSegment(format, values, type, Math.min(Math.max(seeded ?? range.min, range.min), range.max), seed);
265
+ }
266
+ const { min, max } = segmentRange(format, values, type);
267
+ const size = max - min + 1;
268
+ return setSegment(format, values, type, ((current - min + amount) % size + size) % size + min, seed);
269
+ }
270
+ /** Sets a segment to the edge of its range (Home: `min`, End: `max`). */
271
+ function segmentToEdge(format, values, type, edge, seed = {}) {
272
+ return setSegment(format, values, type, segmentRange(format, values, type)[edge], seed);
273
+ }
274
+ /** Whether every editable segment of the format is set. */
275
+ function isSegmentsComplete(format, values) {
276
+ return format.types.every((type) => values[type] !== void 0);
277
+ }
278
+ /**
279
+ * The segment values of a date, a time or a date and time (only the format's segments; a day
280
+ * period follows the hour). `null` gives no value: every segment a placeholder.
281
+ */
282
+ function segmentValuesOf(format, input) {
283
+ if (input == null) return {};
284
+ const wall = format.timeZone?.wallTime(input) ?? input;
285
+ const value = format.calendar?.segmentValuesOf(format, wall) ?? wall;
286
+ const result = {};
287
+ for (const type of format.types) {
288
+ if (type === "dayPeriod") {
289
+ if (value.hour !== void 0) result.dayPeriod = value.hour >= 12 ? 1 : 0;
290
+ continue;
291
+ }
292
+ const field = value[type];
293
+ if (field !== void 0) result[type] = field;
294
+ }
295
+ return result;
296
+ }
297
+ /**
298
+ * The date of complete values (`null` while a date segment is a placeholder); with a calendar
299
+ * system's `format`, the ISO date of its fields (its hook).
300
+ */
301
+ function segmentsToDate(values, format) {
302
+ if (format?.calendar) return format.calendar.segmentsToDate(format, values);
303
+ const { year, month, day } = values;
304
+ return year === void 0 || month === void 0 || day === void 0 ? null : {
305
+ year,
306
+ month,
307
+ day
308
+ };
309
+ }
310
+ /** The time of values (the segments a format has not are 0); `null` with no hour. */
311
+ function segmentsToTime(values) {
312
+ return values.hour === void 0 ? null : {
313
+ hour: values.hour,
314
+ minute: values.minute ?? 0,
315
+ second: values.second ?? 0,
316
+ millisecond: 0
317
+ };
318
+ }
319
+ /**
320
+ * The date and time of values; `null` while the date or the hour is a placeholder. With a time
321
+ * zone's `format`, a `ZonedDateTime` resolved with its `disambiguation` (`null` when `reject`
322
+ * meets a wall time the zone skips or repeats).
323
+ */
324
+ function segmentsToDateTime(values, format) {
325
+ const date = segmentsToDate(values, format);
326
+ const time = segmentsToTime(values);
327
+ return date === null || time === null ? null : format?.timeZone ? format.timeZone.segmentsToDateTime(format, date, time) : {
328
+ ...date,
329
+ ...time
330
+ };
331
+ }
332
+ // src/segments/date-segments.ts
333
+ /** The text of a placeholder: one en dash per digit, two for a day period (no language). */
334
+ const SEGMENT_PLACEHOLDER = "–";
335
+ function editable(format, type) {
336
+ return format.types.includes(type);
337
+ }
338
+ /**
339
+ * The values, completed where they are placeholders, as a UTC date `Intl` formats (a calendar
340
+ * system's is its hook's): 2000 is a leap year, so a day set with no year keeps February 29.
341
+ */
342
+ function sampleDate(format, values) {
343
+ const year = values.year ?? 2e3;
344
+ const month = values.month ?? 1;
345
+ const last = utcDate(year, month + 1, 0).getUTCDate();
346
+ return format.calendar?.segmentSample(format, values) ?? utcDate(year, month, Math.min(values.day ?? 1, last), values.hour ?? (values.dayPeriod === 1 ? 12 : 0), values.minute ?? 0, values.second ?? 0);
347
+ }
348
+ /**
349
+ * The segments of a field (spec/date-field.md, "Segments"): its format's parts in order, each
350
+ * editable one with its text, value, range, value text and name, the literals with their text.
351
+ * `edit` shows the digits being typed into a segment that is not set yet (`0` of a month).
352
+ */
353
+ function dateSegments(format, values, edit = null) {
354
+ const sample = sampleDate(format, values);
355
+ const texts = /* @__PURE__ */ new Map();
356
+ for (const part of format.formatter.formatToParts(sample)) texts.set(part.type, part.value);
357
+ const local = (text) => [...text].map((digit) => format.digits[Number(digit)] ?? digit).join("");
358
+ return format.parts.map((part, index) => {
359
+ const { type } = part;
360
+ const label = format.names[type] ?? null;
361
+ if (!editable(format, type)) return {
362
+ type,
363
+ index,
364
+ text: format.timeZone?.segmentText(format, type, sample) ?? part.literal,
365
+ editable: false,
366
+ placeholder: false,
367
+ value: null,
368
+ min: null,
369
+ max: null,
370
+ valueText: null,
371
+ label
372
+ };
373
+ const range = segmentRange(format, values, type);
374
+ const value = type === "dayPeriod" ? values.dayPeriod : shownValue(format, values, type);
375
+ const typing = edit?.type === type && edit.text !== "";
376
+ const text = value === void 0 ? typing ? local(edit.text) : "–".repeat(Math.max(range.digits, 2)) : type === "dayPeriod" ? format.dayPeriods[value] ?? "" : texts.get(type) ?? local(String(value));
377
+ return {
378
+ type,
379
+ index,
380
+ text,
381
+ editable: true,
382
+ placeholder: value === void 0 && !typing,
383
+ value: value ?? null,
384
+ min: range.min,
385
+ max: range.max,
386
+ valueText: value === void 0 ? null : type === "month" ? format.monthNames.format(sample) : text,
387
+ label
388
+ };
389
+ });
390
+ }
391
+ // src/segments/segment-focus.ts
392
+ /**
393
+ * The focusable segments of `groups` (default: the group `segment` sits in), in their order and
394
+ * each group's document order.
395
+ */
396
+ function focusableSegments(segment, groups) {
397
+ const own = segment.closest("[role=\"group\"]");
398
+ return (groups ?? (own === null ? [] : [own])).flatMap((group) => [...group.querySelectorAll("[role=\"spinbutton\"]")]).filter((element) => element.tabIndex >= 0);
399
+ }
400
+ function center(element) {
401
+ const rect = element.getBoundingClientRect();
402
+ return rect.left + rect.width / 2;
403
+ }
404
+ /**
405
+ * The segment a move reaches from `segment`: `next` and `previous` in the locale's order (the
406
+ * document's), `left` and `right` on screen, wherever the bidirectional text placed the segments
407
+ * (a he-IL date reads left to right in a right-to-left page), `first` the group's first.
408
+ * `null` when there is none that way. With no layout (every box empty), `right` is `next` and
409
+ * `left` is `previous`. `groups` makes several groups one sequence (a range field's start and end,
410
+ * spec/date-range-field.md "Typing and keys"); by default the moves stay in the segment's group.
411
+ */
412
+ function segmentTarget(segment, move, groups) {
413
+ const segments = focusableSegments(segment, groups);
414
+ const index = segments.indexOf(segment);
415
+ if (move === "first") return segments[0] ?? null;
416
+ const laidOut = segments.some((element) => element.getBoundingClientRect().width > 0);
417
+ if (move === "next" || move === "previous" || !laidOut) return index < 0 ? null : segments[index + (move === "next" || move === "right" ? 1 : -1)] ?? null;
418
+ const from = center(segment);
419
+ let best = null;
420
+ let distance = Number.POSITIVE_INFINITY;
421
+ for (const candidate of segments) {
422
+ const offset = center(candidate) - from;
423
+ if (candidate !== segment && Math.sign(offset) === (move === "right" ? 1 : -1) && Math.abs(offset) < distance) {
424
+ best = candidate;
425
+ distance = Math.abs(offset);
426
+ }
427
+ }
428
+ return best;
429
+ }
430
+ /**
431
+ * Focuses the first segment when its field's label is pressed (spec/date-field.md, "In a Field":
432
+ * a label names a group, which no `for` can point at). A press the consumer prevented does
433
+ * nothing. Returns the cleanup.
434
+ */
435
+ function focusOnLabelPress(label, target) {
436
+ const onClick = (event) => {
437
+ if (event.defaultPrevented) return;
438
+ target()?.focus();
439
+ };
440
+ label.addEventListener("click", onClick);
441
+ return () => label.removeEventListener("click", onClick);
442
+ }
443
+ // src/segments/segment-typing.ts
444
+ const SHARED_DIGITS = [
445
+ "0123456789",
446
+ "٠١٢٣٤٥٦٧٨٩",
447
+ "۰۱۲۳۴۵۶۷۸۹"
448
+ ];
449
+ /** The digit a character is in the locale's numbering system or a shared one; `null` for none. */
450
+ function segmentDigit(format, character) {
451
+ for (const digits of SHARED_DIGITS) {
452
+ const index = digits.indexOf(character);
453
+ if (index >= 0 && character.length === 1) return index;
454
+ }
455
+ const index = format.digits.indexOf(character);
456
+ return index >= 0 ? index : null;
457
+ }
458
+ /**
459
+ * Types one character into a segment (spec/date-field.md, "Typing"). A digit continues the
460
+ * segment's typed digits, or starts again when the number would leave the range; the segment is
461
+ * set once the number is in range (`0` alone in a month waits for the next digit), and complete
462
+ * (`advance`) when no further digit could fit: `4` in a month, `12`, or a year's fourth digit. A
463
+ * day period takes a letter: the first letter of its AM or PM text. Anything else is refused
464
+ * (`null`).
465
+ */
466
+ function typeSegment(format, values, type, character, edit, seed = {}) {
467
+ const names = {
468
+ dayPeriod: format.dayPeriods,
469
+ era: format.eras
470
+ }[type];
471
+ if (names) {
472
+ const key = character.toLocaleLowerCase(format.locale);
473
+ const index = names.findIndex((text) => text !== "" && text.toLocaleLowerCase(format.locale).startsWith(key));
474
+ if (index < 0 || key.trim() === "") return null;
475
+ return {
476
+ values: setSegment(format, values, type, index, seed),
477
+ edit: null,
478
+ advance: true
479
+ };
480
+ }
481
+ const digit = segmentDigit(format, character);
482
+ if (digit === null) return null;
483
+ const range = segmentRange(format, values, type);
484
+ let text = `${edit?.type === type ? edit.text : ""}${digit}`;
485
+ if (text.length > range.digits || Number(text) > range.max) text = String(digit);
486
+ const number = Number(text);
487
+ const next = number >= range.min && number <= range.max ? setSegment(format, values, type, number, seed) : setSegment(format, values, type, void 0);
488
+ const advance = text.length >= range.digits || Number(`${text}0`) > range.max;
489
+ return {
490
+ values: next,
491
+ edit: advance ? null : {
492
+ type,
493
+ text
494
+ },
495
+ advance: advance && next[type] !== void 0
496
+ };
497
+ }
498
+ /**
499
+ * Backspace (or Delete) in a segment: removes its last digit (a day period is cleared at once);
500
+ * a segment left with no digit is a placeholder. An empty segment reports `retreat`.
501
+ */
502
+ function eraseSegment(format, values, type, edit) {
503
+ if (type === "dayPeriod" || type === "era") return values[type] === void 0 ? {
504
+ values,
505
+ edit: null,
506
+ retreat: true
507
+ } : {
508
+ values: setSegment(format, values, type, void 0),
509
+ edit: null,
510
+ retreat: false
511
+ };
512
+ const shown = shownValue(format, values, type);
513
+ const current = edit?.type === type ? edit.text : shown === void 0 ? "" : String(shown);
514
+ if (current === "") return {
515
+ values,
516
+ edit: null,
517
+ retreat: true
518
+ };
519
+ const text = current.slice(0, -1);
520
+ if (text === "") return {
521
+ values: setSegment(format, values, type, void 0),
522
+ edit: null,
523
+ retreat: false
524
+ };
525
+ const range = segmentRange(format, values, type);
526
+ const number = Number(text);
527
+ return {
528
+ values: setSegment(format, values, type, number >= range.min && number <= range.max ? number : void 0),
529
+ edit: {
530
+ type,
531
+ text
532
+ },
533
+ retreat: false
534
+ };
535
+ }
536
+ // src/segments/segment-keys.ts
537
+ /**
538
+ * The effect of a key on a focused segment (spec/date-field.md, "Keyboard", the APG spinbutton):
539
+ * ArrowUp / ArrowDown step by one, Page Up / Page Down by the segment's page step, both wrapping
540
+ * within the segment; Home / End go to its minimum / maximum; ArrowLeft / ArrowRight move to the
541
+ * segment on that side; Backspace and Delete erase a digit, or move back from an empty segment.
542
+ * `null` for any other key. A read-only field only moves.
543
+ */
544
+ function segmentKeyDown(format, values, type, key, edit, options = {}) {
545
+ const { seed = {}, readOnly = false } = options;
546
+ if (key === "ArrowLeft" || key === "ArrowRight") return {
547
+ kind: "move",
548
+ values,
549
+ edit: null,
550
+ move: key === "ArrowLeft" ? "left" : "right"
551
+ };
552
+ if (readOnly) return null;
553
+ const step = (amount) => ({
554
+ kind: "step",
555
+ values: stepSegment(format, values, type, amount, seed),
556
+ edit: null,
557
+ move: null
558
+ });
559
+ const page = SEGMENT_PAGE_STEPS[type];
560
+ switch (key) {
561
+ case "ArrowUp": return step(1);
562
+ case "ArrowDown": return step(-1);
563
+ case "PageUp": return step(page);
564
+ case "PageDown": return step(-page);
565
+ case "Home":
566
+ case "End": return {
567
+ kind: "step",
568
+ values: segmentToEdge(format, values, type, key === "Home" ? "min" : "max", seed),
569
+ edit: null,
570
+ move: null
571
+ };
572
+ case "Backspace":
573
+ case "Delete": {
574
+ const result = eraseSegment(format, values, type, edit);
575
+ return {
576
+ kind: "erase",
577
+ values: result.values,
578
+ edit: result.edit,
579
+ move: result.retreat ? "previous" : null
580
+ };
581
+ }
582
+ default: return null;
583
+ }
584
+ }
585
+ export { segmentsToTime as _, focusOnLabelPress as a, stepSegment as b, dateSegments as c, isSegmentsComplete as d, segmentRange as f, segmentsToDateTime as g, segmentsToDate as h, typeSegment as i, SEGMENT_PAGE_STEPS as l, segmentValuesOf as m, eraseSegment as n, segmentTarget as o, segmentToEdge as p, segmentDigit as r, SEGMENT_PLACEHOLDER as s, segmentKeyDown as t, displayHour as u, setSegment as v, segmentFormat as x, shownValue as y };
586
+
587
+ //# sourceMappingURL=segments-eRu5G_Bd.js.map