Skip to content

Commit

Permalink
Merge pull request #383 from ebs-integrator/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
marcellefter authored Mar 25, 2022
2 parents 807a674 + aba93fb commit d7890d0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ebs-design",
"version": "0.0.1-beta.125",
"version": "0.0.1-beta.126",
"description": "EBS Design System React UI elements.",
"author": "EBS Integrator <[email protected]> (https://ebs-integrator.com/)",
"maintainers": [
Expand Down
27 changes: 10 additions & 17 deletions src/components/organisms/Modal/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import cn from 'classnames';
import { usePortal, useScrollToggler } from 'hooks';
import { usePopupClose, usePortal, useScrollToggler } from 'hooks';
import { Mask, Button } from 'components/atoms';
import { ModalContent } from './ModalContent';
import { ModalFooter } from './ModalFooter';
Expand Down Expand Up @@ -38,6 +38,14 @@ const Modal: React.FC<ModalProps> = ({
const createPortal = usePortal('modal-portal');
useScrollToggler(open);

const overlayRef = React.useRef<HTMLDivElement>(null);
usePopupClose(overlayRef, () => {
if (closeOnClickOutside) {
props.onClose?.();
setOpen(false);
}
});

React.useEffect(() => setOpen(isOpen), [isOpen]);

React.useEffect(() => {
Expand All @@ -54,18 +62,6 @@ const Modal: React.FC<ModalProps> = ({
};
}, []);

const onClickOutside = (ev: React.MouseEvent<HTMLDivElement, MouseEvent>): void => {
const target = ev.target as HTMLDivElement;

if (target && target.classList.contains('ebs-modal__wrapper')) {
if (props.onClose !== undefined) {
props.onClose();
}

setOpen(false);
}
};

const showHeader = React.useMemo(() => header || title, [header, title]);

return (
Expand All @@ -75,10 +71,7 @@ const Modal: React.FC<ModalProps> = ({
<>
<Mask />

<div
className={cn(`ebs-modal__wrapper`, className)}
{...(mask ? { ...props, onClick: closeOnClickOutside ? onClickOutside : undefined } : props)}
>
<div className={cn(`ebs-modal__wrapper`, className)} ref={overlayRef} {...props}>
<div className={cn(`ebs-modal`, `ebs-modal__size--${size}`, { 'hide-header': !showHeader })}>
{showHeader ? (
<div className="ebs-modal__header">
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './useScrollToggler';
export * from './useNotify';
export * from './usePortal';
export * from './useTheme';
export * from './usePopupClose';
26 changes: 26 additions & 0 deletions src/hooks/usePopupClose.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { useRef } from 'react';

export function usePopupClose(overlayRef: React.RefObject<Element>, callback: () => void): void {
// Need a ref to callback so it calls the up to date argument
const callbackRef = useRef(callback);
callbackRef.current = callback;

React.useEffect(() => {
let mousedownTarget: EventTarget | null = null;

const mouseDownListener = (e: Event): void => {
mousedownTarget = e.target;
};
const mouseUpListener = (e: Event): void => {
if (e.target === overlayRef.current && mousedownTarget === e.target) callbackRef.current();
mousedownTarget = null;
};
document.addEventListener('mousedown', mouseDownListener);
document.addEventListener('mouseup', mouseUpListener);

return () => {
document.removeEventListener('mousedown', mouseDownListener);
document.removeEventListener('mouseup', mouseUpListener);
};
}, []);
}

0 comments on commit d7890d0

Please sign in to comment.