@cwist/vue-match-media

React to media query changes in your Vue 3 application (useful for adaptive design).
14,779 5
Install
npm install @cwist/vue-match-media
Latest Version:3.0.3
Vue:3
License:MIT
Last Updated:Apr 16, 2021
Links: GitHub  ·  npm
Maintainer: cwist

Vue matchMedia

This fork is a port to Vue 3

React to media query changes in your Vue 3 application (useful for adaptive design).

Installation

npm install @cwist/vue-match-media

Usage

import { createApp } from 'vue';
import App from '@/App.vue';
import { createVueMatchMediaPlugin } from '@cwist/vue-match-media';

// Define breakpoints you want to react to
const breakpoints = {
  s: '48em',
  m: '50em',
  l: '62em',
  xl: '75em',
};

const VueMatchMediaPlugin = createVueMatchMediaPlugin({ breakpoints });

// Load plugin
createApp(App)
  .use(VueMatchMediaPlugin)
  .mount('#app');
<template>
  <div>
    <div v-if="$matchMedia.s">
      This content is visible only on devices above 48em.
    </div>

    <div v-if="$matchMedia.xl">
      This content is visible only on devices above 75em.
    </div>
    <div v-else>This content is visible only on devices under 75em.</div>
  </div>
</template>

<script>
  export default {
    name: 'DemoComponent',
  };
</script>

Using composition API

<template>
  <div>
    <div v-if="matchMedia.s">
      This content is visible only on devices above 48em.
    </div>

    <div v-if="matchMedia.xl">
      This content is visible only on devices above 75em.
    </div>
    <div v-else>This content is visible only on devices under 75em.</div>
  </div>
</template>

<script>
  import { useMatchMedia } from '@cwist/vue-match-media';

  export default {
    name: 'DemoComponent',
    setup() {
      const matchMedia = useMatchMedia();

      return {
        matchMedia,
      };
    },
  };
</script>

Breakpoint syntax

const breakpoints = {
  // @media (min-width: 1920px)
  fullHD: 1920,
  // @media (min-width: 48em)
  medium: '48em',
  // Object notation
  // @media (max-width: 768px)
  mobile: { maxWidth: 768 },
  // @media (orientation: landscape)
  landscape: { orientation: 'landscape' },
  // Multiple features
  // @media (min-width: 62em) and (min-resolution: 150dpi)
  highDPIDesktop: {
    minWidth: '62em',
    minResolution: '150dpi',
  },
  // @media (min-width: 768px) and (max-width: 992px)
  tablet: [768, 992],
  // SSR support
  // In order to use SSR, you must provide a default value
  // @media (min-width: 62em)
  // Defaults to true during SSR, will be updated on client side
  large: {
    breakpoint: '62em',
    defaultValue: true,
  },
  // Defaults to false during SSR, will be updated on client side
  portrait: {
    breakpoint: { orientation: 'portrait' },
    defaultValue: false,
  },
};

Contributing

Please see contributing guide.

License

MIT

TODO List

  • SSR support (set a default breakpoint)
  • Improve composition API use
  • Add demo

Related Packages

@kvass/media

Component for media upload

2,017 0
@dracul/media-frontend

Dracul media ofrece un servicio de almacenamiento de archivos. Se puede utilizar...

9,904
vue-media-query-mixin

A vue mixin to get current media query, xs, sm, md and lg.

988 27
@dongido/vue-viaudio

A package that helps you dynamically/reactively render video and audio elements...

14,976 27
@webx-ui/module-media

The file manager of a WebX UI admin panel: folders, files, uploads and an image...

8,471 0

Version History

Version Released License
3.0.3 MIT
3.0.2 MIT
3.0.1 MIT
3.0.0 MIT