Skip to content

Commit 3e09b5c

Browse files
authored
feat: ability to specify id for container (#489)
1 parent dcc0c74 commit 3e09b5c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

react-responsive-modal/__tests__/index.test.tsx

+30
Original file line numberDiff line numberDiff line change
@@ -502,4 +502,34 @@ describe('modal', () => {
502502
expect(onAnimationEnd).toHaveBeenCalledTimes(1);
503503
});
504504
});
505+
506+
describe('prop: containerId', () => {
507+
it('should renders container div with id', async () => {
508+
const containerId = 'container-id';
509+
const { getByTestId } = render(
510+
<Modal open onClose={() => null} containerId={containerId}>
511+
<div>modal content</div>
512+
</Modal>
513+
);
514+
515+
const containerModal = getByTestId('modal-container');
516+
expect(containerModal.getAttribute('id')).toBe(containerId);
517+
expect(document.getElementById(containerId)).toBeInTheDocument();
518+
});
519+
});
520+
521+
describe('prop: modalId', () => {
522+
it('should renders modal div with id', async () => {
523+
const modalId = 'modal-id';
524+
const { getByTestId } = render(
525+
<Modal open onClose={() => null} modalId={modalId}>
526+
<div>modal content</div>
527+
</Modal>
528+
);
529+
530+
const modal = getByTestId('modal');
531+
expect(modal.getAttribute('id')).toBe(modalId);
532+
expect(document.getElementById(modalId)).toBeInTheDocument();
533+
});
534+
});
505535
});

react-responsive-modal/src/index.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,13 @@ export interface ModalProps {
129129
*/
130130
ariaDescribedby?: string;
131131
/**
132-
* Avoid unpleasant flickering effect when body overflow is hidden. For more information see https://www.npmjs.com/package/body-scroll-lock
132+
* Avoid unpleasant flickering effect when body overflow is hidden. For more information see https://www.npmjs.com/package/body-scroll-lock
133133
*/
134134
reserveScrollBarGap?: boolean;
135+
/**
136+
* id attribute for modal container
137+
*/
138+
containerId?: string;
135139
/**
136140
* id attribute for modal
137141
*/
@@ -177,6 +181,7 @@ export const Modal = React.forwardRef(
177181
role = 'dialog',
178182
ariaDescribedby,
179183
ariaLabelledby,
184+
containerId,
180185
modalId,
181186
onClose,
182187
onEscKeyDown,
@@ -322,6 +327,7 @@ export const Modal = React.forwardRef(
322327
/>
323328
<div
324329
ref={refModal}
330+
id={containerId}
325331
className={cx(
326332
classes.modalContainer,
327333
center && classes.modalContainerCenter,

0 commit comments

Comments
 (0)