Vue utilities for observing DOM elements using Intersection Observer
npm install @intersection-observer/vueVue utilities for observing DOM elements using Intersection Observer API. This package provides composables and components to easily detect when elements enter or leave the viewport.
``bash`
npm install @intersection-observer/vueor
yarn add @intersection-observer/vueor
pnpm add @intersection-observer/vue
- 🎯 useInView Composable: Vue 3 composable for intersection observer functionality
- 🎨 LazyRender Component: Pre-built component for lazy loading and visibility detection
- 📦 TypeScript Support: Full TypeScript support with type definitions
- 🚀 Vue 3 Compatible: Built for Vue 3 Composition API
- âš¡ Lightweight: Minimal bundle size with no external dependencies
The useInView composable provides a reactive way to detect when an element enters or leaves the viewport.
`vue
{{ inView ? 'Element is visible!' : 'Element is hidden' }}
`
#### API
`typescript`
function useInView(options?: InViewOptions): {
ref: Ref
inView: Ref
}
Options:
- threshold: Number or array of numbers (0-1) indicating at what percentage of the target's visibility the observer's callback should be executedrootMargin
- : String similar to CSS margin (e.g., "10px 20px 30px 40px")root
- : Element to use as the viewport (defaults to browser viewport)
The LazyRender component provides a declarative way to render content based on visibility.
#### Basic Usage
`vue
This content will only render when it comes into view
`
#### With Custom Element
` This section will render when visiblevue`
Lazy loaded section
#### With Render Props
`vue`
Content is now visible!
Loading...
#### With Callback
`vue
:onChange="handleVisibilityChange"
>
This element will trigger callbacks when visibility changes
`
#### LazyRender Props
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| as | string | 'div' | HTML element to render |threshold
| | number \| number[] | 0 | Visibility threshold (0-1) |onChange
| | (isVisible: boolean) => void | - | Callback when visibility changes |className
| | string | - | CSS class name |style
| | string \| object | - | Inline styles |
`vue
`
`vue
{{ item.content }}
Loading more items...
`
` {{ section.content }}vue
:key="section.id"
:onChange="(visible) => trackSectionView(section.id, visible)"
>
{{ section.title }}
`
This package uses the Intersection Observer API, which is supported in all modern browsers:
- Chrome 51+
- Firefox 55+
- Safari 12.1+
- Edge 15+
For older browsers, consider using a polyfill like intersection-observer`.
MIT