Skip to content

Composables

Intro

Vant provide some built-in composition APIs, you can directly use these APIs for development.

Install

Although @vant/use is already included in Vant's dependencies, it is still recommended to install this package explicitly:

shell
# with npm
npm i @vant/use

# with yarn
yarn add @vant/use

# with pnpm
pnpm add @vant/use

# with Bun
bun add @vant/use

Demo

js
import { useWindowSize } from '@vant/use';

const { width, height } = useWindowSize();

console.log(width.value); // -> window width
console.log(height.value); // -> window height

API List

NameDescription
useClickAwayTriggers a callback when user clicks outside of the target element
useCountDownUsed to manage the countdown
useCustomFieldValueUsed to custom Field value
useEventListenerUsed to attach event
usePageVisibilityGet the visible state of the page
useRectGet the size of an element and its position relative to the viewport
useRelationEstablish the association relationship between parent and child components
useScrollParentGet the closest parent element that is scrollable
useToggleUsed to switch between true and false
useWindowSizeGet the viewport width and height of the browser window
useRafUsed to manage the requestAnimationFrame

Enterprise-level mobile solution based on Vant