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

@unic/factory-breakpoint-manager

v0.0.5

Published

Lightweight breakpoint manager

Downloads

3

Readme

factory-breakpoint-manager

Lightweight Breakpoint Manager who notifies you when the breakpoint switches or you can check for the current breakpoint.

Installation

$ npm install @unic/factory-breakpoint-manager

Importing

// ES6 Module
import createBreakpointManager from '@unic/factory-breakpoint-manager';

// CommomJS
const createBreakpointManager = require('@unic/factory-breakpoint-manager').default;

Usage/Quickstart

import createBreakpointManager from '@unic/factory-breakpoint-manager';

// Creating a new BreakpointManager (no pun intended)
const BreakpointManager = createBreakpointManager({
  breakpoints: {
    xs: 0,
    sm: 768,
    md: 992,
    lg: 1200,
  },
  unit: 'px'
});

// Subscribe to a breakpoint change
const subscriptionId = BreakpointManager.on('change', () => {

  // Read the current state of your BreakpointManager
  const currentState = BreakpointManager.getState();
  console.log(currentState);

  // Check if the breakpoint is currently value x
  if(BreakpointManager.matches('xs')) {
    console.log('Window width is currently between 0px and 767px');
  }

  // Check if the breakpoint is currently value x or y...
  if(BreakpointManager.matches(['xs', 'md', 'lg'])) {
    console.log('Window is currently on xs/md or lg');
  }

  // Check if matches is false can also result in what you need
  if(!BreakpointManager.matches('sm')) {
    console.log("Same result as when given ['xs', 'md', 'lg'] and checkig if it was true");
  }
});


// Unsubscribe at any given point
BreakpointManager.off(subscriptionId);

Best practises

Having to write your Breakpoints directly into your JS can be a real hazard to maintain, so I'd advise you to write your breakpoint configuration in a JSON file and load this json file into your JS and into your CSS preprocessor. With this you have a single source of truth for your breakpoints and it's very managable.

Good plugins to achieve this

  • https://webpack.js.org/loaders/json-loader/ for laoding JSON into JS
  • https://github.com/acdlite/json-sass for loading JSON into SASS/SCSS

API

BreakpointManager has own functionality and functionality provided by included composites.

Factory createBreakpointManager(options)

Own methods

Observer Composite
Documentation of these methods are extern

createBreakpointManager(options)

Create a new instance of a BreakpointManager

Returns: Object - BreakpointManager

| Param | Type | Description | | --- | --- | --- | | options | Object | Custom options |

Example

const BreakpointManager = createBreakpointManager({
  breakpoints: {
    xs: 0,
    sm: 768,
    md: 992,
    lg: 1200,
  },
  unit: 'px'
});

// Or go with em as a unit

const BreakpointManager = createBreakpointManager({
  breakpoints: {
    xs: 0,
    sm: 48,
    md: 62,
    lg: 75,
  },
  unit: 'em'
});

getState()

Get the current state of your BreakpointManager.

Returns: Object - Returns current state of your BreakpointManager

| Key | Type | Description | | --- | --- | --- | | width | Integer/Float | Current Window width | | breakpoint | Object | Current active breakpoint |

Example

console.log(BreakpointManager.getState());

/*
Could return:
{
  width: 823,
  breakpoint: {
    name: 'sm',
    minWidth: 768
  }
}
*/

matches(test)

Check on which breakpoint you currently are.

Returns: Boolean - Ture if matching, false if not matching

| Param | Type | Description | | --- | --- | --- | | test | String/Array | Custom options |

Example

if(BreakpointManager.matches('xs')) {
  console.log('Yes! current Breakpoint is xs');
}

if(BreakpointManager.matches(['xs', 'md', 'lg'])) {
  console.log('Current breakpoint is currently xs, md or lg');
}

if(!BreakpointManager.matches('sm')) {
  console.log('Current breakpoint is definitely not sm');
}

Information and Ressouces about factories

A factory function is simply a function that returns an object. Factory functions are often used in combination of composites to accomplish the factory/composition pattern.

Helpful Ressources:

  • https://www.youtube.com/watch?v=ImwrezYhw4w
  • https://www.youtube.com/watch?v=wfMtDGfHWpA

Caveats

BreakpointManager is using window.innerWidth to read the width of the frame. This also means, if you have actual content that is 'wider' than 100% of your screen, the value of your BreakpointManager might not be correct, as it's not using document.documentElement.clientWidth which would read the correct value, but not incorporate the scrollbar in its calculation (which would result the BreakpointManager breakpoint of 768px to trigger at different widths than your CSS breakpoint at the same value). To avoid any problems, make sure you have overflow-x: hidden on your body, to avoid any overflow on the x-achsis, so window.innerWidth will always read the correct value.

License

Apache-2.0