Skip to content

Commit f229aef

Browse files
iamAbhi-916rnbot
authored andcommitted
Fix modal Crash after closing DesktopPopupSiteBridge (#15384)
* visual studio 2026 strict check fix * Fix Crash after closing DesktopPopupSiteBridge * Change files * yarn lint:fix amd format * remove debug logs * Revert "visual studio 2026 strict check fix" * visual studio 2026 strict check fix * nit comment
1 parent b21e58e commit f229aef

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Fix Crash after closing DesktopPopupSiteBridge",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative/Fabric/Composition/Modal/WindowsModalHostViewComponentView.cpp

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,38 @@ struct ModalHostState
3535
struct ModalHostView : public winrt::implements<ModalHostView, winrt::Windows::Foundation::IInspectable>,
3636
::Microsoft::ReactNativeSpecs::BaseModalHostView<ModalHostView> {
3737
~ModalHostView() {
38-
if (m_reactNativeIsland) {
39-
m_reactNativeIsland.Island().Close();
40-
}
38+
if (m_popUp) {
39+
// Unregister closing event handler
40+
if (m_appWindow && m_appWindowClosingToken) {
41+
m_appWindow.Closing(m_appWindowClosingToken);
42+
m_appWindowClosingToken.value = 0;
43+
}
4144

42-
// Add AppWindow closing token cleanup
43-
if (m_appWindow && m_appWindowClosingToken) {
44-
m_appWindow.Closing(m_appWindowClosingToken);
45-
m_appWindowClosingToken.value = 0;
46-
}
45+
// Reset topWindowID before destroying
46+
if (m_prevWindowID) {
47+
winrt::Microsoft::ReactNative::ReactCoreInjection::SetTopLevelWindowId(
48+
m_reactContext.Properties().Handle(), m_prevWindowID);
49+
m_prevWindowID = 0;
50+
}
4751

48-
if (m_popUp) {
49-
if (m_departFocusToken && !m_popUp.IsClosed()) {
50-
// WASDK BUG: InputFocusNavigationHost::GetForSiteBridge fails on a DesktopPopupSiteBridge
51-
// https://github.com/microsoft/react-native-windows/issues/14604
52-
/*
53-
auto navHost =
54-
winrt::Microsoft::UI::Input::InputFocusNavigationHost::GetForSiteBridge(m_popUp.as<winrt::Microsoft::UI::Content::IContentSiteBridge>());
55-
navHost.DepartFocusRequested(m_departFocusToken);
56-
*/
52+
// Close island
53+
if (m_reactNativeIsland) {
54+
m_reactNativeIsland.Island().Close();
55+
m_reactNativeIsland = nullptr;
56+
}
57+
58+
// Hide popup
59+
if (m_popUp.IsVisible()) {
60+
m_popUp.Hide();
61+
}
62+
63+
// Destroy AppWindow this automatically resumes parent window to receive inputs
64+
if (m_appWindow) {
65+
m_appWindow.Destroy();
66+
m_appWindow = nullptr;
5767
}
68+
69+
// Close bridge
5870
m_popUp.Close();
5971
m_popUp = nullptr;
6072
}
@@ -88,7 +100,7 @@ struct ModalHostView : public winrt::implements<ModalHostView, winrt::Windows::F
88100
QueueShow(view);
89101
} else {
90102
m_visible = false;
91-
CloseWindow();
103+
HideWindow();
92104
}
93105
}
94106

@@ -219,31 +231,27 @@ struct ModalHostView : public winrt::implements<ModalHostView, winrt::Windows::F
219231
}
220232
}
221233

222-
void CloseWindow() noexcept {
223-
// enable input to parent before closing the modal window, so focus can return back to the parent window
224-
EnableWindow(m_parentHwnd, true);
234+
/*
235+
HideWindow called on visible=false
236+
unmounts the modal window using onDismiss event
237+
*/
238+
void HideWindow() noexcept {
239+
// Hide popup
225240
if (m_popUp) {
226241
m_popUp.Hide();
227242
}
228243

229-
// Unregister closing event handler
230-
if (m_appWindow && m_appWindowClosingToken) {
231-
m_appWindow.Closing(m_appWindowClosingToken);
232-
m_appWindowClosingToken.value = 0;
244+
// Restore message routing to parent
245+
if (m_prevWindowID) {
246+
winrt::Microsoft::ReactNative::ReactCoreInjection::SetTopLevelWindowId(
247+
m_reactContext.Properties().Handle(), m_prevWindowID);
233248
}
234249

235-
// dispatch onDismiss event
250+
// Dispatch onDismiss event
236251
if (auto eventEmitter = EventEmitter()) {
237252
::Microsoft::ReactNativeSpecs::ModalHostViewEventEmitter::OnDismiss eventArgs;
238253
eventEmitter->onDismiss(eventArgs);
239254
}
240-
241-
// reset the topWindowID
242-
if (m_prevWindowID) {
243-
winrt::Microsoft::ReactNative::ReactCoreInjection::SetTopLevelWindowId(
244-
m_reactContext.Properties().Handle(), m_prevWindowID);
245-
m_prevWindowID = 0;
246-
}
247255
}
248256

249257
// creates a new modal window

0 commit comments

Comments
 (0)