Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vue-window-size

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-window-size

Reactivity window size for Vue.js.

  • 2.0.0-beta.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.2K
decreased by-6.82%
Maintainers
1
Weekly downloads
 
Created
Source

vue-window-size

npm version License: MIT CI Status

Provides reactivity window size properties for Vue.js.

Install

for Vue v3

The following command installs vue-window-size v1.

$ yarn add vue-window-size
// or
$ npm i vue-window-size

for Vue v2

The following command installs vue-window-seize v0.

$ yarn add vue-window-size@0.6.2
// or
$ npm i vue-window-size@0.6.2

You can install v1 using the following command.

note: v1.1.0 or later is for vue3 only

Vue.js v2.6.x

When using with vue-window-size v1.0.6 it depends on the @vue/composition-api. And requires Vue.js v2.6.

$ yarn add vue-window-size@1.0.6 @vue/composition-api
// or
$ npm i vue-window-size@1.0.6 @vue/composition-api
Vue.js v2.7.x

For Vue.js v2.7, please use vue-window-size v1.0.8 or higher.

$ yarn add vue-window-size@1.0.x
// or
$ npm i vue-window-size@1.0.x

Usage

Composition API or Plugin or Mixin.

Composition APIPluginMixin
has window sizes propertiesonly in useall componentsonly in use
handle resize eventonly in useall timesall times

Composition API

Use with component.

<template>
  <div>
    <p>window width: {{ windowWidth }}</p>
    <p>window height: {{ windowHeight }}</p>
  </div>
</template>

<script>
import { useWindowSize } from 'vue-window-size';

export default {
  setup() {
    const { width, height } = useWindowSize();
    return {
      windowWidth: width,
      windowHeight: height,
    };
  },
};
</script>

note: useWindowSize handles a Resize Event only when it is in use. Even if it is called by multiple components, the Resize event is processed only once. If it is not used, it will not be handled.

Plugin

Install plugin

import { createApp } from 'vue';
import App from "./App.vue";  // your App component
import { VueWindowSizePlugin } from 'vue-window-size/option-api';

const app = createApp(App);
app.use(VueWindowSizePlugin);

Use with component

<template>
  <div>
    <p>window width: {{ $windowWidth }}</p>
    <p>window height: {{ $windowHeight }}</p>
  </div>
</template>

Mixin

Use with component

<template>
  <div>
    <p>window width: {{ $windowWidth }}</p>
    <p>window height: {{ $windowHeight }}</p>
  </div>
</template>

<script>
import { vueWindowSizeMixin } from 'vue-window-size/option-api';

export default {
  mixins: [vueWindowSizeMixin()],
};
</script>

Config for Option API

delay (option)

  • type: Number
  • default: 33
    • About 30 FPS

Change delay time of resize event.

e.g.

import { createApp } from 'vue';
import App from "./App.vue";  // your App component
import { VueWindowSizePlugin } from 'vue-window-size/option-api';

const app = createApp(App);
app.use(VueWindowSizePlugin, {
  delay: 100,
});

Public API for Option API

config(config: VueWindowSizeOptionApiConfig)

Same as config for Option API.

import { vueWindowSizeAPI } from 'vue-window-size/option-api';

vueWindowSizeAPI.config({
  delay: 100,
});

init()

Initialize the plugin. Usually called automatically. Please call it if you want to use it again after destroy.

import { vueWindowSizeAPI } from 'vue-window-size/option-api';

vueWindowSizeAPI.init();

destroy()

Remove the resize event.

import { vueWindowSizeAPI } from 'vue-window-size/option-api';

vueWindowSizeAPI.destroy();

FAQ

Why is there no Config in the Composition API?

useWindowSize is a singleton and handles the resize event. Therefore, using useWindowSize(config) will affect all components in used. Due to the nature of the Composition API, this is not the desired behavior. I also think that there are not many use cases that need to be set individually.
If requested, I will create a useAtomicWindowSize(config) that can be set atomically, so please create an issue. Or create a factory function for createUseWindowSize(config).

When is the removeEventListener called when using plugin and mixin??

vue-window-size adds addEventListener only once, even if it is used in mixin. So basically you do not need to call removeEventListener. If you want to call removeEventListener please call destroy method.

Contribution

If you find a bug or want to contribute to the code or documentation, you can help by submitting an issue or a pull request.

License

MIT

Keywords

FAQs

Package last updated on 15 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc