@wingnpm/eqio v0.1.5
eqio
A simple, tiny alternative to element/container queries
eqio allows you to attain the holy grail of responsive web development/design systems: components that can adapt their styling based on their width, not the browser‘s.
It uses IntersectionObservers under-the-hood (so is well supported in “good” browsers and easily polyfilled in others) to apply appropriately named classes to the component when necessary.
Table of Contents
Demo
A complete demo is available here: https://codepen.io/stowball/pen/zPYzWd
Installation
npm
npm install eqio --saveDirect <script> include
<script src="https://unpkg.com/eqio/umd/eqio.min.js"></script>Usage
The HTML
- Add a class of
eqioto the element. - Add a
data-eqio-sizesattribute whose value is a JSON-serializable array of sizes. - Optionally add a
data-eqio-prefixattribute whose value is used as the prefix for your class names.
<div
class="media-object eqio"
data-eqio-sizes='["<400", ">700"]'
data-eqio-prefix="media-object"
>
…
</div>The above component will:
- be able to be customised when its width is 400 or smaller (
"<"is a synonym formax-width, not “less than”). - be able to be customised when its width is 700 or greater (
">"is a synonym formin-width, not “greater than”). - apply the following classes
media-object-eqio-<400andmedia-object-eqio->700as appropriate. Ifdata-eqio-prefixhad not been specified, the applied classes would beeqio-<400andeqio->700.
Note: Multiple classes can be applied at once.
The CSS
In your CSS, write class names that match those applied to the HTML.
.media-object-eqio-\<400 {
/* customizations when less than or equal to 400px */
}
.media-object-eqio-\>700 {
/* customizations when greater than or equal to 700px */
}Note:
- eqio classes include the special characters
<&>, so they must be escaped with a\in your CSS. - eqio elements are
position: relativeby default, but your component can override that toabsolute/fixedetc as required. - eqio elements can't be anything but
overflow: visible. - To prevent accidental creation of horizontal scrollbars, a parent element is required to
overflow-x: hidden. It is recommended to apply this tobody.
The JavaScript
If using a module bundler, such as webpack, first import Eqio.
import Eqio from 'eqio';In your JS, tell eqio which elements are to be made responsive by passing a DOM reference to Eqio.
var mediaObject = new Eqio(document.querySelector('.media-object'));Should you need to stop this element from being responsive, you can call .stop() on your instance:
mediaObject.stop();This will remove all observers and eqio internals, except for the class names that were applied at the time.
Copyright (c) 2018 Matt Stow
Licensed under the MIT license (see LICENSE for details)