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

Add input focus with keyboard #896

Merged

Conversation

LeWawan
Copy link
Contributor

@LeWawan LeWawan commented Oct 29, 2021

Description

πŸš€ Add ⌘ + k && Ctrl + k to focus the input element. See #894

Tests

  • All tests passed.

@vercel
Copy link

vercel bot commented Oct 29, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

πŸ” Inspect: https://vercel.com/carloscuesta/gitmoji/5DWSyNu3VwTRtJAu3dUwdbJX3fLo
βœ… Preview: https://gitmoji-git-fork-lewawan-add-input-focus-wi-36e459-carloscuesta.vercel.app

src/components/GitmojiList/Toolbar/index.js Outdated Show resolved Hide resolved
src/components/GitmojiList/Toolbar/index.js Outdated Show resolved Hide resolved
src/components/GitmojiList/Toolbar/index.js Outdated Show resolved Hide resolved
src/components/GitmojiList/Toolbar/styles.module.css Outdated Show resolved Hide resolved
src/components/GitmojiList/Toolbar/styles.module.css Outdated Show resolved Hide resolved
Copy link
Collaborator

@vhoyer vhoyer left a 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?

src/components/GitmojiList/Toolbar/index.js Outdated Show resolved Hide resolved
src/components/GitmojiList/Toolbar/index.js Outdated Show resolved Hide resolved
src/components/GitmojiList/Toolbar/index.js Outdated Show resolved Hide resolved
src/components/GitmojiList/Toolbar/styles.module.css Outdated Show resolved Hide resolved
src/components/GitmojiList/Toolbar/styles.module.css Outdated Show resolved Hide resolved
@vhoyer
Copy link
Collaborator

vhoyer commented Oct 29, 2021

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:
LeWawan#1

best

@LeWawan
Copy link
Contributor Author

LeWawan commented Oct 29, 2021

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!

@vhoyer
Copy link
Collaborator

vhoyer commented Oct 29, 2021

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
@LeWawan
Copy link
Contributor Author

LeWawan commented Oct 30, 2021

Hey @carloscuesta

I've updated the PR πŸš€

Thanks to @vhoyer for the help 🀘

Things to change:

  • conditionally render CMD for macOS and CTRL for Windows/Linux systems
  • use useEffect to remove the event when the component unmount
  • use useRef to focus the input
  • update style magic numbers #1 (@vhoyer)
  • ctrl-k focus the browser navigation bar (reported by @vhoyer)

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()
Copy link
Owner

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 😊

https://reactjs.org/docs/hooks-reference.html#useref

return () => {
document.removeEventListener('keydown', keyboardEventListener, false)
}
})
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
})
}, [])

@carloscuesta
Copy link
Owner

The margin that kbd has, is causing a little issue when the input is focused:

Screen Shot 2021-10-30 at 19 26 11

@carloscuesta carloscuesta linked an issue Oct 31, 2021 that may be closed by this pull request
@LeWawan
Copy link
Contributor Author

LeWawan commented Oct 31, 2021

Hey @carloscuesta

I've updated the PR πŸš€

Things to change:

  • Add empty dependency array to useEffect
  • Update the css to prevent weird focus margin

The margin that kbd has, is causing a little issue when the input is focused:

Screen Shot 2021-10-30 at 19 26 11

@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.

Copy link
Owner

@carloscuesta carloscuesta left a comment

Choose a reason for hiding this comment

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

Thanks! Looking much better now, found another issue with the alignment of the kbd indicator:

I'm using Safari Version 15.0 (16612.1.29.41.4, 16612)

Screen Shot 2021-10-31 at 21 04 05

If you can't reproduce it let me know so I can take a look

Thanks for your work! 😊

@LeWawan
Copy link
Contributor Author

LeWawan commented Oct 31, 2021

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;
Copy link
Collaborator

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 πŸ€”

fails all the checks on contrast

Copy link
Contributor Author

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

@vhoyer
Copy link
Collaborator

vhoyer commented Nov 1, 2021

image

do you see the problem with using the indicator above the input, that's why I left if beside the input, if the problem is the outline around the "input/inputWrapper", we could use a custom :focus-within rule to add the outline on the correct visual element.

color: #b8b8b8;
display: flex;
font-family: system-ui;
margin: 0 0.5rem;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
margin: 0 0.5rem;
margin-right: 0.5rem;

It's a good practice to never set properties you are not using with CSS

@LeWawan
Copy link
Contributor Author

LeWawan commented Nov 1, 2021

image

do you see the problem with using the indicator above the input, that's why I left if beside the input, if the problem is the outline around the "input/inputWrapper", we could use a custom :focus-within rule to add the outline on the correct visual element.

@vhoyer I totally agree with the :focus-within. I didn't like the indicator outside the input because i think that it's clearer for the user if it's inside the input.

Comment on lines 23 to 29
const keyboardEventListener = (event: KeyboardEvent) => {
const searchInput = searchInputRef.current
if (searchInput && (event.ctrlKey || event.metaKey) && event.key === 'k') {
event.preventDefault()
searchInput.focus()
}
}
Copy link
Collaborator

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/

Copy link
Contributor Author

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 πŸ˜„

Copy link
Contributor Author

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 ?

Copy link
Collaborator

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;
Copy link
Collaborator

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

Copy link
Contributor Author

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

probably not

Comment on lines +32 to +38
.searchInput:focus-visible {
outline: none;
}

.searchInput:focus {
outline: none;
}
Copy link
Owner

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

Copy link
Contributor Author

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

Copy link
Collaborator

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;
Copy link
Owner

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?

Copy link
Contributor Author

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;
Copy link
Owner

Choose a reason for hiding this comment

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

Same! CSS variable?

Comment on lines +20 to +22
.inputWrapper:focus-within {
outline: -webkit-focus-ring-color auto 1px;
}
Copy link
Owner

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?

Copy link
Contributor Author

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

Copy link
Owner

@carloscuesta carloscuesta left a 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 ❀️

@carloscuesta carloscuesta merged commit 134ba89 into carloscuesta:master Nov 3, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

⌘ + k && Ctrl + k to focus the input element
3 participants