@human-kit/ui 1.2.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,7 +60,15 @@ export type TimeSelectionState = {
60
60
  */
61
61
  readonly isInvalidDraft: boolean;
62
62
  getSegmentValue: (type: TimePickerEditableSegmentType) => string;
63
- setSegmentValue: (type: TimePickerEditableSegmentType, nextValue: string) => void;
63
+ /**
64
+ * Writes one segment and commits. The draft is held inside min/max unless `clamp` is
65
+ * `false`: a first digit still waiting for its pair is not a time yet.
66
+ */
67
+ setSegmentValue: (type: TimePickerEditableSegmentType, nextValue: string, options?: {
68
+ clamp?: boolean;
69
+ }) => void;
70
+ /** Holds the draft inside min/max and commits it: a pending digit that was left alone. */
71
+ clampDraft: () => void;
64
72
  /** Re-commits the current draft (publishes `null` when it is not committeable). */
65
73
  commitDraft: () => void;
66
74
  /**
@@ -166,12 +166,44 @@ export function createTimeSelectionState(options) {
166
166
  return segmentDraft.second;
167
167
  return segmentDraft.dayPeriod;
168
168
  }
169
- function setSegmentValue(type, nextValue) {
169
+ function setSegmentValue(type, nextValue, { clamp = true } = {}) {
170
170
  if (!options.isEditable())
171
171
  return;
172
172
  writeSegmentValue(type, nextValue);
173
+ if (clamp)
174
+ clampDraftToRange();
173
175
  commitFromDraft();
174
176
  }
177
+ function clampDraft() {
178
+ if (!options.isEditable())
179
+ return;
180
+ clampDraftToRange();
181
+ commitFromDraft();
182
+ }
183
+ /**
184
+ * Holds a typed or stepped draft inside `minValue`/`maxValue`, the way React Aria's
185
+ * segments hold a number inside its bounds: a complete time past the max becomes the
186
+ * max, one before the min becomes the min, at once rather than on blur. A first digit
187
+ * that waits for its pair is skipped (`1` on the way to `19` under a `10:00` min), and
188
+ * held once it is completed or left alone.
189
+ */
190
+ function clampDraftToRange() {
191
+ const granularity = options.granularity();
192
+ const parts = buildTimePartsFromDraft(segmentDraft, granularity, resolvedHourCycle);
193
+ if (!parts)
194
+ return;
195
+ const candidate = formatTimePickerValue(parts, granularity);
196
+ let boundary;
197
+ if (isTimeOutOfRange(candidate, undefined, normalizedMaxValue, granularity)) {
198
+ boundary = normalizedMaxValue;
199
+ }
200
+ else if (isTimeOutOfRange(candidate, normalizedMinValue, undefined, granularity)) {
201
+ boundary = normalizedMinValue;
202
+ }
203
+ if (!boundary)
204
+ return;
205
+ segmentDraft = toDraftFromTimeValue(boundary, resolvedHourCycle);
206
+ }
175
207
  /** Normalizes and writes a segment into the draft without committing it. */
176
208
  function writeSegmentValue(type, nextValue) {
177
209
  options.onBeforeSegmentMutation?.(type);
@@ -385,6 +417,7 @@ export function createTimeSelectionState(options) {
385
417
  },
386
418
  getSegmentValue,
387
419
  setSegmentValue,
420
+ clampDraft,
388
421
  commitDraft: commitFromDraft,
389
422
  setValue,
390
423
  selectWheelValue,
@@ -95,7 +95,7 @@ and `TableCheckboxIndicatorProps`.
95
95
 
96
96
  - Use `Table.Root` as the stateful container for focus, selection, and sorting state.
97
97
  - Use `keyboardNavigation` to choose how far the roving tab stop reaches into the body: `'grid'` (default) makes every body cell a focus target, `'row'` makes the row the only one, and `'none'` leaves the body inert. The cell navigation has a cost: one focus registration and one focus state for each cell. A virtualized table does this again for each row that it makes. Use `'row'` when the body is a list, and not a surface where the user works. Use `'none'` only when nothing in the body does an action. The header keeps its own navigation in every mode.
98
- - Use `selectionBehavior="toggle"` to let the user remove the selection of a selected row. Use `selectionBehavior="replace"` to keep a selected row selected when the user presses it again. Use `selectionBehavior="checkbox"` when a press on the row should only perform its action: then only the selection checkbox, `Space` or the select-all shortcut change the selection.
98
+ - Use `selectionBehavior="toggle"` to let the user remove the selection of a selected row. Use `selectionBehavior="replace"` to keep a selected row selected when the user presses it again. Use `selectionBehavior="checkbox"` when a press on the row must only do its action. Then only the selection checkbox, `Space` or the select-all shortcut change the selection.
99
99
  - `Table.Column` is a logical-only wrapper for column metadata; it does not render DOM by itself and should wrap a single `Table.ColumnHeaderCell`.
100
100
  - Wrap each header cell in `Table.Column` so the table can register stable column metadata.
101
101
  - Add `Table.ColumnResizer` inside `Table.ColumnHeaderCell` to make the owning `Table.Column` resizable.
@@ -182,6 +182,7 @@ Put the groups and their rows in one `items` array. In the `children` snippet of
182
182
  - A click, `Enter` or `Space` calls `onAction` of the section row. The table does not call `onRowAction` for a section row.
183
183
  - A mounted section row tells the table that it is a section row. With a virtualizer, also set `isSectionItem` on `Table.Body`, because the table does not know the section rows that are not mounted.
184
184
  - With a virtualizer, set `sectionRowHeight` when a section row has a different height from `rowHeight`. This also needs `isSectionItem`. Give the rows the same heights in CSS.
185
+ - When the section rows are not all the same height, pass `sectionRowHeight` as a function of the item. The table calls it only for the items that `isSectionItem` accepts.
185
186
  - The cell is not sticky. In a table with pinned columns, add `sticky left-0` (or a similar class) to the content of the section row to keep it in view.
186
187
 
187
188
  ## Composition contract
@@ -98,10 +98,13 @@
98
98
  if (!virtualizer || !virtualizerEnabled || !isSectionItem) return undefined;
99
99
  const { rowHeight, sectionRowHeight } = virtualizer;
100
100
  if (sectionRowHeight === undefined || sectionRowHeight === rowHeight) return undefined;
101
+ const sectionHeight =
102
+ typeof sectionRowHeight === 'function' ? sectionRowHeight : () => sectionRowHeight;
101
103
 
102
104
  const offsets = new Float64Array(itemList.length + 1);
103
105
  for (let index = 0; index < itemList.length; index += 1) {
104
- const height = isSectionItem(itemList[index]) ? sectionRowHeight : rowHeight;
106
+ const item = itemList[index];
107
+ const height = isSectionItem(item) ? sectionHeight(item) : rowHeight;
105
108
  offsets[index + 1] = offsets[index] + height;
106
109
  }
107
110
  return offsets;
@@ -57,13 +57,17 @@ export type TableHeaderProps = Omit<HTMLAttributes<HTMLTableSectionElement>, 'ch
57
57
  children?: Snippet;
58
58
  class?: string;
59
59
  };
60
- export type TableBodyVirtualizer = {
60
+ export type TableBodyVirtualizer<T extends TableRowItem = TableRowItem> = {
61
61
  rowHeight: number;
62
62
  /**
63
63
  * Height, in px, of a section row, when it differs from `rowHeight`. It needs `isSectionItem` on
64
64
  * `Table.Body` to know which items are section rows. Without it, each item is `rowHeight` high.
65
+ *
66
+ * A function sizes each section row on its own, for a body whose section rows are not all
67
+ * alike — a group's header beside the header of an open row's children, say. It is only
68
+ * called for the items `isSectionItem` accepts.
65
69
  */
66
- sectionRowHeight?: number;
70
+ sectionRowHeight?: number | ((item: T) => number);
67
71
  /**
68
72
  * Extra rows rendered above and below the viewport, defaulting to 18. It
69
73
  * buffers against row render cost: while the main thread builds rows, the
@@ -84,7 +88,7 @@ export type TableBodyManualProps = TableBodyBaseProps & {
84
88
  };
85
89
  export type TableBodyItemsProps<T extends TableRowItem = TableRowItem> = TableBodyBaseProps & {
86
90
  items: readonly T[];
87
- virtualizer?: TableBodyVirtualizer;
91
+ virtualizer?: TableBodyVirtualizer<T>;
88
92
  /**
89
93
  * Tells which items render a `Table.SectionRow`. A section row that is mounted declares itself,
90
94
  * so this is optional without a virtualizer. With one, pass it: it keeps the section rows that
@@ -28,7 +28,9 @@ export type TimePickerContext = {
28
28
  setValue: (nextValue: TimePickerTimeValue | null) => void;
29
29
  getSegments: () => TimePickerSegmentPart[];
30
30
  getSegmentValue: (type: Exclude<TimePickerSegmentType, 'literal'>) => string;
31
- setSegmentValue: (type: Exclude<TimePickerSegmentType, 'literal'>, nextValue: string) => void;
31
+ setSegmentValue: (type: Exclude<TimePickerSegmentType, 'literal'>, nextValue: string, options?: {
32
+ clamp?: boolean;
33
+ }) => void;
32
34
  typeSegmentDigit: (type: Exclude<TimePickerSegmentType, 'literal'>, digit: string) => boolean;
33
35
  adjustSegmentValue: (type: Exclude<TimePickerSegmentType, 'literal'>, step: number) => void;
34
36
  getSegmentLabel: (type: TimePickerEditableSegmentType) => string;
@@ -197,6 +197,7 @@
197
197
  if (!nextWithin && activeSegment !== null) {
198
198
  activeSegment = null;
199
199
  segmentTypeBuffer = createEmptyTimePickerDraft();
200
+ timeSelection.clampDraft();
200
201
  }
201
202
  if (focusWithin === nextWithin) return;
202
203
  focusWithin = nextWithin;
@@ -206,6 +207,8 @@
206
207
  if (activeSegment === segment) return;
207
208
  activeSegment = segment;
208
209
  segmentTypeBuffer = createEmptyTimePickerDraft();
210
+ // A digit left waiting, or one erased, is held to min/max once the segment is left.
211
+ timeSelection.clampDraft();
209
212
  }
210
213
 
211
214
  function openPopover(reason: TimePickerOpenChangeReason = 'imperative-action', event?: Event) {
@@ -246,14 +249,11 @@
246
249
 
247
250
  const threshold = getTypingThreshold(type);
248
251
  if (candidate.length === 1) {
249
- timeSelection.setSegmentValue(type, candidate);
250
- segmentTypeBuffer[type] = candidate;
251
- const numeric = Number(candidate);
252
- if (Number.isFinite(numeric) && numeric >= threshold) {
253
- segmentTypeBuffer[type] = '';
254
- return true;
255
- }
256
- return false;
252
+ // A digit that waits for its pair is not held to min/max yet: `1` on the way to `19`.
253
+ const completes = Number(candidate) >= threshold;
254
+ timeSelection.setSegmentValue(type, candidate, { clamp: completes });
255
+ segmentTypeBuffer[type] = completes ? '' : candidate;
256
+ return completes;
257
257
  }
258
258
 
259
259
  const numericCandidate = Number(candidate);
@@ -271,14 +271,10 @@
271
271
 
272
272
  if (numericCandidate < min || numericCandidate > max) {
273
273
  candidate = digit;
274
- timeSelection.setSegmentValue(type, candidate);
275
- segmentTypeBuffer[type] = candidate;
276
- const fallbackNumeric = Number(candidate);
277
- if (fallbackNumeric >= threshold) {
278
- segmentTypeBuffer[type] = '';
279
- return true;
280
- }
281
- return false;
274
+ const completes = Number(candidate) >= threshold;
275
+ timeSelection.setSegmentValue(type, candidate, { clamp: completes });
276
+ segmentTypeBuffer[type] = completes ? '' : candidate;
277
+ return completes;
282
278
  }
283
279
 
284
280
  timeSelection.setSegmentValue(type, candidate);
@@ -308,7 +308,8 @@
308
308
  }
309
309
  return;
310
310
  }
311
- timePicker.setSegmentValue(segment.type, currentValue.slice(0, -1));
311
+ // Erasing is on the way to typing: held to min/max once the segment is left.
312
+ timePicker.setSegmentValue(segment.type, currentValue.slice(0, -1), { clamp: false });
312
313
  return;
313
314
  }
314
315
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@human-kit/ui",
3
- "version": "1.2.1",
3
+ "version": "1.4.0",
4
4
  "description": "Accessible, typed UI components for Svelte 5 — shipped as native ESM with per-component subpath exports so bundlers only include what you import.",
5
5
  "license": "MIT",
6
6
  "author": "Agustin Delgado <agustindariodelgado@gmail.com>",