Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request - close popover when clicked inside #76

Open
ravijaiswal7 opened this issue Jul 29, 2020 · 1 comment
Open

request - close popover when clicked inside #76

ravijaiswal7 opened this issue Jul 29, 2020 · 1 comment

Comments

@ravijaiswal7
Copy link

Recently we came across a use case where i am using button inside the popover. I am using that button to duplicate a tab as it works in chrome. but the new tab which is created has popover in a open state. so i would request to have a functionality to close the popover on click inside the child component.

@ever-dev
Copy link

ever-dev commented Sep 7, 2020

You can try as follows

import Popover, { PopoverProps } from "react-tiny-popover";

interface ContentProps {
  closePopup: () => any;
}

interface ClickoverProps {
  Content: FunctionComponent<ContentProps>;
  hideOnOutsideClick?: boolean;
  align?: PopoverProps["align"];
  padding?: number;
  position?: PopoverProps["position"];
  onVisibleChange?: (changed: boolean) => any;
}

export const Clickover: FunctionComponent<ClickoverProps> = ({
  Content,
  position = "bottom",
  align = "end",
  padding = 0,
  hideOnOutsideClick = true,
  children,
  onVisibleChange
}) => {
  const [visible, setVisible] = useState(false);

  return (
    <Popover
      content={() => (
        <div css={styles.popup} onClick={e => e.stopPropagation()}>
          <Content
            closePopup={() => {
              setVisible(false);
              onVisibleChange && onVisibleChange(false);
            }}
          />
        </div>
      )}
      isOpen={visible}
      onClickOutside={() => {
        hideOnOutsideClick && setVisible(false);
        onVisibleChange && onVisibleChange(false);
      }}
      windowBorderPadding={40}
      padding={padding}
      position={position}
      align={align}
    >
      <div
        onClick={() => {
          onVisibleChange && onVisibleChange(!visible);
          setVisible(!visible);
        }}
      >
        {children}
      </div>
    </Popover>
  );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants