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-url-state-sync-fixed

v0.1.1

Published

A Vue plugin to sync router queries and hash with state.

Downloads

4

Readme

Vue Url State Sync (vuss)

Actions Status Actions Status Maintainability

A Vue plugin to sync router queries and hash with state.

This plugin requires that your project use Vue Router in history mode

Check out the demo

Why?

Allow users to deep link (bookmark) to state outside of normal routing such as:

  • filtered lists
  • searched lists
  • modal states
  • tabs
  • etc.

Install

yarn add -D vue-url-state-sync
# or
npm i -D vue-url-state-sync
import Vue from 'vue';
import VueRouter from 'vue-router';
import VueUrlStateSync from 'vue-url-state-sync';

Vue.use(VueRouter);
Vue.use(VueUrlStateSync);

const router = new Router({
  mode: 'history', // must be in history mode for hashes to work!
  routes: [...]
});

const app = new Vue({
  router
}).$mount('#app')

Examples

Basic sync

<template>
  <input v-model="term" type="text" placeholder="Search term" />
</template>

<script>
  export default {
    name: 'Users',

    data() {
      return {
        showModal: false,
        searchTerm: 'foobar'
      };
    },

    beforeMount() {
      this.$vuss.h.sync('showModal');
      this.$vuss.q.sync('searchTerm');

      /**
       * Resulting URL
       * ?search_term=foobar#show-modal
       */
    }
  };
</script>

Vuex mapState

import { mapState } = 'vuex';

export default {
  name: 'Users',

  computed: {
    ...mapState('users', ['showModal'])
  },

  beforeMount() {
    // define a callback for when the hash changes
    this.$vuss.h.sync('showModal', 'showAddUserModal', (newVal) => {
      this.$store.commit('users/setShowModal', newVal);
    });
  }
};

Vuex computed with setter

export default {
  name: 'Users',

  computed: {
    showModal: {
      get() {
        return this.$store.state.users.showModal;
      },

      set(newVal) {
        this.$store.commit('users/setShowModal', newVal);
      }
    }
  },

  beforeMount() {
    this.$vuss.h.sync('showModal', 'showAddUserModal');
  }
};

Nested State

export default {
  name: 'Users',

  data() {
    return {
      list: {
        filters: {
          animal: 'dog',
          color: 'red',
          gender: 'male'
        }
      }
    };
  },

  beforeMount() {
    this.$vuss.q.sync('list.filters');
    /**
     * Resulting URL
     * ?list_filters=%7B"animal"%3A"dog","color"%3A"red","gender"%3A"male"%7D
     */

    this.$vuss.q.sync('list.filters', 'filters');
    /**
     * Resulting URL
     * ?filters=%7B"animal"%3A"dog","color"%3A"red","gender"%3A"male"%7D
     */
  }
};

Hashes vs Queries

Hashes

Hashes is intended to be used for simple boolean state, such as whether or not to show a modal. It is always converted to kebab-case.

export default {
  // data
  data() {
    return {
      modal: true
    };
  },

  // sync
  beforeMount() {
    // you can define both the state key and the hash value if you wish
    this.$vuss.h.sync('modal', 'showModal');

    // optional - define a callback for when the hash changes
    this.$vuss.h.sync('modal', 'showModal', newVal => {
      this.modal = newVal;
    });
  }
};

// result
('https://mysite.com#show-modal');

Queries

Queries is intended for data such as search terms, filters, current tab, etc. It is stringified on write, and parsed on read. Keys are always converted to snake_case.

export default {
  // data
  data() {
    return {
      filters: {
        animal: 'dog',
        color: 'red',
        gender: 'male'
      }
    };
  },

  // sync
  beforeMount() {
    // you can define both the state key and the query key if you wish
    this.$vuss.q.sync('filters', 'f');

    // optional - define a callback for when the query changes
    this.$vuss.h.sync('filters', 'f', newVal => {
      this.filters = { ...newVal };
    });
  }
};

// result
('https://mysite.com?f=%7B"animal"%3A"dog","color"%3A"red","gender"%3A"male"%7D');

Global Mixins

Two global mixins are installed by this plugin - one for syncing hash and one for syncing query queries.

Computed

vm.$hash

Provides hash

  • Returns {string}
vm.$hash = 'show-modal'

vm.$query

Provides query

  • Returns {object}
{
  term: 'foobar'
}

Methods

vm.$vuss.h.clear()

Clears hash

  • Returns {void}

vm.$vuss.h.set(hash, replaceHistory)

Sets hash

  • Arguments
    • {string} hash
    • {boolean} [replaceHistory=false] -- if true, a new history entry will not be added to the navigation stack
  • Returns {void}

vm.$vuss.h.sync(state, hash, onHashChange)

Syncs the hash with specific component state.

When first called, it will sync the current hash to the state OR state to hash, in that order. It then sets up two watchers -- one for hash changes and one for state changes.

  • Arguments
    • {string} state
    • {string} [hash] -- if not passed, is set to the value of state
    • {function} [onHashChange]
      • Called when Vue Router hash change detected
      • Arguments
        • {*} newVal
  • Returns {void}

vm.$vuss.q.clear()

Clears query

  • Returns {void}

vm.$vuss.q.exists(key)

Provides if query key exists

  • Arguments
    • {string} key
  • Returns {boolean}

vm.$vuss.q.push(hash)

Updates query query object.

  • Arguments
    • {object} query
    • {function} [next=() => {}]
    • {boolean} [replaceHistory=false] -- if true, a new history entry will not be added to the navigation stack
  • Returns {void}

vm.$vuss.q.remove(key)

Removes query value by key

  • Arguments
    • {object} query
  • Returns {void}

vm.$vuss.q.set(key, value, replaceHistory)

Sets query value by key

  • Arguments
    • {string} key
    • {*} value
    • {boolean} [replaceHistory=false] -- if true, a new history entry will not be added to the navigation stack
  • Returns {void}

vm.$vuss.q.sync(state, key, onQueryChange)

Syncs a query key with specific component state.

When first called, it will sync the current query to the state OR state to query, in that order. It then sets up two watchers -- one for query changes and one for state changes.

  • Arguments
    • {string} state
    • {string} [key] -- if not passed, is set to the value of state
    • {function} [onQueryChange]
      • Called when Vue Router query change detected
      • Arguments
        • {*} newVal
  • Returns {void}

Scripts

yarn lint
yarn test
yarn build

How to Contribute

Pull Requests

  1. Fork the repository
  2. Create a new branch for each feature or improvement
  3. Send a pull request from each feature branch to the develop branch

License

MIT