Skip to content

Commit c8de27f

Browse files
Fix stale focus rects left in UI when last focusable component is removed (#14359)
* Fix stale focus rects left in UI when last focusable component is removed * Change files --------- Co-authored-by: Tatiana Kapos <[email protected]>
1 parent c5ee2c2 commit c8de27f

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
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 stale focus rects left in UI when last focusable component is removed",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative/Fabric/ComponentView.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,10 @@ void ComponentView::parent(const winrt::Microsoft::ReactNative::ComponentView &p
279279
m_parent = parent;
280280
if (!parent) {
281281
if (oldRootView && oldRootView->GetFocusedComponent() == *this) {
282-
oldRootView->TrySetFocusedComponent(oldParent, winrt::Microsoft::ReactNative::FocusNavigationDirection::None);
282+
oldRootView->TrySetFocusedComponent(
283+
oldParent,
284+
winrt::Microsoft::ReactNative::FocusNavigationDirection::None,
285+
true /*forceNoSelectionIfCannotMove*/);
283286
}
284287
}
285288
if (parent) {

vnext/Microsoft.ReactNative/Fabric/Composition/RootComponentView.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,16 @@ bool RootComponentView::NavigateFocus(const winrt::Microsoft::ReactNative::Focus
120120

121121
bool RootComponentView::TrySetFocusedComponent(
122122
const winrt::Microsoft::ReactNative::ComponentView &view,
123-
winrt::Microsoft::ReactNative::FocusNavigationDirection direction) noexcept {
123+
winrt::Microsoft::ReactNative::FocusNavigationDirection direction,
124+
bool forceNoSelectionIfCannotMove /*= false*/) noexcept {
124125
auto target = view;
125126
auto selfView = winrt::get_self<winrt::Microsoft::ReactNative::implementation::ComponentView>(target);
126127
if (selfView && !selfView->focusable()) {
127128
target = (direction == winrt::Microsoft::ReactNative::FocusNavigationDirection::Last ||
128129
direction == winrt::Microsoft::ReactNative::FocusNavigationDirection::Previous)
129130
? FocusManager::FindLastFocusableElement(target)
130131
: FocusManager::FindFirstFocusableElement(target);
131-
if (!target)
132+
if (!target && !forceNoSelectionIfCannotMove)
132133
return false;
133134
selfView = winrt::get_self<winrt::Microsoft::ReactNative::implementation::ComponentView>(target);
134135
}
@@ -154,6 +155,8 @@ bool RootComponentView::TrySetFocusedComponent(
154155
winrt::get_self<winrt::Microsoft::ReactNative::implementation::ComponentView>(losingFocusArgs.NewFocusedComponent())
155156
->rootComponentView()
156157
->SetFocusedComponent(gettingFocusArgs.NewFocusedComponent(), direction);
158+
} else {
159+
SetFocusedComponent(nullptr, direction);
157160
}
158161

159162
return true;

vnext/Microsoft.ReactNative/Fabric/Composition/RootComponentView.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ struct RootComponentView : RootComponentViewT<RootComponentView, ViewComponentVi
3232
winrt::Microsoft::ReactNative::FocusNavigationDirection direction) noexcept;
3333
bool TrySetFocusedComponent(
3434
const winrt::Microsoft::ReactNative::ComponentView &view,
35-
winrt::Microsoft::ReactNative::FocusNavigationDirection direction) noexcept;
35+
winrt::Microsoft::ReactNative::FocusNavigationDirection direction,
36+
bool forceNoSelectionIfCannotMove = false) noexcept;
3637

3738
bool NavigateFocus(const winrt::Microsoft::ReactNative::FocusNavigationRequest &request) noexcept;
3839

0 commit comments

Comments
 (0)