Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Define your breakpoints and build responsive design semantically and declaratively in a mobile-first way with Vue.
_Use with vue: ^2.x.x
_
Demo: here
npm install vue-mq
yarn add vue-mq
Define your custom breakpoints by passing breakpoints
option. This let you name the breakpoints as you want
Eg:
{ phone: 500, tablet: 1200, other: Infinity }
{ small: 500, large: 1200, whatever: Infinity }
{ xs: 300, s: 500, m: 800, l: 1200, xl: Infinity }
import Vue from 'vue'
import VueMq from 'vue-mq'
Vue.use(VueMq, {
breakpoints: { // default breakpoints - customize this
sm: 450,
md: 1250,
lg: Infinity,
}
defaultBreakpoint: 'sm' // customize this for SSR
})
$mq
propertyAfter installing the plugin every instance of Vue component is given access to a reactive $mq property. Its value will be a String
which is the current breakpoint.
Eg: (with default breakpoints)
'sm'
=> 0 > screenWidth < 450
'md'
=> 450 >= screenWidth < 1250
'lg'
=> screenWidth >= 1250
//Use it in a component
new Vue({
template: `
<h1>current: {{$mq}}</h1>
`,
})
$mq
property with the mq filterUsing the filter allow to build your responsive design in a declarative way. This can be very useful and elegant to pass down props to layout component. (eg: a grid system)
new Vue({
template: `
<grid-component :column="$mq | mq({ sm: 1, md: 2, lg: 3 })">
</grid-component>
`,
})
Remember that the filter design embrace mobile-first philosophy so writing $mq | mq({ sm: 1, lg: 3 })
will output 1
for md
breakpoint if omited. In short it will always fallback to the smallest breakpoint (aka mobile) if value isn't overriden by a largest breakpoint.
$mq
with a computed property$mq
property is fully reactive (like a data property) so feel free to use it in a computed.
new Vue({
computed: {
displayText() {
return this.$mq === 'sm' ? 'I am small' : 'I am large'
}
},
template: `
<h1>{{displayText}}</h1>
`,
})
In addition to $mq
property this plugin provide a wrapper component to facilitate conditional rendering with media queries.
Usage:
<mq-layout mq="lg">
<span> Display on lg </span>
</mq-layout>
<mq-layout mq="md+">
<span> Display on md and larger </span>
</mq-layout>
<mq-layout :mq="['sm', 'lg']">
<span> Display on sm and lg </span>
</mq-layout>
Props mq => required : String | Array
Important: note that you can append a +
modifier at the end of the string to specify that the conditional rendering happens for all greater breakpoints.
v1.0+ now supports SSR. You can customize the defaultBreakpoint
which let you set the breakpoint used by the server-side-rendering
This plugin relies on matchMedia API to detect screensize change. So for older browsers and IE, you should polyfill this out: Paul Irish: matchMedia polyfill
Please open an issue for support.
FAQs
Handle media queries easily & build responsive design with Vue
We found that vue-mq demonstrated a not healthy version release cadence and project activity because the last version was released 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.