Skip to content

Issue with Toolbar and keyboard navigation #3169

Closed

Description

@KavunCODE

Hi, we are still encountering the issue similar to one mentioned here: #3087 while working with FluentToolbar. Using the arrow keys -> changes the focus to components located to the left or right, depending on the arrow key pressed, instead of moving the cursor through text inside FluentSearch, FluentDatePicker, etc.

image

FluentUI version is 4.11.1

How to reproduce:
Simply wrap a couple of components that allow writing text or values with FluentToolbar:

@page "/Page1"

<FluentToolbar>
<FluentSearch Id="SearchField1" Placeholder="Search" />

<FluentSearch Id="SearchField2" Placeholder="Search" />

<FluentDatePicker Appearance="FluentInputAppearance.Outline" />
</FluentToolbar>

Output image:

Image

Activity

  1. added
    triage
    on Jan 14, 2025
  2. changed the title [-]Hi, we are still encountering this **issue** while working with **FluentToolbar**. Using the **arrow keys** -> changes the **focus** to components located to the left or right, depending on the arrow key pressed, instead of **moving** the cursor **through text** inside FluentSearch, FluentDatePicker, etc.[/-] [+]Issue with Toolbar and keyboard navigation[/+] on Jan 14, 2025
  3. changed the title [-]Issue with Toolbar and keyboard navigation[/-] [+]Arrow keys in-field navigation error having multiple components(Search, DatePicker, etc...) inside FluentToolbar[/+] on Jan 14, 2025
  4. changed the title [-]Arrow keys in-field navigation error having multiple components(Search, DatePicker, etc...) inside FluentToolbar[/-] [+]Issue with Toolbar and keyboard navigation[/+] on Jan 14, 2025
  5. hml-coder commented on Jan 14, 2025

    @hml-coder
    Contributor

    Issue does not occur with the web components.

    This sample provides expected behavior -- if needed as a workaround -- but only limited testing and not aware if it will cause issues elsewhere in the library such as accessibility.

    @inject IJSRuntime JSRuntime
    
    <FluentToolbar>
      <FluentSearch Id="SearchField1" Placeholder="Search" />
      <FluentTextField Id="Text1" Placeholder="Text1" />
      <FluentTextArea Id="TextArea1" Placeholder="TextArea1" />
      <FluentNumberField Id="Number1" TValue="int"/>
      <FluentDatePicker Appearance="FluentInputAppearance.Outline" />
    </FluentToolbar>
    
    @code {
      protected override async Task OnAfterRenderAsync(bool firstRender)
      {
          if (firstRender)
          {
              await JSRuntime.InvokeVoidAsync("preventArrowKeyNavigation");
          }
      }
    }
    
    <script>
         function preventArrowKeyNavigation() {
          console.log('initialize preventArrowKeyNavigation');
    
          document.addEventListener('keydown', function (event) {
              if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
                  const activeElement = document.activeElement;
                  if (activeElement && (activeElement.tagName === 'FLUENT-SEARCH'
                      || activeElement.tagName === 'FLUENT-TEXT-FIELD'
                      || activeElement.tagName === 'FLUENT-NUMBER-FIELD'
                      || activeElement.tagName === 'FLUENT-TEXT-AREA')) {
    
                      // prevent tab-like behavior
                      event.stopPropagation();
                  }
              }
          }, true);
      }
    </script>
    
    
  6. vnbaaij commented on Jan 14, 2025

    @vnbaaij
    Collaborator

    Roland (@hml-coder) With which web components did you test? And how? (just curious)

    For v4.12 we can look at integrating this script into the library. Only thing we would need to change probably is that the event listner would need to be added to the toolbar instead of to the document. If we add to document it will most probably be to greedy again

  7. added
    status:needs-investigation
    and removed
    triage
    on Jan 14, 2025
  8. added this to the v4.12 milestone on Jan 14, 2025
  9. hml-coder commented on Jan 14, 2025

    @hml-coder
    Contributor

    Vincent Baaij (@vnbaaij) Great question. I somehow missed that the web components test I ran wasn’t valid because the components weren’t inside a toolbar parent container, which is the root cause of the bug :-)

    Completely agree on adding the script to the toolbar -- just wanted to share a simple example that can be verified in IssueTester.

  10. hml-coder commented on Jan 14, 2025

    @hml-coder
    Contributor

    Vincent Baaij (@vnbaaij) When I wrote that last post I was thinking that a fluent-toolbar web component did not exist but found that it does when I opened FluentToolbar.razor. I added the fluent-toolbar to my my test using web components 2.6.1 and this IS a web components issue.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="module" src="https://unpkg.com/@fluentui/web-components@2.6.1"></script>
    </head>
    <body>
        <div>
            <fluent-toolbar>
                <fluent-search placeholder="Search"></fluent-search>
                <fluent-text-field placeholder="Text1"></fluent-text-field>
                <fluent-text-area placeholder="TextArea1"></fluent-text-area>
                <fluent-number-field></fluent-number-field>
                <fluent-button appearance="accent">Submit</fluent-button>
            </fluent-toolbar>
        </div>
    </body>
    </html>
    
  11. hml-coder commented on Jan 14, 2025

    @hml-coder
    Contributor

    I haven't reviewed the full history of this issue but based on the fluent-toolbar code, it intercepts keyboard events to initiate arrow key navigation. However, it does not prevent focus changes for input elements where focus should stay within the field.

    public keydownHandler(e: KeyboardEvent): boolean | void {
        const key = e.key;
    
        if (!(key in ArrowKeys) || e.defaultPrevented || e.shiftKey) {
            return true;
        }
    
        const incrementer = this.getDirectionalIncrementer(key);
        if (!incrementer) {
            return !(e.target as HTMLElement).closest("[role=radiogroup]");
        }
    
        const nextIndex = this.activeIndex + incrementer;
        if (this.focusableElements[nextIndex]) {
            e.preventDefault();
        }
    
        this.setFocusedElement(nextIndex);
    
        return true;
    }
    
    
  12. vnbaaij commented on Jan 17, 2025

    @vnbaaij
    Collaborator

    Roland (@hml-coder) (@chmod-600) Think you can wrap this up in a fix that we can deploy in an upcoming release?

  13. added
    community:contribution
    and removed
    status:needs-investigation
    on Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    community:contribution

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions