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

react-click-outside-listener

v3.0.0

Published

Wrapping component to register click outside

Downloads

158

Readme

ci codecov downloads node npm MIT npm bundle size Conventional Commits

react-click-outside-listener

Wrapping component to register click outside.

Demo

Here is a small playground with couple of examples.

Installation

As any other npm package react-click-outside-listener can be added to your project by following command:

npm i -S react-click-outside-listener

It requires any version of react with new context API support as peer dependency, so it should be installed as well.

npm i -S react

Quick start

Nothing is easier than use ClickOutsideListener component - just wrap your content with it:

import { ClickOutsideListener } from 'react-click-outside-listener';

const Parent = () => {
    const handleClickOutside = ...;

    return (
        <ClickOutsideListener onClickOutside={ handleClickOutside }>
            <div>Just put your content inside</div>
            <div>You can put several elements, if you need</div>
            <div>ClickOutsideListener component will call listener only if none of those clicked</div>
            <div>
                <div>Of course we can nest items</div>
            </div>
        </ClickOutsideListener>
    );
}

It is only possible to track clicks on html elements, so children should be html elements or React components forwarding refs:

const Child = React.forwardRef((props, ref) => (
    <div ref={ ref }>
        We should attach ref to html container
    </div>
));

const Parent = () => {
    ...

    return (
        <ClickOutsideListener onClickOutside={ handleClickOutside }>
            <Child />
        </ClickOutsideListener>
    );
}

If you need to support custom properties for refs or want to track selected items, use render prop as a child:

const Parent = () => {
    return (
        <ClickOutsideListener onClickOutside={ handleClickOutside }>
            {refs => (
                <div>
                    Click here to invoke callback
                    <div ref={ refs(0) }>
                        No callback when click here
                    </div>
                    <div>Beware callbacks</div>
                    <div ref={ refs(1) }>
                        Another safe zone
                    </div>
                </div>
            )}
        </ClickOutsideListener>
    );
}

renderProp argument can be also used as regular ref:

const Component = () => {
    return (
        <ClickOutsideListener onClickOutside={ handleClickOutside }>
            {ref => (
                <div>
                    <div ref={ ref }>
                        No callback when click here
                    </div>
                </div>
            )}
        </ClickOutsideListener>
    );
}

Let's consider small example with dropdown menu:

const Menu = ({ label, items }) => {
    const [ isShown, setIsShown ] = useState(false);
    const toggleMenu = useCallback(
        () => setIsShown(isShown => !isShown),
        []
    );
    const handleClickOutside = useCallback(
        () => setIsShown(false),
        []
    );

    return (
        <ul className="menu">
            <ClickOutsideListener onClickOutside={ handleClickOutside }>
                {refs => (
                    <React.Fragment>
                        <button
                            className="menu__trigger"
                            onClick={ toggleMenu }
                            ref={ refs(0) }
                        >
                            { label }
                        </button>
                        <Dropdown
                            items={ items }
                            wrapperRef={ refs(1) }
                            />
                    </React.Fragment>
                )}
            </ClickOutsideListener>
        </ul>
    );
}

...

const Dropdown = ({ items, wrapperRef }) => (
    <ul
        className='dropdown'
        ref={ wrapperRef }
    >
        { items.map(
            ({ id, label, action }) => (
                <li
                    key={ id || label }
                    className="dropdown__item"
                    onClick={ action }
                >
                    { label }
                </li>
            )
        ) }
    </ul>
);

Hook

react-click-outside-listener package also exposes hook to achieve the same functionality. It returns refs item similar to ClickOutsideListener renderProp argument. It cab be used to set several refs or only one:

import { useClickOutsideListener } from 'react-click-outside-listener';

const Component = () => {
    const refs = useClickOutsideListener(
        () => { ... }
    );

    // several refs
    return (
        <div>
            Click here to invoke callback
            <div ref={ refs(0) }>
                No callback when click here
            </div>
            <div>Beware callbacks</div>
            <div ref={ refs(1) }>
                Another safe zone
            </div>
        </div>
    );
}

// or

const Component = () => {
    const ref = useClickOutsideListener(
        () => { ... }
    );

    // one ref
    return (
        <div>
            <div ref={ ref }>
                No callback when click here
            </div>
        </div>
    );
}