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

@echoghi/hooks

v1.5.2

Published

A library of useful hooks from across the [`web`](#inspiration).

Downloads

3

Readme

Hooks

A library of useful hooks from across the web.

Install

Note: React 16.8+ is required for Hooks.

With npm

npm i @echoghi/hooks --save

Or with yarn

yarn add @echoghi/hooks

API

Hooks

useNetworkStatus()

Retrieve network status from the browser.

Returns

Object containing:

  • isOnline: boolean: true if the browser has network access. false otherwise.

  • offlineAt?: Date: Date when network connection was lost.

Example

import { useNetworkStatus } from '@echoghi/hooks';

const Example = () => {
    const { isOnline, offlineAt } = useNetworkStatus();

    // ...
};

useWindowScrollPosition()

Returns

Object containing:

  • x: number: Horizontal scroll in pixels (window.pageXOffset).
  • y: number: Vertical scroll in pixels (window.pageYOffset).

Example

import { useWindowScrollPosition } from '@echoghi/hooks';

const Example = () => {
    const { x, y } = useWindowScrollPosition();

    // ...
};

useWindowSize()

Returns

Object containing:

  • width: Width of browser viewport (window.innerWidth)
  • height: Height of browser viewport (window.innerHeight)

Example

import { useWindowSize } from '@echoghi/hooks';

const Example = () => {
    const { width, height } = useWindowSize();

    // ...
};

useLocalStorage()

Arguments

  • key: string: Key to the value in local storage
  • value: string: value to the key in local storage

Returns

Array containing:

  • [name]: Value to the key in local storage
  • [setName]: Setter function to the provided key

Example

import { useLocalStorage } from '@echoghi/hooks';

const Example = () => {
    // Similar to useState but first arg is key
    // to the value in local storage.
    const [name, setName] = useLocalStorage('name', 'Bob');

    // ...
};

useOnClickOutside()

Arguments

  • ref: useRef: A ref to an element created with useRef
  • func: function: a function to be fired within an effect when a click outside the ref is detected

Example

import { useOnClickOutside } from '@echoghi/hooks';

const Example = () => {
    // Create a ref that we add to the element for which we want to detect outside clicks
    const ref = useRef();
    // State for our modal
    const [isModalOpen, setModalOpen] = useState(false);
    // Call hook passing in the ref and a function to call on outside click
    useOnClickOutside(ref, () => setModalOpen(false));

    // ...
};

useMediaQuery()

Accepts a media query string then uses the matchMedia API to determine if it matches with the current document.

Arguments

  • string: media query string:

Example

import { useMediaQuery } from '@echoghi/hooks';

const Example = () => {
    const isSmall = useMediaQuery('(max-width: 48rem)');
    const isLarge = useMediaQuery('(min-width: 48rem)');

    return (
        <Demo>
            <p>Small view? {isSmall ? 'yes' : 'no'}</p>
            <p>Large view? {isLarge ? 'yes' : 'no'}</p>
        </Demo>
    );
};

usePrefersReducedMotion()

A hook to allow access to the CSS media query prefers-reduced-motion.

Example

import { usePrefersReducedMotion } from '@echoghi/hooks';

const Example = ({ isBig }) => {
    const prefersReducedMotion = usePrefersReducedMotion();
    const styles = {
        transform: isBig ? 'scale(2)' : 'scale(1)',
        transition: prefersReducedMotion ? undefined : 'transform 300ms'
    };

    return <Demo styles={styles}>Stuff</Demo>;
};

useRandomInterval()

A hook itended for animations and microinteractions that fire on a spontaneous interval. More info here...

Arguments

  • callback: function
  • minDelay?: number
  • maxDelay?: number

Example

import { useRandomInterval } from '@echoghi/hooks';

function LaggyClock() {
    // Update between every 1 and 4 seconds
    const delay = [1000, 4000];

    const [currentTime, setCurrentTime] = React.useState(Date.now);

    useRandomInterval(() => setCurrentTime(Date.now()), ...delay);

    return <>It is currently {new Date(currentTime).toString()}.</>;
}

useInterval()

A hook based on Dan Abramov's blog post about setInterval.

Arguments

  • callback: function
  • delay: number

Example

import { useInterval } from '@echoghi/hooks';

function Example() {
    let [count, setCount] = useState(0);
    let [delay, setDelay] = useState(1000);

    useInterval(() => {
        // Your custom logic here
        setCount(count + 1);
    }, delay);

    function handleDelayChange(e) {
        setDelay(Number(e.target.value));
    }

    return (
        <Demo>
            {count}
            <input value={delay} onChange={handleDelayChange} />
        </Demo>
    );
}

useTimeout()

A declarative adaptation of setTimeout based on Dan Abramov's blog post about setInterval

Arguments

  • callback: function
  • delay: number

Example

import { useTimeout } from '@echoghi/hooks';

function Example() {
    const [message, setMessage] = useState('changing in 2 seconds...');

    useTimeout(() => setMessage('changed!'), 2000);

    return <Demo>{message}</Demo>;
}

Inspiration and sources


CircleCI

MIT License