- Notifications
You must be signed in to change notification settings - Fork5
VueJS mixin plugin for creating element size queries in components
License
e2o/vue-element-query
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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.
npm install vue-element-query --save
yarn add vue-element-query
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.
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>
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");}}};
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>
A small example (mainly used for development and testing purposes) can be found inside theexample folder
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.
Feel free to provide feedback, open issues or create pull-requests to this repository.
vue-element-query isMIT licensed.
About
VueJS mixin plugin for creating element size queries in components