@shelf/react-outside-click
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

@shelf/react-outside-click

React library for handling outside clicks of a specified element.

Installation

Install the library using npm:

$ npm install @shelf/react-outside-click

Install the library using yarn:

$ yarn add @shelf/react-outside-click

Usage

ClickOutsideProvider

ClickOutsideProvider is a component that wraps its children and detects clicks outside of its container element.

import { ClickOutsideProvider } from '@shelf/react-outside-click';

const Component = () => {
  const onOutsideClick = () => {
    console.log('Clicked outside')
  };

  return (
    <ClickOutsideProvider onOutsideClick={onOutsideClick}>
      <span>Shelf.io</span>
    </ClickOutsideProvider>
  );
};

Props

onOutsideClick

Type: function (required)

A function that will be called when an outside click is detected.

disabled

Type: boolean (optional)

If set to true, the outside click detection will be disabled.

mouseEvent

Type: string (optional)

The mouse event to listen for. Defaults to 'mousedown'

listenerOptions

Type: boolean | AddEventListenerOptions (optional)

Additional options for the event listener. By default, it uses capture phase.This behavior ensures that the click outside event is captured before any click events on the descendants, even if stopPropagation is used. See https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture

useClickOutside

useClickOutside is a custom hook that attaches an outside click event listener to a specified element.

import { useRef } from 'react'
import { useClickOutside } from '@shelf/react-outside-click';

const Component() {
  const ref = useRef(null)

  const handleClickOutside = () => {
    console.log('Clicked outside')
  }

  useOnClickOutside(ref, handleClickOutside, {mouseEvent: 'mouseup'})

  return <span ref={ref}>Shelf.io</span>
}

Props

ref

Type: RefObject<T> (required)

A ref object that points to the target element.

onOutsideClick

Type: function (required)

A function that will be called when an outside click is detected.

options: (optional)

disabled

Type: boolean (optional)

If set to true, the outside click detection will be disabled.

mouseEvent

Type: string (optional)

The mouse event to listen for. Defaults to 'mousedown'

listenerOptions

Type: boolean | AddEventListenerOptions (optional)

Additional options for the event listener. By default, it uses capture phase.This behavior ensures that the click outside event is captured before any click events on the descendants, even if stopPropagation is used. See https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture

Publish

$ git checkout master
$ yarn version
$ yarn publish
$ git push origin master --tags

License

MIT © Shelf

Readme

Keywords

none

Package Sidebar

Install

npm i @shelf/react-outside-click

Weekly Downloads

1,125

Version

0.1.4

License

MIT

Unpacked Size

9.38 kB

Total Files

17

Last publish

Collaborators

  • vladmarchuk
  • vadymaslovskyi
  • andrew_batutin
  • marianna-milovanova
  • petro.bodnarchuk
  • kateryna-kochina
  • maksym.tarnavskyi
  • andrii-nastenko
  • mykhailo.yatsko
  • ahavrysh
  • nikita_shelf
  • maciej.orlowski
  • monopotan
  • andrew214
  • bogdan.kolesnyk
  • andrii.batutin
  • kristina.zhak
  • anton-russo
  • mmazurowski
  • mateuszgajdashelf
  • kchlon
  • dmytro.harazdovskiy
  • duch0416
  • i5adovyi
  • olesiamuller
  • mykola.khytra
  • yuliiakovalchuk
  • el_scrambone
  • bodyaflesh
  • andriisermiahin
  • mpushkin
  • batovpavlo
  • domovoj
  • vozemer
  • oleksii.dymnich
  • dima-bond
  • maksym.hayovets
  • oles.zadorozhnyy
  • ss1l
  • gemshelf
  • hartzler
  • vladgolubev
  • hmelenok
  • knupman
  • maaraanas
  • terret
  • chapelskyi.slavik
  • pihorb
  • irynah
  • diana.kryskuv
  • andy.raven
  • rafler
  • sskalp88
  • demiansua
  • yuriil
  • ktv18
  • drews_abuse
  • rostyslav-horytskyi
  • whodeen