Skip to content
This repository was archived by the owner on Jun 20, 2022. It is now read-only.

Commit 26c1d0c

Browse files
committed
fix(modal): fix default opened
1 parent 5984cc2 commit 26c1d0c

File tree

4 files changed

+143
-146
lines changed

4 files changed

+143
-146
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"classnames": "^2.2.5",
7676
"polished": "^1.9.2",
7777
"prop-types": "^15.6.1",
78+
"react-transition-group": "^2.3.1",
7879
"recompact": "^3.3.0"
7980
},
8081
"peerDependencies": {

src/Modal.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { createPortal } from 'react-dom'
44
import PropTypes from 'prop-types'
55
import styled from 'styled-components'
66
import classNames from 'classnames'
7+
import Transition from './Transition'
78
import * as defaultTheme from './theme/defaultTheme'
89
import { th } from './utils'
9-
import Transition from './Transition'
1010

1111
class ModalComponent extends React.Component {
1212
constructor(props) {
@@ -28,7 +28,7 @@ class ModalComponent extends React.Component {
2828
document.removeEventListener('keyup', this.handleKeyup)
2929
}
3030

31-
componentDidUpdate() {
31+
setupKeyHandling() {
3232
if (this.props.opened) {
3333
document.body.style.overflow = 'hidden'
3434
document.addEventListener('keyup', this.handleKeyup)
@@ -38,21 +38,31 @@ class ModalComponent extends React.Component {
3838
}
3939
}
4040

41+
componentDidMount() {
42+
this.setupKeyHandling()
43+
}
44+
45+
componentDidUpdate() {
46+
this.setupKeyHandling()
47+
}
48+
4149
render() {
4250
const { className, theme, opened, onClose, children, ...props } = this.props
4351
if (!this.container) return null
4452
return createPortal(
45-
<Transition ms={theme.modalTransitionDuration} toggle={this.props.opened}>
46-
{({ entering, exiting }) => (
53+
<Transition
54+
timeout={theme.modalTransitionDuration}
55+
in={this.props.opened}
56+
>
57+
{transitionState => (
4758
<div
4859
role="dialog"
4960
tabIndex="-1"
5061
className={classNames(
5162
'sui-modal',
5263
{
53-
'sui-modal-opened': opened || exiting || entering,
54-
'sui-modal-fade-in': entering,
55-
'sui-modal-fade-out': exiting,
64+
'sui-modal-opened': opened || transitionState === 'exiting',
65+
[`sui-modal-transition-${transitionState}`]: transitionState,
5666
},
5767
className,
5868
)}
@@ -75,23 +85,30 @@ const Modal = styled(ModalComponent)`
7585
bottom: 0;
7686
left: 0;
7787
z-index: ${th('zIndexModal')};
78-
display: none;
88+
visibility: hidden;
7989
overflow: hidden;
8090
outline: 0;
81-
opacity: 0;
82-
transition: opacity ${th('modalTransitionDuration')}ms;
91+
transition: opacity ${th('modalTransitionDuration')}ms ease-in-out;
8392
8493
&.sui-modal-opened {
85-
display: block;
94+
visibility: visible;
8695
overflow-x: hidden;
8796
overflow-y: auto;
8897
}
8998
90-
&.sui-modal-fade-in {
99+
&.sui-modal-transition-entering {
100+
opacity: 1;
101+
}
102+
103+
&.sui-modal-transition-entered {
91104
opacity: 1;
92105
}
93106
94-
&.sui-modal-fade-out {
107+
&.sui-modal-transition-exited {
108+
opacity: 0;
109+
}
110+
111+
&.sui-modal-transition-exiting {
95112
opacity: 0;
96113
}
97114

src/Transition.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1 @@
1-
/* eslint-disable no-underscore-dangle */
2-
import React from 'react'
3-
4-
class Transition extends React.Component {
5-
state = { entering: false, exiting: false, toggle: this.props.toggle }
6-
7-
componentDidUpdate() {
8-
if (this.state.toggle === this.props.toggle) return
9-
10-
setTimeout(() => {
11-
this.setState({
12-
entering: this.props.toggle,
13-
exiting: !this.props.toggle,
14-
})
15-
})
16-
17-
clearTimeout(this.timeout)
18-
this.timeout = setTimeout(() => {
19-
this.setState({
20-
entering: false,
21-
exiting: false,
22-
})
23-
}, this.props.ms)
24-
}
25-
26-
componentWillUnmount() {
27-
clearTimeout(this.timeout)
28-
}
29-
30-
render() {
31-
return this.props.children(this.state)
32-
}
33-
}
34-
35-
export default Transition
1+
export { default } from 'react-transition-group/Transition'

0 commit comments

Comments
 (0)