responsive-watch
v0.1.0
Published
Watch some media queries and react when they change
Downloads
5
Maintainers
Readme
responsive-watch
Watch some media queries and react when they change.
Install
npm install responsive-watch --save
Usage
import responsiveWatch from 'responsive-watch';
const watchers = responsiveWatch({
sizes: [
{name: 'small', breakpoint: 40, unit: 'em'},
{name: 'medium', breakpoint: 70, unit: 'em'},
{name: 'large'}
],
orientations: true,
queries: {
smallTelevision: 'tv and (max-device-height: 30cm)'
}
}, (status)=> {
// Called every time one of the media query results changes
// (also called once at init)
// Example on a television with 60em width and 25cm height
console.log(status);
// {
// sizes: {
// small: false,
// medium: true,
// large: false
// },
// orientations: {
// landscape: true,
// portrait: false
// },
// queries: {
// smallTelevision: true
// },
// lt: { small: false, medium: false, large: true },
// lte: { small: false, medium: true, large: true },
// gt: { small: true, medium: false, large: false },
// gte: { small: true, medium: true, large: false }
// }
});
// You can access the current status at any time
watchers.status();
FAQ
Does it support IE6? Not out of the box. It only needs the matchMedia
function. You can find great polyfills online.
Is it possible to have multiple instances of responsive watch? Yes. Just call the responsiveWatch
as many times as you need.
Could you describe a use case for this lib? I'm using it inside my React applications. When I need responsive inline styles, I plug the lib with my Flux dispatcher so each time the callback is called, it will update a store and impact all my components to re-render with the new style.
Examples
- Pure JavaScipe: demo - source code
- React: demo - source code
API
responsiveWatch(options, callback)
Return an object with one method status
which return the current status.
options
sizes
(default[]
): an array of{name, breakpoint, unit}
. The breakpoint is the max width before switching to the next size. This is why the last size doesn't need any breakpoint. You can mix different units but shouldn't do it.orientations
(defaulttrue
): a boolean to enable or disable orientation watchers. Results are instatus.orientations.landscape
andstatus.orientations.portrait
.medias
(defaulttrue
): a boolean to enable or disable media watchers (screen, print, tv, ...). Results are instatus.medias
.queries
(default[]
): an array of{name, query}
. You can create custom media queries. The result will be instatus.querys[name]
.check
(defaulttrue
): enable or disable all checks to see if options seems valid.
Warning There is no check to test if all size breakpoints are each bigger than the previous one if you mix different units inside your sizes. That's because it would be just impossible since some units are not absolute.
callback
Invoked once when initializing the responsive watch and then every time a media query match changes. Receive one argument which is the current status.
status
An object with map your options to a boolean depending if the media query currently matches or not. The structure is the following (it's a bit complex to explain with words but I'm pretty sure the example right after will make everything way easier):
sizes
: for eachoptions.size
, will create a key with the name of the size. Among all sizes, only one can betrue
while all other will befalse
.orientations
: got two keys,landscape
andportrait
, onetrue
and the otherfalse
depending on the screen.medias
: one key for each type of media. Allfalse
except for one.queries
: for eachoptions.queries
, will create a key with the name of the query and the value will be if it currently matches or not.lt
,lte
,gt
,gte
: each of those keys will contains an object with all the size names. Each one indicates if we are currently lower than (lt
), lower or equal than (lte
), greater than (gt
) or greater or equal than (gte
) compared to the current size. For example, ifstatus.lte.medium
is true, it means the width of the screen is currently smaller or equal to the medium size.
{
sizes: {
small: false,
medium: true,
large: false
},
orientations: {
landscape: true,
portrait: false
},
medias: {
braille: false,
embossed: false,
handheld: false,
print: false,
projection: false,
screen: true,
speech: false,
tty: false,
tv: false
},
queries: {
smallTelevision: true,
print: false,
highDpi: true
},
lt: { small: false, medium: false, large: true },
lte: { small: false, medium: true, large: true },
gt: { small: true, medium: false, large: false },
gte: { small: true, medium: true, large: false }
}
License
This software is licensed under the Apache 2 license, quoted below.
Copyright 2015 Paul Dijou (http://pauldijou.fr).
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.