Giter Club home page Giter Club logo

react-clicks-without-borders's Introduction

Installation

yarn add react-clicks-without-borders
  • It's quite small in size.
  • It's built with TypeScript.
  • It supports both Mouse and Touch Events.
  • Bring your own element!

Usage

Bring your own div

import { useOutsideClickListener } from 'react-clicks-without-borders';

const App = () => {
  const ref = useOutsideClickListener(() => {
    console.log('Hey, you can close the Popup now');
  });

  return (
    <div id="app">
      <div ref={ref}> Some Popup, Nav or anything </div>
      <div id="rest-of-the-app">Don't name a div like that :(</div>
    </div>
  );
};

Bring your own span!

import { useOutsideClickListener } from 'react-clicks-without-borders';

const App = () => {
  const ref = useOutsideClickListener<HTMLSpanElement>(() => {
    console.log('Hey, you can close the Popup now');
  });

  return (
    <div id="app">
      <span ref={ref}> Some Popup, Nav or anything </span>
      <div id="rest-of-the-app">Don't name a div like that :(</div>
    </div>
  );
};

Bring your own ref!

import { useOutsideClickListener } from 'react-clicks-without-borders';

const App = () => {
  const ref = React.useRef();
  useOutsideClickListener(
    () => {
      console.log('Hey, you can close the Popup now');
    },
    { ref },
  );

  return (
    <div id="app">
      <span ref={ref}> Some Popup, Nav or anything </span>
      <div id="rest-of-the-app">Don't name a div like that :(</div>
    </div>
  );
};

Use it's div

import OutsideClickListener from 'react-clicks-without-borders';

const App = () => {
  const handleClickAway = () => {
    console.log('Hey, you can close the Popup now');
  };

  return (
    <div id="app">
      <OutsideClickListener onClickAway={handleClickAway}>
        <div> Some Popup, Nav or anything </div>
      </OutsideClickListener>
      <div id="rest-of-the-app">Don't name a div like that :(</div>
    </div>
  );
};

Examples

LICENSE

MIT

Thanks to: ooade

Forked from https://github.com/ooade/react-clicks-without-borders as the author did not want the hook abstraction

react-clicks-without-borders's People

Contributors

ooade avatar alphy11 avatar dependabot[bot] avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.