npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mq-genie

v1.0.0

Published

Adjusts CSS media queries in browsers that include the scrollbar width in the viewport width so they fire at the intended size

Downloads

74

Readme

mqGenie

Your media queries are wrong!

mqGenie adjusts CSS media queries in browsers that include the scrollbar's width in the viewport width so they fire at the intended size

WebKit browsers (and Chrome/Blink prior to 29.0.1547.57) are the only browsers that don't include the scrollbar in the viewport. While this is technically incorrect (http://www.w3.org/TR/css3-mediaqueries/#width), it makes sense since scrollbar widths vary across platforms and in the case of "mobile" don't exist.

However, it means that the media queries every Windows developer - and Mac developers who enable scrollbars - write actually fire at a different size when viewed on a mobile device or another OS.

View this demo to see mqGenie in action

How does mqGenie work?

  • If the browser is "modern" and not one of the above WebKit-based ones, on 'domready' (DOMContentLoaded), mqGenie forces a vertical scrollbar on the <html> element.
  • It then compares window.innerWidth to document.documentElement.clientWidth. If they're different, this value equals the width of the browser's scrollbar.
  • Then it loops through your stylesheets' media queries and increases all of the min-width and max-width ones by the width of the scrollbar so that they fire at the correct size. It also converts em-based ones, using the HTML's computed font-size.

It returns a JavaScript object called mqGenie, which contains the following properties:

  • adjusted (boolean - whether your media queries were adusted)
  • fontSize (the computed HTML font-size)
  • width (the width adjusted by)
  • adjustMediaQuery(media-query) (function) allows you to re-calculate media queries that are written in JavaScript. Simply pass adjustMediaQuery the media query string and it will return one that's adjusted appropriately.

Do we need it?

Consider mqGenie as progressive enhancement.

Ideally, your responsive projects will be built in a flexible way, such that a 15-20px difference in media queries shouldn't matter too much.

However, there are definitely times where things can go awry. Fixed-width ads and other modules may rely on more precise measurements, and - while I don't condone targeting device widths specifically - writing a 768px media query and not having it triggered on an a portrait iPad, for example, is a little disconcerting.


####Usage:

  1. Include the tiny (~1.1KB minified and gzipped) mq.genie.js in the <head> of your document

  2. If you develop in Safari, Chrome/Blink prior to 29.0.1547.57 or Firefox's scrollbar-less RWD View, write your media queries as you always have. mqGenie will adjust them for every other browser as required.

  3. If you use another browser (or have scrollbars enabled on Mac), subtract mqGenie.width from the browser's reported viewport width. You can use my Viewport Genie bookmarklet to tell you the "actual" viewport size.

  4. If you have media queries triggering events in JavaScript, such as with enquire.js, use adjustMediaQuery(mq-string) as opposed to mq-string


Copyright (c) 2014 Matt Stow
Licensed under the MIT license (see LICENSE for details)
Minified version created with UglifyJS (http://jscompress.com/)