Skip to content

Commit 8730020

Browse files
committed
feat(CModal): add the container property to allow appending the modal to a specific element
1 parent f5d53a8 commit 8730020

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

packages/coreui-react/src/components/modal/CModal.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ export interface CModalProps extends HTMLAttributes<HTMLDivElement> {
3131
* A string of all className you want applied to the base component.
3232
*/
3333
className?: string
34+
/**
35+
* Appends the react modal to a specific element. You can pass an HTML element or function that returns a single element. By default `document.body`.
36+
*
37+
* @since 5.3.0
38+
*/
39+
container?: DocumentFragment | Element | (() => DocumentFragment | Element | null) | null
3440
/**
3541
* @ignore
3642
*/
3743
duration?: number
3844
/**
3945
* Puts the focus on the modal when shown.
4046
*
41-
* @since v4.10.0
47+
* @since 4.10.0
4248
*/
4349
focus?: boolean
4450
/**
@@ -101,6 +107,7 @@ export const CModal = forwardRef<HTMLDivElement, CModalProps>(
101107
alignment,
102108
backdrop = true,
103109
className,
110+
container,
104111
duration = 150,
105112
focus = true,
106113
fullscreen,
@@ -223,7 +230,7 @@ export const CModal = forwardRef<HTMLDivElement, CModalProps>(
223230
timeout={transition ? duration : 0}
224231
>
225232
{(state) => (
226-
<CConditionalPortal portal={portal}>
233+
<CConditionalPortal container={container} portal={portal}>
227234
<CModalContext.Provider value={contextValues}>
228235
<div
229236
className={classNames(
@@ -259,7 +266,7 @@ export const CModal = forwardRef<HTMLDivElement, CModalProps>(
259266
)}
260267
</Transition>
261268
{backdrop && (
262-
<CConditionalPortal portal={portal}>
269+
<CConditionalPortal container={container} portal={portal}>
263270
<CBackdrop visible={_visible} />
264271
</CConditionalPortal>
265272
)}
@@ -273,6 +280,7 @@ CModal.propTypes = {
273280
backdrop: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf<'static'>(['static'])]),
274281
children: PropTypes.node,
275282
className: PropTypes.string,
283+
container: PropTypes.any, // HTMLElement
276284
duration: PropTypes.number,
277285
focus: PropTypes.bool,
278286
fullscreen: PropTypes.oneOfType([

packages/docs/content/api/CModal.api.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import CModal from '@coreui/react/src/components/modal/CModal'
1010
| **alignment** | Align the modal in the center or top of the screen. | `'top'` \| `'center'` | - |
1111
| **backdrop** | Apply a backdrop on body while modal is open. | `boolean` \| `'static'` | true |
1212
| **className** | A string of all className you want applied to the base component. | `string` | - |
13-
| **focus** **_v4.10.0+_** | Puts the focus on the modal when shown. | `boolean` | true |
13+
| **container** **_5.3.0+_** | Appends the react modal to a specific element. You can pass an HTML element or function that returns a single element. By default `document.body`. | `Element` \| `DocumentFragment` \| `(() => Element` \| `DocumentFragment)` | - |
14+
| **focus** **_4.10.0+_** | Puts the focus on the modal when shown. | `boolean` | true |
1415
| **fullscreen** | Set modal to covers the entire user viewport. | `boolean` \| `'sm'` \| `'md'` \| `'lg'` \| `'xl'` \| `'xxl'` | - |
1516
| **keyboard** | Closes the modal when escape key is pressed. | `boolean` | true |
1617
| **onClose** | Callback fired when the component requests to be closed. | `() => void` | - |

0 commit comments

Comments
 (0)