npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-query-ref

v1.0.0

Published

Type-safe query parameter state manager for Vue.js. Sync URL search params with Vue refs easily.

Downloads

70

Readme

Vue Query Ref

Type-safe URL query parameter state manager for Vue 3.0+
Sync Vue refs with URL query parameters seamlessly, enhancing your app's state persistence and shareability.

🔄 Sync Vue State with URL Query Parameters Now, if you enter a value in the input field, it will be synced with the corresponding query parameter in the URL. For example, when you update the name field, the ?name= query parameter is updated in real-time. When the page is reloaded or shared, the state is restored from the query parameter, ensuring a consistent and persistent user experience. with minimal setup and no external dependencies.

📦 Features

  • Reactive URL Parameters: Use URL query parameters as reactive state with minimal setup.
  • Type-Safe: Supports string, number, and boolean types for query parameters.
  • Easy Integration: It is a ref but syncs with URL query parameters.
  • No External Dependencies: Only requires Vue 3.0+.

🚀 Installation

Install via NPM:

npm install vue-query-ref

Or with Yarn:

yarn add vue-query-ref

🌟 Usage

Basic Example

<template>
  <h1>Vue Query Sync Example</h1>
  <div>
    Name: {{ name }}
    <input
      type="text"
      v-model="name"
      placeholder="Enter your name" />
  </div>
</template>

<script setup lang="ts">
  import { useQueryParam } from 'vue-query-ref';

  // Initialize `name` with the URL query parameter `name` or default to empty string
  const name = useQueryParam('name', '');
</script>

How It Works

  1. Syncs with URL:

    • When you initialize a reactive ref using useQueryParam, it synchronizes its value with a specific URL query parameter.
    • If the parameter exists in the URL, it uses that value; otherwise, it falls back to the provided default.
  2. Auto-Update:

    • Changes to the ref value automatically update the URL query parameter.
    • Updating the input field in the example above modifies the ?name= query parameter in real-time.

Advanced Example

<template>
  <h1>Advanced Query Sync</h1>
  <div>
    <p>User ID: {{ userId }}</p>
    <p>Is Admin: {{ isAdmin }}</p>

    <input
      type="text"
      v-model="userId"
      placeholder="Enter user ID" />
    <label>
      <input
        type="checkbox"
        v-model="isAdmin" />
      Is Admin
    </label>
  </div>
</template>

<script setup lang="ts">
  import { useQueryParam } from 'vue-query-ref';

  // Sync with `userId` and `isAdmin` query parameters
  const userId = useQueryParam<number>('userId', 0);
  const isAdmin = useQueryParam('isAdmin', false);
</script>

URL Example:

  • http://localhost:3000/?userId=123&isAdmin=true
  • In this case, userId is 123 and isAdmin is true.

🛠️ API

useQueryParam

Type Signature

function useQueryParam<T extends string | number | boolean>(
  key: string,
  defaultValue: T
): Ref<T>;

Parameters:

  • key (string): The query parameter key to sync with.
  • defaultValue (T): The default value used when the query parameter is absent.

Returns:

  • A reactive Vue ref (Ref<T>) synced with the specified query parameter.

Example:

const userId = useQueryParam<number>('userId', 0);
const username = useQueryParam<string>('username', 'Guest');
const isAdmin = useQueryParam<boolean>('isAdmin', false);

🔧 Configuration

The configuration for this library is currently a work in progress. Please stay tuned for updates. In the meantime, if you have any specific configuration needs or suggestions, feel free to contribute by opening an issue or submitting a pull request on the GitHub repository.

🛡️ TypeScript Support

The library is fully typed, ensuring seamless integration with TypeScript projects. When you use the useQueryParam composable, TypeScript infers the type based on the defaultValue provided:

const isActive = useQueryParam<boolean>('isActive', false); // Type inferred as Ref<boolean>

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

💬 Feedback & Contributions

Feedback and contributions are welcome! Feel free to open an issue or create a pull request on the GitHub repository.

👤 Author

Ebraheem Alhetari


Thank you for using Vue Query Ref! ❤️