• Stars
    star
    122
  • Rank 292,031 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Provides reactivity window size properties for Vue.js

vue-window-size

npm version License: MIT CI

Provides reactivity window size properties for Vue.js. Supports Vue.js v3.x and v2.7 or higher.

Install

The following command installs vue-window-size v2.

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

Note: For use with v2.6, see here.

Usage

Composition API or Plugin or Mixin.

Composition API Plugin Mixin
has window sizes properties only in use all components only in use
handle resize event only in use all times all times

Composition API

Use with component.

<script lang="ts">
import { defineComponent } from 'vue';
import { useWindowSize } from 'vue-window-size';

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

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

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/plugin';

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

<script lang="ts">
import { defineComponent } from 'vue';
import { vueWindowSizeMixin } from 'vue-window-size/mixin';

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

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

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/plugin';

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/plugin'; // or 'vue-window-size/mixin'

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/plugin'; // or 'vue-window-size/mixin'

vueWindowSizeAPI.init();

destroy()

Remove the resize event.

import { vueWindowSizeAPI } from'vue-window-size/plugin'; // or 'vue-window-size/mixin'

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

More Repositories

1

nuxt-on-lambda

Nuxt.jsをAWS Lambdaで動かす
TypeScript
82
star
2

vue-tips-samples

Vue.js の tips 本のサンプルコード
JavaScript
33
star
3

vuepress-plugin-component-catalog

Generating a component catalog of Vue.js
TypeScript
28
star
4

vuejs-pwa-and-ssr-on-lambda

Vue.jsでPWA作って、LambdaでSSRしてるリポジトリ
JavaScript
19
star
5

vuejs-spa-dev-env

Vue.js 2.0でSPA(Single Page Application)作るための開発環境的なやつ
Vue
17
star
6

vue-slot-checker

Provide the function to check slot like checking props.
JavaScript
14
star
7

vue-use-reducer

useReducer for Vue.js.
TypeScript
11
star
8

portfolio

My site
TypeScript
9
star
9

vue-composition-api-realworld

"The mother of all demo apps" — Vue Composition API
TypeScript
7
star
10

vue-uid

Unique ID for Vue.js component
JavaScript
6
star
11

nuxt-api-error-handling-sample

JavaScript
6
star
12

vuepress-component-catalog

JavaScript
4
star
13

nuxt-uid-module

vue-uid for Nuxt.js
JavaScript
3
star
14

mapped-promise-all

TypeScript
3
star
15

try-pwa-apis

JavaScript
3
star
16

webpack2-boilerplate-simple

view系のフレームワークを入れていないシンプルなボイラープレート
JavaScript
3
star
17

nuxt-resource-module

TypeScript
2
star
18

my-authoring

JavaScript
2
star
19

vue-accessibility-samples

Vue
2
star
20

reverse-resolution-webpack

逆引きwebpackのサンプルコードリポジトリ
JavaScript
2
star
21

angular-universal-on-lambda

Angular Universal を AWS Lambda で動かすコードを置いているリポジトリ。 まだプロトタイプ。
TypeScript
2
star
22

nuxtjs-boilerplate

Nuxt.js のボイラープレート
JavaScript
2
star
23

nuxt-on-docker

JavaScript
2
star
24

eldjs

Eld.js is a library to manipulate the DOM. That is similar to jQuery.
JavaScript
1
star
25

okoze-usage-example

TypeScript
1
star
26

riotjs-hands-on

Riot.js 勉強会 @福岡 #0のサンプル共有用
HTML
1
star
27

vuepress-theme-component-catalog

Vue
1
star
28

window-resize-subject

Handler for the window resize event of the observer pattern
TypeScript
1
star
29

try-rxjs

RxJSを試してるリポジトリのはずがVueを試してる感じのリポジトリになってきてる
JavaScript
1
star
30

try-vue-plugin

Vue.jsのプラグインを試してみてるリポジトリ
JavaScript
1
star