Skip to content

Fix FocusOrder attribute not being respected in focus traversal#282

Merged
LittleLittleCloud merged 8 commits intomainfrom
copilot/fix-focus-order-in-textbutton
Feb 10, 2026
Merged

Fix FocusOrder attribute not being respected in focus traversal#282
LittleLittleCloud merged 8 commits intomainfrom
copilot/fix-focus-order-in-textbutton

Conversation

Copy link
Contributor

Copilot AI commented Jan 14, 2026

Fix FocusOrder Attribute in Focus Management

This PR fixes the issue where the FocusOrder parameter on focusable components (like TextButton and TextInput) is not being respected during focus traversal.

Changes Made:

  • Analyzed current FocusManager implementation
  • Identified root cause: CollectTargets method doesn't sort by data-focus-order
  • Modified CollectTargets to sort focusable elements by FocusOrder after collection
  • Added comprehensive tests for FocusOrder sorting (both with and without mixed order attributes)
  • Verified Core library builds successfully
  • Refactored to use static lambdas and move OrderBy logic to Select (based on review feedback)
  • Request code review

Implementation Details:

The fix modifies the CollectTargets method in FocusManager.cs to:

  1. Collect all focusable elements in DOM order (as before)
  2. Extract FocusOrder value in the Select projection (avoiding repeated OrderBy calls)
  3. Sort them by the extracted FocusOrder value (lower values first) using static lambdas
  4. Elements without a focus order come last, maintaining DOM order
  5. Elements with the same focus order maintain their relative DOM order (stable sort)

Test Coverage:

Added two test cases:

  • FocusOrder_RespectsFocusOrderAttribute: Tests sequential FocusOrder values (2, 3, 4) with out-of-order DOM
  • FocusOrder_MixedWithoutOrderAttribute: Tests mixed scenarios with and without FocusOrder attributes

This ensures that when components like TextButton set FocusOrder="2", FocusOrder="3", and FocusOrder="4", they will be focused in that numeric order (2, 3, 4) regardless of their position in the DOM.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: Focus order not working.</issue_title>
<issue_description>### Description

Tried to test new FocusOrder attribute in TextButton component (#279 )

Steps to Reproduce

Change text button gallery to this (use new text button version from #279):

    <Panel Title="Preview" Border="BoxBorder.Rounded" Expand="true">
        <Columns>
            <TextButton Content="4"
                        FocusOrder="4"
                        BackgroundColor="@_restingColor"
                        OnClick="ToggleAsync" />
            <TextButton Content="2"
                        BackgroundColor="@_restingColor"
                        OnClick="ToggleAsync"
                        FocusOrder="2"/>
            <TextButton Content="3"
                        FocusOrder="3"
                        BackgroundColor="@_restingColor"
                        OnClick="ToggleAsync" />
            <Markup Content="@_status"
                    Foreground="@_statusColor"
                    Decoration="@_statusDecoration" />
        </Columns>
    </Panel>

Expected Behavior

Focus must be, as the button labels describes.

Actual Behavior

Focus by elements layout order.

Environment

No response

Additional Context

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: LittleLittleCloud <16876986+LittleLittleCloud@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix focus order issue in TextButton component Fix FocusOrder attribute not being respected in focus traversal Jan 14, 2026
@github-actions
Copy link

github-actions bot commented Jan 14, 2026

🚀 Preview Deployment

A preview build has been generated for this PR from CI run #21815517723!

Download the artifact:
website-preview-282-b52bbe99f83b83a7c1b766eef258fbeb13715be6

To view the preview locally:

  1. Download the artifact from the CI workflow run
  2. Extract the ZIP file
  3. Serve the files with a local web server
    (e.g., npx serve dist)

🌐 Live Preview URL: https://0c5d3de8.razorconsole.pages.dev

The live preview will be automatically updated when you push new
commits to this PR.

@LittleLittleCloud LittleLittleCloud marked this pull request as ready for review January 18, 2026 06:41
// Sort by focus order if specified, maintaining DOM order for elements with same/no order
targets = targets
.Select((target, index) => new { Target = target, OriginalIndex = index })
.OrderBy(item =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OrderBy can be called multiple times. It's better to move it to select.
Also it whould be nice to mark as delegate static

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot fix the comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 66ed768. Moved the FocusOrder extraction logic to the Select projection and marked all lambdas as static to improve performance.

…c to Select

Co-authored-by: LittleLittleCloud <16876986+LittleLittleCloud@users.noreply.github.com>
ParadiseFallen
ParadiseFallen previously approved these changes Jan 26, 2026
Signed-off-by: Xiaoyun Zhang <bigmiao.zhang@gmail.com>
LittleLittleCloud added a commit that referenced this pull request Feb 9, 2026
* Initial plan

* Fix FocusOrder attribute to sort focus targets correctly

Co-authored-by: LittleLittleCloud <16876986+LittleLittleCloud@users.noreply.github.com>

* Refactor FocusOrder logic to use static lambdas and move OrderBy logic to Select

Co-authored-by: LittleLittleCloud <16876986+LittleLittleCloud@users.noreply.github.com>

* Refactor focus order sorting in FocusManager

Signed-off-by: Xiaoyun Zhang <bigmiao.zhang@gmail.com>

---------

Signed-off-by: Xiaoyun Zhang <bigmiao.zhang@gmail.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: LittleLittleCloud <16876986+LittleLittleCloud@users.noreply.github.com>
Co-authored-by: Xiaoyun Zhang <bigmiao.zhang@gmail.com>
LittleLittleCloud added a commit that referenced this pull request Feb 9, 2026
* Initial plan

* Fix FocusOrder attribute to sort focus targets correctly

Co-authored-by: LittleLittleCloud <16876986+LittleLittleCloud@users.noreply.github.com>

* Refactor FocusOrder logic to use static lambdas and move OrderBy logic to Select

Co-authored-by: LittleLittleCloud <16876986+LittleLittleCloud@users.noreply.github.com>

* Refactor focus order sorting in FocusManager

Signed-off-by: Xiaoyun Zhang <bigmiao.zhang@gmail.com>

---------

Signed-off-by: Xiaoyun Zhang <bigmiao.zhang@gmail.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: LittleLittleCloud <16876986+LittleLittleCloud@users.noreply.github.com>
Co-authored-by: Xiaoyun Zhang <bigmiao.zhang@gmail.com>
@LittleLittleCloud LittleLittleCloud merged commit 360141c into main Feb 10, 2026
13 checks passed
@LittleLittleCloud LittleLittleCloud deleted the copilot/fix-focus-order-in-textbutton branch February 10, 2026 02:34
@github-actions github-actions bot added this to the v0.4.0 milestone Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Focus order not working.

3 participants