Collection of essential Vue Composition API (inspired by react-use)
- β‘ 0 dependencies: Don't worry about your bundle size
- π΄ Fully tree shakable: Only take what you want
- π¦Ύ Type Strong: Written in Typescript
- πΆ Seamless migration: Works for both Vue 3 and 2
- π Browser compatible: Use it through CDN
- πͺ Interactive docs & demos: Check out the Storybook!
- π Optional Add-ons
import { defineComponent } from 'vue'
import { useMouse, usePreferredDark, useLocalStorage } from '@vueuse/core'
const Component = defineComponent({
setup() {
// tracks mouse position
const { x, y } = useMouse()
// is user prefers dark theme
const isDark = usePreferredDark()
// persist state in localStorage
const state = useLocalStorage(
'my-storage',
{
name: 'Apple',
color: 'red',
},
)
return { x, y, isDark, state }
}
})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 # yarn add @vueuse/coreVue 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β simple wrapper forsetIntervaluseNowβ reactive current timestampuseRafβ reactive time elapsed on everyrequestAnimationFrameuseRafFnβ call function on everyrequestAnimationFrameuseTimeoutβ update value after a given timeuseTimeoutFnβ call function after a given timeuseTransitionβ 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
-
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
onStartTypingβ fires when users start typing on non-editable elementsuseBatteryβ reactive Battery Status APIuseDeviceLightβ reactive DeviceLightEventuseDeviceMotionβ reactive DeviceMotionEventuseDeviceOrientationβ reactive DeviceOrientationEventuseDevicePixelRatioβ reactively trackWindow.devicePixelRatiouseDocumentVisibilityβ reactively trackDocument.visibilityStateuseElementVisibilityβ 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 in an elementuseNetworkβ reactive Network statususeOnlineβ reactive online stateusePageLeaveβ reactive state to show whether 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 reused across Vue instancesuseCounterβ basic counter with utility functionsuseLocalStorageβ reactive LocalStorageuseSessionStorageβ reactive SessionStorageuseStorageβ reactive LocalStorage/SessionStorage
-
Utilities
asyncComputedβ like computed refsbiSyncRefβ 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 reftryOnMountedβ safeonMountedtryOnUnmountedβ safeonUnmounteduseAsyncStateβ 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 functionwhenβ 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.
-
Integrations (
@vueuse/integrations) - Integration wrappers for utility librariesuseAxiosβ wrapper foraxiosuseCookiesβ wrapper foruniversal-cookieuseQRCodeβ 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
