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

boolean-state-throttle

v0.1.1

Published

Logic utility for use cases such as hiding header when scrolling down

Downloads

2

Readme

Boolean State Throttle

Version

You may want to display a top bar header floating fixed on the screen. However on mobile space is limited and there can be a need to hide the header when scrolling down and bringing it back when scrolling up. You might have already seen solutions like Headroom.js or sneakpeek. These often provide far more features than you may want to have, especially if you are working with a framework such as React or maybe Angular. Boolean State Throttle handles the hard logic for you, but doesn't provide full convenience in order to give more control to you. Due to this agnostic design you can use whatever framework or library you want.

Usage samples

Boolean state throttle is very convenient to use and gives you the power to handle exceptions!

  1. You provide it a callback that is only called when the state changes.
  2. You provide it a state boolean. This is the current display state of a header (or anything else).
  3. You provide it a value every time you get an event (onscroll).

You can link it with horizontal scrolling or vertical scrolling. Or maybe you have checkboxes and if 10 checkboxes are checked within 1 second you want to display a message. Boolean State Throttle works for all time depending changes that are based on a numeric value.

Vanilla ES5 JavaScript sample

View demo

(function() {
    var headerElement = document.querySelector('header'),
        notifyElement = document.getElementById('header-is-hidden')

    // this is our boolean state, owned and controlled by this component
    var isHidden = false

    function getScrollY() {
        return (window.pageYOffset !== undefined)
            ? window.pageYOffset
            : (document.documentElement || document.body.parentNode || document.body).scrollTop
    }

    // this function is only triggered when the boolean changes
    // you could optimize render by using requestAnimationFrame; or simply use setState in React
    function updateHeaderState(newState) {
        isHidden = newState
        notifyElement.className = headerElement.className = isHidden ? 'hidden' : ''
    }

    // convenience for you!
    var headerStateTrigger = new BooleanStateThrottle(updateHeaderState)

    function updateScrollValue() {
        var y = getScrollY()
        // one of the reasons to have the logic separate is that you can handle exceptions better!
        if (isHidden && y < 50) {
            // header should always be shown when at the top
            updateHeaderState(false)
        } else {
            // bombard the boolean state throttle
            headerStateTrigger(isHidden, y)
        }
    }

    window.addEventListener('scroll', updateScrollValue, false)
})()

ReactJS sample

Code below only has the core logic for displaying and hiding a header; the actual render is up to you.

var Component = React.createClass({
/* ... */
    getInitialState: function() {
        return {
            isHeaderHidden: false
        }
    },

    componentDidMount: function() {
        this.headerStateTrigger = new BooleanStateThrottle(this.updateHeaderState)
        window.addEventListener('scroll', this.updateScrollValue)
    },

    componentWillUnmount: function() {
        window.removeEventListener('scroll', this.updateScrollValue)
    },

    updateHeaderState: function(isHeaderHidden) {
        this.setState({ isHeaderHidden: isHeaderHidden })
    },

    updateScrollValue: function() {
        var y = (window.pageYOffset !== undefined)
            ? window.pageYOffset
            : (document.documentElement || document.body.parentNode || document.body).scrollTop

        if (this.state.isHeaderHidden && y < 50) {
            // header should always be shown when at the top
            this.updateHeaderState(false)
        } else {
            // bombard the boolean state throttle
            this.headerStateTrigger(this.state.isHeaderHidden, y)
        }
    }
/* ... */
})

Customizing

BooleanStateThrottle takes two arguments: callback function (required) and options object (optional).

// values shown are the defaults
var options = {
    // time in ms after which changes are checked for
    interval: 200,
    // how much value can change down until `false` state is triggered
    falseTolerance: 150,
    // how much value can change up until `false` state is triggered
    trueTolerance: 150,
    // tells the initial value
    initialValue: 0
}

Compatibility

This utility is compatible with probably everything since Internet Explorer 5 or so.