diff --git a/change/react-native-windows-5bdf4573-dfbe-41fd-9dc0-4c4a7119957e.json b/change/react-native-windows-5bdf4573-dfbe-41fd-9dc0-4c4a7119957e.json new file mode 100644 index 00000000000..d26de73abfc --- /dev/null +++ b/change/react-native-windows-5bdf4573-dfbe-41fd-9dc0-4c4a7119957e.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Fix Crash after closing DesktopPopupSiteBridge", + "packageName": "react-native-windows", + "email": "74712637+iamAbhi-916@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/Fabric/Composition/Modal/WindowsModalHostViewComponentView.cpp b/vnext/Microsoft.ReactNative/Fabric/Composition/Modal/WindowsModalHostViewComponentView.cpp index 99632acbcc9..968d070aeb6 100644 --- a/vnext/Microsoft.ReactNative/Fabric/Composition/Modal/WindowsModalHostViewComponentView.cpp +++ b/vnext/Microsoft.ReactNative/Fabric/Composition/Modal/WindowsModalHostViewComponentView.cpp @@ -35,26 +35,38 @@ struct ModalHostState struct ModalHostView : public winrt::implements, ::Microsoft::ReactNativeSpecs::BaseModalHostView { ~ModalHostView() { - if (m_reactNativeIsland) { - m_reactNativeIsland.Island().Close(); - } + if (m_popUp) { + // Unregister closing event handler + if (m_appWindow && m_appWindowClosingToken) { + m_appWindow.Closing(m_appWindowClosingToken); + m_appWindowClosingToken.value = 0; + } - // Add AppWindow closing token cleanup - if (m_appWindow && m_appWindowClosingToken) { - m_appWindow.Closing(m_appWindowClosingToken); - m_appWindowClosingToken.value = 0; - } + // Reset topWindowID before destroying + if (m_prevWindowID) { + winrt::Microsoft::ReactNative::ReactCoreInjection::SetTopLevelWindowId( + m_reactContext.Properties().Handle(), m_prevWindowID); + m_prevWindowID = 0; + } - if (m_popUp) { - if (m_departFocusToken && !m_popUp.IsClosed()) { - // WASDK BUG: InputFocusNavigationHost::GetForSiteBridge fails on a DesktopPopupSiteBridge - // https://github.com/microsoft/react-native-windows/issues/14604 - /* - auto navHost = - winrt::Microsoft::UI::Input::InputFocusNavigationHost::GetForSiteBridge(m_popUp.as()); - navHost.DepartFocusRequested(m_departFocusToken); - */ + // Close island + if (m_reactNativeIsland) { + m_reactNativeIsland.Island().Close(); + m_reactNativeIsland = nullptr; + } + + // Hide popup + if (m_popUp.IsVisible()) { + m_popUp.Hide(); + } + + // Destroy AppWindow this automatically resumes parent window to receive inputs + if (m_appWindow) { + m_appWindow.Destroy(); + m_appWindow = nullptr; } + + // Close bridge m_popUp.Close(); m_popUp = nullptr; } @@ -88,7 +100,7 @@ struct ModalHostView : public winrt::implementsonDismiss(eventArgs); } - - // reset the topWindowID - if (m_prevWindowID) { - winrt::Microsoft::ReactNative::ReactCoreInjection::SetTopLevelWindowId( - m_reactContext.Properties().Handle(), m_prevWindowID); - m_prevWindowID = 0; - } } // creates a new modal window