tigran tumasov

useClickOutside

Buttons 4

About

React hook to handle click outside of component

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds useClickOutside to your project and fits it to your theme.

Code

How useClickOutside is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

import React, { useRef, useState } from "react";
import clsx from "clsx";
import { useClickOutside } from "@/components/ui/use-click-outside";

export default function DefaultDemo() {
  const [open, setOpen] = useState<boolean>(false);
  const ref = useRef<HTMLDivElement>(null);

  useClickOutside(ref, () => setOpen(false));

  return (
    <>
      <button onClick={() => setOpen(true)}>
        {!open ? "Open" : "Close"} Drawer
      </button>
      <div className={clsx(
        "fixed top-0 left-0 w-full h-full bg-overlay z-[99999] duration-300",
        open ? "opacity-100" : "opacity-0 pointer-events-none"
      )}>
      </div>
      <div
        className={clsx(
          "fixed bottom-0 left-0 w-full h-1/4 bg-gray-200 shadow-lg transition-transform duration-300 z-[999999] rounded-t-lg",
          open ? "translate-y-0" : "translate-y-full"
        )}
        ref={ref}
      >
        <div className="h-full flex items-center justify-center">
          Click outside
        </div>
      </div>
    </>
  );
}

More by tigran tumasov

View all
  • Product Hunt Badge

    Product Hunt Badge

    1.9K
  • Book

    Book

    165
  • Calendar

    Calendar

    154
  • Theme Switcher

    Theme Switcher

    136
  • Snippet

    Snippet

    124
  • Table

    Table

    123
  • Toggle

    Toggle

    91
  • Feedback

    Feedback

    90

More in Buttons

View all
  • Button

    Button

    5
  • Texture Button

    Texture Button

    97
  • Button Group

    Button Group

  • Eclipse Button

    Eclipse Button

    39
  • Button

    Button

    4
  • HeroUI Button Group

    HeroUI Button Group

  • Base Button

    Base Button

    4
  • App Store Button

    App Store Button

    458