Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modal dismiss #246

Merged
merged 13 commits into from
Dec 18, 2024
6 changes: 6 additions & 0 deletions .yarn/versions/261792a0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
releases:
"@nimbus-ds/components": patch
"@nimbus-ds/modal": patch

declined:
- nimbus-design-system
7 changes: 7 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Nimbus is an open-source Design System created by Tiendanube / Nuvesmhop’s team to empower and enhance more stories every day, with simplicity, accessibility, consistency and performance.

## 2024-08-05 `5.5.5`

### πŸ› Bug fixes

- Made `onDismiss` property optional for `Modal` component. If `onDismiss` is not provided, the modal can no longer be closed by clicking outside or pressing the close button
- Removed the close button (X) from `Modal` component when `onDismiss` is not provided. ([#246](https://github.com/TiendaNube/nimbus-design-system/pull/246) by [@dommirr](https://github.com/dommirr))

## 2024-04-22 `5.5.4`

### πŸ’‘ Others
Expand Down
5 changes: 3 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nimbus-ds/components",
"version": "5.5.4",
"version": "5.5.5-rc.1",
"license": "MIT",
"main": "dist/index.js",
"files": [
Expand Down Expand Up @@ -31,5 +31,6 @@
},
"devDependencies": {
"@nimbus-ds/webpack": "workspace:^"
}
},
"stableVersion": "5.5.4"
}
7 changes: 7 additions & 0 deletions packages/react/src/composite/Modal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

The Modal component allows us to call the user's attention to a floating box that can have text, actions or forms to perform tasks by changing the focus from the background. It is an intrusive component as it interrupts the user's operation to present a message or content.

## 2024-08-05 `1.5.3`

### πŸ› Bug fixes

- Made `onDismiss` property optional for `Modal` component. If `onDismiss` is not provided, the modal can no longer be closed by clicking outside or pressing the close button
- Removed the close button (X) from `Modal` component when `onDismiss` is not provided. ([#246](https://github.com/TiendaNube/nimbus-design-system/pull/246) by [@dommirr](https://github.com/dommirr))

## 2023-12-22 `1.5.1`

#### πŸŽ‰ New features
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/composite/Modal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nimbus-ds/modal",
"version": "1.5.2",
"version": "1.5.3-rc.1",
"license": "MIT",
"main": "dist/index.js",
"files": [
Expand Down Expand Up @@ -36,5 +36,6 @@
"@nimbus-ds/text": "workspace:^",
"@nimbus-ds/title": "workspace:^",
"@nimbus-ds/webpack": "workspace:^"
}
},
"stableVersion": "1.5.2"
}
4 changes: 3 additions & 1 deletion packages/react/src/composite/Modal/src/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ const Modal: React.FC<ModalProps> & ModalComponents = ({

const click = useClick(context);
const role = useRole(context);
const dismiss = useDismiss(context, { outsidePressEvent: "mousedown" });
const dismiss = useDismiss(context, {
outsidePressEvent: onDismiss ? "mousedown" : undefined,
});

const { getFloatingProps } = useInteractions([click, role, dismiss]);

Expand Down
26 changes: 16 additions & 10 deletions packages/react/src/composite/Modal/src/modal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ import { ModalProps } from "./modal.types";

const mockedOnDismiss = jest.fn();

const makeSut = (rest: Pick<ModalProps, "children" | "padding">) => {
render(
<Modal
{...rest}
onDismiss={mockedOnDismiss}
open
data-testid="modal-element"
/>
);
const makeSut = (
rest: Pick<ModalProps, "children" | "padding" | "onDismiss">
) => {
render(<Modal {...rest} open data-testid="modal-element" />);
};

describe("GIVEN <Modal />", () => {
Expand All @@ -25,10 +20,21 @@ describe("GIVEN <Modal />", () => {
});

it("AND should correctly call the onDismiss function when closing the modal", () => {
makeSut({ children: <div>My content</div> });
makeSut({ children: <div>My content</div>, onDismiss: mockedOnDismiss });
fireEvent.click(screen.getByTestId("dismiss-modal-button"));
expect(mockedOnDismiss).toBeCalledWith(false);
});

it("THEN should not close the modal if the close function is not provided", () => {
makeSut({ children: <div>My content</div> });
fireEvent.keyDown(document, {
key: "Escape",
code: "Escape",
keyCode: 27,
});
expect(screen.queryByTestId("dismiss-modal-button")).toBeNull();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no me quedΓ³ claro la ejecuciΓ³n de esta prueba, si en el modal que no tiene el dismiss ejecutamos Escape cuando vamos a verificar el elemento con el testId dismiss-modal-button no deberiΓ‘ existir? .. Eso quiere decir que con el Escape se estarΓ­a cerrando? πŸ€”

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hola @harrytiendanube, gracias por la observaciΓ³n. En realidad el botΓ³n no se renderiza nunca, este evento se colΓ³ haciendo algunas pruebas. Ahora lo saco

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harrytiendanube ahΓ­ subΓ­ los cambios en este commit. No saquΓ© el evento, sino que agreguΓ© un assert para determinar que el modal quedΓ³ abierto

expect(screen.getByText("My content")).toBeDefined();
});
});

describe("THEN should correctly render the submitted padding", () => {
Expand Down
31 changes: 31 additions & 0 deletions packages/react/src/composite/Modal/src/modal.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,34 @@ export const skeleton: Story = {
),
},
};

export const noDismiss: Story = {
render: (args: ModalProps) => {
const [open, setOpen] = useState(false);
const handleClose = () => setOpen((prevState) => !prevState);
return (
<>
<Button onClick={handleClose}>Open</Button>
<Modal {...args} open={open} />
</>
);
},
args: {
onDismiss: undefined,
children: (
<>
<Modal.Header title="Undismissable Modal" />
<Modal.Body padding="none">
<Text textAlign="left">
This modal cannot be dismissed by clicking the X icon or clicking
outside.
</Text>
</Modal.Body>
<Modal.Footer>
<Button appearance="neutral">Button</Button>
<Button appearance="primary">Button</Button>
</Modal.Footer>
</>
),
},
};
2 changes: 1 addition & 1 deletion packages/react/src/composite/Modal/src/modal.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface ModalProperties extends ModalSprinkle {
* Callback fired when the component requests to be closed.
* @TJS-type (open: boolean) => void;
*/
onDismiss: (open: boolean) => void;
onDismiss?: (open: boolean) => void;
/**
* Id to be embedded in the portal element
*/
Expand Down
Loading