npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

v-resize-observer

v2.1.0

Published

Resize observer for Vue. Detect size changes of DOM elements. Support Vue's directive and component.

Downloads

4,238

Readme

v-resize-observer

version download languages license vue@2.x vue@3.x

[ English | 中文 ]

Resize observer for Vue.
Detect size changes of DOM elements. Support Vue's directive and component.

Feature

  • 🕰 Based on ResizeObservable API implementation
  • 🎁 Support vue2 and vue3
  • 💊 Support the use of directives or components
  • 🧲 Optimize the frequency of triggering resize events
  • 🛠 Support browsers: IE9+/Edge/Chrome/Safari/Firefox

Install

npm

npm install v-resize-observer

browser

<script src="https://cdn.jsdelivr.net/npm/vue-demi/lib/index.iife.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/v-resize-observer/dist/index.iife.js"></script>

Usage

<template>
  <div id="app">
    <!-- directives -->
    <div v-resize:50.immediate="onResize">
      Listened to elements
    </div>
    
    <!-- Components -->
    <ResizeComponent @resize="onResize" :delay="100" :disabled="disabled">
      <div>Listened to elements</div>
    </ResizeComponent>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ResizeComponent, resizeDirective as vResize } from 'v-resize-observer'

const disabled = ref(false)

function onResize({ width, height }, target) {
  console.log(target, width, height)
}
</script>

1. Global Configuration

// main.js
import Resizer from 'v-resize-observer'

// [email protected]
const app = createApp(App)
app.use(Resizer, {
  // Custom command names and component names
  directive: 'resize',
  component: 'ResizeComponent'
})

// [email protected]
Vue.use(Resizer)

2. On demand

<script setup>
import { ref } from 'vue'
import {
  ResizeComponent,
  resizeDirective as vResizeObserver //You can change the directive name, the default: `v-resize, 
} from 'v-resize-observer'

// OR
// import Resizer from 'v-resize-observer'
// const ResizeComponent = Resizer.component
// const vResize = Resizer.directive

function onResize({ width, height }, target) {
  console.log(target, width, height)
}
</script>

<template>
  <div id="app">
    <!-- directives -->
    <div v-resize-observer:100="onResize">
      Listened to elements
    </div>
    
    <!-- Components -->
    <ResizeComponent @resize="onResize">
      <div>Listened to elements</div>
    </ResizeComponent>
  </div>
</template>

立即执行一次callback

API

| Parameter | Type | Default | Description | | ---------- | ----------------------- | ------- | ------------------------------------------------------- | | target | string, HTMLElement | - | Target elements to listen to | | delay | number | 150 | Delayed execution time | | immediate | boolean | false | executed immediately | | disabled | boolean | false | disable listening | | resize | function | - | Callback function to listen for changes in element size |

resize(data, target)

  • data : elements size { width, height }
  • target : Listening elements

use directive

The directive default name is v-resize, if you want to change it, you can specify it when you import it.

<div v-resize="onResize" />

<div v-resize:100="onResize" />
<div v-resize:100.immediate="onResize" />
<!-- No limit on trigger frequency -->
<div v-resize:0="onResize" />

Parameter:

  • arg: => delay
  • value: => resize
  • modifiers.immediate

use Component

<ResizeComponent target="#app" :delay="0" disabled="false" @resize="onResize">
  <div>Listened to elements</div>
</ResizeComponent>

props

  • target: The target element to listen to, the default current element.
  • delay: Delay execution time, default: 150.
  • immediate: Execute immediately, default: false.
  • disabled: disable listening, default: false.

events

  • resize: Triggered when listening for element size changes.

ChangeLog

v2.0.0

🚀 Features

  • feat: compatible with vue2.x and 3.x;
  • feat: add Typescript type hints;
  • feat: support for global registration of custom directive names and component names;
  • feat: add re-listening function;
  • perf: remove the limiter trigger limit option;
  • perf: change the delay time wait to delay;

🐞 Bug Fixes

  • fix: Fixed failure to listen again after disabling listening.

v1.x