
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
nuxt-viewport
Advanced tools
Define custom viewports for your Nuxt️ project
Note
This version is Nuxt 3 & Nuxt Bridge only. For Nuxt 2 see 1.0.1
nuxt-viewport
dependency to your projectnpx nuxi@latest module add nuxt-viewport
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 */
},
}
<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>
<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>
<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>
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
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
viewport
The breakpoint key to be used, if the device was not detected.
feature
'minWidth' | 'maxWidth'
'minWidth'
CSS media feature.
{
// ...
viewport: {
breakpoints: {
desktop: 1024,
desktopMedium: 1280,
desktopWide: 1600,
mobile: 320,
mobileMedium: 375,
mobileWide: 425,
tablet: 768,
},
cookie: {
expires: 365, // 365 days
name: 'viewport',
path: '/',
sameSite: 'Strict',
secure: true,
},
defaultBreakpoints: {
desktop: 'desktop',
mobile: 'mobile',
tablet: 'tablet',
},
fallbackBreakpoint: 'desktop',
feature: 'minWidth',
},
// ...
}
{
// ...
viewport: {
breakpoints: {
xs: 320,
sm: 640,
md: 768,
lg: 1024,
xl: 1280,
'2xl': 1536,
},
defaultBreakpoints: {
desktop: 'lg',
mobile: 'xs',
tablet: 'md',
},
fallbackBreakpoint: 'lg'
},
// ...
}
You can override the global configuration for specific pages using definePageMeta
.
<script setup>
definePageMeta({
viewport: {
breakpoints: {
desktop: 1024,
mobile: 320,
tablet: 768
},
cookie: {
name: 'viewport-per-page'
}
// Other fields will be inherited from the global configuration
}
})
</script>
viewport.breakpoint
Current breakpoint.
viewport.breakpointValue
// Example using defaults.
viewport.breakpointValue('desktop') // Result: 1024.
viewport.breakpointValue('tablet') // Result: 768.
viewport.breakpointValue('mobile') // Result: 320.
viewport.isGreaterThan
// Example: viewport.breakpoint is "mobile".
viewport.isGreaterThan('mobile') // Result: false.
viewport.isGreaterThan('desktop') // Result: false.
viewport.isGreaterOrEquals
// Example: viewport.breakpoint is "mobile".
viewport.isGreaterOrEquals('mobile') // Result: true.
viewport.isGreaterOrEquals('desktop') // Result: false.
viewport.isLessThan
// Example: viewport.breakpoint is "desktop".
viewport.isLessThan('desktopWide') // Result: true.
viewport.isLessThan('mobile') // Result: false.
viewport.isLessOrEquals
// Example: viewport.breakpoint is "tablet".
viewport.isLessOrEquals('tablet') // Result: true.
viewport.isLessOrEquals('mobile') // Result: false.
viewport.match
// Example: viewport.breakpoint is "tablet".
viewport.match('tablet') // Result: true.
viewport.match('desktop') // Result: false.
viewport.matches
// Example: viewport.breakpoint is "mobileWide".
viewport.matches('tablet', 'mobileWide') // Result: true.
viewport.matches('mobile', 'tablet') // Result: false.
viewport.queries
Object with generated media queries.
You can contribute to this module online with CodeSandBox:
Or locally:
yarn install
or npm install
yarn dev
or npm run dev
Copyright (c) mvrlin mvrlin@pm.me
FAQs
Define custom viewports for your Nuxt project
The npm package nuxt-viewport receives a total of 12,926 weekly downloads. As such, nuxt-viewport popularity was classified as popular.
We found that nuxt-viewport demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.