Skip to content

Commit 5359a54

Browse files
authored
feat(Modal): modal component with basic functionality (#2417)
1 parent 4999a84 commit 5359a54

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.overlay {
2+
position: fixed;
3+
inset: 0;
4+
background-color: rgba(41, 47, 76, 0.7);
5+
}
6+
7+
.modal {
8+
position: relative;
9+
display: flex;
10+
flex-direction: column;
11+
background-color: var(--primary-background-color);
12+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React, { forwardRef } from "react";
2+
import cx from "classnames";
3+
import { getTestId } from "../../../tests/test-ids-utils";
4+
import { ComponentDefaultTestId } from "../../../tests/constants";
5+
import styles from "./Modal.module.scss";
6+
import { ModalProps } from "./Modal.types";
7+
import ModalTopActions from "../ModalTopActions/ModalTopActions";
8+
9+
const Modal = forwardRef(
10+
(
11+
{
12+
// Would be implemented in a later PR
13+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
14+
show,
15+
// Would be implemented in a later PR
16+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
17+
size = "medium",
18+
renderHeaderAction,
19+
closeButtonTheme,
20+
closeButtonAriaLabel,
21+
onClose,
22+
children,
23+
className,
24+
id,
25+
"data-testid": dataTestId
26+
}: ModalProps,
27+
ref: React.ForwardedRef<HTMLDivElement>
28+
) => {
29+
return (
30+
<div id="overlay" className={styles.overlay}>
31+
<div
32+
ref={ref}
33+
className={cx(styles.modal, className)}
34+
id={id}
35+
data-testid={dataTestId || getTestId(ComponentDefaultTestId.MODAL, id)}
36+
role="dialog"
37+
aria-modal
38+
aria-labelledby="TODO" // TODO
39+
aria-describedby="TODO" // TODO
40+
>
41+
<ModalTopActions
42+
renderAction={renderHeaderAction}
43+
color={closeButtonTheme}
44+
closeButtonAriaLabel={closeButtonAriaLabel}
45+
onClose={onClose}
46+
/>
47+
{children}
48+
</div>
49+
</div>
50+
);
51+
}
52+
);
53+
54+
export default Modal;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { VibeComponentProps } from "../../../types";
2+
import React from "react";
3+
import { ModalTopActionsProps } from "../ModalTopActions/ModalTopActions.types";
4+
5+
export type ModalSize = "small" | "medium" | "large";
6+
7+
export interface ModalProps extends VibeComponentProps {
8+
show: boolean;
9+
size?: ModalSize;
10+
closeButtonTheme?: ModalTopActionsProps["color"];
11+
closeButtonAriaLabel?: ModalTopActionsProps["closeButtonAriaLabel"];
12+
onClose?: ModalTopActionsProps["onClose"];
13+
renderHeaderAction?: ModalTopActionsProps["renderAction"];
14+
children: React.ReactNode;
15+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from "react";
2+
import { render, fireEvent } from "@testing-library/react";
3+
import Modal from "../Modal";
4+
5+
describe("Modal", () => {
6+
const closeButtonAriaLabel = "Close modal";
7+
const childrenContent = <span>My content</span>;
8+
9+
it("renders the modal with the correct role", () => {
10+
const { getByTestId } = render(
11+
<Modal show data-testid="modal">
12+
{childrenContent}
13+
</Modal>
14+
);
15+
16+
expect(getByTestId("modal")).toHaveAttribute("role", "dialog");
17+
});
18+
19+
it("renders the modal with the correct aria-modal", () => {
20+
const { getByTestId } = render(
21+
<Modal show data-testid="modal">
22+
{childrenContent}
23+
</Modal>
24+
);
25+
26+
expect(getByTestId("modal")).toHaveAttribute("aria-modal", "true");
27+
});
28+
29+
it("renders the children content correctly", () => {
30+
const { getByText } = render(
31+
<Modal show closeButtonAriaLabel={closeButtonAriaLabel}>
32+
{childrenContent}
33+
</Modal>
34+
);
35+
36+
expect(getByText("My content")).toBeInTheDocument();
37+
});
38+
39+
it("calls onClose when the close button is clicked", () => {
40+
const mockOnClose = jest.fn();
41+
const { getByLabelText } = render(
42+
<Modal show onClose={mockOnClose} closeButtonAriaLabel={closeButtonAriaLabel}>
43+
{childrenContent}
44+
</Modal>
45+
);
46+
47+
fireEvent.click(getByLabelText(closeButtonAriaLabel));
48+
expect(mockOnClose).toHaveBeenCalled();
49+
});
50+
51+
it.todo("does not render when 'show' is false");
52+
53+
it.todo("renders the correct aria-labelledby");
54+
55+
it.todo("renders the correct aria-describedby");
56+
});

0 commit comments

Comments
 (0)