Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the reverse navigation order of focusable elements within KTable cells #840

Merged
merged 2 commits into from
Feb 11, 2025

Conversation

shivam-daksh
Copy link
Contributor

@shivam-daksh shivam-daksh commented Nov 21, 2024

Description

This pull request addresses an issue with the Shift+Tab key navigation behavior in the KTable component (follow-up to issue #795). Previously, pressing Shift+Tab to navigate backward through table cells did not follow the expected reverse order of focusable elements, leading to an unintuitive and confusing user experience.

Issue addressed

Closes #837

Addresses #PR# HERE
#795

Before/after videos:

Before:
View actual behavior video

After:
View expected behavior video

Example:

Given a table with the following structure:

  • Row 1:

    • Cell 1
    • Cell 2
    • Cell 3 containing:
      • Button 1
      • Button 2
  • Row 2:

    • Cell 1 (current focus)
    • Cell 2
    • Cell 3 containing:
      • Button 1
      • Button 2

Expected Behavior:

  • When using Shift+Tab, the focus should move backward through the focusable elements within each table cell in reverse DOM order.
  • For example, if the focus is on Cell 1 in Row 2, pressing Shift+Tab should move the focus to Button 2 inside Cell 3 of Row 1, then to Button 1, then to Cell 3, and so on.

Changes Made:

  • Modified the handleTabKey function to ensure that Shift+Tab navigation moves the focus through focusable elements in reverse DOM order.
  • Corrected the behavior to ensure that pressing Shift+Tab from a focusable element moves focus to the previous focusable element within the cell, and continues navigating through the table cells in reverse order.
  • Removed the unintended Cell 4 from the example in the associated issue description for clarity.

Changelog

Steps to Test:

  1. Open the KTable component with multiple rows and focusable elements within cells (e.g., buttons).
  2. Focus on a cell (e.g., Cell 1 of Row 2).
  3. Press Shift+Tab and verify that the focus moves to the correct previous focusable element (e.g., Button 2 inside Cell 3 of Row 1).
  4. Continue pressing Shift+Tab and verify that focus correctly moves through the elements in reverse order, respecting the DOM structure.

(optional) Implementation notes

At a high level, how did you implement this?

Initially focusCell in updateFocusState method was causing diffculty in implementation of the ideal navigation. It can be tackled by introducing a third parameter in updateFocusState to check if focusCell is necessary or not.

Code

      updateFocusState(nextRowIndex, nextColIndex, shouldFocusCell = true) {
        this.focusedRowIndex = nextRowIndex === -1 ? null : nextRowIndex;
        this.focusedColIndex = nextColIndex;
        this.highlightHeader(nextColIndex);

        if (shouldFocusCell) {
          this.focusCell(nextRowIndex, nextColIndex);
        }
      },
            const prevCellAndFocusableElements = [prevCell, ...prevFocusableElements];
            prevCellAndFocusableElements[prevCellAndFocusableElements.length - 1].focus();
            this.updateFocusState(nextRowIndex, nextColIndex, false);
            event.preventDefault();

Does this introduce any tech-debt items?

Testing checklist

  • Contributor has fully tested the PR manually
  • If there are any front-end changes, before/after screenshots are included
  • Critical and brittle code paths are covered by unit tests
  • The change is described in the changelog section above

Reviewer guidance

  • Is the code clean and well-commented?
  • Are there tests for this change?
  • Are all UI components LTR and RTL compliant (if applicable)?
  • Add other things to check for here

Comments

@shivam-daksh
Copy link
Contributor Author

@MisRob and @BabyElias , Please have a look at it.

@BabyElias
Copy link
Contributor

BabyElias commented Nov 22, 2024

@shivam-daksh , The overall working looks nicee to me now. Thanks :)
This PR contains all commits that the #804 PR about Refactoring handlekeydown has. So, I was just wondering if we should consider both the PRs or just this one for both the issues and have QA and further performance reviews for this PR itself? Opinions on this @MisRob?

getFocusableElements(cell) {
if (!cell) return [];
const selectors = 'button, a, input, select, textarea';
return Array.from(cell.querySelectorAll(selectors));
Copy link
Contributor

Choose a reason for hiding this comment

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

About the use of querySelectorAll here, I was actually having a little lag in the navigation behaviour when moving through the focusable elements - this reminded me of an earlier code review comment here. We were using querySelectorAll earlier as well, but it caused performance issues, so we shifted to getElementByTagname and it had a very positive impact on the same. Can you update this code to make use of the previous getElementbyTagname logic? or maybe if there's any other conclusion you derive from that conversation about performance improvement- it is more than welcome. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BabyElias, Thanks for noticing. I've changed it to getElementsByTagName.

@shivam-daksh
Copy link
Contributor Author

shivam-daksh commented Nov 22, 2024

I was just wondering if we should consider both the PRs or just this one for both the issues and have QA and further performance reviews for this PR itself? Opinions on this @MisRob?

@BabyElias , I was thinking the same. Let's leave it to @MisRob what she decides.

@MisRob
Copy link
Member

MisRob commented Nov 22, 2024

Thanks @shivam-daksh and @BabyElias.

Since the first PR review is almost complete, I would recommend to wait until we finish QA and merge #804. Then @shivam-daksh you could rebase this PR on top of the latest develop which will only keep the new diff.

@MisRob MisRob added the TODO: needs review Waiting for review label Nov 22, 2024
@MisRob MisRob self-assigned this Nov 22, 2024
@MisRob
Copy link
Member

MisRob commented Jan 28, 2025

Hi @shivam-daksh, I've just merged #804. Could you now rebase this PR on top of the latest develop?

@shivam-daksh
Copy link
Contributor Author

@MisRob, Sure

@shivam-daksh
Copy link
Contributor Author

@MisRob, Please have a look.

@MisRob
Copy link
Member

MisRob commented Jan 31, 2025

Hi @shivam-daksh, there are many conflicting files now - perhaps something went wrong during the rebase/merge?

@shivam-daksh
Copy link
Contributor Author

Hi @MisRob, I'm running into an issue while rebasing—it’s taking much longer than usual, likely due to changes in yarn.lock. I tried a couple of times, but after waiting for 20–30 minutes, I had to abort the process.

A quick solution I’m considering is starting a new branch from the latest develop and applying my changes there. Let me know what you think or if you have any suggestions.

Looking forward to your guidance!

@MisRob
Copy link
Member

MisRob commented Feb 3, 2025

Hi @shivam-daksh, ah I'm sorry this is such a hassle. One idea I had was to try to reset all files that are not relevant to this work to their develop version via git checkout develop -- <file_name> (https://stackoverflow.com/a/46571620). This would include package.json and yarn.lock. Then you can run yarn install again. This way you could avoid having to start a new branch. If this doesn't work, of course feel free to start a new branch. You could even force push it to this one perhaps.

@shivam-daksh
Copy link
Contributor Author

@MisRob, Could you please take a look at this? I believe it should work as expected.

Copy link
Member

@MisRob MisRob left a comment

Choose a reason for hiding this comment

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

Thanks @shivam-daksh! The diff now looks meaningful and I confirmed locally that the fix still works. I haven't observed any regressions. Yey, finally merging. Thanks everyone for patience.

@MisRob MisRob merged commit 5d8b2b9 into learningequality:develop Feb 11, 2025
9 of 10 checks passed
learning-equality-bot bot pushed a commit that referenced this pull request Feb 11, 2025
@shivam-daksh
Copy link
Contributor Author

Big Thanks to @MisRob, @BabyElias and @AlexVelezLl for being with me and guiding through the whole. I'm really grateful for your support and all the learning I've gained along the way.

@shivam-daksh
Copy link
Contributor Author

shivam-daksh commented Feb 11, 2025

Hey @MisRob, I’m ready to dive deeper into fixing complex issues and am fully committed to investing time in learning and improving. Looking forward to growing and contributing even more!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TODO: needs review Waiting for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[KTable]: Improve Shift+Tab key navigation in KTable
3 participants