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

vue-element-query

v3.1.0

Published

VueJS mixin plugin for creating element size queries in components

Downloads

43

Readme

vue-element-query

VueJS mixin plugin for creating element size queries in components.

How does this library function under the hood?

This plugin uses the ResizeObserver API to observe element sizing changes. As ResizeObserver is not widely supported yet, we make use of this ponyfill 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. Other libraries use the window size to determine breakpoints. 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.

import Vue from "vue";
import VueElementQuery from "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 the eq.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 when all of it's queries match.

<template>
  <div>
    <h1>I'm a title that is always rendered.</h1>
    <h2 v-if="$eq.medium">Only visible when my component has a medium size.</h2>
    <h3 v-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>
export default {
  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.

export default {
  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>
  <div id="app">
    <component1 v-if="$eq.medium" />
    <component2 v-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>
  <div id="app">
    <template v-if="$eq.ready">
      <component1 v-if="$eq.medium" />
      <component2 v-else />
    </template>
  </div>
</template>

<script>
import Component1 from "@/components/Component1";
import Component2 from "@/components/Component2";

export default {
  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 the example folder

Browser support

IE | Chrome | Firefox | Opera | Safari --- | --- | --- | --- | --- | 10+ | 65+ | 59+ | 52+ | 10+ |

For a complete list of browsers, check the 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 is MIT licensed.