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
Add input focus with keyboard #896
Add input focus with keyboard #896
Conversation
This pull request is being automatically deployed with Vercel (learn more). π Inspect: https://vercel.com/carloscuesta/gitmoji/5DWSyNu3VwTRtJAu3dUwdbJX3fLo |
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 tested this, and in my machine it doesn't work on chrome, as chrome focuses on the URL Bar when ctrl-k
, maybe you forgot a preventDefault
?
I've also created this PR onto your branch to help you with the CSS changes, you can also propose suggestions to the changes I made there: best |
Thanks for all your feedback @vhoyer and @carloscuesta ! I'm coming from vuejs, so I'm not too familiar with best practices in React but I'll do my best to improve the PR! |
Oh, no problem, I also come from vuejs so I know the struggle haha |
β¦stions π Prevent magic numbers and shift in layout βΏ Choose better colors for accessibility/contrast
Hey @carloscuesta I've updated the PR π Thanks to @vhoyer for the help π€ Things to change:
P.S @vhoyer i've update the position of the keyboard indicator in the input, update the colors and check with WCAG. If you want to discuss of the position of the indicator and/or the colors, i'm open. |
/> | ||
</div> | ||
) | ||
const searchInputRef = createRef() |
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 would try to use useRef instead π
return () => { | ||
document.removeEventListener('keydown', keyboardEventListener, false) | ||
} | ||
}) |
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.
}) | |
}, []) |
Hey @carloscuesta I've updated the PR π Things to change:
@carloscuesta I can't reproduce the bug in my environment (Mac & Windows) but I have tweaked the css and I think it should fix the issue. |
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 time it's the right one π I tested on the same version as you : Version 15.0 (16612.1.29.41.4, 16612) Thank you for let me do the feature π |
align-items: center; | ||
border-radius: 3px; | ||
border: solid 1px #999; | ||
color: #b8b8b8; |
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.
P.S @vhoyer i've update the position of the keyboard indicator in the input, update the colors and check with WCAG.
I've just checked it out and it seems to fail the tests π€
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.
Oh boy, I tested it with the dark theme π
I update the code to put back the value you put for the light theme (#595959) and I put (#b8b8b8) for the dark theme
color: #b8b8b8; | ||
display: flex; | ||
font-family: system-ui; | ||
margin: 0 0.5rem; |
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.
margin: 0 0.5rem; | |
margin-right: 0.5rem; |
It's a good practice to never set properties you are not using with CSS
@vhoyer I totally agree with the |
β¦tmoji into add-input-focus-with-keyboard
const keyboardEventListener = (event: KeyboardEvent) => { | ||
const searchInput = searchInputRef.current | ||
if (searchInput && (event.ctrlKey || event.metaKey) && event.key === 'k') { | ||
event.preventDefault() | ||
searchInput.focus() | ||
} | ||
} |
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.
It's best you create this function inside the useEffect, you can read more about it here: https://overreacted.io/a-complete-guide-to-useeffect/
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'll check it out, thanks for the tip π
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.
Okay. got it ! Each render has it's own "instance" so it's better to create the function inside. However in this specific situation, it's not required for the right functioning to do it but rather to follow good practices right ?
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.
correct, good practice prevents future errors
} | ||
|
||
.inputWrapper:focus-within { | ||
outline: black auto 1px; |
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 haven't tested this as I'm in mobile right now, but, isn't there an "initial" value to be used here?, I'm worried about this black on the dark theme
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've foud this value: -webkit-focus-ring-color
but i wonder if it's compatible with all the browsers
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.
probably not
.searchInput:focus-visible { | ||
outline: none; | ||
} | ||
|
||
.searchInput:focus { | ||
outline: none; | ||
} |
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 we should keep this for a11y
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'm not sure but i think the focus-within keep the accessibility okay
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.
It's not a problem to remove the outline if we provide an suitable replacement for it :D
right: 0; | ||
align-items: center; | ||
border-radius: 3px; | ||
border: solid 1px #999; |
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.
Maybe we should move this color into a CSS variable to support both light and dark themes?
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.
It already support light and dark theme actually, i've tested it with a WCAG tool to make it usable for both
align-items: center; | ||
border-radius: 3px; | ||
border: solid 1px #999; | ||
color: #595959; |
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.
Same! CSS variable?
.inputWrapper:focus-within { | ||
outline: -webkit-focus-ring-color auto 1px; | ||
} |
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.
Why do we need to specify this?
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.
This one is the replacement for the focus && focus-visible: none
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.
π
Thanks for the work and time you put into this β€οΈ
Description
π Add β + k && Ctrl + k to focus the input element. See #894
Tests