Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OverlayTrigger on custom component not working

I'm trying to show a Popover when a custom component (a button) is hovered:

class MyComponent extends React.PureComponent<MyComponentProperties>  {
    public render(): JSX.Element {
        const overlay: JSX.Element = (
            <Popover id={this.uuid}>
                <Popover.Content>
                    Hello world!
                </Popover.Content>
            </Popover>
        );

        return <OverlayTrigger trigger="hover" placement="auto" delay={{show : 700, hide : 0}} overlay={overlay}>
            <MyFancyButton ... />
        </OverlayTrigger >
    }
}
class MyFancyButton extends React.PureComponent<MyFancyButtonProperties> {
    public render(): JSX.Element {
        return <button ...>Hover me!</button>
    }
}

The Popover doesn't show. However, it works if I change the return of the render function of MyComponent to:

return <OverlayTrigger trigger="hover" placement="auto" delay={{show : 700, hide : 0}} overlay={overlay}>
    <button>Hover me!</button>
</OverlayTrigger >

I checked https://react-bootstrap.github.io/components/overlays/#overlay-trigger and it says:

Note that triggering components must be able to accept a ref since will attempt to add one. You can use forwardRef() for function components.

I'm not sure how to do this. I tried adding a ref property in MyFancyButtonProperties and forwarding it to button, but it doesn't work:

class MyFancyButton extends React.PureComponent<MyFancyButtonProperties> {
    public render(): JSX.Element {
        return <button ref={this.props.ref} ...>Hover me!</button>
    }
}

My versions:

  • React 16.8.6
  • React bootstrap 1.0.0-beta.14
like image 225
Simon Arsenault Avatar asked Apr 16 '26 18:04

Simon Arsenault


2 Answers

I finally found a solution here: https://github.com/react-bootstrap/react-bootstrap/issues/2208#issuecomment-301737531

I wrapped the component, which should get a tooltip, with a <span> in the render function of MyComponent to

return <OverlayTrigger trigger="hover" placement="auto" delay={{show : 700, hide : 0}} overlay={overlay}>
    <span><MyFancyButton ... /></span>
</OverlayTrigger >

and now it works.

like image 187
Simon Arsenault Avatar answered Apr 18 '26 15:04

Simon Arsenault


Your custom component needs to pass the event handler props through.

const FancyButton = ({ children, ...props }) => (
  <Button {...props}>
    {children}
  </Button>
);

Source: https://github.com/react-bootstrap/react-bootstrap/issues/2208

like image 36
Geoffrey Hale Avatar answered Apr 18 '26 14:04

Geoffrey Hale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!