Skip to content

Commit

Permalink
fix(ui-modal): fix second modal closing the first one if two of them …
Browse files Browse the repository at this point in the history
…are open
  • Loading branch information
balzss committed Nov 8, 2023
1 parent da016c8 commit 8921641
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
72 changes: 35 additions & 37 deletions packages/ui-modal/src/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class Modal extends Component<ModalProps, ModalState> {
super(props)

this.state = {
transitioning: false
transitioning: false,
open: props.open ?? false
}
}

Expand All @@ -114,11 +115,8 @@ class Modal extends Component<ModalProps, ModalState> {
}

componentDidUpdate(prevProps: ModalProps) {
if (prevProps.open && !this.props.open) {
// closing
this.setState({
transitioning: prevProps.transition !== null
})
if (this.props.open !== prevProps.open) {
this.setState({ transitioning: true, open: !!this.props.open })
}
this.props.makeStyles?.()
}
Expand Down Expand Up @@ -146,7 +144,7 @@ class Modal extends Component<ModalProps, ModalState> {
this.DOMNode = DOMNode
}

handleTransitionExited: ModalProps['onExited'] = () => {
handleTransitionComplete: ModalProps['onEntered' | 'onExited'] = () => {
this.setState({
transitioning: false
})
Expand Down Expand Up @@ -252,46 +250,46 @@ class Modal extends Component<ModalProps, ModalState> {
onExiting,
onExited,
constrain,
overflow,
overflow, // TODO this is not used currently
...passthroughProps
} = this.props

const portalIsOpen = open || this.state.transitioning
const portalIsOpen = this.state.open || this.state.transitioning

return (
<Portal
mountNode={mountNode}
insertAt={insertAt}
open={portalIsOpen}
onOpen={createChainedFunction(this.handlePortalOpen, onOpen)}
onClose={onClose}
elementRef={this.handleRef}
onOpen={this.handlePortalOpen}
>
{portalIsOpen && (
<Transition
in={open}
transitionOnMount
unmountOnExit
type={transition}
onEnter={onEnter}
onEntering={onEntering}
onEntered={onEntered}
onExit={onExit}
onExiting={onExiting}
onExited={createChainedFunction(
this.handleTransitionExited,
onExited
)}
>
{constrain === 'parent' ? (
<span css={this.props.styles?.constrainContext}>
{this.renderDialog(passthroughProps)}
</span>
) : (
this.renderDialog(passthroughProps)
)}
</Transition>
)}
<Transition
in={open}
transitionOnMount
type={transition}
onEnter={onEnter}
onEntering={onEntering}
onEntered={createChainedFunction(
this.handleTransitionComplete,
onEntered,
onOpen
)}
onExit={onExit}
onExiting={onExiting}
onExited={createChainedFunction(
this.handleTransitionComplete,
onExited,
onClose
)}
>
{constrain === 'parent' ? (
<span css={this.props.styles?.constrainContext}>
{this.renderDialog(passthroughProps)}
</span>
) : (
this.renderDialog(passthroughProps)
)}
</Transition>
</Portal>
)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/ui-modal/src/Modal/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type ModalPropsForPortal = {
/**
* Callback fired when `<Modal />` content has been mounted in the DOM
*/
onOpen?: (DOMNode: PortalNode) => void
onOpen?: (type?: TransitionType) => void

/**
* Callback fired when `<Modal />` has been unmounted from the DOM
Expand Down Expand Up @@ -201,6 +201,7 @@ type ModalStyle = ComponentStyle<'modal' | 'constrainContext'>

type ModalState = {
transitioning: boolean
open: boolean
}

const propTypes: PropValidators<PropKeys> = {
Expand Down

0 comments on commit 8921641

Please sign in to comment.