@vizejs/composable 0.809.0 → 0.811.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.
- package/dist/abort-signal.mjs +0 -62
- package/dist/active-element.mjs +0 -11
- package/dist/animate.mjs +0 -19
- package/dist/async-resource.mjs +0 -17
- package/dist/battery.mjs +0 -13
- package/dist/breakpoints.mjs +0 -24
- package/dist/capability.mjs +0 -12
- package/dist/{catalog-BRX-Wg8B.mjs → catalog-CcpCnLjb.mjs} +0 -14
- package/dist/catalog.mjs +1 -1
- package/dist/color-mode.mjs +0 -23
- package/dist/computed-with-control.mjs +0 -22
- package/dist/create-event-hook.mjs +0 -27
- package/dist/create-global-state.mjs +0 -23
- package/dist/create-shared-composable.mjs +0 -23
- package/dist/device-motion.mjs +0 -11
- package/dist/device-orientation.mjs +0 -23
- package/dist/device-pixel-ratio.mjs +0 -11
- package/dist/disposal-scope.mjs +0 -25
- package/dist/document-visibility.mjs +0 -14
- package/dist/draggable.mjs +0 -14
- package/dist/element-bounding.mjs +0 -14
- package/dist/element-by-point.mjs +0 -10
- package/dist/element-hover.mjs +0 -13
- package/dist/element-target.mjs +0 -30
- package/dist/focus.mjs +0 -23
- package/dist/fps.mjs +0 -12
- package/dist/geolocation.mjs +0 -14
- package/dist/i18n.mjs +0 -86
- package/dist/icu-message.mjs +0 -58
- package/dist/idle.mjs +0 -16
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/infinite-scroll.mjs +0 -16
- package/dist/intersection-observer.mjs +0 -27
- package/dist/locale.mjs +0 -20
- package/dist/magic-keys.mjs +0 -35
- package/dist/math.mjs +0 -37
- package/dist/media-query.mjs +0 -29
- package/dist/mouse-in-element.mjs +0 -12
- package/dist/mouse.mjs +0 -12
- package/dist/mutation-observer.mjs +0 -15
- package/dist/network.mjs +0 -24
- package/dist/on-click-outside.mjs +0 -16
- package/dist/on-key-stroke.mjs +0 -33
- package/dist/on-long-press.mjs +0 -14
- package/dist/on-start-typing.mjs +0 -23
- package/dist/page-leave.mjs +0 -11
- package/dist/parent-element.mjs +0 -9
- package/dist/pinch.mjs +0 -14
- package/dist/pointer-lock.mjs +0 -23
- package/dist/pointer.mjs +0 -18
- package/dist/preferences.mjs +0 -31
- package/dist/preferred-languages.mjs +0 -13
- package/dist/ref-auto-reset.mjs +0 -24
- package/dist/ref-debounced.mjs +0 -22
- package/dist/ref-default.mjs +0 -21
- package/dist/resize-observer.mjs +0 -31
- package/dist/resolver.mjs +1 -58
- package/dist/retry-async.mjs +0 -25
- package/dist/retry-delay.mjs +0 -23
- package/dist/scope.mjs +0 -14
- package/dist/screen-orientation.mjs +0 -15
- package/dist/scroll-lock.mjs +0 -14
- package/dist/scroll.mjs +0 -27
- package/dist/standard-schema.mjs +0 -31
- package/dist/swipe.mjs +0 -36
- package/dist/temporal.mjs +0 -30
- package/dist/text-selection.mjs +0 -15
- package/dist/textarea-autosize.mjs +0 -13
- package/dist/to-reactive.mjs +0 -20
- package/dist/transition.mjs +0 -7
- package/dist/until.mjs +0 -25
- package/dist/use-array.mjs +0 -52
- package/dist/use-barcode-detector.mjs +0 -25
- package/dist/use-bluetooth.mjs +0 -27
- package/dist/use-broadcast-channel.mjs +0 -30
- package/dist/use-builtin-ai.mjs +0 -68
- package/dist/use-clipboard.mjs +0 -27
- package/dist/use-compression-stream.mjs +0 -78
- package/dist/use-contact-picker.mjs +0 -22
- package/dist/use-cookie.mjs +0 -63
- package/dist/use-countdown.mjs +0 -22
- package/dist/use-counter.mjs +0 -29
- package/dist/use-css-var.mjs +0 -28
- package/dist/use-date-format.mjs +0 -48
- package/dist/use-date-time-format.mjs +0 -22
- package/dist/use-debounced.mjs +0 -39
- package/dist/use-display-media.mjs +0 -22
- package/dist/use-display-names.mjs +0 -19
- package/dist/use-document-title.mjs +0 -25
- package/dist/use-drop-zone.mjs +0 -36
- package/dist/use-event-source.mjs +0 -25
- package/dist/use-eye-dropper.mjs +0 -19
- package/dist/use-favicon.mjs +0 -23
- package/dist/use-fetch.mjs +0 -27
- package/dist/use-field.mjs +0 -27
- package/dist/use-file-dialog.mjs +0 -21
- package/dist/{use-form-C2JXZ0i7.mjs → use-form-BZqwMs6k.mjs} +143 -70
- package/dist/use-form-field-props.mjs +0 -57
- package/dist/use-form.d.mts +123 -18
- package/dist/use-form.mjs +1 -1
- package/dist/use-fullscreen.mjs +0 -23
- package/dist/use-gamepad.mjs +0 -50
- package/dist/use-hid.mjs +0 -25
- package/dist/use-history.mjs +0 -43
- package/dist/use-id-generator.mjs +0 -21
- package/dist/use-image.mjs +0 -22
- package/dist/use-indexed-db.mjs +0 -39
- package/dist/use-interval.mjs +0 -28
- package/dist/use-list-format.mjs +0 -18
- package/dist/use-machine.mjs +0 -34
- package/dist/use-map.mjs +0 -28
- package/dist/use-media-controls.mjs +0 -28
- package/dist/use-mounted.mjs +0 -18
- package/dist/use-navigation-api.mjs +0 -27
- package/dist/use-notification.mjs +0 -24
- package/dist/use-number-format.mjs +0 -23
- package/dist/use-object-url.mjs +0 -22
- package/dist/use-offset-pagination.mjs +0 -18
- package/dist/use-payment-request.mjs +0 -25
- package/dist/use-performance-observer.mjs +0 -27
- package/dist/use-permission.mjs +0 -26
- package/dist/use-plural-rules.mjs +0 -21
- package/dist/use-push-subscription.mjs +0 -28
- package/dist/use-queue.mjs +0 -29
- package/dist/use-raf-fn.mjs +0 -23
- package/dist/use-relative-time-format.mjs +0 -35
- package/dist/use-request-idle-callback.mjs +0 -42
- package/dist/use-route-query.mjs +0 -17
- package/dist/use-scheduler-post-task.mjs +0 -27
- package/dist/use-screen-details.mjs +0 -42
- package/dist/use-script-tag.mjs +0 -28
- package/dist/use-segmenter.mjs +0 -20
- package/dist/use-serial.mjs +0 -24
- package/dist/use-service-worker.mjs +0 -27
- package/dist/use-set.mjs +0 -23
- package/dist/use-share.mjs +0 -23
- package/dist/use-sorted-locale.mjs +0 -20
- package/dist/use-speech-recognition.mjs +0 -24
- package/dist/use-speech-synthesis.mjs +0 -26
- package/dist/use-stack.mjs +0 -27
- package/dist/use-storage-estimate.mjs +0 -41
- package/dist/use-storage.mjs +0 -68
- package/dist/use-style-tag.mjs +0 -22
- package/dist/use-supported.mjs +0 -17
- package/dist/use-throttled.mjs +0 -46
- package/dist/use-time-ago.mjs +0 -22
- package/dist/use-toggle.mjs +0 -19
- package/dist/use-url-hash.mjs +0 -28
- package/dist/use-url-search-params.mjs +0 -99
- package/dist/use-usb.mjs +0 -23
- package/dist/use-user-activation.mjs +0 -22
- package/dist/use-user-media.mjs +0 -50
- package/dist/use-vibrate.mjs +0 -21
- package/dist/use-view-transition.mjs +0 -22
- package/dist/use-visual-viewport.mjs +0 -25
- package/dist/use-wake-lock.mjs +0 -21
- package/dist/use-web-authn.mjs +0 -61
- package/dist/use-web-locks.mjs +0 -25
- package/dist/use-web-midi.mjs +0 -22
- package/dist/use-web-vitals.mjs +0 -41
- package/dist/use-websocket.mjs +0 -27
- package/dist/use-worker-fn.mjs +0 -37
- package/dist/virtual-list.mjs +0 -14
- package/dist/whenever.mjs +0 -21
- package/dist/window-size.mjs +0 -12
- package/package.json +2 -2
- package/registry/files/form-control-types.ts +260 -0
- package/registry/files/form-dirty.ts +131 -0
- package/registry/files/form-options.ts +156 -0
- package/registry/files/form-path-types.ts +135 -0
- package/registry/files/use-form.ts +102 -449
- package/registry/registry.json +28 -4
package/dist/use-web-midi.mjs
CHANGED
|
@@ -18,28 +18,6 @@ function describe(port) {
|
|
|
18
18
|
function hasMessageData(event) {
|
|
19
19
|
return "data" in event;
|
|
20
20
|
}
|
|
21
|
-
/**
|
|
22
|
-
* Access MIDI devices with the Web MIDI API.
|
|
23
|
-
*
|
|
24
|
-
* Nothing is requested until `request()` is called. Once granted, `inputs`
|
|
25
|
-
* and `outputs` follow `statechange` events, and `onMessage` receives
|
|
26
|
-
* messages from every input. Failures land in `error`. Listeners are removed
|
|
27
|
-
* when the owning reactive scope stops; outside a scope call `stop()`.
|
|
28
|
-
*
|
|
29
|
-
* Server rendering: `supported` is false, `status` stays `"idle"` and no
|
|
30
|
-
* access is requested.
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* ```ts
|
|
34
|
-
* const midi = useWebMIDI({ onMessage: ({ data }) => console.log(data) });
|
|
35
|
-
* await midi.request();
|
|
36
|
-
* midi.send(midi.outputs.value[0]!.id, [0x90, 60, 127]);
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @param options Host, access flags, and message callback.
|
|
40
|
-
* @default options {}
|
|
41
|
-
* @returns MIDI state and actions.
|
|
42
|
-
*/
|
|
43
21
|
function useWebMIDI(options = {}) {
|
|
44
22
|
const status = ref("idle");
|
|
45
23
|
const inputs = shallowRef([]);
|
package/dist/use-web-vitals.mjs
CHANGED
|
@@ -2,11 +2,6 @@ import { tryOnScopeDispose } from "./scope.mjs";
|
|
|
2
2
|
import { usePerformanceObserver } from "./use-performance-observer.mjs";
|
|
3
3
|
import { computed, hasInjectionContext, readonly, shallowRef, toValue, watch, watchPostEffect } from "vue";
|
|
4
4
|
//#region src/use-web-vitals.ts
|
|
5
|
-
/**
|
|
6
|
-
* Standard `[good, poor]` thresholds: values up to the first bound are
|
|
7
|
-
* "good", values above the second are "poor". Times are milliseconds; CLS is
|
|
8
|
-
* unitless.
|
|
9
|
-
*/
|
|
10
5
|
const WEB_VITALS_THRESHOLDS = {
|
|
11
6
|
LCP: [2500, 4e3],
|
|
12
7
|
CLS: [.1, .25],
|
|
@@ -14,13 +9,6 @@ const WEB_VITALS_THRESHOLDS = {
|
|
|
14
9
|
FCP: [1800, 3e3],
|
|
15
10
|
TTFB: [800, 1800]
|
|
16
11
|
};
|
|
17
|
-
/**
|
|
18
|
-
* Rate a metric value with {@link WEB_VITALS_THRESHOLDS}.
|
|
19
|
-
*
|
|
20
|
-
* @param name Metric name.
|
|
21
|
-
* @param value Metric value (milliseconds, or the unitless CLS score).
|
|
22
|
-
* @returns The rating bucket.
|
|
23
|
-
*/
|
|
24
12
|
function rateMetric(name, value) {
|
|
25
13
|
if (Number.isNaN(value)) throw new RangeError(`[VIZE_COMPOSE_WEB_VITALS_INVALID_VALUE] ${name} value must be a number`);
|
|
26
14
|
const [good, poor] = WEB_VITALS_THRESHOLDS[name];
|
|
@@ -31,35 +19,6 @@ const MAX_INTERACTIONS = 10;
|
|
|
31
19
|
function browserDocument() {
|
|
32
20
|
return typeof window === "undefined" ? void 0 : window.document;
|
|
33
21
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Measure LCP, CLS, INP, FCP and TTFB locally, without dependencies.
|
|
36
|
-
*
|
|
37
|
-
* - LCP: last candidate before the first input or hidden page.
|
|
38
|
-
* - CLS: largest session window (shifts < 1 s apart, window ≤ 5 s),
|
|
39
|
-
* ignoring shifts after recent input.
|
|
40
|
-
* - INP: per-interaction maximum duration; the highest after ignoring one
|
|
41
|
-
* per 50 interactions (a p98 approximation over the 10 longest). The
|
|
42
|
-
* interaction count only includes interactions above `durationThreshold`.
|
|
43
|
-
* - FCP: `first-contentful-paint` before the page was first hidden.
|
|
44
|
-
* - TTFB: navigation `responseStart - activationStart`, clamped at 0.
|
|
45
|
-
*
|
|
46
|
-
* Values are not adjusted for prerender activation except TTFB. Observers
|
|
47
|
-
* and listeners are released when the owning reactive scope stops; outside
|
|
48
|
-
* a scope the caller owns `stop()`.
|
|
49
|
-
*
|
|
50
|
-
* Server rendering: nothing is observed, `metrics` is `{}` and `supported`
|
|
51
|
-
* is false.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```ts
|
|
55
|
-
* const { metrics, onMetric } = useWebVitals();
|
|
56
|
-
* onMetric((metric) => navigator.sendBeacon("/vitals", JSON.stringify(metric)));
|
|
57
|
-
* ```
|
|
58
|
-
*
|
|
59
|
-
* @param options Hosts and reporting policy.
|
|
60
|
-
* @default options {}
|
|
61
|
-
* @returns Reactive metrics and a report hook.
|
|
62
|
-
*/
|
|
63
22
|
function useWebVitals(options = {}) {
|
|
64
23
|
const metrics = shallowRef({});
|
|
65
24
|
const reported = /* @__PURE__ */ new Map();
|
package/dist/use-websocket.mjs
CHANGED
|
@@ -24,33 +24,6 @@ function defaultScheduler() {
|
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
-
/**
|
|
28
|
-
* Reactive WebSocket with typed messages, auto-reconnect, and heartbeat.
|
|
29
|
-
*
|
|
30
|
-
* Unexpected closes reconnect with exponential backoff when `autoReconnect`
|
|
31
|
-
* is enabled; `close()` is always final until `open()` is called again.
|
|
32
|
-
* Messages sent while connecting are queued. The connection and all timers
|
|
33
|
-
* are released when the owning reactive scope stops; outside a scope the
|
|
34
|
-
* caller owns `close()`. Server rendering never connects: automatic
|
|
35
|
-
* connection requires a browser or an explicit `host`, and status stays
|
|
36
|
-
* `"closed"`.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```ts
|
|
40
|
-
* const chat = useWebSocket<ChatEvent, ChatCommand>("wss://example.test/chat", {
|
|
41
|
-
* parse: (raw) => parseChatEvent(String(raw)),
|
|
42
|
-
* serialize: (command) => JSON.stringify(command),
|
|
43
|
-
* autoReconnect: { retries: 5 },
|
|
44
|
-
* heartbeat: true,
|
|
45
|
-
* });
|
|
46
|
-
* chat.send({ type: "join", room: "general" });
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* @param url Reactive URL; changing it reconnects.
|
|
50
|
-
* @param options Codecs, reconnection, heartbeat, and host options.
|
|
51
|
-
* @default options {}
|
|
52
|
-
* @returns Reactive state and controls.
|
|
53
|
-
*/
|
|
54
27
|
function useWebSocket(url, options = {}) {
|
|
55
28
|
const status = shallowRef("closed");
|
|
56
29
|
const data = shallowRef(void 0);
|
package/dist/use-worker-fn.mjs
CHANGED
|
@@ -24,18 +24,6 @@ function defaultScheduler() {
|
|
|
24
24
|
function workerError(code, message, cause) {
|
|
25
25
|
return cause === void 0 ? /* @__PURE__ */ new Error(`[${code}] ${message}`) : new Error(`[${code}] ${message}`, { cause });
|
|
26
26
|
}
|
|
27
|
-
/**
|
|
28
|
-
* Build the worker script for `fn`. Exposed for inspection and testing.
|
|
29
|
-
*
|
|
30
|
-
* The function source is embedded with `Function.prototype.toString`, so it
|
|
31
|
-
* must be self-contained: no closures over module variables, only its
|
|
32
|
-
* arguments, `localDependencies`, `dependencies`, and worker globals.
|
|
33
|
-
*
|
|
34
|
-
* @param fn Function to embed.
|
|
35
|
-
* @param options Imported scripts and inlined helper functions.
|
|
36
|
-
* @default options {}
|
|
37
|
-
* @returns JavaScript source for a dedicated worker.
|
|
38
|
-
*/
|
|
39
27
|
function createWorkerFnScript(fn, options = {}) {
|
|
40
28
|
const imports = (options.dependencies ?? []).map((url) => JSON.stringify(url)).join(", ");
|
|
41
29
|
const helpers = (options.localDependencies ?? []).map((helper) => {
|
|
@@ -55,31 +43,6 @@ function createWorkerFnScript(fn, options = {}) {
|
|
|
55
43
|
"};"
|
|
56
44
|
].filter((line) => line !== "").join("\n");
|
|
57
45
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Run a CPU-heavy function in a dedicated Blob-URL worker, off the main thread.
|
|
60
|
-
*
|
|
61
|
-
* Each `run` spawns a fresh worker, posts the arguments, and resolves with the
|
|
62
|
-
* function's (awaited) result; the worker is terminated and its URL revoked
|
|
63
|
-
* once the run settles, times out, or is terminated. Concurrent runs reject
|
|
64
|
-
* with `VIZE_COMPOSE_WORKER_FN_BUSY`. The running worker is terminated when
|
|
65
|
-
* the owning reactive scope stops. Server rendering never spawns workers:
|
|
66
|
-
* `supported` is `false` and `run` rejects with
|
|
67
|
-
* `VIZE_COMPOSE_WORKER_FN_UNSUPPORTED`.
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* ```ts
|
|
71
|
-
* const { run, status } = useWorkerFn((values: number[]) =>
|
|
72
|
-
* [...values].sort((a, b) => a - b),
|
|
73
|
-
* );
|
|
74
|
-
* const sorted = await run(bigArray);
|
|
75
|
-
* ```
|
|
76
|
-
*
|
|
77
|
-
* @param fn Self-contained function; see {@link createWorkerFnScript}.
|
|
78
|
-
* @param options Timeout, dependencies, host, and scheduler.
|
|
79
|
-
* @default options {}
|
|
80
|
-
* @throws {RangeError} `[VIZE_COMPOSE_WORKER_FN_INVALID_TIMEOUT]` for an invalid timeout.
|
|
81
|
-
* @returns Reactive status and run controls.
|
|
82
|
-
*/
|
|
83
46
|
function useWorkerFn(fn, options = {}) {
|
|
84
47
|
const { timeoutMs } = options;
|
|
85
48
|
if (timeoutMs !== void 0 && (!Number.isFinite(timeoutMs) || timeoutMs < 0)) throw new RangeError(`[VIZE_COMPOSE_WORKER_FN_INVALID_TIMEOUT] timeoutMs must be a finite non-negative number; received ${String(timeoutMs)}`);
|
package/dist/virtual-list.mjs
CHANGED
|
@@ -2,19 +2,6 @@ import { useResizeObserver } from "./resize-observer.mjs";
|
|
|
2
2
|
import { useElementRef } from "./element-ref.mjs";
|
|
3
3
|
import { computed, shallowRef, toValue } from "vue";
|
|
4
4
|
//#region src/virtual-list.ts
|
|
5
|
-
/**
|
|
6
|
-
* Render only the visible slice of a long list (fixed or variable item sizes).
|
|
7
|
-
*
|
|
8
|
-
* Offsets are prefix sums recomputed when the items change; the visible range
|
|
9
|
-
* is found by binary search, so scrolling is O(log n). Until the container is
|
|
10
|
-
* measured (always on the server) the first `initialItemCount` items render,
|
|
11
|
-
* so server and first client render match. The container is measured on
|
|
12
|
-
* scroll and via `ResizeObserver`, released with the owning reactive scope.
|
|
13
|
-
*
|
|
14
|
-
* @param items Reactive source list.
|
|
15
|
-
* @param options Item size, overscan, axis, initial count, and capability.
|
|
16
|
-
* @returns Items to render plus container/wrapper bindings.
|
|
17
|
-
*/
|
|
18
5
|
function useVirtualList(items, options) {
|
|
19
6
|
const overscan = Math.max(0, Math.floor(options.overscan ?? 5));
|
|
20
7
|
const vertical = (options.orientation ?? "vertical") === "vertical";
|
|
@@ -110,7 +97,6 @@ function useVirtualList(items, options) {
|
|
|
110
97
|
}
|
|
111
98
|
};
|
|
112
99
|
}
|
|
113
|
-
/** Largest index whose start offset is ≤ `position`. */
|
|
114
100
|
function findIndex(offsets, count, position) {
|
|
115
101
|
let low = 0;
|
|
116
102
|
let high = Math.max(0, count - 1);
|
package/dist/whenever.mjs
CHANGED
|
@@ -1,26 +1,5 @@
|
|
|
1
1
|
import { watch } from "vue";
|
|
2
2
|
//#region src/whenever.ts
|
|
3
|
-
/**
|
|
4
|
-
* Run a callback whenever a source becomes (or changes while) truthy.
|
|
5
|
-
*
|
|
6
|
-
* The callback's value is narrowed with `Exclude<Value, Falsy>`, so a
|
|
7
|
-
* `Ref<User | null>` delivers `User`. With `once`, the watcher stops after
|
|
8
|
-
* the first truthy delivery, not after the first change (unlike Vue's
|
|
9
|
-
* `once`). With `immediate`, an already-truthy source is delivered right
|
|
10
|
-
* away. Safe during server rendering and bound to the owning scope.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* whenever(currentUser, (user) => greet(user.name), { immediate: true, once: true });
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* @param source Ref or getter.
|
|
18
|
-
* @param callback Receives the truthy value, the previous value, and
|
|
19
|
-
* `onCleanup`.
|
|
20
|
-
* @param options Vue watch options; `once` counts truthy deliveries only.
|
|
21
|
-
* @default options {}
|
|
22
|
-
* @returns Vue's watch handle.
|
|
23
|
-
*/
|
|
24
3
|
function whenever(source, callback, options = {}) {
|
|
25
4
|
const { once = false, ...watchOptions } = options;
|
|
26
5
|
let handle;
|
package/dist/window-size.mjs
CHANGED
|
@@ -1,18 +1,6 @@
|
|
|
1
1
|
import { tryOnScopeDispose } from "./scope.mjs";
|
|
2
2
|
import { hasInjectionContext, onMounted, readonly, ref, toValue, watch } from "vue";
|
|
3
3
|
//#region src/window-size.ts
|
|
4
|
-
/**
|
|
5
|
-
* Track the window size reactively.
|
|
6
|
-
*
|
|
7
|
-
* Server renders expose `initialWidth`/`initialHeight` (default `0`), so the
|
|
8
|
-
* first client render matches; the real size is applied as soon as a window
|
|
9
|
-
* resolves (after mounting when called inside a component). Resize (and orientation) listeners are passive and removed with
|
|
10
|
-
* the owning reactive scope.
|
|
11
|
-
*
|
|
12
|
-
* @param options Initial size, measurement kind, and capability.
|
|
13
|
-
* @default options {}
|
|
14
|
-
* @returns Reactive width/height plus a support flag.
|
|
15
|
-
*/
|
|
16
4
|
function useWindowSize(options = {}) {
|
|
17
5
|
const { initialWidth = 0, initialHeight = 0, includeScrollbar = true, type = "inner", listenOrientation = true } = options;
|
|
18
6
|
const width = ref(initialWidth);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vizejs/composable",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.811.0",
|
|
4
4
|
"description": "Lifecycle-safe composable foundations for Vize applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"composables",
|
|
@@ -978,7 +978,7 @@
|
|
|
978
978
|
},
|
|
979
979
|
"devDependencies": {
|
|
980
980
|
"@types/node": "25.9.2",
|
|
981
|
-
"@vizejs/native": "0.
|
|
981
|
+
"@vizejs/native": "0.811.0",
|
|
982
982
|
"happy-dom": "21.0.4",
|
|
983
983
|
"typescript": "6.0.3",
|
|
984
984
|
"vite-plus": "0.1.24",
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public control surface returned by {@link useForm}.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ComputedRef, Ref, ShallowRef, WritableComputedRef } from "vue";
|
|
6
|
+
|
|
7
|
+
import type { FormResetOptions, FormResult, SetFieldValueOptions } from "./form-options.ts";
|
|
8
|
+
import type {
|
|
9
|
+
DirtyFields,
|
|
10
|
+
FormArrayItem,
|
|
11
|
+
FormArrayPath,
|
|
12
|
+
FormErrorRecord,
|
|
13
|
+
FormErrors,
|
|
14
|
+
FormPath,
|
|
15
|
+
FormPathValue,
|
|
16
|
+
ValidatorResult,
|
|
17
|
+
} from "./form-path-types.ts";
|
|
18
|
+
|
|
19
|
+
/** Reactive controls for one field, returned by `field`. */
|
|
20
|
+
export interface FormFieldControls<Value, Path extends string = string> {
|
|
21
|
+
/** Dotted path of the field (also a suitable `name` attribute). */
|
|
22
|
+
readonly path: Path;
|
|
23
|
+
|
|
24
|
+
/** Writable value bound to the form state (use with `v-model`). */
|
|
25
|
+
readonly value: WritableComputedRef<Value>;
|
|
26
|
+
|
|
27
|
+
/** Current error messages. */
|
|
28
|
+
readonly errors: ComputedRef<readonly string[]>;
|
|
29
|
+
|
|
30
|
+
/** First error message. */
|
|
31
|
+
readonly error: ComputedRef<string | undefined>;
|
|
32
|
+
|
|
33
|
+
/** Whether the value differs from its initial value. */
|
|
34
|
+
readonly dirty: ComputedRef<boolean>;
|
|
35
|
+
|
|
36
|
+
/** Whether the field was blurred or touched programmatically. */
|
|
37
|
+
readonly touched: ComputedRef<boolean>;
|
|
38
|
+
|
|
39
|
+
/** Whether a validation of this field is pending. */
|
|
40
|
+
readonly validating: ComputedRef<boolean>;
|
|
41
|
+
|
|
42
|
+
/** Mark the field touched (wire to `blur`); validates when configured. */
|
|
43
|
+
readonly onBlur: () => void;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Validate this field.
|
|
47
|
+
*
|
|
48
|
+
* @returns Whether the field is valid (`false` also when superseded).
|
|
49
|
+
*/
|
|
50
|
+
readonly validate: () => Promise<boolean>;
|
|
51
|
+
|
|
52
|
+
/** Restore the initial value and clear errors and touched state. */
|
|
53
|
+
readonly reset: () => void;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** One entry of a field array, with a stable key for `v-for`. */
|
|
57
|
+
export interface FormArrayEntry<Item, Path extends string = string> {
|
|
58
|
+
/** Stable key that follows the item through moves. */
|
|
59
|
+
readonly key: number;
|
|
60
|
+
|
|
61
|
+
/** Current index. */
|
|
62
|
+
readonly index: number;
|
|
63
|
+
|
|
64
|
+
/** Dotted path of the item, e.g. `items.2`. */
|
|
65
|
+
readonly path: `${Path}.${number}`;
|
|
66
|
+
|
|
67
|
+
/** Current item value. */
|
|
68
|
+
readonly value: Item;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Typed operations on an array field, returned by `fieldArray`. */
|
|
72
|
+
export interface FormFieldArrayControls<Item, Path extends string = string> {
|
|
73
|
+
/** Entries with stable keys. */
|
|
74
|
+
readonly entries: ComputedRef<readonly FormArrayEntry<Item, Path>[]>;
|
|
75
|
+
|
|
76
|
+
/** Append items. */
|
|
77
|
+
readonly append: (...items: Item[]) => void;
|
|
78
|
+
|
|
79
|
+
/** Prepend items. */
|
|
80
|
+
readonly prepend: (...items: Item[]) => void;
|
|
81
|
+
|
|
82
|
+
/** Insert an item at `index`. */
|
|
83
|
+
readonly insert: (index: number, item: Item) => void;
|
|
84
|
+
|
|
85
|
+
/** Remove the item at `index`, shifting errors and touched state. */
|
|
86
|
+
readonly remove: (index: number) => void;
|
|
87
|
+
|
|
88
|
+
/** Move an item from one index to another, carrying its state along. */
|
|
89
|
+
readonly move: (from: number, to: number) => void;
|
|
90
|
+
|
|
91
|
+
/** Swap two items. */
|
|
92
|
+
readonly swap: (first: number, second: number) => void;
|
|
93
|
+
|
|
94
|
+
/** Replace all items and reset their state. */
|
|
95
|
+
readonly replace: (items: readonly Item[]) => void;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Reactive state and actions returned by `useForm`. */
|
|
99
|
+
export interface FormControls<Values extends object, Output> {
|
|
100
|
+
/** Current values. Mutations are tracked (dirty state derives from them). */
|
|
101
|
+
readonly values: Ref<Values>;
|
|
102
|
+
|
|
103
|
+
/** Current errors keyed by path; `""` holds form-level errors. */
|
|
104
|
+
readonly errors: Readonly<ShallowRef<FormErrorRecord>>;
|
|
105
|
+
|
|
106
|
+
/** Whether no errors are currently recorded. */
|
|
107
|
+
readonly valid: ComputedRef<boolean>;
|
|
108
|
+
|
|
109
|
+
/** Whether any value differs from its initial value. */
|
|
110
|
+
readonly dirty: ComputedRef<boolean>;
|
|
111
|
+
|
|
112
|
+
/** Nested flags for values that differ from the initial values. */
|
|
113
|
+
readonly dirtyFields: ComputedRef<DirtyFields<Values>>;
|
|
114
|
+
|
|
115
|
+
/** Whether any validation is pending. */
|
|
116
|
+
readonly validating: ComputedRef<boolean>;
|
|
117
|
+
|
|
118
|
+
/** Whether a submission is in progress. */
|
|
119
|
+
readonly submitting: Readonly<Ref<boolean>>;
|
|
120
|
+
|
|
121
|
+
/** Number of submit attempts. Disabled submits are not counted. */
|
|
122
|
+
readonly submitCount: Readonly<Ref<number>>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Whether the latest finished submit was valid and its handler settled.
|
|
126
|
+
* A superseded submit leaves this flag unchanged.
|
|
127
|
+
*/
|
|
128
|
+
readonly isSubmitSuccessful: Readonly<Ref<boolean>>;
|
|
129
|
+
|
|
130
|
+
/** Whether value writes, field-array edits, and submission are ignored. */
|
|
131
|
+
readonly disabled: ComputedRef<boolean>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Typed controls for one field.
|
|
135
|
+
*
|
|
136
|
+
* @param path Dotted path.
|
|
137
|
+
* @returns Field controls whose value type follows the path.
|
|
138
|
+
*/
|
|
139
|
+
readonly field: <Path extends FormPath<Values>>(
|
|
140
|
+
path: Path,
|
|
141
|
+
) => FormFieldControls<FormPathValue<Values, Path>, Path>;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Typed operations for an array field.
|
|
145
|
+
*
|
|
146
|
+
* @param path Dotted path of an array.
|
|
147
|
+
* @returns Field array controls.
|
|
148
|
+
*/
|
|
149
|
+
readonly fieldArray: <Path extends FormArrayPath<Values>>(
|
|
150
|
+
path: Path,
|
|
151
|
+
) => FormFieldArrayControls<FormArrayItem<Values, Path>, Path>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Read the value at a path.
|
|
155
|
+
*
|
|
156
|
+
* @param path Dotted path.
|
|
157
|
+
* @returns The value.
|
|
158
|
+
*/
|
|
159
|
+
readonly getValue: <Path extends FormPath<Values>>(path: Path) => FormPathValue<Values, Path>;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Read a path as a computed ref that tracks later edits.
|
|
163
|
+
*
|
|
164
|
+
* @param path Dotted path.
|
|
165
|
+
* @returns The value at that path.
|
|
166
|
+
*/
|
|
167
|
+
readonly watch: <Path extends FormPath<Values>>(
|
|
168
|
+
path: Path,
|
|
169
|
+
) => ComputedRef<FormPathValue<Values, Path>>;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Write the value at a path. Does nothing while `disabled` is true.
|
|
173
|
+
*
|
|
174
|
+
* @param path Dotted path.
|
|
175
|
+
* @param value New value.
|
|
176
|
+
* @param options Touch and validation behavior.
|
|
177
|
+
*/
|
|
178
|
+
readonly setValue: <Path extends FormPath<Values>>(
|
|
179
|
+
path: Path,
|
|
180
|
+
value: FormPathValue<Values, Path>,
|
|
181
|
+
options?: SetFieldValueOptions,
|
|
182
|
+
) => void;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Replace (or shallowly merge) the values without touching initial values.
|
|
186
|
+
* Does nothing while `disabled` is true.
|
|
187
|
+
*
|
|
188
|
+
* @param values New values.
|
|
189
|
+
* @param options `merge: true` shallowly merges top-level keys.
|
|
190
|
+
*/
|
|
191
|
+
readonly setValues: (values: Partial<Values>, options?: { readonly merge?: boolean }) => void;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Whether a field was touched.
|
|
195
|
+
*
|
|
196
|
+
* @param path Dotted path.
|
|
197
|
+
* @returns Touched state.
|
|
198
|
+
*/
|
|
199
|
+
readonly isTouched: (path: FormPath<Values>) => boolean;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Replace errors (for example with server-side errors).
|
|
203
|
+
*
|
|
204
|
+
* @param errors Errors keyed by path.
|
|
205
|
+
*/
|
|
206
|
+
readonly setErrors: (errors: FormErrors<Values>) => void;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Set one path's errors without replacing the others.
|
|
210
|
+
*
|
|
211
|
+
* @param path Dotted path. `""` is the form-level error.
|
|
212
|
+
* @param message Messages for that path. An empty result clears it.
|
|
213
|
+
*/
|
|
214
|
+
readonly setError: (path: FormPath<Values> | "", message: ValidatorResult) => void;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Clear errors of one path, or all errors.
|
|
218
|
+
*
|
|
219
|
+
* @param path Dotted path; omit to clear everything.
|
|
220
|
+
*/
|
|
221
|
+
readonly clearErrors: (path?: FormPath<Values> | "") => void;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Run every validator, aborting any older validation still in flight.
|
|
225
|
+
*
|
|
226
|
+
* @returns The discriminated result; never rejects on validation failure.
|
|
227
|
+
*/
|
|
228
|
+
readonly validate: () => Promise<FormResult<Output>>;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Validate and, when valid, call `handler` (or the `onSubmit` option).
|
|
232
|
+
* A newer submit aborts an older one's pending validation. Does nothing
|
|
233
|
+
* while `disabled` is true.
|
|
234
|
+
*
|
|
235
|
+
* @param handler Submit handler overriding `onSubmit`.
|
|
236
|
+
* @returns The validation result.
|
|
237
|
+
*/
|
|
238
|
+
readonly submit: (
|
|
239
|
+
handler?: (value: Output, context: { readonly signal: AbortSignal }) => unknown,
|
|
240
|
+
) => Promise<FormResult<Output>>;
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Create a `submit` event listener that prevents the native submission.
|
|
244
|
+
*
|
|
245
|
+
* @param handler Submit handler overriding `onSubmit`.
|
|
246
|
+
* @returns Event listener for `<form @submit>`.
|
|
247
|
+
*/
|
|
248
|
+
readonly handleSubmit: (
|
|
249
|
+
handler?: (value: Output, context: { readonly signal: AbortSignal }) => unknown,
|
|
250
|
+
) => (event?: Event) => Promise<FormResult<Output>>;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Restore initial values (or install new ones). Omitted keep-flags clear
|
|
254
|
+
* that piece of state. Still runs while `disabled` is true.
|
|
255
|
+
*
|
|
256
|
+
* @param values New initial values.
|
|
257
|
+
* @param options State to retain.
|
|
258
|
+
*/
|
|
259
|
+
readonly reset: (values?: Values, options?: FormResetOptions) => void;
|
|
260
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dirty-field comparison for `useForm`.
|
|
3
|
+
*
|
|
4
|
+
* Equality matches the form's own deep compare (dates by instant, arrays by
|
|
5
|
+
* item, plain objects by own keys). Prototype-bearing values are leaves.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { isContainer } from "./form-value-paths.ts";
|
|
9
|
+
|
|
10
|
+
/** Whether two form values are the same, ignoring object identity. */
|
|
11
|
+
export function valuesEqual(left: unknown, right: unknown): boolean {
|
|
12
|
+
if (Object.is(left, right)) return true;
|
|
13
|
+
if (left instanceof Date && right instanceof Date) return left.getTime() === right.getTime();
|
|
14
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
15
|
+
return (
|
|
16
|
+
left.length === right.length && left.every((item, index) => valuesEqual(item, right[index]))
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
if (isContainer(left) && isContainer(right) && !Array.isArray(left) && !Array.isArray(right)) {
|
|
20
|
+
const keys = new Set([...Object.keys(left), ...Object.keys(right)]);
|
|
21
|
+
for (const key of keys) if (!valuesEqual(left[key], right[key])) return false;
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function blankDirty(value: unknown): boolean {
|
|
28
|
+
if (value === undefined) return true;
|
|
29
|
+
if (Array.isArray(value)) return value.every((item) => blankDirty(item));
|
|
30
|
+
if (isContainer(value)) return Object.keys(value).length === 0;
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Nested dirty flags of `current` against `previous`.
|
|
36
|
+
*
|
|
37
|
+
* Equal objects become `{}`, equal arrays become `[]`, and equal leaves
|
|
38
|
+
* become `undefined` so callers can omit them.
|
|
39
|
+
*/
|
|
40
|
+
export function dirtyFieldsOf(current: unknown, previous: unknown): unknown {
|
|
41
|
+
if (valuesEqual(current, previous)) {
|
|
42
|
+
if (Array.isArray(current)) return [];
|
|
43
|
+
if (isContainer(current)) return {};
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(current) || Array.isArray(previous)) {
|
|
47
|
+
const left = Array.isArray(current) ? current : [];
|
|
48
|
+
const right = Array.isArray(previous) ? previous : [];
|
|
49
|
+
const length = Math.max(left.length, right.length);
|
|
50
|
+
return Array.from({ length }, (_, index) => {
|
|
51
|
+
if (index >= left.length || index >= right.length) return true;
|
|
52
|
+
const child = dirtyFieldsOf(left[index], right[index]);
|
|
53
|
+
return blankDirty(child) ? undefined : child;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
if (
|
|
57
|
+
isContainer(current) &&
|
|
58
|
+
isContainer(previous) &&
|
|
59
|
+
!Array.isArray(current) &&
|
|
60
|
+
!Array.isArray(previous)
|
|
61
|
+
) {
|
|
62
|
+
const result: Record<string, unknown> = {};
|
|
63
|
+
const keys = new Set([...Object.keys(current), ...Object.keys(previous)]);
|
|
64
|
+
for (const key of keys) {
|
|
65
|
+
const child = dirtyFieldsOf(
|
|
66
|
+
Object.hasOwn(current, key) ? current[key] : undefined,
|
|
67
|
+
Object.hasOwn(previous, key) ? previous[key] : undefined,
|
|
68
|
+
);
|
|
69
|
+
if (blankDirty(child)) continue;
|
|
70
|
+
result[key] = child;
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function own(record: unknown, key: string): unknown {
|
|
78
|
+
if (!isContainer(record) || Array.isArray(record)) return undefined;
|
|
79
|
+
return Object.hasOwn(record, key) ? record[key] : undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function mergeKept(base: unknown, current: unknown, previous: unknown): unknown {
|
|
83
|
+
if (valuesEqual(current, previous)) return base;
|
|
84
|
+
if (Array.isArray(current) && Array.isArray(previous)) {
|
|
85
|
+
return current.map((item, index) => {
|
|
86
|
+
if (index >= previous.length) return item;
|
|
87
|
+
const baseItem = Array.isArray(base) ? base[index] : undefined;
|
|
88
|
+
return mergeKept(baseItem, item, previous[index]);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
isContainer(current) &&
|
|
93
|
+
isContainer(previous) &&
|
|
94
|
+
!Array.isArray(current) &&
|
|
95
|
+
!Array.isArray(previous)
|
|
96
|
+
) {
|
|
97
|
+
const baseObject = isContainer(base) && !Array.isArray(base) ? base : {};
|
|
98
|
+
const keys = new Set([
|
|
99
|
+
...Object.keys(baseObject),
|
|
100
|
+
...Object.keys(current),
|
|
101
|
+
...Object.keys(previous),
|
|
102
|
+
]);
|
|
103
|
+
const result: Record<string, unknown> = {};
|
|
104
|
+
for (const key of keys) {
|
|
105
|
+
const inCurrent = Object.hasOwn(current, key);
|
|
106
|
+
const inPrevious = Object.hasOwn(previous, key);
|
|
107
|
+
if (!inCurrent && inPrevious) continue;
|
|
108
|
+
if (inCurrent && !inPrevious) {
|
|
109
|
+
result[key] = current[key];
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (!inCurrent) {
|
|
113
|
+
if (Object.hasOwn(baseObject, key)) result[key] = baseObject[key];
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
result[key] = mergeKept(own(baseObject, key), current[key], previous[key]);
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
return current;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Reset values that keep user edits.
|
|
125
|
+
*
|
|
126
|
+
* Fields equal to `previous` take `base`. Dirty leaves, added keys, and
|
|
127
|
+
* array-length edits keep `current`. A removed key stays removed.
|
|
128
|
+
*/
|
|
129
|
+
export function valuesKeepingDirty<Value>(base: Value, current: Value, previous: Value): Value {
|
|
130
|
+
return mergeKept(base, current, previous) as Value;
|
|
131
|
+
}
|