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-use-kit

v2.8.0

Published

Useful kit of Vue composition API utilities

Downloads

37

Readme

NPM Version NPM Downloads Build Status GitHub license Demo

🛠️ Vue kit of useful Vue Composition API functions.

Install

npm install vue-use-kit

Since Vue 3.0 has not yet been released, you must also install @vue/composition-api library, which will enable the composition API in Vue 2.0.

npm install @vue/composition-api

Setup

import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)

Usage

<template>
  <div>isDesktop: {{ isDesktop ? 'Yes' : 'No' }}</div>
</template>

<script lang="ts">
  import Vue from 'vue'
  import { useMedia } from 'vue-use-kit'

  export default Vue.extend({
    name: 'UseMedia',
    setup() {
      const query = '(min-width: 1024px)'
      const isDesktop = useMedia(query)
      return { isDesktop }
    }
  })
</script>

APIs

  • Sensors
  • Animations
    • useInterval — updates counter value repeatedly on a fixed time delay. Demo
    • useIntervalFn — calls function repeatedly on a fixed time delay. Demo
    • useRaf — returns elapsedTime with requestAnimationFrame. Demo
    • useRafFn — calls function with requestAnimationFrame. Demo Demo
    • useTimeout — returns isReady true when timer is completed. Demo
    • useTimeoutFn — calls function when timer is completed. Demo
  • Side Effects
    • useBeforeUnload — shows browser alert when user try to reload or close the page. Demo
    • useCookie — provides way to read, update and delete a cookie. Demo
    • useFetch — provides a way to fetch resources asynchronously across the network. Demo
    • useLocalStorage — provides way to read, update and delete a localStorage key. Demo
    • useSessionStorage — provides way to read, update and delete a sessionStorage key. Demo
  • UI
    • useClickAway — triggers callback when user clicks outside target area. Demo
    • useFullscreen — display an element in full-screen mode Demo
  • Utils
    • getQuery — get a CSS media query string. Demo

🎁 Other examples of composition API functions

Some Vue composition API functions have not been included in this library but can be created easily by following the steps below.

Creating a useStore function connected to Vuex store is pretty straightforward. For example, given the following store:

// @src/mystore.ts
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: { searchTerm: '' },
  mutations: {
    SET_SEARCH(state, newVal) {
      state.searchTerm = newVal
    }
  },
  getters: { searchTerm: state => state.searchTerm },
  actions: {},
  modules: {}
})

export default store

We can get the store from the vm and expose it in our useStore function:

// @src/useStore.ts
import { getCurrentInstance } from '@vue/composition-api'

export function useStore() {
  const vm = getCurrentInstance()
  if (!vm) throw new Error('Vue instance not found!')
  return vm.$store
}

Now we can use useStore inside the setup() method of our component:

// MyComponent.vue
<template>
  <input type="text" v-model="searchTerm" placeholder="🔎 Search..." />
</template>

<script lang="ts">
  import Vue from 'vue'
  import { ref, watch } from '@src/api'
  import { useStore } from '@src/useStore'

  export default Vue.extend({
    name: 'UseStoreDemo',
    setup() {
      const { commit, getters } = useStore()
      const searchTerm = ref(getters['searchTerm'])
      watch(searchTerm, newVal => commit('SET_SEARCH', newVal))
      return { searchTerm }
    }
  })
</script>

Creating a useRouter function connected to VueRouter is rather simple. We can get $route and $router from the vm and expose them in our useRouter function:

// @src/useRouter.ts
import { getCurrentInstance } from '@vue/composition-api'

export function useRouter() {
  const vm = getCurrentInstance()
  if (!vm) throw new Error('Vue instance not found!')
  const route = vm.$route
  const router = vm.$router
  return { route, router }
}

Now we can use useRouter inside the setup() method of our component:

// MyComponent.vue
<template>
  <div>
    Current id: {{ id }}
  </div>
</template>

<script lang="ts">
  import Vue from 'vue'
  import { useRouter } from '@src/useRouter'

  export default Vue.extend({
    name: 'UseRouterDemo',
    setup() {
      const { route } = useRouter()
      return { id: route.params.id }
    }
  })
</script>

Inspiration

Made with