Skip to content

Commit

Permalink
feat: update modal component
Browse files Browse the repository at this point in the history
- show modal overlay with pointer cursor when clickable
- increase spacing
- add internal & external open states, allowing to be uncontrolled
- add open & close handlers
  • Loading branch information
abelflopes committed Dec 21, 2023
1 parent ba355c3 commit 4b4787d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
72 changes: 45 additions & 27 deletions packages/components/modal/src/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import classNames from "classnames";
import styles from "./styles/index.module.scss";
import React, { useCallback, useMemo, useState } from "react";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { ModalContext, type ModalContextProps, type ModalContextValue } from "./context";
import { Card } from "@react-ck/card";
import { Overlay } from "@react-ck/overlay";
Expand Down Expand Up @@ -40,33 +40,51 @@ export const Modal = ({
[setContextValue],
);

// Invoke open / close handlers
useEffect(() => {
if (internalOpen) onOpen?.();
else onClose?.();
}, [internalOpen, onClose, onOpen]);

// Synchronize internal state with external state
useEffect(() => {
setInternalOpen(open);
}, [open]);

return (
<Layer elevation="overlay">
<div {...otherProps} className={classNames(styles.root, className)}>
<Overlay />
<Card className={styles.card}>
<ModalContext.Provider value={contextProps}>
{props.header && (
<header
{...props.header}
className={classNames(styles.header, props.header.className)}>
<Text type="h3" as="p" margin={false}>
{props.header.heading}
</Text>
internalOpen && (
<Layer elevation="overlay">
<div {...otherProps} className={classNames(styles.root, className)}>
<Overlay
className={classNames(dismissable && styles.clickable_overlay)}
onClick={handleClose}
/>
<Card className={styles.card}>
<ModalContext.Provider value={contextProps}>
{props.header && (
<header
{...props.header}
className={classNames(styles.header, props.header.className)}>
<Text type="h3" as="p" margin={false}>
{props.header.heading}
</Text>

<Button skin="ghost" icon={<Icon name="close" />} />
</header>
)}
<main className={styles.content}>{children}</main>
{props.footer && (
<footer
{...props.footer}
className={classNames(styles.footer, props.footer.className)}
/>
)}
</ModalContext.Provider>
</Card>
</div>
</Layer>
{dismissable && (
<Button skin="ghost" icon={<Icon name="close" />} onClick={handleClose} />
)}
</header>
)}
<main className={styles.content}>{children}</main>
{props.footer && (
<footer
{...props.footer}
className={classNames(styles.footer, props.footer.className)}
/>
)}
</ModalContext.Provider>
</Card>
</div>
</Layer>
)
);
};
11 changes: 8 additions & 3 deletions packages/components/modal/src/styles/index.module.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@import "@react-ck/theme";

.root {
@include define-css-var(modal, spacing, get-spacing(4));
@include define-css-var(modal, spacing, get-spacing(5));

display: flex;
align-items: center;
Expand All @@ -13,6 +13,10 @@
width: 100%;
}

.clickable_overlay {
cursor: pointer;
}

.card {
position: relative;
}
Expand All @@ -22,12 +26,13 @@
align-items: center;
justify-content: space-between;
gap: get-css-var(modal, spacing);
padding-bottom: get-css-var(modal, spacing);
margin-bottom: get-css-var(modal, spacing);
}

.footer {
display: flex;
align-items: center;
justify-content: flex-end;
gap: get-css-var(modal, spacing);
padding-top: get-css-var(modal, spacing);
margin-top: get-css-var(modal, spacing);
}

0 comments on commit 4b4787d

Please sign in to comment.