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

@taikonauten/windowatch

v2.1.6

Published

Library to handle scroll-, resize- and breakpoint-change events globally.

Downloads

159

Readme

Windowatch is a singleton class managing scroll-, resize- and breakpoint-change events globally. It uses passive resize & scroll event listeners and requestAnimationFrame to optimize dom interactions (get, update). It removes unnecessary window event listeners automatically, if no listeners are attached.

Tests

Installation

npm install --save @taikonauten/windowatch

Usage

Retrieve env properties

import Windowatch from '@taikonauten/windowatch';

// get the current window width
let width = Windowatch.getWindowWidth();
// -> 2560

// get the current window height
let height = Windowatch.getWindowHeight();
// -> 1440

// get the current vertical window scroll position
let scrollY = Windowatch.getScrollY();
// -> 42

Work with breakpoints

Add breakpoint specification to your script's entry point:

import Windowatch from '@taikonauten/windowatch';

Windowatch.setBreakpointSpecs({
  s: { min: null, max: 899 },
  m: { min: 900, max: 1199 },
  l: { min: 1200, max: null }
});

ℹ️ You can enhance the specifications of each breakpoint with your own information.

You can then receive information about the current breakpoint and its specs.

// get the current breakpoint name
Windowatch.getBreakpoint();
// -> 's'

// get the current breakpoint specifications
Windowatch.getBreakpointSpec();
// -> { min: null, max: 899 }

Or you can check if the current breakpoint is smaller or larger than another one. Both methods are called with a breakpoint name.

// check if the current breakpoint is smaller than the given one
Windowatch.isSmallerThan('m');
// if current breakpoint is s -> true

// check if the current breakpoint is larger than the given one
Windowatch.isBiggerThan('m');
// if current breakpoint is s -> false

Working with events

You can add 3 types of event listeners:

  • scroll listener
  • resize listener
  • breakpoint listener

Each callback can return another function which is called at the end of a frame. Do all measurements and calculations first and put modifications like adding or removing classes in the listener callback.

Scroll listener example:

import Windowatch from '@taikonauten/windowatch';

const scrollHandler = (scrollY) => {
  // dom measurements
  let measurement = $element.classList.contains('block--foo');

  // return update callback (optional)
  // gets called at the end of the frame
  return () => {
    // dom updates
    $element.classList.toggle('block--bar', measurement);
  }
}

Windowatch.addScrollListener(scrollHandler);

Scroll event

You can add and remove scroll listeners which are called after the scroll position of the document has changed. The callback function accepts a single parameter, representing the current vertical scroll position.

const scrollHandler = (scrollY) => {};

Windowatch.addScrollListener(scrollHandler);
Windowatch.removeScrollListener(scrollHandler);

You can call addScrollListener with a second parameter, if your callback should only be called at specific breakpoints.

ℹ️ You need to specify breakpoints first to use this feature.

// The scrollHandler function is only called,
// if the current breakpoint has the name 'm' or 'l'
Windowatch.addScrollListener(scrollHandler, ['m', 'l']);

Resize event

You can add a callback function which is triggered after the size of the document has changed. This callback receives the width and the height of the window as its parameter.

const resizeHandler = (width, height) => {};

Windowatch.addResizeListener(resizeHandler);
Windowatch.removeResizeListener(resizeHandler);

Breakpoint event

If you do not need to react to each and every resize event but only if the current breakpoint has changed, you can register a breakpoint listener. The callback function is called with the new breakpoint and its related specifications as its parameters.

ℹ️ You need to specify breakpoints first to use this feature.

const breakpointChangeHandler = (breakpoint, spec) => {};

Windowatch.addBreakpointListener(breakpointChangeHandler);
Windowatch.removeBreakpointListener(breakpointChangeHandler);

Development & Playground

npm start

Tests

Running the tests requires Chrome or Brave to be installed on in local environment.

To execute all tests, run the following command:

npm run test

In case you are getting an error regarding the missing CHROME_BIN environment variable, please follow this guide:

  • locate Chrome or Brave binary by running ls /usr/bin | grep 'chrome\|brave'
  • update the CHROME_BIN env variable by running export CHROME_BIN=/usr/bin/brave-browser

Migration

Changes in 2.x

Update the main import

-import {windowatch as TaikoLibWindowatch} from '@libs/taiko-lib-windowatch';
+import TaikoLibWindowatch from '@taikonauten/windowatch';

Changes in 2.1

Update the main import

-import TaikoLibWindowatch from '@taikonauten/windowatch';
+import Windowatch from '@taikonauten/windowatch';

Made with ♡ at Taikonauten