Skip to content

fix: Replace unsafe 'as any' cast with proper type narrowing in useOutsideClickCallback hook #1245

@sammy200-ui

Description

@sammy200-ui

The useOutsideClickCallback hook uses an unsafe as any type cast when checking if a click target is outside a referenced element. This bypasses TypeScript's type safety and could potentially hide runtime errors.

How to Reproduce

visit this file - apps/studio/src/helpers/useOutsideClickCallback.ts

export function useOutsideClickCallback(ref: MutableRefObject<HTMLElement | null>, callback: () => void) {
  useEffect(() => {
    function handleClickOutside(event: MouseEvent) {
      if (ref.current && !ref.current.contains(event.target as any)) {  
        callback();
      }
    }

    document.addEventListener('mousedown', handleClickOutside);
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, [ref]);
}

Expected behavior

MouseEvent.target has type EventTarget | null, but HTMLElement.contains() expects a Node | null parameter.

If event.target is somehow not a Node (e.g., in edge cases with SVG elements or shadow DOM), the contains() method could behave unexpectedly

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions