Skip to content

Commit

Permalink
Fix stale focus rects left in UI when last focusable component is rem…
Browse files Browse the repository at this point in the history
…oved (#14359)

* Fix stale focus rects left in UI when last focusable component is removed

* Change files

---------

Co-authored-by: Tatiana Kapos <[email protected]>
  • Loading branch information
acoates-ms and TatianaKapos authored Feb 27, 2025
1 parent c5ee2c2 commit c8de27f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix stale focus rects left in UI when last focusable component is removed",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch"
}
5 changes: 4 additions & 1 deletion vnext/Microsoft.ReactNative/Fabric/ComponentView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ void ComponentView::parent(const winrt::Microsoft::ReactNative::ComponentView &p
m_parent = parent;
if (!parent) {
if (oldRootView && oldRootView->GetFocusedComponent() == *this) {
oldRootView->TrySetFocusedComponent(oldParent, winrt::Microsoft::ReactNative::FocusNavigationDirection::None);
oldRootView->TrySetFocusedComponent(
oldParent,
winrt::Microsoft::ReactNative::FocusNavigationDirection::None,
true /*forceNoSelectionIfCannotMove*/);
}
}
if (parent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,16 @@ bool RootComponentView::NavigateFocus(const winrt::Microsoft::ReactNative::Focus

bool RootComponentView::TrySetFocusedComponent(
const winrt::Microsoft::ReactNative::ComponentView &view,
winrt::Microsoft::ReactNative::FocusNavigationDirection direction) noexcept {
winrt::Microsoft::ReactNative::FocusNavigationDirection direction,
bool forceNoSelectionIfCannotMove /*= false*/) noexcept {
auto target = view;
auto selfView = winrt::get_self<winrt::Microsoft::ReactNative::implementation::ComponentView>(target);
if (selfView && !selfView->focusable()) {
target = (direction == winrt::Microsoft::ReactNative::FocusNavigationDirection::Last ||
direction == winrt::Microsoft::ReactNative::FocusNavigationDirection::Previous)
? FocusManager::FindLastFocusableElement(target)
: FocusManager::FindFirstFocusableElement(target);
if (!target)
if (!target && !forceNoSelectionIfCannotMove)
return false;
selfView = winrt::get_self<winrt::Microsoft::ReactNative::implementation::ComponentView>(target);
}
Expand All @@ -154,6 +155,8 @@ bool RootComponentView::TrySetFocusedComponent(
winrt::get_self<winrt::Microsoft::ReactNative::implementation::ComponentView>(losingFocusArgs.NewFocusedComponent())
->rootComponentView()
->SetFocusedComponent(gettingFocusArgs.NewFocusedComponent(), direction);
} else {
SetFocusedComponent(nullptr, direction);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ struct RootComponentView : RootComponentViewT<RootComponentView, ViewComponentVi
winrt::Microsoft::ReactNative::FocusNavigationDirection direction) noexcept;
bool TrySetFocusedComponent(
const winrt::Microsoft::ReactNative::ComponentView &view,
winrt::Microsoft::ReactNative::FocusNavigationDirection direction) noexcept;
winrt::Microsoft::ReactNative::FocusNavigationDirection direction,
bool forceNoSelectionIfCannotMove = false) noexcept;

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

Expand Down

0 comments on commit c8de27f

Please sign in to comment.