Skip to content

Commit

Permalink
feat(Modal): wrap the overlay and modal inside a container, to allow …
Browse files Browse the repository at this point in the history
…portaling with layer provider to the container (#2703)
  • Loading branch information
YossiSaadi authored Jan 7, 2025
1 parent 7c7608d commit 6f4b9d5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 60 deletions.
83 changes: 43 additions & 40 deletions packages/core/src/components/Modal/Modal/Modal.module.scss
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
$modal-default-z-index: 10000;

.overlay {
.container {
position: fixed;
inset: 0;
background-color: var(--color-surface);
z-index: var(--monday-modal-z-index, $modal-default-z-index);
}

.modal {
--top-actions-spacing: var(--spacing-large);
--top-actions-width: var(--spacing-xl);
--modal-inline-padding: var(--spacing-xl);

position: fixed;
top: 50%;
left: 50%;
z-index: var(--monday-modal-z-index, $modal-default-z-index);

display: flex;
flex-direction: column;
width: var(--modal-width);
max-height: var(--modal-max-height);
background-color: var(--primary-background-color);
overflow: hidden;
border-radius: var(--border-radius-big);
box-shadow: var(--box-shadow-large);

&.withHeaderAction {
--top-actions-width: calc(var(--spacing-xl) * 2);
}

&.sizeSmall {
--modal-max-height: 50%;
--modal-width: 480px;
}
z-index: var(--monday-modal-z-index, 10000);

&.sizeMedium {
--modal-max-height: 80%;
--modal-width: 580px;
.overlay {
position: fixed;
inset: 0;
height: 100%;
background-color: var(--color-surface);
}

&.sizeLarge {
--modal-max-height: 80%;
--modal-width: 840px;
.modal {
--top-actions-spacing: var(--spacing-large);
--top-actions-width: var(--spacing-xl);
--modal-inline-padding: var(--spacing-xl);

position: relative;
top: 50%;
left: 50%;

display: flex;
flex-direction: column;
width: var(--modal-width);
max-height: var(--modal-max-height);
background-color: var(--primary-background-color);
overflow: hidden;
border-radius: var(--border-radius-big);
box-shadow: var(--box-shadow-large);

&.withHeaderAction {
--top-actions-width: calc(var(--spacing-xl) * 2);
}

&.sizeSmall {
--modal-max-height: 50%;
--modal-width: 480px;
}

&.sizeMedium {
--modal-max-height: 80%;
--modal-width: 580px;
}

&.sizeLarge {
--modal-max-height: 80%;
--modal-width: 840px;
}
}
}
37 changes: 17 additions & 20 deletions packages/core/src/components/Modal/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const Modal = forwardRef(

const modalRef = useRef<HTMLDivElement>(null);
const modalMergedRef = useMergeRef<HTMLDivElement>(ref, modalRef);
const overlayRef = useRef<HTMLDivElement>(null);
const containerRef = useRef<HTMLDivElement>(null);

const [titleId, setTitleId] = useState<string>();
const [descriptionId, setDescriptionId] = useState<string>();
Expand Down Expand Up @@ -103,28 +103,25 @@ const Modal = forwardRef(
: modalAnimationCenterPopVariants;

const zIndexStyle = zIndex ? ({ "--monday-modal-z-index": zIndex } as React.CSSProperties) : {};
const modalStyle = { ...zIndexStyle, ...style };

return (
<AnimatePresence>
{show && (
<LayerProvider layerRef={overlayRef}>
<LayerProvider layerRef={containerRef}>
<ModalProvider value={contextValue}>
{createPortal(
<>
<motion.div
ref={overlayRef}
variants={modalAnimationOverlayVariants}
initial="initial"
animate="enter"
exit="exit"
data-testid={getTestId(ComponentDefaultTestId.MODAL_NEXT_OVERLAY, id)}
className={styles.overlay}
onClick={onBackdropClick}
aria-hidden
style={zIndexStyle}
/>
<FocusLockComponent returnFocus>
<FocusLockComponent returnFocus>
<div ref={containerRef} className={styles.container} style={zIndexStyle}>
<motion.div
variants={modalAnimationOverlayVariants}
initial="initial"
animate="enter"
exit="exit"
data-testid={getTestId(ComponentDefaultTestId.MODAL_NEXT_OVERLAY, id)}
className={styles.overlay}
onClick={onBackdropClick}
aria-hidden
/>
<RemoveScroll forwardProps ref={modalMergedRef}>
<motion.div
variants={modalAnimationVariants}
Expand All @@ -144,7 +141,7 @@ const Modal = forwardRef(
aria-modal
aria-labelledby={ariaLabelledby || titleId}
aria-describedby={ariaDescribedby || descriptionId}
style={modalStyle}
style={style}
onKeyDown={onModalKeyDown}
tabIndex={-1}
>
Expand All @@ -157,8 +154,8 @@ const Modal = forwardRef(
/>
</motion.div>
</RemoveScroll>
</FocusLockComponent>
</>,
</div>
</FocusLockComponent>,
portalTargetElement
)}
</ModalProvider>
Expand Down

0 comments on commit 6f4b9d5

Please sign in to comment.