Skip to content

Commit 93fffc4

Browse files
bindoonlakerswgq
authored andcommitted
fix(Dialog): rollback body style while unmount fix alibaba-fusion#3725
1 parent bdccd84 commit 93fffc4

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/dialog/dialog-v2.jsx

+32-18
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ const Dialog = props => {
6666

6767
const [firstVisible, setFirst] = useState(pvisible || false);
6868
const [visible, setVisible] = useState(pvisible);
69-
const [isAnimationEnd, markAnimationEnd] = useState(false);
7069
const getContainer =
7170
typeof popupContainer === 'string'
7271
? () => document.getElementById(popupContainer)
@@ -81,6 +80,13 @@ const Dialog = props => {
8180
const [uuid] = useState(guid());
8281
const { setVisibleOverlayToParent, ...otherContext } = useContext(OverlayContext);
8382
const childIDMap = useRef(new Map());
83+
const isAnimationEnd = useRef(false);
84+
const [, forceUpdate] = useState();
85+
86+
const markAnimationEnd = state => {
87+
isAnimationEnd.current = state;
88+
forceUpdate({});
89+
};
8490

8591
let canCloseByEsc = false;
8692
let canCloseByMask = false;
@@ -160,11 +166,35 @@ const Dialog = props => {
160166
}
161167
}, [container]);
162168

169+
const handleExited = () => {
170+
if (!isAnimationEnd.current) {
171+
markAnimationEnd(true);
172+
dom.setStyle(wrapperRef.current, 'display', 'none');
173+
scrollLocker.unlock(document.body, locker.current);
174+
175+
if (autoFocus && lastFocus.current) {
176+
try {
177+
lastFocus.current.focus();
178+
} finally {
179+
// ignore ...
180+
}
181+
lastFocus.current = null;
182+
}
183+
afterClose();
184+
}
185+
};
186+
187+
useEffect(() => {
188+
return () => {
189+
handleExited();
190+
};
191+
}, []);
192+
163193
if (firstVisible === false || !container) {
164194
return null;
165195
}
166196

167-
if (!visible && !cache && isAnimationEnd) {
197+
if (!visible && !cache && isAnimationEnd.current) {
168198
return null;
169199
}
170200

@@ -206,22 +236,6 @@ const Dialog = props => {
206236
setVisibleOverlayToParent(uuid, wrapperRef.current);
207237
};
208238

209-
const handleExited = () => {
210-
markAnimationEnd(true);
211-
dom.setStyle(wrapperRef.current, 'display', 'none');
212-
scrollLocker.unlock(document.body, locker.current);
213-
214-
if (autoFocus && lastFocus.current) {
215-
try {
216-
lastFocus.current.focus();
217-
} finally {
218-
// ignore ...
219-
}
220-
lastFocus.current = null;
221-
}
222-
afterClose();
223-
};
224-
225239
const wrapperCls = classNames({
226240
[`${prefix}overlay-wrapper`]: true,
227241
opened: visible,

test/dialog/index-v2-spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('v2', () => {
109109
document.body.style='';
110110
});
111111

112-
it('should show and hide', async () => {
112+
it('should show and hide with no cache', async () => {
113113
wrapper = render(<Demo2 animation={false} />);
114114
const btn = document.querySelector('button');
115115
ReactTestUtils.Simulate.click(btn);
@@ -119,6 +119,7 @@ describe('v2', () => {
119119
const okBtn = document.querySelector('.next-btn-primary.next-dialog-btn');
120120
ReactTestUtils.Simulate.click(okBtn);
121121
await delay(40);
122+
// no cache should unmount
122123
assert(!document.querySelector('.next-dialog'));
123124

124125
ReactTestUtils.Simulate.click(btn);

0 commit comments

Comments
 (0)