-
Notifications
You must be signed in to change notification settings - Fork 77
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
fix(menu-item): Improve keyboard navigability when href
populated
#8408
Merged
macandcheese
merged 7 commits into
main
from
macandcheese/8135-allow-keyboard-navigation-menu-item-href
Dec 15, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c81a595
fix(menu-item): Improve keyboard navigability when `href` populated
macandcheese 8395aab
Merge branch 'main' into macandcheese/8135-allow-keyboard-navigation-…
macandcheese 1470f7c
Clean up
macandcheese cf029e3
Merge branch 'main' into macandcheese/8135-allow-keyboard-navigation-…
macandcheese 79d2057
Clean up
macandcheese 6ddb92d
Merge branch 'main' into macandcheese/8135-allow-keyboard-navigation-…
macandcheese d0135b9
Merge branch 'main' into macandcheese/8135-allow-keyboard-navigation-…
macandcheese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -266,61 +266,54 @@ export class CalciteMenuItem implements LoadableComponent, T9nComponent, Localiz | |
}; | ||
|
||
private keyDownHandler = async (event: KeyboardEvent): Promise<void> => { | ||
// opening and closing of submenu is handled here. Any other functionality is bubbled to parent menu. | ||
if (event.key === " " || event.key === "Enter") { | ||
this.selectMenuItem(event); | ||
if ( | ||
this.hasSubmenu && | ||
(!this.href || (this.href && event.target === this.dropdownActionEl)) | ||
) { | ||
this.open = !this.open; | ||
const { hasSubmenu, href, layout, open, submenuItems } = this; | ||
const key = event.key; | ||
const targetIsDropdown = event.target === this.dropdownActionEl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @anveshmekala this look ok to you? I think at some point we might want to break the dropdown key / click and the anchor key / click apart so we can simplify some of this logic. |
||
if (key === " " || key === "Enter") { | ||
macandcheese marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (hasSubmenu && (!href || (href && targetIsDropdown))) { | ||
macandcheese marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.open = !open; | ||
} | ||
event.preventDefault(); | ||
} else if (event.key === "Escape") { | ||
if (this.open) { | ||
if (!(href && targetIsDropdown) && key !== "Enter") { | ||
this.selectMenuItem(event); | ||
} | ||
if (key === " " || (href && targetIsDropdown)) { | ||
event.preventDefault(); | ||
} | ||
} else if (key === "Escape") { | ||
if (open) { | ||
this.open = false; | ||
return; | ||
} | ||
this.calciteInternalMenuItemKeyEvent.emit({ event }); | ||
event.preventDefault(); | ||
} else if (event.key === "ArrowDown" || event.key === "ArrowUp") { | ||
} else if (key === "ArrowDown" || key === "ArrowUp") { | ||
event.preventDefault(); | ||
if ( | ||
(event.target === this.dropdownActionEl || !this.href) && | ||
this.hasSubmenu && | ||
!this.open && | ||
this.layout === "horizontal" | ||
) { | ||
if ((targetIsDropdown || !href) && hasSubmenu && !open && layout === "horizontal") { | ||
macandcheese marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.open = true; | ||
return; | ||
} | ||
this.calciteInternalMenuItemKeyEvent.emit({ | ||
event, | ||
children: this.submenuItems, | ||
isSubmenuOpen: this.open && this.hasSubmenu, | ||
children: submenuItems, | ||
isSubmenuOpen: open && hasSubmenu, | ||
}); | ||
} else if (event.key === "ArrowLeft") { | ||
} else if (key === "ArrowLeft") { | ||
event.preventDefault(); | ||
this.calciteInternalMenuItemKeyEvent.emit({ | ||
event, | ||
children: this.submenuItems, | ||
children: submenuItems, | ||
isSubmenuOpen: true, | ||
}); | ||
} else if (event.key === "ArrowRight") { | ||
} else if (key === "ArrowRight") { | ||
event.preventDefault(); | ||
if ( | ||
(event.target === this.dropdownActionEl || !this.href) && | ||
this.hasSubmenu && | ||
!this.open && | ||
this.layout === "vertical" | ||
) { | ||
if ((targetIsDropdown || !href) && hasSubmenu && !open && layout === "vertical") { | ||
this.open = true; | ||
return; | ||
} | ||
this.calciteInternalMenuItemKeyEvent.emit({ | ||
event, | ||
children: this.submenuItems, | ||
isSubmenuOpen: this.open && this.hasSubmenu, | ||
children: submenuItems, | ||
isSubmenuOpen: open && hasSubmenu, | ||
}); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@driskull @jcfranco any other ideas for how to test this? All I could come up with is checking for a named anchor with
#
, anyhref
that changed the full url, I wasn't having luck testing against.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this works. I don’t think we can use Puppeteer’s
waitForNavigation
method, but this seems like a good alternative. If you were able to make the test fail when expected (e.g. removing/bypassing the fix), this should be good to go. 🚀