antfu / vueuse
- четверг, 23 июля 2020 г. в 00:22:55
TypeScript
🧰 Collection of Composition API utils for Vue 2 and 3
Collection of essential Vue Composition API (inspired by react-use)
import { useMouse, usePreferredDark, useLocalStorage } from '@vueuse/core'
new Vue({
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/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
— simple wrapper for setInterval
useNow
— reactive current timestampuseRaf
— reactive time elapsed on every requestAnimationFrame
useRafFn
— call function on every requestAnimationFrame
useTimeout
— update value after a given timeuseTimeoutFn
— call function after a given timeuseTransition
— transition between valuesBrowser
useBrowserLocation
— reactive browser locationuseClipboard
— reactive Clipboard APIuseCssVar
— manipulate CSS variablesuseEventListener
— use EventListener with easeuseFullscreen
— reactive Fullscreen APIuseMediaQuery
— reactive Media QueryusePermission
— reactive Permissions APIusePreferredColorScheme
— reactive prefers-color-scheme media queryusePreferredDark
— reactive dark theme preferenceusePreferredLanguages
— reactive Navigator LanguagesuseShare
— reactive Web Share APIMisc
useWebSocket
— reactive simple WebSocket clientuseWebWorker
— simple Web Workers registration and communicationuseWebWorkerFn
— run expensive function without blocking the UISensors
useBattery
— reactive Battery Status APIuseDeviceLight
— reactive DeviceLightEventuseDeviceMotion
— reactive DeviceMotionEventuseDeviceOrientation
— reactive DeviceOrientationEventuseDocumentVisibility
— reactively track Document.visibilityState
useElementVisibility
— tracks the visibility of an element within the viewportuseGeolocation
— reactive Geolocation APIuseMouse
— 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 easilyuseWindowScroll
— reactive window scrolluseWindowSize
— reactive window sizeSide Effects
useDebounce
— debounce execution of a ref valueuseDebounceFn
— debounce execution of a functionuseThrottle
— throttle changing of a ref valueuseThrottleFn
— throttle execution of a functionState
createGlobalState
— keep state in global scope to be reused across Vue instancesuseAsyncState
— reactive async stateuseCounter
— basic counter with utility functionsuseIdle
— tracks whether user is being inactiveuseLocalStorage
— reactive LocalStorageuseSessionStorage
— reactive SessionStorageuseStorage
— reactive LocalStorage/SessionStorageMore functions to be added. Please keep turned. (PRs are also welcome!)
The core package aims to be lightweight and dependence free. While the add-ons are wrapping popular packages into the consistent API style.
@vueuse/i18n
) - Composition wrapper for vue-i18n
createI18n
— create a global i18n instance to be reuseduseI18n
— use vue-i18n instance@vueuse/firebase
) - enables realtime bindings for Firebase
useFirestore
— reactive Firestore bindinguseRTDB
— reactive Firebase Realtime Database bindingSee the Contributing Guide
This project is heavily inspired by the following awesome projects.
Thanks!
This project exists thanks to all the people who contribute. [Contribute].
Become a financial contributor and help us sustain our community. [Contribute]
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