Vue Component Media Queries

1.0.0 · active · verified Tue Apr 21

Vue Component Media Queries is a lightweight library (less than 1kb gzipped) that provides Vue 2 components for integrating the `window.matchMedia` API directly into your templates and script logic. It is currently at a stable v1.0.0 release. The library differentiates itself by being highly optimized for server-side rendering (SSR), specifically tested with Nuxt.js to prevent hydration errors and support predictive rendering, making it suitable for complex responsive layouts where CSS alone might lead to performance issues or require logic. While there isn't an explicit release cadence, the library has moved from several alpha versions to a stable 1.0.0, suggesting a focus on stability for its current major version. It offers flexibility by allowing media queries to be defined globally via a `MediaQueryProvider` or used for single queries with `MatchMedia`, and can also inject query states into components.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up global media queries using `MediaQueryProvider` and reactively consume their states within a child `MatchMedia` component. It shows a basic responsive layout changing based on mobile, tablet, or desktop viewport sizes.

<template>
  <div id="app">
    <h1>Responsive Layout Example</h1>
    <p>Resize your browser window to see the layout adapt.</p>

    <MediaQueryProvider :queries="{
      mobile: '(max-width: 680px)',
      tablet: '(min-width: 681px) and (max-width: 1024px)',
      desktop: '(min-width: 1025px)'
    }">
      <MatchMedia v-slot="{ mobile, tablet, desktop }">
        <div :class="{
          'layout-box': true,
          'mobile-layout': mobile,
          'tablet-layout': tablet,
          'desktop-layout': desktop
        }">
          <h2 v-if="mobile">Mobile Layout</h2>
          <h2 v-else-if="tablet">Tablet Layout</h2>
          <h2 v-else-if="desktop">Desktop Layout</h2>
          <h2 v-else>Default Layout</h2>
          <p>This content changes based on media queries.</p>
          <p>Current state: {{ mobile ? 'Mobile' : (tablet ? 'Tablet' : (desktop ? 'Desktop' : 'Unknown')) }}</p>
        </div>
      </MatchMedia>
    </MediaQueryProvider>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import { MediaQueryProvider, MatchMedia } from 'vue-component-media-queries';

export default Vue.extend({
  name: 'App',
  components: {
    MediaQueryProvider,
    MatchMedia,
  },
});
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.layout-box {
  border: 1px solid #42b983;
  padding: 20px;
  margin: 20px auto;
  max-width: 800px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: all 0.3s ease;
}
.mobile-layout { background-color: #e0f7fa; color: #00796b; }
.tablet-layout { background-color: #fffde7; color: #fbc02d; }
.desktop-layout { background-color: #e8f5e9; color: #388e3c; }
</style>

view raw JSON →