@human-kit/ui 1.4.3 → 1.4.5

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.
@@ -1,5 +1,6 @@
1
1
  <script lang="ts">
2
2
  import type { HTMLInputAttributes } from 'svelte/elements';
3
+ import { TypedBeforeHydration } from '../../primitives/typed-before-hydration.svelte';
3
4
  import { untrack } from 'svelte';
4
5
  import { useAutocompleteContext } from '../root/context';
5
6
  import {
@@ -45,6 +46,7 @@
45
46
  const resolvedId = untrack(() => id) ?? generatedId;
46
47
 
47
48
  let inputRef = $state<HTMLInputElement | null>(null);
49
+ const typed = new TypedBeforeHydration(() => inputRef);
48
50
  const ctx = useAutocompleteContext();
49
51
 
50
52
  let isFocused = $state(false);
@@ -105,7 +107,7 @@
105
107
  ? `autocomplete-item-${ctx.instanceId}-${ctx.focusedItemId}`
106
108
  : undefined}
107
109
  autocomplete="off"
108
- value={ctx.inputValue}
110
+ bind:value={() => typed.read(ctx.inputValue), typed.adopt}
109
111
  disabled={ctx.isDisabled}
110
112
  readonly={ctx.isReadOnly}
111
113
  aria-disabled={ctx.isDisabled || undefined}
@@ -2,6 +2,7 @@
2
2
  import { untrack } from 'svelte';
3
3
  import type { HTMLInputAttributes } from 'svelte/elements';
4
4
  import { shouldShowFocusVisible, trackInteractionModality } from '../primitives/input-modality';
5
+ import { TypedBeforeHydration } from '../primitives/typed-before-hydration.svelte';
5
6
  import { isAriaInvalidValue } from '../utils/aria-invalid';
6
7
  import type { ClassValue } from '../utils/cn';
7
8
  import { composeEventHandlers } from '../utils/compose-event-handlers';
@@ -52,6 +53,7 @@
52
53
  const resolvedId = untrack(() => id) ?? generatedId;
53
54
 
54
55
  let inputRef: HTMLInputElement | null = $state(null);
56
+ const typed = new TypedBeforeHydration(() => inputRef);
55
57
  let hovered = $state(false);
56
58
  let focused = $state(false);
57
59
  let focusVisible = $state(false);
@@ -98,6 +100,16 @@
98
100
  focusVisible = false;
99
101
  }
100
102
 
103
+ // The binding below compares a number input's value as a number (`bind:value` does), so it
104
+ // is handed one; the value itself stays the string the input writes.
105
+ function boundValue() {
106
+ const shown = typed.read(value);
107
+ if (type !== 'number' && type !== 'range') return shown;
108
+ if (shown === '' || shown === null || shown === undefined) return null;
109
+ const numeric = Number(shown);
110
+ return Number.isNaN(numeric) ? shown : numeric;
111
+ }
112
+
101
113
  function handleInput(event: Event) {
102
114
  value = (event.currentTarget as HTMLInputElement).value;
103
115
  }
@@ -136,7 +148,7 @@
136
148
  bind:this={inputRef}
137
149
  id={resolvedId}
138
150
  {type}
139
- {value}
151
+ bind:value={boundValue, typed.adopt}
140
152
  {disabled}
141
153
  {readonly}
142
154
  {required}
@@ -1,5 +1,6 @@
1
1
  <script lang="ts">
2
2
  import type { HTMLInputAttributes } from 'svelte/elements';
3
+ import { TypedBeforeHydration } from '../../primitives/typed-before-hydration.svelte';
3
4
  import {
4
5
  shouldShowFocusVisible,
5
6
  trackInteractionModality
@@ -63,6 +64,7 @@
63
64
 
64
65
  const numberField = useNumberFieldContext();
65
66
  let inputRef: HTMLInputElement | null = $state(null);
67
+ const typed = new TypedBeforeHydration(() => inputRef);
66
68
  let hovered = $state(false);
67
69
 
68
70
  $effect(() => {
@@ -192,7 +194,7 @@
192
194
  role="spinbutton"
193
195
  {inputmode}
194
196
  {autocomplete}
195
- value={numberField.inputValue}
197
+ bind:value={() => typed.read(numberField.inputValue), typed.adopt}
196
198
  disabled={numberField.isDisabled}
197
199
  readonly={numberField.isReadOnly}
198
200
  required={numberField.isRequired}
@@ -37,6 +37,22 @@ export function getMotionTime(element) {
37
37
  const animationTime = getMaxTime(styles.animationDuration, styles.animationDelay);
38
38
  return Math.max(transitionTime, animationTime);
39
39
  }
40
+ /**
41
+ * Whether the element still has a transition or an animation in progress, at one of its end events.
42
+ *
43
+ * It asks the element, not the clock. The clock starts in a frame callback, and the animation
44
+ * started at the time of that frame, before the callback: when the frame ran late, the time measured
45
+ * at the end event came out short of the duration, and the last end event was ignored. The motion
46
+ * then finished on the fallback timer, 50ms late — long enough to show an exit animation without a
47
+ * fill mode at full opacity again: an overlay that closes, opens and closes. The clock remains only
48
+ * where the browser cannot list the animations.
49
+ */
50
+ function isStillMoving(element, startTime, total) {
51
+ if (typeof element.getAnimations !== 'function') {
52
+ return performance.now() - startTime < total - 5;
53
+ }
54
+ return element.getAnimations().some((animation) => animation.playState === 'running');
55
+ }
40
56
  /**
41
57
  * Call `onComplete` once the element's current transition/animation finishes. Resolves on the next
42
58
  * frame when there is no motion (e.g. prefers-reduced-motion), listens for animation/transition end
@@ -73,10 +89,10 @@ export function trackMotionEnd(element, onComplete) {
73
89
  return;
74
90
  // A multi-property transition (e.g. `opacity 150ms, transform 300ms`)
75
91
  // fires one end event per property; finishing on the first one cut the
76
- // motion short (overlays unmounted mid-exit). Only finish once the
77
- // longest tracked duration has actually elapsed — the timeout below
78
- // still covers the case where the final event never fires.
79
- if (performance.now() - startTime < total - 5)
92
+ // motion short (overlays unmounted mid-exit). So an end event only
93
+ // finishes once nothing else on the element still moves — the timeout
94
+ // below still covers the case where the final event never fires.
95
+ if (isStillMoving(element, startTime, total))
80
96
  return;
81
97
  finish();
82
98
  };
@@ -0,0 +1,36 @@
1
+ type TypedElement = HTMLInputElement | HTMLTextAreaElement;
2
+ /**
3
+ * Keeps what a person types into a server-rendered field before the page hydrates.
4
+ *
5
+ * The server's markup is on screen, and answers typing, before the script that owns it runs.
6
+ * A field that writes its value one way (`value={…}`) then has hydration write the server's
7
+ * value over what was typed, and the state never hears of it. A native `bind:value` doesn't:
8
+ * on hydration it finds the element's value different from the one it was rendered with
9
+ * (`defaultValue`) and adopts it.
10
+ *
11
+ * This lends a field that native path without changing who owns its value. Pass `read` and
12
+ * `adopt` to a function binding (`bind:value={() => typed.read(value), typed.adopt}`): before
13
+ * the field mounts, `adopt` keeps what hydration found, and `read` shows it instead of the
14
+ * owner's value, so nothing is written over it. Once the element is there, the typed text is
15
+ * replayed as an `input` event, so it reaches the owner by the same path as a keystroke —
16
+ * the field's own handler, the consumer's `oninput`, a form listening above.
17
+ *
18
+ * After that the field's `oninput` is still what writes. The binding hears each keystroke
19
+ * first, and checks the value again a microtask later, which comes before the field's own
20
+ * handler (a delegated one, on the root). Until the owner answers, `read` shows the keystroke,
21
+ * so that check finds nothing to undo; once the owner's value moves, it is the owner's again,
22
+ * whatever it made of the keystroke. An owner that ignores a keystroke leaves it on screen, as
23
+ * a one-way `value` always did.
24
+ */
25
+ export declare class TypedBeforeHydration {
26
+ #private;
27
+ constructor(element: () => TypedElement | null);
28
+ /** The value the element shows: the owner's, or what was typed while the owner catches up. */
29
+ read: (owned: unknown) => unknown;
30
+ /**
31
+ * Called by the binding with what the element holds. Before mount that is hydration finding
32
+ * typed text; after it, a keystroke on its way to the field's own handler.
33
+ */
34
+ adopt: (value: unknown) => void;
35
+ }
36
+ export {};
@@ -0,0 +1,66 @@
1
+ import { untrack } from 'svelte';
2
+ /**
3
+ * Keeps what a person types into a server-rendered field before the page hydrates.
4
+ *
5
+ * The server's markup is on screen, and answers typing, before the script that owns it runs.
6
+ * A field that writes its value one way (`value={…}`) then has hydration write the server's
7
+ * value over what was typed, and the state never hears of it. A native `bind:value` doesn't:
8
+ * on hydration it finds the element's value different from the one it was rendered with
9
+ * (`defaultValue`) and adopts it.
10
+ *
11
+ * This lends a field that native path without changing who owns its value. Pass `read` and
12
+ * `adopt` to a function binding (`bind:value={() => typed.read(value), typed.adopt}`): before
13
+ * the field mounts, `adopt` keeps what hydration found, and `read` shows it instead of the
14
+ * owner's value, so nothing is written over it. Once the element is there, the typed text is
15
+ * replayed as an `input` event, so it reaches the owner by the same path as a keystroke —
16
+ * the field's own handler, the consumer's `oninput`, a form listening above.
17
+ *
18
+ * After that the field's `oninput` is still what writes. The binding hears each keystroke
19
+ * first, and checks the value again a microtask later, which comes before the field's own
20
+ * handler (a delegated one, on the root). Until the owner answers, `read` shows the keystroke,
21
+ * so that check finds nothing to undo; once the owner's value moves, it is the owner's again,
22
+ * whatever it made of the keystroke. An owner that ignores a keystroke leaves it on screen, as
23
+ * a one-way `value` always did.
24
+ */
25
+ export class TypedBeforeHydration {
26
+ #typed = $state(null);
27
+ #settled = false;
28
+ #echo = null;
29
+ #owned = undefined;
30
+ constructor(element) {
31
+ $effect(() => {
32
+ const node = element();
33
+ if (!node || this.#settled)
34
+ return;
35
+ this.#settled = true;
36
+ untrack(() => this.#replay(node));
37
+ });
38
+ }
39
+ /** The value the element shows: the owner's, or what was typed while the owner catches up. */
40
+ read = (owned) => {
41
+ if (this.#typed)
42
+ return this.#typed.value;
43
+ if (this.#echo && Object.is(owned, this.#echo.over))
44
+ return this.#echo.value;
45
+ this.#echo = null;
46
+ this.#owned = owned;
47
+ return owned;
48
+ };
49
+ /**
50
+ * Called by the binding with what the element holds. Before mount that is hydration finding
51
+ * typed text; after it, a keystroke on its way to the field's own handler.
52
+ */
53
+ adopt = (value) => {
54
+ if (this.#settled) {
55
+ this.#echo = { value, over: this.#owned };
56
+ return;
57
+ }
58
+ this.#typed = { value };
59
+ };
60
+ #replay(node) {
61
+ if (!this.#typed)
62
+ return;
63
+ node.dispatchEvent(new Event('input', { bubbles: true }));
64
+ this.#typed = null;
65
+ }
66
+ }
@@ -1,5 +1,6 @@
1
1
  <script lang="ts">
2
2
  import type { HTMLInputAttributes } from 'svelte/elements';
3
+ import { TypedBeforeHydration } from '../../primitives/typed-before-hydration.svelte';
3
4
  import { watchFocusVisible } from '../../primitives/focus-visible.svelte';
4
5
  import {
5
6
  shouldShowFocusVisible,
@@ -51,6 +52,7 @@
51
52
 
52
53
  const ctx = useSearchFieldContext('SearchField.Input');
53
54
  let inputRef: HTMLInputElement | null = $state(null);
55
+ const typed = new TypedBeforeHydration(() => inputRef);
54
56
  let hovered = $state(false);
55
57
 
56
58
  $effect(() => {
@@ -129,7 +131,7 @@
129
131
  {type}
130
132
  {enterkeyhint}
131
133
  name={ctx.name}
132
- value={ctx.value}
134
+ bind:value={() => typed.read(ctx.value), typed.adopt}
133
135
  defaultValue={ctx.defaultValue}
134
136
  disabled={ctx.isDisabled}
135
137
  readonly={ctx.isReadOnly}
@@ -389,9 +389,21 @@ export function createTableContext(options = {}) {
389
389
  if (defaultWidth !== undefined) {
390
390
  return defaultWidth;
391
391
  }
392
- return resizerLayoutReady && columnsWithResizers.size > 0 ? '1fr' : undefined;
392
+ // The server resolves an implicit column the way the browser will (see
393
+ // `getMeasuredTableWidth`), so it reads the resizers registered so far as ready.
394
+ return (resizerLayoutReady || !IS_BROWSER) && columnsWithResizers.size > 0
395
+ ? '1fr'
396
+ : undefined;
393
397
  }
394
398
  function getMeasuredTableWidth() {
399
+ // The server has no box to measure. It resolves `%`/`fr` columns as if the table had
400
+ // no room left for them, which puts each one at its `minWidth`: that is where the
401
+ // browser settles them for a table wider than its box, and it does not depend on the
402
+ // columns that have not registered yet when an early header cell serializes. The table
403
+ // is then exactly as wide as `getTableSsrMinWidth` reserves, so hydration only grows a
404
+ // table that fits its box with room to spare.
405
+ if (!IS_BROWSER)
406
+ return 0;
395
407
  if (hasMeasuredTableWidthCache) {
396
408
  return measuredTableWidthCache;
397
409
  }
@@ -682,6 +694,15 @@ export function createTableContext(options = {}) {
682
694
  return Number.isInteger(rounded) ? `${rounded}` : `${rounded}`;
683
695
  }
684
696
  function getColumnWidthStyle(columnId) {
697
+ // On the server every visible column already has its px width (see
698
+ // `getMeasuredTableWidth`). A `%` or a `calc()` would resolve against a table whose
699
+ // width the browser decides, and a column without one would collapse to nothing under
700
+ // the fixed layout; both moved on hydration.
701
+ if (!IS_BROWSER && !isColumnHidden(columnId)) {
702
+ const serverWidth = getResolvedVisibleColumnWidths().get(columnId);
703
+ if (serverWidth !== undefined)
704
+ return `${serverWidth}px`;
705
+ }
685
706
  const resolvedWidth = getColumnWidth(columnId);
686
707
  if (resolvedWidth !== undefined) {
687
708
  return `${resolvedWidth}px`;
@@ -693,7 +714,13 @@ export function createTableContext(options = {}) {
693
714
  return `${clampColumnWidth(columnId, parsed.value)}px`;
694
715
  }
695
716
  if (parsed.unit === '%') {
696
- return `${parsed.value}%`;
717
+ // Once resolved, the px width: it already holds each column at its `minWidth`. A
718
+ // bare `%` is shared out again by the browser, which ignores those floors and splits
719
+ // what is left between the `%` columns by their ratio.
720
+ const resolvedPercent = isColumnHidden(columnId)
721
+ ? undefined
722
+ : getResolvedVisibleColumnWidths().get(columnId);
723
+ return resolvedPercent === undefined ? `${parsed.value}%` : `${resolvedPercent}px`;
697
724
  }
698
725
  let fixedPxTotal = 0;
699
726
  let percentTotal = 0;
@@ -29,9 +29,8 @@ export type GetTableSsrMinWidthOptions = {
29
29
  *
30
30
  * Pass the result to `Table.Root`'s `ssrMinTableWidth`. A table's columns only
31
31
  * register after its `<table>` serializes, so on the server it can't derive its
32
- * own minimum width: it falls back to a content-driven auto layout that
33
- * collapses flexible columns until hydration measures the real widths. Reserving
34
- * this width up-front commits SSR to the final layout and avoids that jump under
35
- * overflow.
32
+ * own minimum width. Its header cells do carry these same floors as px widths,
33
+ * which is where the browser settles a table wider than its box; reserving their
34
+ * sum here makes the table that wide too, so hydration doesn't move a column.
36
35
  */
37
36
  export declare function getTableSsrMinWidth(columns: Iterable<TableColumnWidthSpec>, options?: GetTableSsrMinWidthOptions): number;
@@ -20,10 +20,9 @@ function columnFloorWidth(column, defaultMinWidth) {
20
20
  *
21
21
  * Pass the result to `Table.Root`'s `ssrMinTableWidth`. A table's columns only
22
22
  * register after its `<table>` serializes, so on the server it can't derive its
23
- * own minimum width: it falls back to a content-driven auto layout that
24
- * collapses flexible columns until hydration measures the real widths. Reserving
25
- * this width up-front commits SSR to the final layout and avoids that jump under
26
- * overflow.
23
+ * own minimum width. Its header cells do carry these same floors as px widths,
24
+ * which is where the browser settles a table wider than its box; reserving their
25
+ * sum here makes the table that wide too, so hydration doesn't move a column.
27
26
  */
28
27
  export function getTableSsrMinWidth(columns, options = {}) {
29
28
  const { hiddenColumns, defaultColumnMinWidth = DEFAULT_TABLE_COLUMN_MIN_WIDTH, extraFixedWidth = 0 } = options;
@@ -2,6 +2,7 @@
2
2
  import { tick, untrack } from 'svelte';
3
3
  import type { HTMLTextareaAttributes } from 'svelte/elements';
4
4
  import { shouldShowFocusVisible, trackInteractionModality } from '../primitives/input-modality';
5
+ import { TypedBeforeHydration } from '../primitives/typed-before-hydration.svelte';
5
6
  import { isAriaInvalidValue } from '../utils/aria-invalid';
6
7
  import type { ClassValue } from '../utils/cn';
7
8
  import { composeEventHandlers } from '../utils/compose-event-handlers';
@@ -78,6 +79,7 @@
78
79
  const resolvedId = untrack(() => id) ?? generatedId;
79
80
 
80
81
  let textAreaRef: HTMLTextAreaElement | null = $state(null);
82
+ const typed = new TypedBeforeHydration(() => textAreaRef);
81
83
  let hovered = $state(false);
82
84
  let focused = $state(false);
83
85
  let focusVisible = $state(false);
@@ -231,7 +233,7 @@
231
233
  {...restProps}
232
234
  bind:this={textAreaRef}
233
235
  id={resolvedId}
234
- {value}
236
+ bind:value={() => typed.read(value), typed.adopt}
235
237
  {disabled}
236
238
  {readonly}
237
239
  {required}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@human-kit/ui",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
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>",
@@ -25,7 +25,14 @@
25
25
  "headless",
26
26
  "design-system",
27
27
  "tailwind",
28
- "human-kit"
28
+ "human-kit",
29
+ "humankit",
30
+ "headless-ui",
31
+ "svelte-components",
32
+ "svelte-ui",
33
+ "sveltekit",
34
+ "aria",
35
+ "wai-aria"
29
36
  ],
30
37
  "type": "module",
31
38
  "engines": {