@@ -4,7 +4,7 @@ import PropTypes from 'prop-types'
4
4
import classNames from 'classnames'
5
5
import { CSSTransition } from 'react-transition-group'
6
6
7
- import { CModalBackdrop } from './CModalBackdrop '
7
+ import { CBackdrop } from '../backdrop/CBackdrop '
8
8
import { CModalContent } from './CModalContent'
9
9
import { CModalDialog } from './CModalDialog'
10
10
@@ -15,6 +15,11 @@ export interface CModalProps extends HTMLAttributes<HTMLDivElement> {
15
15
* @default 'top'
16
16
*/
17
17
alignment ?: 'top' | 'center'
18
+ /**
19
+ * Apply a backdrop on body while modal is open. [docs]
20
+ * @default true
21
+ */
22
+ backdrop ?: boolean
18
23
/**
19
24
* A string of all className you want applied to the base component. [docs]
20
25
*/
@@ -29,9 +34,18 @@ export interface CModalProps extends HTMLAttributes<HTMLDivElement> {
29
34
*/
30
35
fullscreen ?: boolean | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'
31
36
/**
32
- * TODO: Does the modal dialog itself scroll, or does the whole dialog scroll within the window. [docs]
37
+ * Closes the modal when escape key is pressed. [docs]
38
+ * @default true
39
+ */
40
+ keyboard ?: boolean
41
+ /**
42
+ * Method called before the dissmiss animation has started. [docs]
33
43
*/
34
44
onDismiss ?: ( ) => void
45
+ /**
46
+ * TODO: [docs]
47
+ */
48
+ portal ?: boolean
35
49
/**
36
50
* Create a scrollable modal that allows scrolling the modal body. [docs]
37
51
*/
@@ -53,10 +67,13 @@ export interface CModalProps extends HTMLAttributes<HTMLDivElement> {
53
67
export const CModal : FC < CModalProps > = ( {
54
68
children,
55
69
alignment,
70
+ backdrop = true ,
56
71
className,
57
72
duration = 150 ,
58
73
fullscreen,
74
+ keyboard = true ,
59
75
onDismiss,
76
+ portal = true ,
60
77
scrollable,
61
78
size,
62
79
transition = true ,
@@ -112,7 +129,7 @@ export const CModal: FC<CModalProps> = ({
112
129
113
130
const handleKeyDown = useCallback (
114
131
( event ) => {
115
- if ( event . key === 'Escape' ) {
132
+ if ( event . key === 'Escape' && keyboard ) {
116
133
return handleDismiss ( )
117
134
}
118
135
} ,
@@ -121,28 +138,30 @@ export const CModal: FC<CModalProps> = ({
121
138
122
139
const modal = ( ref ?: React . Ref < HTMLDivElement > , transitionClass ?: string ) => {
123
140
return (
124
- < div
125
- className = { classNames ( _className , transitionClass ) }
126
- tabIndex = { - 1 }
127
- role = "dialog"
128
- ref = { ref }
129
- >
130
- < CModalDialog
131
- alignment = { alignment }
132
- fullscreen = { fullscreen }
133
- scrollable = { scrollable }
134
- size = { size }
135
- onClick = { ( event ) => event . stopPropagation ( ) }
141
+ < >
142
+ < div
143
+ className = { classNames ( _className , transitionClass ) }
144
+ tabIndex = { - 1 }
145
+ role = "dialog"
146
+ ref = { ref }
136
147
>
137
- < CModalContent > { children } </ CModalContent >
138
- </ CModalDialog >
139
- </ div >
148
+ < CModalDialog
149
+ alignment = { alignment }
150
+ fullscreen = { fullscreen }
151
+ scrollable = { scrollable }
152
+ size = { size }
153
+ onClick = { ( event ) => event . stopPropagation ( ) }
154
+ >
155
+ < CModalContent > { children } </ CModalContent >
156
+ </ CModalDialog >
157
+ </ div >
158
+ { backdrop && < CBackdrop visible = { visible } /> }
159
+ </ >
140
160
)
141
161
}
142
162
143
163
return (
144
164
< div onClick = { handleDismiss } onKeyDown = { handleKeyDown } >
145
- { visible && < CModalBackdrop className = "show" /> }
146
165
< CSSTransition
147
166
in = { visible }
148
167
timeout = { ! transition ? 0 : duration }
@@ -152,8 +171,8 @@ export const CModal: FC<CModalProps> = ({
152
171
>
153
172
{ ( state ) => {
154
173
const transitionClass = getTransitionClass ( state )
155
- return typeof window
156
- ? 'undefined' && createPortal ( modal ( ref , transitionClass ) , document . body )
174
+ return typeof window !== 'undefined' && portal
175
+ ? createPortal ( modal ( ref , transitionClass ) , document . body )
157
176
: modal ( ref , transitionClass )
158
177
} }
159
178
</ CSSTransition >
0 commit comments