orio-ui 1.15.0 → 1.16.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/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": "^3.0.0 || ^4.0.0"
6
6
  },
7
- "version": "1.15.0",
7
+ "version": "1.16.0",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.2",
10
10
  "unbuild": "3.6.1"
@@ -0,0 +1,38 @@
1
+ import { Ref } from "vue";
2
+ /**
3
+ * Syncs a reactive object's keys to/from URL query params.
4
+ *
5
+ * Behaviour:
6
+ * - **On call (synchronous)**: reads current URL params and pre-populates the
7
+ * state so child components see the correct values before they mount.
8
+ * - **Reactive**: watches the state and updates the URL whenever synced keys
9
+ * change (via `router.replace` — no new history entry).
10
+ *
11
+ * SSR-safe: initial population uses `route.query` (available on the server),
12
+ * so the server renders the same HTML as the client hydrates — no mismatches.
13
+ * Reactive URL writes use `useUrlSearchParams` (client-only, via window).
14
+ *
15
+ * Value handling:
16
+ * - Nested objects → dot notation (`filters.category=shirts`)
17
+ * - Arrays → bracket notation (`tags[0]=cats&tags[1]=dogs`)
18
+ * - Combinations → mixed (`items[0].name=John`)
19
+ * - File / Blob → silently skipped (not serialisable)
20
+ * - Empty strings → removed from URL
21
+ *
22
+ * @param state Reactive object to sync
23
+ * @param keys Explicit list of top-level keys to sync.
24
+ * When omitted, ALL top-level keys currently in the URL are
25
+ * used for initial population, and ALL keys in the state are
26
+ * watched going forward.
27
+ *
28
+ * @example
29
+ * // Sync specific keys (recommended — avoids accidentally syncing private state)
30
+ * const properties = ref<Record<string, string | File[]>>({})
31
+ * useUrlSync(properties, ['variant', 'size', 'product-color'])
32
+ *
33
+ * @example
34
+ * // Sync all — useful when state IS the URL state
35
+ * const filters = ref({ category: '', sort: 'newest' })
36
+ * useUrlSync(filters)
37
+ */
38
+ export declare function useUrlSync(state: Ref<Record<string, unknown>>, keys?: string[]): void;
@@ -0,0 +1,37 @@
1
+ import { useUrlSearchParams } from "@vueuse/core";
2
+ import {
3
+ flattenParams,
4
+ unflattenParams,
5
+ topLevelKeys
6
+ } from "../utils/urlParams.js";
7
+ import { computed, watch } from "vue";
8
+ export function useUrlSync(state, keys) {
9
+ const routeQuery = useRoute().query;
10
+ const initKeys = keys ?? topLevelKeys(routeQuery);
11
+ for (const key of initKeys) {
12
+ const flat = {};
13
+ for (const [k, v] of Object.entries(routeQuery)) {
14
+ if (v !== null && (k === key || k.startsWith(`${key}.`) || k.startsWith(`${key}[`))) {
15
+ flat[k] = v;
16
+ }
17
+ }
18
+ if (Object.keys(flat).length === 0) continue;
19
+ const reconstructed = unflattenParams(flat);
20
+ if (key in reconstructed) state.value[key] = reconstructed[key];
21
+ }
22
+ const urlParams = useUrlSearchParams("history");
23
+ const syncKeys = computed(() => keys ?? Object.keys(state.value));
24
+ watch(
25
+ () => syncKeys.value.map((k) => JSON.stringify(flattenParams(state.value[k], k))).join("|"),
26
+ () => {
27
+ for (const key of syncKeys.value) {
28
+ for (const k of Object.keys(urlParams)) {
29
+ if (k === key || k.startsWith(`${key}.`) || k.startsWith(`${key}[`)) {
30
+ delete urlParams[k];
31
+ }
32
+ }
33
+ Object.assign(urlParams, flattenParams(state.value[key], key));
34
+ }
35
+ }
36
+ );
37
+ }
@@ -38,3 +38,5 @@ export { usePressAndHold } from "./composables/usePressAndHold.js";
38
38
  export { useSound, type SoundOptions } from "./composables/useSound.js";
39
39
  export { useValidation, isFilled, isEmail, type ValidationRule, } from "./composables/useValidation.js";
40
40
  export { iconRegistry, type IconName } from "./utils/icon-registry.js";
41
+ export { flattenParams, unflattenParams, parsePath, topLevelKeys, } from "./utils/urlParams.js";
42
+ export { useUrlSync } from "./composables/useUrlSync.js";
@@ -50,3 +50,10 @@ export {
50
50
  isEmail
51
51
  } from "./composables/useValidation.js";
52
52
  export { iconRegistry } from "./utils/icon-registry.js";
53
+ export {
54
+ flattenParams,
55
+ unflattenParams,
56
+ parsePath,
57
+ topLevelKeys
58
+ } from "./utils/urlParams.js";
59
+ export { useUrlSync } from "./composables/useUrlSync.js";
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Utilities for serialising/deserialising nested values to/from flat URL query params.
3
+ *
4
+ * Encoding conventions:
5
+ * Object keys → dot notation e.g. filters.category=shirts
6
+ * Array items → bracket notation e.g. tags[0]=cats&tags[1]=dogs
7
+ * Combined → e.g. items[0].name=John
8
+ *
9
+ * Non-serialisable values (File, Blob, null, undefined) are silently skipped.
10
+ * Empty strings produce no URL entry.
11
+ */
12
+ /**
13
+ * Recursively flattens a value to flat URL-compatible key-value pairs.
14
+ *
15
+ * @param value The value to flatten (any depth)
16
+ * @param prefix URL param prefix / key name (e.g. "filters", "items[0]")
17
+ *
18
+ * @example
19
+ * flattenParams({ category: 'shirts', tags: ['a', 'b'] }, 'filters')
20
+ * // → { 'filters.category': 'shirts', 'filters.tags[0]': 'a', 'filters.tags[1]': 'b' }
21
+ */
22
+ export declare function flattenParams(value: unknown, prefix: string): Record<string, string>;
23
+ /**
24
+ * Reconstructs a nested object from flat URL query params.
25
+ * Handles dot notation (obj.key) and bracket notation (arr[0]), and combinations.
26
+ *
27
+ * When a URL param has multiple values (string[]) only the first is used.
28
+ *
29
+ * @example
30
+ * unflattenParams({ 'filters.category': 'shirts', 'tags[0]': 'cats', 'tags[1]': 'dogs' })
31
+ * // → { filters: { category: 'shirts' }, tags: ['cats', 'dogs'] }
32
+ */
33
+ export declare function unflattenParams(flat: Record<string, string | string[]>): Record<string, unknown>;
34
+ /**
35
+ * Parses a flat URL param key into path segments.
36
+ *
37
+ * Supports dot notation and bracket notation, including keys with hyphens.
38
+ *
39
+ * @example
40
+ * parsePath('a.b.c') // ['a', 'b', 'c']
41
+ * parsePath('product-color') // ['product-color']
42
+ * parsePath('items[0].name') // ['items', 0, 'name']
43
+ * parsePath('a[0][1].c') // ['a', 0, 1, 'c']
44
+ */
45
+ export declare function parsePath(path: string): (string | number)[];
46
+ /**
47
+ * Extracts the distinct top-level keys from a flat params record.
48
+ *
49
+ * @example
50
+ * topLevelKeys({ 'a': '1', 'b.c': '2', 'd[0]': '3' }) // ['a', 'b', 'd']
51
+ */
52
+ export declare function topLevelKeys(flat: Record<string, unknown>): string[];
@@ -0,0 +1,67 @@
1
+ export function flattenParams(value, prefix) {
2
+ const result = {};
3
+ _flatten(value, prefix, result);
4
+ return result;
5
+ }
6
+ function _flatten(value, key, out) {
7
+ if (value === null || value === void 0) return;
8
+ if (value instanceof File || value instanceof Blob) return;
9
+ if (typeof value === "string") {
10
+ if (value !== "") out[key] = value;
11
+ return;
12
+ }
13
+ if (typeof value === "number" || typeof value === "boolean") {
14
+ out[key] = String(value);
15
+ return;
16
+ }
17
+ if (Array.isArray(value)) {
18
+ for (const [i, item] of value.entries()) {
19
+ _flatten(item, `${key}[${i}]`, out);
20
+ }
21
+ return;
22
+ }
23
+ if (typeof value === "object") {
24
+ for (const [k, v] of Object.entries(value)) {
25
+ _flatten(v, `${key}.${k}`, out);
26
+ }
27
+ }
28
+ }
29
+ export function unflattenParams(flat) {
30
+ const result = {};
31
+ for (const [key, raw] of Object.entries(flat)) {
32
+ const value = Array.isArray(raw) ? raw[0] : raw;
33
+ if (value === void 0 || value === "") continue;
34
+ _setByPath(result, parsePath(key), value);
35
+ }
36
+ return result;
37
+ }
38
+ function _setByPath(root, path, value) {
39
+ if (path.length === 0) return;
40
+ let current = root;
41
+ for (let i = 0; i < path.length - 1; i++) {
42
+ const seg = path[i];
43
+ const nextSeg = path[i + 1];
44
+ const parent = current;
45
+ if (parent[seg] == null || typeof parent[seg] !== "object") {
46
+ parent[seg] = typeof nextSeg === "number" ? [] : {};
47
+ }
48
+ current = parent[seg];
49
+ }
50
+ const lastSeg = path[path.length - 1];
51
+ current[lastSeg] = value;
52
+ }
53
+ export function parsePath(path) {
54
+ const segments = [];
55
+ for (const match of path.matchAll(/([^.[]+)|\[(\d+)\]/g)) {
56
+ segments.push(match[1] !== void 0 ? match[1] : parseInt(match[2], 10));
57
+ }
58
+ return segments;
59
+ }
60
+ export function topLevelKeys(flat) {
61
+ const keys = /* @__PURE__ */ new Set();
62
+ for (const k of Object.keys(flat)) {
63
+ const match = k.match(/^([^.[]+)/);
64
+ if (match) keys.add(match[1]);
65
+ }
66
+ return [...keys];
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orio-ui",
3
- "version": "1.15.0",
3
+ "version": "1.16.0",
4
4
  "description": "Modern Nuxt component library with theme support",
5
5
  "type": "module",
6
6
  "main": "./dist/module.mjs",