#[Popover] How to close a controlled Popover when click outside?

18 messages · Page 1 of 1 (latest)

tacit field
#

Is there a way to assign a custom handler when click outside a controlled Popover?

const SimplePopOver: FunctionComponent<SimplePopOverProps> = ({
  defaultOpen = false,
  renderToggler,
  renderContent,
  ...props
}) => {
  const [isOpen, { toggle, open }] = useDisclosure(defaultOpen);
  return (
    <Popover opened={isOpen} {...props}>
      <Popover.Target>{renderToggler({ toggle, open })}</Popover.Target>
      <Popover.Dropdown>{renderContent({ toggle, open })}</Popover.Dropdown>
    </Popover>
  );
};
fervent viper
tacit field
fervent viper
#

I guess you can use clickOutsideEvents and fire a custom event.

fresh vault
tacit field
cosmic copper
#
const [opened, setOpened] = useState(false);
const ref = useClickOutside(() => setOpened(false));

<Popover opened={opened}>
  ...
  <Popover.Dropdown>
    <div ref={ref}>
cosmic copper
#

But as mentioned, you should use Hovercard so that it is accessible. Nvm, I'm wrong and going slightly insane

fresh vault
cosmic copper
fresh vault
#

Nice, I might need to look on HoverCard also 👀

cosmic copper
#

Sounds good. To clarify, Hovercard is NOT accessible while Popover is

tacit field
cosmic copper
# tacit field Honestly, I don't know how to use this prop. Do you have any examples?

I don't think you need that prop if I understand you correctly. The prop clickOutsideEvents allows you to filter which of mouseup or mousedown or pointerup or pointerdown hides the popover for an uncontrolled example.

For a controlled popover, you need to do something like in my code snippet. useClickOutside is a hook provided by the @mantine/hooks package.

analog iris
#

Which won't enable you to close the popover by clicking the Target again

#

Which I don't think is a good UX