DEV Community

Cover image for VueUse - The library that makes Vue 3 worth the upgrade
Fotis Adamakis
Fotis Adamakis

Posted on • Updated on • Originally published at Medium

VueUse - The library that makes Vue 3 worth the upgrade

Composition API promotes a much better paradigm of sharing code between components. Explicit dependencies and namespacing by design enable us to share code even between different projects. This is the main idea behind VueUse.

VueUse is a collection of 200+ essential utility functions for interacting with various APIs like browser, state, network, animation, time and more.

Image description

A small sample of these utilities enables you to:

and so many more!

Extensible with add-ons

The core package aims to be lightweight and dependence free. Integration with popular packages is supported using add-ons. This ensures a consistent API style.

Some existing add-ons currently are:

More information about VueUse addons here.

Examples

Letโ€™s explore some examples from the official VueUse docs.

Dropzone

Creates a zone where files can be dropped.

Dropzone demo

<script setup lang="ts">
import { useDropZone } from '@vueuse/core'

const dropZoneRef = ref<HTMLDivElement>()

function onDrop(files: File[] | null) {
  // called when files are dropped on zone
}

const { isOverDropZone } = useDropZone(dropZoneRef, onDrop)
</script>

<template>
  <div ref="dropZoneRef">
    Drop files here
  </div>
</template>
Enter fullscreen mode Exit fullscreen mode

Dropzone interactive demo

Clipboard

Reactive Clipboard API.

Clipboard demo

import { useClipboard } from '@vueuse/core'

const source = ref('Hello')
const { 
    text, 
    copy, 
    copied, 
    isSupported 
} = useClipboard({ source })
Enter fullscreen mode Exit fullscreen mode
<button @click='copy()'>
  <!-- by default, `copied` will be reset in 1.5s -->
  <span v-if='!copied'>Copy</span>
  <span v-else>Copied!</span>
</button>
Enter fullscreen mode Exit fullscreen mode

Clipboard interactive demo

Click outside utility

Listen for clicks outside of an element.

Click outside demo

<template>
  <div ref="target">
    Hello world
  </div>
  <div>
    Outside element
  </div>
</template>

<script>
import { ref } from 'vue'
import { onClickOutside } from '@vueuse/core'
export default {
  setup() {
    const target = ref(null)
    onClickOutside(target, (event) => console.log(event))
    return { target }
  }
}
</script>
Enter fullscreen mode Exit fullscreen mode

Click outside interactive demo

Parallax

_Create a parallax effect. It uses useDeviceOrientation and fallback to useMouse if orientation is not supported.

Parallax demo

Parallax interactive demo

WatchDebounced

Something that you definitely have implemented in the past. A debounced watch.

Debounced demo

import { watchDebounced } from '@vueuse/core'

watchDebounced(
  source,
  () => { console.log('changed!') },
  { debounce: 500, maxWait: 1000 },
)
Enter fullscreen mode Exit fullscreen mode

WatchDebounced interactive demo

TimeAgo

TimeAgo demo

import { useTimeAgo } from '@vueuse/core'

const timeAgo = useTimeAgo(new Date(2021, 0, 1))
Enter fullscreen mode Exit fullscreen mode

useTimeAgo interactive demo

Conclusion

We only touched the surface, but hopefully, you got the idea of how awesome this library is. There are hundreds of utilities to explore (currently 274!), and the number is constantly rising. Of course, you can contribute your own If you find somethingโ€™s missing.

Libraries like VueUse are leading the way to make Vue awesome again. They promote code reusability and improve the overall developer experience.

Dive into the awesome documentation, spread the word and maybe give the author a credit (or buy him a coffee โ˜•๏ธ).

Vue Use

Top comments (2)

Collapse
 
michaelsynan profile image
Michael Synan

Excellent job

Collapse
 
alinasir profile image
Ali Nasir

๐Ÿ‘๐Ÿผ๐Ÿ‘๐Ÿผ๐Ÿ‘๐Ÿผ