-
Notifications
You must be signed in to change notification settings - Fork 526
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
UnderlineNav2: Only allow Enter
and Space
to select item
#2553
UnderlineNav2: Only allow Enter
and Space
to select item
#2553
Conversation
🦋 Changeset detectedLatest commit: ce590a3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
size-limit report 📦
|
if ([' ', 'Enter'].includes(event.key)) { | ||
if (!event.defaultPrevented && typeof onSelect === 'function') onSelect(event) | ||
if (!event.defaultPrevented && typeof afterSelect === 'function') afterSelect(event) | ||
setSelectedLink(ref as RefObject<HTMLElement>) | ||
} |
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.
Just wanted to leave a potential refactor idea, shouldn't change any functionality. Feel free to dismiss if you think they aren't really helping anything 👀
- Since there are only two members in
[' ', 'Enter']
it might make sense to just explicitly check for both of them. If there are more expected members, though, I totally would get having a collection - Instead of doing the explicit guard for each callback, this might be able to be simplified into a single
if
branch and use the optional chaining syntaxname?.()
if ([' ', 'Enter'].includes(event.key)) { | |
if (!event.defaultPrevented && typeof onSelect === 'function') onSelect(event) | |
if (!event.defaultPrevented && typeof afterSelect === 'function') afterSelect(event) | |
setSelectedLink(ref as RefObject<HTMLElement>) | |
} | |
if (event.key === ' ' || event.key === 'Enter') { | |
if (!event.defaultPrevented) { | |
onSelect?.(event) | |
afterSelect?.(event); | |
} | |
setSelectedLink(ref as RefObject<HTMLElement>) | |
} |
Just a couple of thoughts, feel free to disregard if they're not helpful!
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 like it ✨ They are helpful!!
onSelect?.(event)
makes sense. How do I make sure that it is a function type as well? Or checking its type isn't necessary here at all? 🤔
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.
@broccolinisoup for the type of onSelect
, do you know if it's either a function or undefined/null or could it be types other than those two?
I think if it's the former (can be a function or undefined), then it should be good. If it's the latter, then I think you're totally right in pointing out that you would need a type guard for it and what you already have is exactly what would be needed 👍
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.
Because onSelect
is an exposed event to the API, I feel like it might worth to check the type. It is unlikely that will error out when there is type checking and proper linting rules in place on consumer side but still just to be double cautious to prevent them passing things on to onSelect
other than functions. I'd like to keep the function type checking if that is okay with you and see how it goes 👀
I'll apply your other suggestions on a commit from my local ✨
Addressing an accessibility sign-off review issue that described below.
Merge checklist
Take a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.