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

@readymade/res

v1.0.0-beta.0

Published

A swiss-army knife for responsive web sites

Downloads

2

Readme

res.js

Res is a Swiss-army knife for responsive sites. The class handles responsive state, parses the user agent, displays a visual grid for engineers to reference, acts as a singleton for resize and orientation change events.

Minified, Res comes in at 1.45KB gzipped, making it a perfect complement to your next project. Res is not dependent on any library or framework.

Res is simple to include in your project. Res takes one argument, an Object that describes responsive states: name, breakpoint information, and visual grid settings. Each state is defined with a key: name (state). breakpoint is the only required property on this Object, the maximum pixel value of the responsive state, equivocal to max-width in a CSS media query.

If you wish to position elements via responsive design grid, you can also provide optional number of columns (numberOfColumns), outer margin (gutterOnOutside) and gutter (gutter). The visual grid is by default centered horizontally, although an optional offset (offset) from the left can be provided to position the grid appropriately. Res will calculate the position of columns and size of column spans automatically. An example of setting multiple breakpoints and supplying the units needed to create a grid for each breakpoint is below.

const states = {
  mobile: {
    breakpoint: 640,
    numberOfColumns: 4,
    gutterOnOutside: true,
    gutter: 10,
    offset: 0,
  },
  tablet: {
    breakpoint: 1024,
    numberOfColumns: 12,
    gutterOnOutside: true,
    gutter: 20,
    offset: 0,
  },
  desktop: {
    breakpoint: 1920,
    totalWidth: 960,
    columnWidth: 60,
    numberOfColumns: 16,
    gutterOnOutside: true,
    gutter: 22,
    offset: 0,
    center: true,
  },
};

Reference the position and width of elements on a visual design grid in JavaScript. In the below example, width is set to the horizontal width of four columns by passing 4 to the columnSpan method. The element can be placed on the eighth column by calling the columnPosition method.

const width = r.grid.columnSpan(4);
const xPosition = r.grid.columnPosition(8);

Singleton

Res emits a stateChange Event on window.onresize or window.onorientationchange. You can listen for the stateChange event on the window and reposition and resize elements based on stateChange. This way you can make your app more performant by not repeating window.onresize events.

window.addEventListener("stateChange", function (ev) {
  if (r.state === "portrait") {
    width = r.grid.columnSpan(4);
    xPosition = r.grid.columnPosition(8);
  }

  if (r.state === "tablet") {
    width = r.grid.columnSpan(6);
    xPosition = r.grid.columnPosition(6);
  }
});

Browser Detection

Res doesn't just define breakpoints in Javascript. The instance of the Res class also provides easy access to OS and Browser type, detects whether a device is in portrait or landscape, and if the device operates with mouse or touch input. Once instantiated as an object, you can do things based on device type, input, orientation, os, and breakpoint. For example, this statement checks if the device type is Android and the device is oriented in landscape.

if (r.os === "android" && r.device === "tablet" && r.orientation === "landscape") {
  // do something here for android tablets in landscape mode
}

Some may think browser detection like in the above example is gross. Given the fragmentation of the browser landscape, feature detection en masse comes at the cost of bundle size. It wouldn't be viable to provide a solution that detects every feature, for instance. Detecting features should be done ad hoc for performance.

The small footprint of Res is meant to provide an easy-to-understand interface for browser detection that compliments feature detection methods. By standardizing the way the user agent is referenced, Res makes it easier to deprecate browser detection past its use value. User-agent detection is tried and tested in Res since 2013.

Beta

Res is currently in beta. Use at your discretion.

Res has been around since 2013 in one form or another and portions of the library have been tested in production since then. The user agent parsing and grid were in use on production websites for several years before getting refactored to TypeScript and ES2020 compliant code. This refactor, combined with new features, and inclusion under the @readymade namespace prompted a new beta. Res is not dependent on other parts of the @readymade ecosystem.

Roadmap

Prior to 1.0.0, Res should gain the following features:

  • Web Component that displays the visual design grid for development
  • CSS framework that provides CSS variables in real-time for the grid

Report a Problem

Submit issues in the issue tracker, fork the repo and make a pull request to add a feature.