useOutsideClickRef

About

A hook that can track a click event outside a ref. Returns a callbackRef.

Examples

import "./styles.css";
import { useOutsideClickRef } from "rooks";

function App() {
  function myComponent() {
    alert("Clicked outside");
  }
  const [ref] = useOutsideClickRef(myComponent);
  return (
    <div
      className="App"
      style={{ display: "flex", flexDirection: "column", alignItems: "center" }}
    >
      <h1>Rooks : useOutsideClick Example</h1>

      <div
        style={{
          display: "flex",
          flexDirection: "column",
          alignItems: "center",
          width: "100%",
          backgroundColor: "lightblue",
        }}
        ref={ref}
      >
        <h2 className="inside">This is inside</h2>
      </div>
    </div>
  );
}

export default App;

Arguments

ArgumentsTypeDescriptionDefault value
handlerfunctionCallback to fire on outside click
whenbooleanA boolean which which activates the hook only when it is true. Useful for conditionally enable the outside clicktrue

Returns

Returned Array itemsTypeDescription
refCallbackRefref

Join the community!

Join our discord server! You can click the floating discord icon at the bottom right of the screen and chat with us!

Powered by vercel