nygardk / react-share

Social media share buttons and share counts for React
MIT License
2.63k stars 433 forks source link

Manually call onClick Event for share buttons #416

Open ndueber opened 3 years ago

ndueber commented 3 years ago

This is a potentially great improvement. It would be nice to pass in a prop like "autoOpen={true}" to a share button and for it to trigger the openShareDialog.

The reason this is nice is because i am using MUI and there is a parent component that wont allow the button to expand. I still want this parent element to call the openShareDialog... so I found this work around

const myRef = useRef(null);

  const clickElement = (ref) => {
    ref.current.dispatchEvent(
      new MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true,
        buttons: 1,
      }),
    );
  };
<ListItem onClick={() => clickElement(myRef)}>
    <FacebookShareButton ref={myRef}>
    </FacebookShareButton>
</ListItem >