Skip to content

Instantly share code, notes, and snippets.

View roykho's full-sized avatar
👋
Focusing

Roy Ho roykho

👋
Focusing
View GitHub Profile
@roykho
roykho / gist:738d7745e562cf14558ee44f9eb69282
Created August 3, 2025 15:19
React - Handy click outside container handler
import { useEffect, useRef } from 'react';
const useClickOutside = (callback: () => void) => {
const ref = useRef<HTMLElement>(null);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
// If the ref exists and the clicked element is not inside the ref's element
if (ref.current && !ref.current.contains(event.target as Node)) {
callback(); // Execute the provided callback function