Skip to content

Commit 429a91c

Browse files
committed
refactor: rename "useQueryRef" to "queryRef" // added details in README
1 parent 04fdc6b commit 429a91c

File tree

10 files changed

+10562
-18
lines changed

10 files changed

+10562
-18
lines changed

README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ Find and replace all on all files (CMD+SHIFT+F):
77
- Description: My new Nuxt module
88
-->
99

10-
# `useQueryRef` - URL-persisted `ref` for Nuxt
10+
# `queryRef()` - URL-persisted `ref()` for Nuxt
1111

1212
[![npm version][npm-version-src]][npm-version-href]
1313
[![npm downloads][npm-downloads-src]][npm-downloads-href]
1414
[![License][license-src]][license-href]
1515
[![Nuxt][nuxt-src]][nuxt-href]
1616

17+
![](public/images/preview.gif)
18+
1719
## Features
1820

1921
<!-- Highlight some of the features your module provide here -->
@@ -30,7 +32,62 @@ Install the module to your Nuxt application with one command:
3032
npx nuxi module add nuxt-queryref
3133
```
3234

33-
That's it! You can now use `useQueryRef` in your Nuxt app ✨
35+
That's it! You can now use `queryRef` in your Nuxt app ✨
36+
37+
## Usage
38+
39+
Use `queryRef()` just like you would use a normal `ref()`
40+
41+
```ts
42+
const variable = queryRef(<key>, <value>)
43+
```
44+
45+
It takes two Parameters:
46+
47+
- `<key>`: The key for the URL Query Param
48+
- `<value>`: The actual value, just like you would use with `ref()`
49+
50+
Following types are supported for `<value>`:
51+
52+
- `string`
53+
- `number`
54+
- `boolean`
55+
- `Object`
56+
- `Array (of each of the above)`
57+
58+
---
59+
60+
### Examples
61+
62+
```ts
63+
const name = queryRef('name', 'Lukas')
64+
```
65+
66+
```ts
67+
// With generic type (optional, default is based on <value>)
68+
const name = queryRef<string>('name', 'Lukas')
69+
```
70+
71+
```ts
72+
// With Object and custom type
73+
const name = queryRef<{ firstName: string }>('name', { firstName: 'Lukas' })
74+
```
75+
76+
## Use-cases
77+
78+
There are multiple scenarios where URL-persisting makes sense:
79+
80+
- In general: making URLs shareable or look the same on reload
81+
- Persisting **_filters or sorting_** for sharing
82+
- Persisting **_selected tabs or popups_**
83+
- Persisting **_selected image (-index)_** for e.g. a slider
84+
85+
## Under the Hood
86+
87+
Some insights:
88+
89+
- The value is loaded on page load via **_useRoute()_**, which ensures that the value will already be loaded during **_SSR_** and no flickering will occur
90+
- If the provided value is an **_array_**, the type will be **_inferred by the first item_** of the value. Therefore mixed-type arrays are currently not supported (or will lead to problems)
3491

3592
## Contribution
3693

playground/app.vue

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
<template>
2-
<div>
3-
Nuxt module playground!
4-
5-
<p>name: {{ name }}</p>
6-
7-
<button @click="changeName()">Change Name</button>
2+
<div class="bg-black text-white w-screen h-screen p-4">
3+
<input type="text" placeholder="Type name" v-model="name" class="bg-transparent border border-white px-4 py-2 rounded-md" />
84
</div>
95
</template>
106

117
<script setup lang="ts">
12-
const name = useQueryRef('name', 123)
13-
14-
const changeName = () => {
15-
name.value++
16-
}
8+
const name = queryRef('name', '')
179
</script>

playground/assets/css/tailwind.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;

playground/nuxt.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export default defineNuxtConfig({
2-
modules: ['../src/module'],
2+
modules: [
3+
'../src/module',
4+
'@nuxtjs/tailwindcss',
5+
],
36
myModule: {},
47
devtools: { enabled: true },
58
compatibilityDate: '2025-01-22',

0 commit comments

Comments
 (0)