Skip to content

Commit

Permalink
Fix consecutive shows of same modal with different attrs
Browse files Browse the repository at this point in the history
We need to specify a unique key for each modal so that the modals are fully destroyed and recreated. For instance, this fixes the signup modal being empty with OAuth register flows.
  • Loading branch information
askvortsov1 committed Dec 27, 2021
1 parent a55b61e commit 6868e58
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
19 changes: 12 additions & 7 deletions js/src/common/components/ModalManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ export default class ModalManager extends Component<IModalManagerAttrs> {

view(vnode: Mithril.VnodeDOM<IModalManagerAttrs, this>): Mithril.Children {
const modal = this.attrs.state.modal;
const Tag = modal?.componentClass;

return (
<div className="ModalManager modal fade">
{!!modal &&
modal.componentClass.component({
...modal.attrs,
animateShow: this.animateShow.bind(this),
animateHide: this.animateHide.bind(this),
state: this.attrs.state,
})}
{!!Tag && (
<div>
<Tag
key={modal?.key}
{...modal.attrs}
animateShow={this.animateShow.bind(this)}
animateHide={this.animateHide.bind(this)}
state={this.attrs.state}
/>
</div>
)}
</div>
);
}
Expand Down
9 changes: 8 additions & 1 deletion js/src/common/states/ModalManagerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ export default class ModalManagerState {
modal: null | {
componentClass: UnsafeModalClass;
attrs?: Record<string, unknown>;
key: number;
} = null;

/**
* Used to force re-initialization of modals if a modal
* is replaced by another of the same type.
*/
private key = 0;

private closeTimeout?: NodeJS.Timeout;

/**
Expand All @@ -48,7 +55,7 @@ export default class ModalManagerState {

if (this.closeTimeout) clearTimeout(this.closeTimeout);

this.modal = { componentClass, attrs };
this.modal = { componentClass, attrs, key: this.key++ };

m.redraw.sync();
}
Expand Down

0 comments on commit 6868e58

Please sign in to comment.