GA-MO / react-confirm-alert

react component confirm dialog.
https://ga-mo.github.io/react-confirm-alert/demo/
MIT License
272 stars 105 forks source link

Please export the static close method #5

Closed jcalfee closed 6 years ago

jcalfee commented 6 years ago

https://github.com/GA-MO/react-confirm-alert/blob/master/src/index.js#L37-L43

This would be useful in a react dumb component (without state) where confirmAlert was used to show the dialog.

jcalfee commented 6 years ago

Be aware that if the dialog is already close the method should probably not error. I would recommend to return true if you found the root node and closed it but false if the dialog was not found in the DOM.

function reactConfirmClose() {
  const target = document.getElementById('react-confirm-alert');
  if(!target) {
    return false
  }
  const svg = document.getElementById('react-confirm-alert-firm-svg');
  target.parentNode.removeChild(target);
  svg.parentNode.removeChild(svg);
  const root = document.body.children[0];
  root.classList.remove('react-confirm-alert-blur');
  return true
}
jcalfee commented 6 years ago

I believe this close logic is denying childrenElement their react life cycle events.

If I use an embedded QrReader (react-qr-reader) this close method leaves the camera on. If include the QrReader directly in render then the component gets removed via a higher level react condition, the camera does turn off.

GA-MO commented 6 years ago

@jcalfee Can you please create PR to solve this?

For the close logic it should have props willClose prop type is a function to clear event listener or component that it wrapped.

GA-MO commented 6 years ago

Solve this in version 2 use option willUnmount