vue-router-query-sync 0.0.10 → 0.0.13

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 (2) hide show
  1. package/README.md +175 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,175 @@
1
+ # vue-router-query-sync
2
+
3
+ ![demo](src/assets/demo.gif)
4
+
5
+ > Effortlessly sync Vue Router query parameters with your store or local refs — no boilerplate.
6
+
7
+ [![npm version](https://img.shields.io/npm/v/vue-router-query-sync.svg)](https://www.npmjs.com/package/vue-router-query-sync)
8
+ [![license](https://img.shields.io/npm/l/vue-router-query-sync.svg)](LICENSE)
9
+
10
+ ---
11
+
12
+ ## ✨ What is this?
13
+
14
+ `vue-router-query-sync` is a tiny, fully-typed Vue 3 utility that keeps your reactive state
15
+ (Pinia store fields or local refs) in sync with the current URL query string — both ways.
16
+
17
+ - Changes in your store update the URL query.
18
+ - Changes in the URL query update your store.
19
+ - Batched updates to avoid excessive router.replace calls.
20
+
21
+ ---
22
+
23
+ ## ✅ Requirements
24
+
25
+ - Vue 3 (`vue@^3.3.13`)
26
+ - Vue Router 4 (`vue-router@^4.0.3`)
27
+
28
+ ---
29
+
30
+ ## 🚀 Installation
31
+
32
+ ```bash
33
+ npm install vue-router-query-sync
34
+ # or
35
+ yarn add vue-router-query-sync
36
+ # or
37
+ pnpm add vue-router-query-sync
38
+ ```
39
+
40
+ ---
41
+
42
+ ## ⚙️ Setup
43
+
44
+ ```ts
45
+ // main.ts
46
+ import { createApp } from 'vue'
47
+ import { createRouter, createWebHistory } from 'vue-router'
48
+ import routerQuerySync from 'vue-router-query-sync'
49
+ import App from './App.vue'
50
+
51
+ const router = createRouter({
52
+ history: createWebHistory(),
53
+ routes: []
54
+ })
55
+
56
+ createApp(App)
57
+ .use(router)
58
+ .use(routerQuerySync, { router })
59
+ .mount('#app')
60
+ ```
61
+
62
+ ---
63
+
64
+ ## 🧩 Basic Usage
65
+
66
+ Sync a value with a query param named `tab`:
67
+
68
+ ```ts
69
+ import { useQuerySync } from 'vue-router-query-sync'
70
+ import { ref } from 'vue'
71
+
72
+ const tab = ref<'favorite' | 'all'>('all')
73
+
74
+ useQuerySync(
75
+ 'tab',
76
+ () => tab.value,
77
+ (val) => (tab.value = val)
78
+ )
79
+ ```
80
+
81
+ Now:
82
+ - Visiting `?tab=favorite` sets `tab.value = 'favorite'`.
83
+ - Changing `tab.value = 'all'` updates the URL to `?tab=all`.
84
+
85
+ ---
86
+
87
+ ## 🔧 API
88
+
89
+ ```ts
90
+ function useQuerySync<T extends string | number>(
91
+ key: string,
92
+ get: () => T | null,
93
+ set: (val: T) => void,
94
+ options?: QuerySyncOptions
95
+ ): void
96
+
97
+ type QuerySyncOptions = {
98
+ deps?: Ref<unknown>[]
99
+ context?: string
100
+ }
101
+ ```
102
+
103
+ - **key**: Query parameter name, e.g., `'tab'`.
104
+ - **get**: Function returning the current value from your store/ref. Return `null` to indicate “no value”.
105
+ - **set**: Function that writes the value to your store/ref.
106
+ - **options.deps**: Array of refs. If provided, the initial sync runs after any of these deps change (useful when state is populated asynchronously).
107
+ - **options.context**: If the same query key may be used multiple times on the same page, provide a unique context to avoid collisions. The actual query key becomes `${context}_${key}`.
108
+
109
+ ---
110
+
111
+ ## ❗ Important
112
+
113
+ If a page can have multiple components with the same query key, pass a unique `context` to avoid conflicts.
114
+ The real key used in the URL will be `${context}_${key}`. Otherwise, use unique keys per component.
115
+
116
+ ---
117
+
118
+ ## 🏷️ Context Example
119
+
120
+ If two widgets on the same page need a `tab` param, use unique contexts:
121
+
122
+ ```ts
123
+ useQuerySync('tab', () => usersStore.tab, (v) => (usersStore.tab = v), { context: 'users' })
124
+ useQuerySync('tab', () => ordersStore.tab, (v) => (ordersStore.tab = v), { context: 'orders' })
125
+ // URL keys will be: users_tab and orders_tab
126
+ ```
127
+
128
+ ---
129
+
130
+ ## ⏳ Delayed/Dependent Initialization
131
+
132
+ If your state becomes available later (e.g., after a request), delay the initial sync with `deps`:
133
+
134
+ ```ts
135
+ const isReady = ref(false)
136
+
137
+ useQuerySync('sort', () => store.sort, (v) => (store.sort = v), { deps: [isReady] })
138
+
139
+ // Later when data is loaded:
140
+ isReady.value = true
141
+ ```
142
+
143
+ ---
144
+
145
+ ## 📦 Exports
146
+
147
+ ```ts
148
+ import routerQuerySync, { useQuerySync, replaceRouterQueue } from 'vue-router-query-sync'
149
+ ```
150
+
151
+ - **default**: Vue plugin to register with `app.use(routerQuerySync, { router })`.
152
+ - **useQuerySync**: The composable described above.
153
+ - **replaceRouterQueue**: Internal helper that batches query updates (exported in case you need manual batching).
154
+
155
+ Note: `setRouter/getRouter` are internal; prefer using the plugin install.
156
+
157
+ ---
158
+
159
+ ## 🔒 TypeScript
160
+
161
+ `useQuerySync` is fully typed. You can annotate the expected type if needed:
162
+
163
+ ```ts
164
+ useQuerySync<number>('page', () => page.value, (v) => (page.value = v))
165
+ ```
166
+
167
+ Values must be `string` or `number`.
168
+
169
+ ---
170
+
171
+ ## 📄 License
172
+
173
+ MIT © Ivan Chikachev
174
+
175
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-router-query-sync",
3
- "version": "0.0.10",
3
+ "version": "0.0.13",
4
4
  "description": "Vue composable for syncing router query params with store",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",