Vue MQ: Responsive Design Plugin

1.0.1 · active · verified Sun Apr 19

Vue MQ is a Vue.js plugin designed to simplify responsive web design by providing a declarative and semantic approach to handling media queries. It integrates directly into Vue instances, exposing a reactive `$mq` property that reflects the current breakpoint. Additionally, it offers a `MqLayout` component for conditional rendering of content based on specified breakpoints, including support for ranges (e.g., `md+`) and multiple specific breakpoints. The current stable version is 1.0.1, with significant updates like Server-Side Rendering (SSR) support introduced in v1.0.0. Releases appear to be infrequent, focusing on stability and key features. A core differentiator is its mobile-first design philosophy, particularly evident in its filter utility, allowing developers to define breakpoints centrally and manage responsive behavior directly within Vue templates and component logic, rather than relying solely on CSS media queries. This abstraction aims to streamline the development of responsive user interfaces.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates Vue MQ installation, reactive `$mq` property usage, the `MqLayout` component for conditional rendering, and the `mq` filter for dynamic values based on breakpoints.

import Vue from 'vue';
import VueMq from 'vue-mq';

// Create a root element for the Vue application in a browser environment
const appElement = document.createElement('div');
appElement.id = 'app';
document.body.appendChild(appElement);

// Install the Vue MQ plugin with custom breakpoints
Vue.use(VueMq, {
  breakpoints: { // Define custom, named breakpoints
    xs: 0,
    sm: 450,
    md: 800,
    lg: 1250,
    xl: Infinity
  },
  defaultBreakpoint: 'sm' // Customize for SSR or initial state
});

// Create a new Vue instance to demonstrate reactive behavior
new Vue({
  el: '#app',
  data: {
    appMessage: 'Responsive Demo with Vue MQ'
  },
  computed: {
    // Reactive computed property based on the current breakpoint
    displayText() {
      return this.$mq === 'sm' ? 'Viewing on a small screen!' : `Current breakpoint: ${this.$mq}`;
    }
  },
  template: `
    <div>
      <h1>{{ appMessage }}</h1>
      <p>{{ displayText }}</p>

      <!-- Conditional rendering using MqLayout component -->
      <mq-layout mq="sm">
        <p>This content is specifically for small screens (sm).</p>
      </mq-layout>

      <mq-layout mq="md+">
        <p>This content displays on medium screens and larger (md, lg, xl).</p>
      </mq-layout>

      <mq-layout :mq="['lg', 'xl']">
        <p>This content shows on large or extra-large screens (lg or xl).</p>
      </mq-layout>

      <!-- Using the $mq property with the mq filter for dynamic props -->
      <div style="border: 1px solid #ccc; padding: 10px; margin-top: 20px;">
        <p>Dynamic grid columns via filter: <strong>{{ $mq | mq({ xs: 1, sm: 2, md: 3, lg: 4, xl: 5 }) }}</strong></p>
        <p>Resize your browser window to see the breakpoints and content change!</p>
      </div>
    </div>
  `,
});

view raw JSON →