All Projects → mvrlin → nuxt-viewport

mvrlin / nuxt-viewport

Licence: other
🌈 Define custom viewports for your Nuxt project

Programming Languages

typescript
32286 projects
Vue
7211 projects

Projects that are alternatives of or similar to nuxt-viewport

nuxt-winston-log
Nuxt module for logging SSR errors using winston
Stars: ✭ 41 (-25.45%)
Mutual labels:  nuxt, nuxt-module
nuxt-gsap-module
GSAP module for Nuxt.js
Stars: ✭ 183 (+232.73%)
Mutual labels:  nuxt, nuxt-module
yamlful
YAML-based HTTP client code generation
Stars: ✭ 77 (+40%)
Mutual labels:  nuxt, nuxt-module
nuxt-stencil
Easy Stencil.js component library integration with Nuxt.js.
Stars: ✭ 16 (-70.91%)
Mutual labels:  nuxt, nuxt-module
k-domains
A simple module to manage multiple subdomains with just one project
Stars: ✭ 41 (-25.45%)
Mutual labels:  nuxt, nuxt-module
nuxt-prune-html
🔌⚡ Nuxt module to prune html before sending it to the browser (it removes elements matching CSS selector(s)), useful for boosting performance showing a different HTML for bots/audits by removing all the scripts with dynamic rendering
Stars: ✭ 69 (+25.45%)
Mutual labels:  nuxt, nuxt-module
separate-env-module
Tear your variables apart!
Stars: ✭ 53 (-3.64%)
Mutual labels:  nuxt, nuxt-module
supabase
An easy way to integrate Supabase with NuxtJS
Stars: ✭ 39 (-29.09%)
Mutual labels:  nuxt, nuxt-module
global-components
Module to register global components for Nuxt.js
Stars: ✭ 57 (+3.64%)
Mutual labels:  nuxt, nuxt-module
nuxt-feature-toggle
The nuxt feature toggle module
Stars: ✭ 78 (+41.82%)
Mutual labels:  nuxt, nuxt-module
nuxt-jsonld
A Nuxt.js module to manage JSON-LD in Vue component.
Stars: ✭ 198 (+260%)
Mutual labels:  nuxt, nuxt-module
Auth Module
auth.nuxtjs.org
Stars: ✭ 1,624 (+2852.73%)
Mutual labels:  nuxt, nuxt-module
nuxt-sanity
Easily integrate Sanity in your Nuxt.js project.
Stars: ✭ 14 (-74.55%)
Mutual labels:  nuxt, nuxt-module
nuxt-mail
Adds email sending capability to a Nuxt.js app. Adds a server route, an injected variable, and uses nodemailer to send emails.
Stars: ✭ 62 (+12.73%)
Mutual labels:  nuxt, nuxt-module
nuxt-stripejs
💳 NuxtJS module for Stripe.js which loads only when required and w/ retry mechanism
Stars: ✭ 17 (-69.09%)
Mutual labels:  nuxt, nuxt-module
date-fns-module
Modern JavaScript date utility library - date-fns for Nuxt.js
Stars: ✭ 68 (+23.64%)
Mutual labels:  nuxt, nuxt-module
nuxt-modules
AX2's Nuxt modules
Stars: ✭ 30 (-45.45%)
Mutual labels:  nuxt, nuxt-module
nuxt-humans-txt
🧑🏻👩🏻 "We are people, not machines" - An initiative to know the creators of a website. Contains the information about humans to the web building - A Nuxt Module to statically integrate and generate a humans.txt author file - Based on the HumansTxt Project.
Stars: ✭ 27 (-50.91%)
Mutual labels:  nuxt, nuxt-module
nuxt-babel
Use normal .babelrc file with your Nuxt app
Stars: ✭ 32 (-41.82%)
Mutual labels:  nuxt, nuxt-module
nuxt-storyblok-queries
Nuxt.js module to simplify queries to the Storyblok API
Stars: ✭ 17 (-69.09%)
Mutual labels:  nuxt, nuxt-module

nuxt-viewport

npm version npm downloads License

Define custom viewports for your Nuxt️ project

Features

  • ⚡️  Fast & Light with MatchMedia API ⚡️
  • 🕶  Auto detects the device viewport from Cookie & User-Agent
  • 👌  Zero configuration to start
  • 👴️  Supports IE9+

Note
This version is Nuxt 3 & Nuxt Bridge only. For Nuxt 2 see 1.0.1

Quick Setup

  1. Add nuxt-viewport dependency to your project
# Using npm
npm install --save-dev nuxt-viewport
# Using yarn
yarn add --dev nuxt-viewport
  1. Add nuxt-viewport to the modules section of nuxt.config.js
{
  modules: [
    [
      'nuxt-viewport', {
        /* Viewport options */
      }
    ],
  ]
}

using top level options

{
  modules: [
    'nuxt-viewport',
  ],

  viewport: {
    /* Viewport options */
  },
}

Usage

<script setup>
import { useNuxtApp } from '#app'
const { $viewport } = useNuxtApp()

watch($viewport.breakpoint, (newBreakpoint, oldBreakpoint) => {
  console.log('Breakpoint updated:', oldBreakpoint, '->', newBreakpoint)
})
</script>

<template>
  <div>
    <div v-if="$viewport.isLessThan('tablet')">Should render only on mobile</div>
    <div v-else>Current breakpoint: {{ $viewport.breakpoint }}</div>
  </div>
</template>

Usage with composable

<script setup>
const viewport = useViewport()

watch(viewport.breakpoint, (newBreakpoint, oldBreakpoint) => {
  console.log('Breakpoint updated:', oldBreakpoint, '->', newBreakpoint)
})
</script>

<template>
  <div>
    <div v-if="viewport.isLessThan('tablet')">Should render only on mobile</div>
    <div v-else>Current breakpoint: {{ viewport.breakpoint }}</div>
  </div>
</template>

Usage with "@nuxt/bridge"

<script setup>
const viewport = useViewport()

watch(viewport.breakpoint, (newBreakpoint, oldBreakpoint) => {
  console.log('Breakpoint updated:', oldBreakpoint, '->', newBreakpoint)
})
</script>

<template>
  <div>
    <div v-if="viewport.isLessThan('tablet')">Should render only on mobile</div>
    <div v-else>Current breakpoint: {{ $viewport.breakpoint }}</div>
  </div>
</template>

Configuration

breakpoints

  • Type: Object

An object where the key is the name of the viewport, and the value is the viewport size.

cookieName

  • Type: String
  • Default: viewport

The key for the document cookie.

defaultBreakpoints

  • Type: Object
  • Detectable devices: console, desktop, embedded, mobile, smarttv, tablet, wearable

An object where the key is the name of the detected device, and the value is the breakpoint key.

fallbackBreakpoint

  • Type: String
  • Default: viewport

The breakpoint key to be used, if the device was not detected.

Default configuration

{
  // ...
  viewport: {
    breakpoints: {
      desktop: 1024,
      desktopMedium: 1280,
      desktopWide: 1600,

      mobile: 320,
      mobileMedium: 375,
      mobileWide: 425,

      tablet: 768,
    },

    cookieName: 'viewport',

    defaultBreakpoints: {
      desktop: 'desktop',
      mobile: 'mobile',
      tablet: 'tablet',
    },

    fallbackBreakpoint: 'desktop',
  },
  // ...
}

Example configuration for Tailwind CSS

{
  // ...
  viewport: {
    breakpoints: {
      xs: 320,
      sm: 640,
      md: 768,
      lg: 1024,
      xl: 1280,
      '2xl': 1536,
    },

    defaultBreakpoints: {
      desktop: 'lg',
      mobile: 'xs',
      tablet: 'md',
    },

    fallbackBreakpoint: 'lg'
  },
  // ...
}

API

viewport.breakpoint

  • Type: String

Current breakpoint.

viewport.isGreaterThan

  • Type: Boolean
// Example: viewport.breakpoint is "mobile".

viewport.isGreaterThan('mobile') // Result: false.
viewport.isGreaterThan('desktop') // Result: false.

viewport.isGreaterOrEquals

  • Type: Boolean
// Example: viewport.breakpoint is "mobile".

viewport.isGreaterOrEquals('mobile') // Result: true.
viewport.isGreaterOrEquals('desktop') // Result: false.

viewport.isLessThan

  • Type: Boolean
// Example: viewport.breakpoint is "desktop".

viewport.isLessThan('desktopWide') // Result: true.
viewport.isLessThan('mobile') // Result: false.

viewport.match

  • Type: Boolean
// Example: viewport.breakpoint is "tablet".

viewport.match('tablet') // Result: true.
viewport.match('desktop') // Result: false.

viewport.matches

  • Type: Boolean
// Example: viewport.breakpoint is "mobileWide".

viewport.matches('tablet', 'mobileWide') // Result: true.
viewport.matches('mobile', 'tablet') // Result: false.

viewport.queries

  • Type: Object

Object with generated media queries.

Contributing

You can contribute to this module online with CodeSandBox:

Edit nuxt-viewport

Or locally:

  1. Clone this repository
  2. Install dependencies using yarn install or npm install
  3. Start development server using yarn dev or npm run dev

License

MIT License

Copyright (c) mvrlin [email protected]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].