Skip to content

Commit 81d47be

Browse files
committed
refactor: update animations, add createPortal to render modal in good place
1 parent 8f6801c commit 81d47be

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

src/components/modal/CModal.tsx

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, { FC, HTMLAttributes, useCallback, useLayoutEffect, useRef, useState } from 'react'
2+
import { createPortal } from 'react-dom'
23
import PropTypes from 'prop-types'
34
import classNames from 'classnames'
4-
import { Transition } from 'react-transition-group'
5+
import { CSSTransition } from 'react-transition-group'
56

67
import { CModalBackdrop } from './CModalBackdrop'
78
import { CModalContent } from './CModalContent'
@@ -88,7 +89,7 @@ export const CModal: FC<CModalProps> = ({
8889
'modal',
8990
{
9091
'modal-static': staticBackdrop,
91-
'fade': transition
92+
fade: transition,
9293
},
9394
className,
9495
)
@@ -118,32 +119,44 @@ export const CModal: FC<CModalProps> = ({
118119
[ref, handleDismiss],
119120
)
120121

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+
121143
return (
122144
<div onClick={handleDismiss} onKeyDown={handleKeyDown}>
123145
{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+
>
125153
{(state) => {
126154
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)
145158
}}
146-
</Transition>
159+
</CSSTransition>
147160
</div>
148161
)
149162
}

src/components/modal/CModalBackdrop.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import React, { FC, HTMLAttributes } from 'react'
1+
// TODO: add smooth transition.
2+
3+
import React, { forwardRef, HTMLAttributes } from 'react'
4+
import { createPortal } from 'react-dom'
25
import PropTypes from 'prop-types'
36
import classNames from 'classnames'
47

@@ -9,18 +12,20 @@ export interface CModalBackdropProps extends HTMLAttributes<HTMLDivElement> {
912
className?: string
1013
}
1114

12-
export const CModalBackdrop: FC<CModalBackdropProps> = ({ children, className, ...rest }) => {
13-
const _className = classNames('modal-backdrop', className)
15+
export const CModalBackdrop = forwardRef<HTMLDivElement, CModalBackdropProps>(
16+
({ className, ...rest }, ref) => {
17+
const _className = classNames('modal-backdrop fade', className)
1418

15-
return (
16-
<div className={_className} {...rest}>
17-
{children}
18-
</div>
19-
)
20-
}
19+
const backdrop = (ref?: React.Ref<HTMLDivElement>) => {
20+
return (
21+
<div className={_className} {...rest} ref={ref} />
22+
)
23+
}
24+
25+
return typeof window ? 'undefined' && createPortal(backdrop(ref), document.body) : backdrop(ref)
26+
})
2127

2228
CModalBackdrop.propTypes = {
23-
children: PropTypes.node,
2429
className: PropTypes.string,
2530
}
2631

0 commit comments

Comments
 (0)