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-next-breakpoints

v1.0.1

Published

Vue 3 Breakpoints using window.matchMedia and the Composition API

Downloads

908

Readme

Vue 3 Breakpoints

JavaScript-based Media Queries for Vue 3. Internally uses window.matchMedia and the Composition API.

Installation

NPM: npm i vue-next-breakpoints

Yarn: yarn add vue-next-breakpoints

Usage

<template>
    <desktop-nav v-if="breakpoints.desktop.matches" />
    <mobile-nav v-if="breakpoints.mobile.matches" />
</template>

<script>
import useBreakpoints from "vue-next-breakpoints";

export default {
    name: "YourComponent",
    setup() {
        // Define your breakpoints with any name
        const breakpoints = useBreakpoints({
            mobile: 600, // max-width: 600px
            desktop: [601] // min-width: 601px
        });

        return {
            // If you want to use different key, feel free do do so, e.g.:
            // mediaqueries: breakpoints
            // and then use mediaqueries.desktop.matches etc.
            breakpoints
        };
    },
    created() {
        // Event listeners are optional but may come in handy.
        // You can register them inside setup, created or mounted methods.
        this.breakpoints.mobile.on("enter", (mq) => {
            console.log("Entered mobile breakpoint");
            console.log("Media Query", mq);
        });

        this.breakpoints.mobile.on("leave", (mq) => {
            console.log("Left mobile breakpoint");
            console.log("Media Query", mq);
        });
    }
};
</script>

Options and methods

1. Defining breakpoints

Simply pass an object with key-value pairs to useBreakpoints function, where the key is any name you want and the value is one of the following:

  • numeric value, e.g. 200 which converts to @media screen and (max-width: 200px)
  • string value with any units you prefer, e.g. 80% which converts to @media screen and (max-width: 80%)
  • an array with one of the above values, e.g. [600] which converts to @media screen and (min-width: 600px)
  • an array with two of the above values, e.g. [601, "1200px"] which converts to @media screen and (min-width: 601px) and (max-width: 1200px)
  • any other media query, e.g. @media screen and (-webkit-min-device-pixel-ratio: 1) which will not be converted in any way (@media prefix is not required, e.g. @media print is the same as just print).

2. Determining media query state

Each of the specified media queries have .matches property which is either true or false. You can use them in a template or in any other method, computed property etc:

<template>
    <desktop-nav v-if="breakpoints.desktop.matches" />
    <mobile-nav v-if="breakpoints.mobile.matches" />
</template>

3. Assigning event listeners

In most cases using .matches property will be enough, but if you want to listen for the specific breakpoint, there are two types of listeners: "enter" and "leave" and these can be assigned to any media query previously configured.

const enterCallback = (mq) => {
    console.log("Entered mobile breakpoint");
    console.log("Media Query", mq);
};

const leaveCallback = (mq) => {
    console.log("Left mobile breakpoint");
    console.log("Media Query", mq);
};

created() {
    this.breakpoints.mobile.on("enter", enterCallback);
    this.breakpoints.mobile.on("leave", leaveCallback);
}

Event listener can be easily removed as well:

this.breakpoints.mobile.off("enter", enterCallback);

You don't have to remove those event listeners manually before the component is unmounted, it's done automatically behind the scenes.

If you don't specify the callback to remove, all listeners of the given type will be removed:

this.breakpoints.mobile.off("enter"); // all previously assigned listeners are gone

Issues

If you find any problems using this library or you want to propose new features to it, feel free to open an issue on Github.