ReactBootstrap popover dismiss on click outside
For React Bootstrap 4.4 it's necessary to add a onHide
function alongside rootClose
, also these properties are for Overlay
Component (not OverlayTrigger
).
Here is an example:
function Example() {
const [show, setShow] = useState(false);
const target = useRef(null);
const handleClick = (event) => {
setShow(!show);
};
return (
<div ref={ref}>
<Button onClick={handleClick} ref={target}>Holy guacamole!</Button>
<Overlay
show={show}
target={target.current}
placement="bottom"
rootClose
onHide={() => setShow(false)}
>
<Popover id="popover-contained">
<Popover.Title as="h3">Popover bottom</Popover.Title>
<Popover.Content>
<strong>Holy guacamole!</strong> Check this info.
</Popover.Content>
</Popover>
</Overlay>
</div>
);
}
render(<Example />);
No you don't need any custom code. Just include rootClose
prop and this will do for you. Its in the react bootstrap official documentation https://react-bootstrap.netlify.com/components/overlays/#overlays-api
<OverlayTrigger trigger='click' rootClose>
....
</OverlayTrigger>
I think this should work for you:
const Hello = () => (
<ReactBootstrap.OverlayTrigger
trigger="focus"
placement="bottom"
overlay={
<ReactBootstrap.Popover title="Popover bottom">
<strong>Holy guacamole!</strong> Check this info.
</ReactBootstrap.Popover>
}
>
<ReactBootstrap.Button bsStyle="default">Holy guacamole!</ReactBootstrap.Button>
</ReactBootstrap.OverlayTrigger>
);
ReactDOM.render(<Hello />, document.getElementById('app'));
Here is jsfiddle