Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

VueJS mixin plugin for creating element size queries in components

License

NotificationsYou must be signed in to change notification settings

e2o/vue-element-query

Repository files navigation

NPM latest versionNPM total downloadsLicense

VueJS mixin plugin for creating element size queries in components.

How does this library function under the hood?

This plugin uses theResizeObserver API to observe element sizing changes.As ResizeObserver isnot widely supported yet, we make use of thisponyfill to provide this API across non-supporting browsers.

How is this different than the other libraries out there?

This plugin gives each component it's own sizing queries and active breakpoint state.Otherlibrariesusethewindowsizeto determinebreakpoints. This is less powerful because each component should be able to define it's own behaviour without being aware of the components around it. And that's exactly what this plugin brings to you.


Table of contents


Installation

Using NPM

npm install vue-element-query --save

Using Yarn

yarn add vue-element-query

VueJS

Import the plugin and register it with the current Vue instance.

importVuefrom"vue";importVueElementQueryfrom"vue-element-query";Vue.use(VueElementQuery);

This will make the mixin available for use in every component you register under this Vue instance.

Usage

Basic

The breakpoint queries passed in theeq.breakpoints option will have their active state exposed on the$eq object.

Any combination of the following queries can be combined to create a breakpoint:

  • minWidth
  • maxWidth
  • minHeight
  • maxHeight

A breakpoint will only be active whenall of it's queries match.

<template><div><h1>I'm a title that is always rendered.</h1><h2v-if="$eq.medium">Only visible when my component has a medium size.</h2><h3v-if="$eq.medium || $eq.large">Visible on either medium or large size.</h3><h4:class="{'very-tiny': $eq.small}">I get a special class when my component is small.</h4></div></template><script>exportdefault{eq:{breakpoints:{small:{maxWidth:499},medium:{minWidth:500,maxWidth:1199},large:{minWidth:1200},potato:{minHeight:123,maxHeight:1234}...}}};</script>

Watching component size changes

A listener can be set on the breakpoint states, to watch them get (de-)activated.This way you can trigger additional actions if needed on separate component sizes.

exportdefault{eq:{breakpoints:{small:{maxWidth:499},medium:{minWidth:500,maxWidth:1199},large:{minWidth:1200}}},watch:{"$eq.small":function(newState){if(newState)console.log("small breakpoint activated");if(!newState)console.log("small breakpoint de-activated");},"$eq.medium":function(newState){if(newState)console.log("medium breakpoint activated");if(!newState)console.log("medium breakpoint de-activated");},"$eq.large":function(newState){if(newState)console.log("large breakpoint activated");if(!newState)console.log("large breakpoint de-activated");}}};

Waiting for ready state

Since v3.1.0 of this package, an additional property$eq.ready has been added. This can be useful in cases where you want to render a fallback-component, without explicitly defining a breakpoint for it:

<template><divid="app"><component1v-if="$eq.medium"/><component2v-else/></div></template>

If the medium sized breakpoint is supposed to be active, without an$eq.ready wrapper<component2 /> would render briefly until $eq is properly initialized, which may cause unwanted behaviour. In that case, you can wait for $eq to be properly set up:

<template><divid="app"><templatev-if="$eq.ready"><component1v-if="$eq.medium"/><component2v-else/></template></div></template><script>importComponent1from"@/components/Component1";importComponent2from"@/components/Component2";exportdefault{name:"App",eq:{breakpoints:{medium:{minWidth:500},},},components:{    Component1,    Component2,},};</script>

Example

A small example (mainly used for development and testing purposes) can be found inside theexample folder

Browser support

IEChromeFirefoxOperaSafari
10+65+59+52+10+

For a complete list of browsers, checkthe browserlist query.

Note: other browsers than the ones listed might (and probably will) work as well.Please refrain from opening issues for functionalities that are not working in these browsers.

Contributing

Feel free to provide feedback, open issues or create pull-requests to this repository.

License

vue-element-query isMIT licensed.

About

VueJS mixin plugin for creating element size queries in components

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp