Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/ui/src/components/Modal/Modal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ describe("Components / Modal", () => {
expect(modal).not.toBeInTheDocument();
});

it("should not render close button when modal is not dismissible", async () => {
const user = userEvent.setup();

render(<TestModal />);

await user.click(triggerButton());

const modal = dialog();
expect(modal).toBeInTheDocument();

const closeButton = screen.queryByLabelText("Close");
expect(closeButton).not.toBeInTheDocument();
});

it("should render close button when modal is dismissible", async () => {
const user = userEvent.setup();

render(<TestModal dismissible />);

await user.click(triggerButton());

const modal = dialog();
expect(modal).toBeInTheDocument();

const closeButton = screen.queryByLabelText("Close");
expect(closeButton).toBeInTheDocument();
});

it("should append to root element when root prop is provided", async () => {
const root = document.createElement("div");
const user = userEvent.setup();
Expand Down Expand Up @@ -82,7 +110,7 @@ describe("Components / Modal", () => {
it("should close `Modal` when `Space` is pressed on any of its buttons", async () => {
const user = userEvent.setup();

render(<TestModal />);
render(<TestModal dismissible />);

const openButton = triggerButton();

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export const Modal = forwardRef<HTMLDivElement, ModalProps>((props, ref) => {
clearTheme: props.clearTheme,
applyTheme: props.applyTheme,
popup,
dismissible,
onClose,
setHeaderId,
}}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/Modal/ModalContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ModalTheme } from "./Modal";

export interface ModalContextValue extends ThemingProps<ModalTheme> {
popup?: boolean;
dismissible?: boolean;
setHeaderId: (id: string | undefined) => void;
onClose?: () => void;
}
Expand Down
9 changes: 6 additions & 3 deletions packages/ui/src/components/Modal/ModalHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const ModalHeader = forwardRef<HTMLDivElement, ModalHeaderProps>((props,
clearTheme: rootClearTheme,
applyTheme: rootApplyTheme,
popup,
dismissible,
onClose,
setHeaderId,
} = useModalContext();
Expand Down Expand Up @@ -64,9 +65,11 @@ export const ModalHeader = forwardRef<HTMLDivElement, ModalHeaderProps>((props,
<Component id={headerId} className={theme.title}>
{children}
</Component>
<button aria-label="Close" className={theme.close.base} type="button" onClick={onClose}>
<OutlineXIcon aria-hidden className={theme.close.icon} />
</button>
{dismissible && (
<button aria-label="Close" className={theme.close.base} type="button" onClick={onClose}>
<OutlineXIcon aria-hidden className={theme.close.icon} />
</button>
)}
</div>
);
});
Expand Down
Loading