nuxt-viewport
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
- Add
nuxt-viewport
dependency to your project
npx nuxi@latest module add nuxt-viewport
- Add
nuxt-viewport
to the modules
section of nuxt.config.js
{
modules: [
[
'nuxt-viewport', {
}
],
]
}
using top level options
{
modules: [
'nuxt-viewport',
],
viewport: {
},
}
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
An object where the key is the name of the viewport, and the value is the viewport size.
cookie
An object with options for cookie.
See more https://www.npmjs.com/package/cookiejs#cookie-attributes
defaultBreakpoints
- Type: Object
- Detectable devices:
bot
, desktop
, mobile
, tablet
, tv
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.
feature
- Type:
'minWidth' | 'maxWidth'
- Default:
'minWidth'
CSS media feature.
Default configuration
{
viewport: {
breakpoints: {
desktop: 1024,
desktopMedium: 1280,
desktopWide: 1600,
mobile: 320,
mobileMedium: 375,
mobileWide: 425,
tablet: 768,
},
cookie: {
expires: 365,
name: 'viewport',
path: '/',
sameSite: 'Strict',
secure: true,
},
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
Current breakpoint.
viewport.breakpointValue
viewport.breakpointValue('desktop')
viewport.breakpointValue('tablet')
viewport.breakpointValue('mobile')
viewport.isGreaterThan
viewport.isGreaterThan('mobile')
viewport.isGreaterThan('desktop')
viewport.isGreaterOrEquals
viewport.isGreaterOrEquals('mobile')
viewport.isGreaterOrEquals('desktop')
viewport.isLessThan
viewport.isLessThan('desktopWide')
viewport.isLessThan('mobile')
viewport.match
viewport.match('tablet')
viewport.match('desktop')
viewport.matches
viewport.matches('tablet', 'mobileWide')
viewport.matches('mobile', 'tablet')
viewport.queries
Object with generated media queries.
Contributing
You can contribute to this module online with CodeSandBox:
Or locally:
- Clone this repository
- Install dependencies using
yarn install
or npm install
- Start development server using
yarn dev
or npm run dev
License
MIT License
Copyright (c) mvrlin mvrlin@pm.me