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

@pofo/click-outside

v1.0.2

Published

React click outside ref hook. Handles escape key.

Downloads

6

Readme

React useClickOutsideRef Hook

Lightweight and accessible clickoutside React ref

Install

yarn add @pofo/click-outside
npm i @pofo/click-outside

Jump Around

🚀 Features 🚀

  • [x] Call onClick function when user clicks outside of component
  • [x] Call onClick function when user presses the escape key
  • [x] Support multiple refs with the same onClick function

Basic Usage

Refs are always returned in an array of 1 or more refs.

import useClickOutisdeRef from '@pofo/click-outside';

const MyComponent = () => {
    
    const onClick = () => {
        // ... do something
    }

    /* CLICKOUTSIDE REF */
    const [ ref ] = useClickOutsideRef( onClick );

    return (
        <div ref={ref}>
            Click outisde of me to trigger the onClick function!
        </div>
    )
}

Multiple Refs

If you want more than one ref attached to the same onClick function, specify the number of refs you want in the optional options object.

import useClickOutisdeRef from '@pofo/click-outside';

const MyComponent = () => {
    
    const onClick = () => {
        // ... do something
    }

    /* CLICKOUTSIDE REF */
    const [ ref1, ref2 ] = useClickOutsideRef( onClick, { numRefs: 2 } );

    return (
        <div ref={ref1}>
            Click outside of me to trigger the onClick function!
        </div>
        <div ref={ref2}>
            Clicking outside of me will also trigger the same onClick function!
        </div>
    )
}

Customizing Options

By default, pressing the escape key will call the onClick function as well. If you wish to disable this functionality, pass an options object with enableEscape as false.

import useClickOutisdeRef from '@pofo/click-outside';

const MyComponent = () => {
    
    const onClick = () => {
        // ... do something
    }

    /* CLICKOUTSIDE REF */
    const [ ref ] = useClickOutsideRef( onClick, { enableEscape: false } );

    return (
        <div ref={ref}>
            Pressing escape will not trigger the onClick, but clicking outside me will!
        </div>
    )
}

API

useClickOutsideRef(
    // requried
    onClick: () => void,
    // optional
    options: {
        numRefs?: number,
        enableEscape?: boolean,
    },
)

| Parameter | Default | Type | Description | | ----------- | ----------- | ----------- | ----------- | | onClick | REQUIRED | () => void| onClick function that gets called when user clicks off the component. By default, also gets called when user clicks the escape key | options | { numRefs: 1, enableEscape: true } | { numRefs: number, enableEscape: boolean } | Optional options object. numRefs: Specifies the number of refs you would like returned. enableEscape: When set to true, pressing the escape key will trigger the onClick function. If set to false, pressing the escape key will not trigger the onClick function. Defaults to true.

Typescript

You can specify what kind of HTML Element your ref is going to be attached to.

const [ ref ] = useClickOutsideRef<HTMLDivElement>( () => {} );

If you are returning multiple refs that are different kinds of HTML Elements.

import { RefObject } from 'react';
import useClickOutisdeRef from '@pofo/click-outside';

const MyComponent = () => {

    const onClick = () => {
        // ...do something
    }

    /* CLICKOUTSIDE REF */
    const [ ref1, ref2 ] = useClickOutsideRef( onClick );

    return (
        <section>
            <div ref={ref1 as RefObject<HTMLDivElement>}>
                I am a div!
            </div>
            <span ref={ref2 as RefObject<HTMLSpanElement>}>
                I am a span!
            </span>
        </section>
    )
}

IF YOU KNOW A BETTER WAY TO SUPPORT TYPESCRIPT, PLS HELP AND CREATE A PULL REQUEST

Extra

This library plays well with useFocusTrap

LICENSE

MIT