Collection of essential Vue Composition Utilities
- β‘ Fully tree shakable: Only take what you want, bundle size
- π¦Ύ Type Strong: Written in Typescript, with TS Docs
- πΆ Seamless migration: Works for both Vue 3 and 2
- π© Flexible: Configurable event filters
- π No bundler required: Usable it via CDN
- π SSR Friendly
- πͺ Interactive docs & demos: Check out the Storybook!
- π Optional Add-ons: Router, Firebase, RxJS, etc.
import { useMouse, usePreferredDark, useLocalStorage } from '@vueuse/core'
export default {
setup() {
// tracks mouse position
const { x, y } = useMouse()
// is user prefers dark theme
const isDark = usePreferredDark()
// persist state in localStorage
const store = useLocalStorage(
'my-storage',
{
name: 'Apple',
color: 'red',
},
)
return { x, y, isDark, store }
}
})
Refer to functions list or documentations for more details.
π© From v4.0, it works for Vue 2 & 3 within a single package by the power of vue-demi!
npm i @vueuse/core
Vue 3 Demo: Vite, Webpack / Vue 2 Demo: Vue CLI
<script src="https://unpkg.com/@vueuse/core"></script>
It will be exposed to global as window.VueUse
You can check out the full documents and live demos in Storybook.
-
Animation
useInterval
β reactive counter increases on every intervaluseIntervalFn
β wrapper forsetInterval
with controlsuseRafFn
β call function on everyrequestAnimationFrame
useTimeout
β update value after a given time with controlsuseTimeoutFn
β wrapper forsetTimeout
with controlsuseTimestamp
β reactive current timestampuseTransition
β transition between values
-
Browser
useBrowserLocation
β reactive browser locationuseClipboard
β reactive Clipboard APIuseCssVar
β manipulate CSS variablesuseEventListener
β use EventListener with easeuseFavicon
β reactive faviconuseFullscreen
β reactive Fullscreen APIuseMediaQuery
β reactive Media QueryuseMutationObserver
β watch for changes being made to the DOM treeusePermission
β reactive Permissions APIusePreferredColorScheme
β reactive prefers-color-scheme media queryusePreferredDark
β reactive dark theme preferenceusePreferredLanguages
β reactive Navigator LanguagesuseResizeObserver
β reports changes to the dimensions of an Element's content or the border-boxuseShare
β reactive Web Share APIuseTitle
β reactive document title
-
Component
tryOnMounted
β safeonMounted
tryOnUnmounted
β safeonUnmounted
useVModel
β shorthand for v-model binding
-
Misc
useEventSource
β an EventSource or Server-Sent-Events instance opens a persistent connection to an HTTP serveruseWebSocket
β reactive simple WebSocket clientuseWebWorker
β simple Web Workers registration and communicationuseWebWorkerFn
β run expensive function without blocking the UI
-
Sensors
onClickOutside
β listen for clicks outside of an elementonStartTyping
β fires when users start typing on non-editable elementsuseBattery
β reactive Battery Status APIuseDeviceLight
β reactive DeviceLightEventuseDeviceMotion
β reactive DeviceMotionEventuseDeviceOrientation
β reactive DeviceOrientationEventuseDevicePixelRatio
β reactively trackwindow.devicePixelRatio
useDocumentVisibility
β reactively trackdocument.visibilityState
useElementVisibility
β tracks the visibility of an element within the viewportuseGeolocation
β reactive Geolocation APIuseIdle
β tracks whether the user is being inactiveuseIntersectionObserver
β detects that a target element's visibilityuseMouse
β reactive mouse positionuseMouseInElement
β reactive mouse position related to an elementuseNetwork
β reactive Network statususeOnline
β reactive online stateusePageLeave
β reactive state to show whether the mouse leaves the pageuseParallax
β create parallax effect easilyuseSpeechRecognition
β reactive SpeechRecognitionuseWindowScroll
β reactive window scrolluseWindowSize
β reactive window size
-
State
createGlobalState
β keep states in the global scope to be reusable across Vue instancesuseCounter
β basic counter with utility functionsuseLocalStorage
β reactive LocalStorageuseSessionStorage
β reactive SessionStorageuseStorage
β reactive LocalStorage/SessionStorage
-
Utilities
asyncComputed
β computed for async functionsbiSyncRef
β two-way refs synchronizationcontrolledComputed
β explicitly define the deps of computedextendRef
β add extra attributes to RefmakeDestructurable
β make isomorphic destructurable for object and array at the same timesyncRef
β keep target refs in sync with a source refuseAsyncState
β reactive async stateuseDebounce
β debounce execution of a ref valueuseDebounceFn
β debounce execution of a functionuseRefHistory
β track the change history of a refuseThrottle
β throttle changing of a ref valueuseThrottleFn
β throttle execution of a function
-
Watch
debouncedWatch
β debounced watchignorableWatch
β ignorable watchpausableWatch
β pausable watchthrottledWatch
β throttled watchwatchWithFilter
βwatch
with additional EventFilter controlwhen
β promised one-time watch for ref changes
More functions to be added. Please stay tuned. (PRs are also welcome!)
Meanwhile, try also vue-composable by @pikax!
The core package aims to be lightweight and dependence free. While the add-ons are wrapping popular packages into the consistent API style.
-
Router (
@vueuse/router
) - Utilities for vue-routeruseRouteHash
β shorthand for reactive route.hashuseRouteQuery
β shorthand for reactive route.query
-
Integrations (
@vueuse/integrations
) - Integration wrappers for utility librariesuseAxios
β wrapper foraxios
useCookies
β wrapper foruniversal-cookie
useQRCode
β wrapper forqrcode
-
RxJS (
@vueuse/rxjs
) - Enables RxJS reactive functions in Vuefrom
β two wrappers around of the original functions to allow use ref objectstoObserver
β sugar function to convert a ref in an observeruseObservable
β use an ObservableuseSubscription
β uses subscriptions without worry about unsubscribing to it or memory leaks
-
Firebase (
@vueuse/firebase
) - Enables realtime bindings for FirebaseuseFirestore
β reactive Firestore bindinguseRTDB
β reactive Firebase Realtime Database binding
See the Contributing Guide
This project is heavily inspired by the following awesome projects.
- streamich/react-use
- u3u/vue-hooks
- shuidi-fed/vue-composition-toolkit
- logaretm/vue-use-web
- kripod/react-hooks
Thanks!
This project exists thanks to all the people who contribute. How to Contribute.
Become a financial contributor and help us sustain our community. Contribute via Open Collective
Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]
MIT License Β© 2019-2020 Anthony Fu