Develop ultra fast with Prismane 🎉

useOutsideClick

useOutsideClick hook provides a simple way to handle a click out of the element's constraints.

Import

import { useOutsideClick } from "@prismane/core/hooks";

Usage

function Demo() {
  const ref = useRef(null);

  const [clicked, setClicked] = useState(false);

  useOutsideClick(ref, () => {
    setClicked(true);
  });

  return (
    <Stack>
      <Box w={fr(30)} h={fr(30)} bg="primary" ref={ref} />
      <Text
        cl={(theme) => (theme.mode === "dark" ? ["base", 200] : ["base", 700])}
      >
        Clicked outside: {clicked.toString()}
      </Text>
    </Stack>
  );
}

API

Parameters

NameTypeDescriptionDefault
refRefObject<any>The ref of the element that will be tracked.-
callback() => voidThe function that will be called when the event occurs.-