Dialog.jsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { Overlay } from '../overlay/Overlay.jsx';
  2. import style from './dialog.css';
  3. function Dialog ({closeOnEsc = true}, children) {
  4. function createHandlers (closeDialog) {
  5. function handleKeyDown ({which}) {
  6. (which === 27) && closeDialog();
  7. }
  8. return {
  9. onCreate () {
  10. document.addEventListener('keydown', handleKeyDown, true);
  11. },
  12. onRemove (element, done) {
  13. document.removeEventListener('keydown', handleKeyDown, true);
  14. done();
  15. }
  16. };
  17. }
  18. return function (state, actions) {
  19. if (!state.dialog.isOpen) {
  20. return null;
  21. }
  22. const closeDialog = actions.dialog.close;
  23. const elementHandlers = closeOnEsc ? createHandlers(closeDialog) : {};
  24. const {onCreate, onRemove} = elementHandlers;
  25. return (
  26. <Overlay oncreate={onCreate} onremove={onRemove} onBackgroundClick={closeDialog}>
  27. <button className={style.close} onclick={closeDialog} />
  28. <div className={style.content}>
  29. {children}
  30. </div>
  31. </Overlay>
  32. );
  33. };
  34. }
  35. export {
  36. Dialog
  37. };