|
1 | 1 | import React, { FC, HTMLAttributes, useCallback, useLayoutEffect, useRef, useState } from 'react'
|
| 2 | +import { createPortal } from 'react-dom' |
2 | 3 | import PropTypes from 'prop-types'
|
3 | 4 | import classNames from 'classnames'
|
4 |
| -import { Transition } from 'react-transition-group' |
| 5 | +import { CSSTransition } from 'react-transition-group' |
5 | 6 |
|
6 | 7 | import { CModalBackdrop } from './CModalBackdrop'
|
7 | 8 | import { CModalContent } from './CModalContent'
|
@@ -88,7 +89,7 @@ export const CModal: FC<CModalProps> = ({
|
88 | 89 | 'modal',
|
89 | 90 | {
|
90 | 91 | 'modal-static': staticBackdrop,
|
91 |
| - 'fade': transition |
| 92 | + fade: transition, |
92 | 93 | },
|
93 | 94 | className,
|
94 | 95 | )
|
@@ -118,32 +119,44 @@ export const CModal: FC<CModalProps> = ({
|
118 | 119 | [ref, handleDismiss],
|
119 | 120 | )
|
120 | 121 |
|
| 122 | + const modal = (ref?: React.Ref<HTMLDivElement>, transitionClass?: string) => { |
| 123 | + 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()} |
| 136 | + > |
| 137 | + <CModalContent>{children}</CModalContent> |
| 138 | + </CModalDialog> |
| 139 | + </div> |
| 140 | + ) |
| 141 | + } |
| 142 | + |
121 | 143 | return (
|
122 | 144 | <div onClick={handleDismiss} onKeyDown={handleKeyDown}>
|
123 | 145 | {visible && <CModalBackdrop className="show" />}
|
124 |
| - <Transition in={visible} timeout={!transition ? 0 : duration} onExit={onDismiss}> |
| 146 | + <CSSTransition |
| 147 | + in={visible} |
| 148 | + timeout={!transition ? 0 : duration} |
| 149 | + onExit={onDismiss} |
| 150 | + mountOnEnter |
| 151 | + unmountOnExit |
| 152 | + > |
125 | 153 | {(state) => {
|
126 | 154 | const transitionClass = getTransitionClass(state)
|
127 |
| - return ( |
128 |
| - <div |
129 |
| - className={classNames(_className, transitionClass)} |
130 |
| - tabIndex={-1} |
131 |
| - role="dialog" |
132 |
| - ref={ref} |
133 |
| - > |
134 |
| - <CModalDialog |
135 |
| - alignment={alignment} |
136 |
| - fullscreen={fullscreen} |
137 |
| - scrollable={scrollable} |
138 |
| - size={size} |
139 |
| - onClick={(event) => event.stopPropagation()} |
140 |
| - > |
141 |
| - <CModalContent>{children}</CModalContent> |
142 |
| - </CModalDialog> |
143 |
| - </div> |
144 |
| - ) |
| 155 | + return typeof window |
| 156 | + ? 'undefined' && createPortal(modal(ref, transitionClass), document.body) |
| 157 | + : modal(ref, transitionClass) |
145 | 158 | }}
|
146 |
| - </Transition> |
| 159 | + </CSSTransition> |
147 | 160 | </div>
|
148 | 161 | )
|
149 | 162 | }
|
|
0 commit comments