fine-mq
A fine API to manage media queries in JS with ease. It has first-class integration with VueJS.
Installation
- Using NPM/Yarn
- Using unpkg.com
This main file exposes a install
function (Vue plugin), a Mq
class and toMqString
function.
These are all the builds available via unpkg:
https://unpkg.com/fine-mq/dist/fine-mq.min.js
(umd)https://unpkg.com/fine-mq/dist/fine-mq.cjs.js
(commonjs)https://unpkg.com/fine-mq/dist/fine-mq.es.js
(es module)https://unpkg.com/fine-mq/dist/mq.min.js
(umd)https://unpkg.com/fine-mq/dist/mq.cjs.js
(commonjs)https://unpkg.com/fine-mq/dist/mq.es.js
(es module)https://unpkg.com/fine-mq/dist/to-mq-string.min.js
(umd)https://unpkg.com/fine-mq/dist/to-mq-string.cjs.js
(commonjs)https://unpkg.com/fine-mq/dist/to-mq-string.es.js
(es module)
Usage
This package is a set of three libs.
- A vue plugin that exposes useful properties on vue instances, a component and a directive. This lib includes the two others. (
FineMq
) - A media query interface that lets you add/remove aliases to media queries, register/unregister handlers that listen to matching state of a media query or alias. (
Mq
) - A lib that exposes a single function that helps you transorm a media query written as a JavaScript object to its string value. (
toMqString
)
Vue plugin
Define your breakpoints and/or aliases to media queries and start using them with Vue.
NOTE 1: Absurd modifiers will not be created (ex: when the lower bound is 0, there is no need for the «!» modifier, or if the upper bound is Infinity, there is no need for both «!» and «+» modifiers).
NOTE 2: If you specify the unit for your size, the + 1
operation will not be performed.
Then in your templates, you can use the globally defined MqShow
component or v-mq-show-if
directive.
- The
MqShow
component facilitate conditional rendering with media queries. It renders its content only when one fo the given media queries matches. Cons: If the slot content has multiple root nodes, they will be wrapped by a DIV tag but you can customize the wrapper tag to render with thetag
prop.
The if
prop if required and can be a String/Array/Object. Modifiers are also supported (for both the component and the directive) by appending it at the end of the alias («+» for greater breakpoints, «!» for current AND greater breakpoints).
- The
v-mq-show-if
directive sets the display property of the bound element's style to'none'
only when none of the given media queries matches. Cons: The rendered element will still appear in the DOM tree even if not displayed. Usage is the same as for the component.
Mq
API
The Mq
class exposes a fine API that lets you handle media query changes on your page on the JavaScript side.
Usage example
API description
const mq = new Mq(aliases)
Initialise a new Mq
. Aliases can be registered afterwards.
mq.alias(alias[, query]) / mq.unalias(alias)
Register an alias
for a query
, or register multiple aliases at once via an object.
Then you can unregister previously registered aliases with mq.unalias(alias)
.
mq.on(query, callback, context)
Register a callback
which will be executed with the given context
everytime the match state (true/false) for a query or alias. If context
is not specified, it will default to the mq
instance.
mq.off(query, callback, context)
Remove all callbacks for all queries:
Remove all callbacks for a query
or alias:
Remove a specific callback
for a query
or alias:
Remove a specific callback
with a specific context
for a query
or alias:
Tip: All methods (alias
, unalias
, on
and off
) return this
so that you can chain them.
NOTE: The Mq class is written independantly from toMqString
meaning that the object-style notation for queries is not supported here.
toMqString
This function can generate a valid media query string from a javascript object.
Usage example
Media type
Media type with negation
Media features can be specified in camel case
px is added to numeric dimension values
Multiple media queries can be passed as an array
NOTE: When passing an array to the Vue component/directive and you want it to be considered as one media query like in the last example, remember to wrap it in another array like this: [[{screen: true, minWidth: 100}, {handheld: true, orientation: 'landscape'}]]
(that will register a single listener for 'screen and (min-width: 100px), handheld and (orientation: landscape)' instead of multiple listeners, one for 'screen and (min-width: 100px)' and another for 'handheld and (orientation: landscape)' )