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

Focus search input when modal shown #115

Merged
merged 5 commits into from
Sep 12, 2023
Merged

Conversation

humitos
Copy link
Member

@humitos humitos commented Sep 7, 2023

This is a small hack for now.
We should find a cleaner way to solve this issue.
It works fow now :)

Closes #78

This is a small hack for now.
We should find a cleaner way to solve this issue.
It works fow now :)

Closes #78
@humitos humitos requested a review from a team as a code owner September 7, 2023 16:41
@agjohnson
Copy link
Contributor

Part of me feels like we should honor the site/theme keyboard shortcut, even though it's in conflict with our UI. This is the main problem with relying on keyboard shortcuts, it's always an arms race of sorts to ensure your code captures the event first.

The approach with firing a timer off probably works for most cases, but it will effectively duplicate the key press event. If the theme/site handler for the existing key press has side effects other than focus, like showing up of a separate search modal or something, then the reader will get that UI and our search modal UI. That is, the use of a timer doesn't intercept the event, but duplicates it.

I know you can manipulate previously registered event handlers, however I've never done this for third party code. Normally, you know the exact function you are trying to deregister. If you can find the existing handler, it's possible to wrap that handler and intercept the keypress event for just /, bubbling all other event through to the handler.

@humitos
Copy link
Member Author

humitos commented Sep 7, 2023

Part of me feels like we should honor the site/theme keyboard shortcut, even though it's in conflict with our UI.

I'm not sure what this means. Can you expand on this?

The approach with firing a timer off probably works for most cases, but it will effectively duplicate the key press event. If the theme/site handler for the existing key press has side effects other than focus, like showing up of a separate search modal or something, then the reader will get that UI and our search modal UI. That is, the use of a timer doesn't intercept the event, but duplicates it.

Yeah, I've already hit this with Material for MkDocs, yeah. I'm not sure what to do in those cases.

I know you can manipulate previously registered event handlers, however I've never done this for third party code. Normally, you know the exact function you are trying to deregister. If you can find the existing handler, it's possible to wrap that handler and intercept the keypress event for just /, bubbling all other event through to the handler.

I researched about this and I wasn't able to de-register event handlers from others. I'm tracking this case and the one from the previous paragraph in #25


I know the hack from this PR is not good, but I don't have anything better at the moment and I thought that at least it was better than the current behavior, but I know this is not the solution 🤷🏼

@agjohnson
Copy link
Contributor

I'm not sure what this means. Can you expand on this?

Just that we shouldn't try to replace the override keyboard shortcut. These shortcuts may not be there for any particular reason, but unfortunately the shortcuts might also be important for the site.

Yeah, I've already hit this with Material for MkDocs, yeah. I'm not sure what to do in those cases.

Ah yes, that has a UI there too. There really isn't a great answer here, as we don't control the website we're injecting the features into. I don't think this is the primary method we want for triggering this UI long term anyways though, so maybe this is okay.

This, among other reasons like screen reader overlap, is why I'm generally -1 on implementing keyboard shortcuts (and I've been known to use qutebrowser!)

I researched about this and I wasn't able to de-register event handlers from others.

Yeah, I can only say it's possible when you have the instance of the function handy. Otherwise, I don't know how feasible deregistering is.

I know the hack from this PR is not good, but I don't have anything better at the moment

I might lean towards not doing anything magical or unexpected here. If the user wants our search/flyout/etc, they should disable whatever is interfering with the hotkeys. Long term, we should avoid relying on shortcuts to trigger our UI, but I understand this is just a short term solution to avoid UI that we haven't added yet.

We could make a secondary keyboard shortcut available perhaps? ie / or C-k?

@humitos
Copy link
Member Author

humitos commented Sep 8, 2023

Ah yes, that has a UI there too. There really isn't a great answer here, as we don't control the website we're injecting the features into. I don't think this is the primary method we want for triggering this UI long term anyways though, so maybe this is okay.

Agreed. I imagine we can probably just document "How to disable the default search on Material for MkDocs" and "How to disable the default search on Sphinx" and other doctools giving people a chunk of code or similar and rely on that.

This, among other reasons like screen reader overlap, is why I'm generally -1 on implementing keyboard shortcuts (and I've been known to use qutebrowser!)

Regarding using or not using keyboard shortcuts at all, we have this conversation opened: #80. I was on the fence, but considering that GitHub makes extensive usage of them, I'm kind of convinced they are not bad.

I might lean towards not doing anything magical or unexpected here. If the user wants our search/flyout/etc, they should disable whatever is interfering with the hotkeys. Long term, we should avoid relying on shortcuts to trigger our UI, but I understand this is just a short term solution to avoid UI that we haven't added yet.

Yes, even having a UI element this may be required. Well, at least right now that we are using an input field inside the flyout that triggers the search modal. This is because the focus stays in the input field from the flyout instead of jumping to the input from the search modal. However, with this PR, it works fine and moves to the input from the search modal. At least, it fixes that issue 😄

We could make a secondary keyboard shortcut available perhaps? ie / or C-k?

Yes. I did some tests with C-k, but it requires a little more work since you also have to check that none of the other modifiers are pressed and things like that. It was not trivial / quick when I tried it. GitHub uses s or / to open the search bar (see #80).

@humitos
Copy link
Member Author

humitos commented Sep 8, 2023

Are we OK merging this half-baked solution for now? At least it solves the problem in our own theme. We can continue with the proper solution in the other issues that I linked.

@agjohnson
Copy link
Contributor

Agreed. I imagine we can probably just document "How to disable the default search on Material for MkDocs" and "How to disable the default search on Sphinx" and other doctools giving people a chunk of code or similar and rely on that.

👍 This seems like a great place to start for now. If we do have to hack things in later to support themes/tools, I'd consider that work polish level adjustments for later in our roadmap. I bet we'll find better solutions in the meantime though.

Regarding using or not using keyboard shortcuts at all, we have this conversation opened: #80. I was on the fence, but considering that GitHub makes extensive usage of them, I'm kind of convinced they are not bad.

Aye, though I'm generally -1, I still feel shortcuts are okay as an addition to addons.

I just don't like focusing on this as a primary part of our UX as it's much harder to discover and use as a feature, especially for less technical users. As an alternative UX, it's usually okay though, as long as adaptive technologies and normal browser use aren't negatively affected.

Yes. I did some tests with C-k, but it requires a little more work

Yup, modifiers make this more complicated. Keyboard support is a rabbit hole and there is a lot that goes into doing keyboard support well.

Are we OK merging this half-baked solution for now?

There are definitely some downsides to this approach, and I feel there is probably a more standard fix for the problem. The focus issue is almost certainly either an async code issue or a matter of CSS transitions throwing the timing off.

I'd try to rule out async troubles first, as it seems the most likely. I believe we need to be using the updateComplete event, as the focus() call is firing before the template renders. For example, this fix describes updateComplete usage on an element in the template:

lit/lit-element#605 (comment)

Otherwise, I would have assumed that the Element.updated event is what we would have wanted to sync the event up correctly:

https://lit.dev/docs/v1/components/lifecycle/#updated

I'd give those a try first if you haven't already.

@humitos
Copy link
Member Author

humitos commented Sep 9, 2023

Otherwise, I would have assumed that the Element.updated event is what we would have wanted to sync the event up correctly

Excellent! This was it 🚀 . In fact, in the documentation link you shared, there is the exact same case I need here 😅 . It works perfect now without any type of hack.

Aye, though I'm generally -1, I still feel shortcuts are okay as an addition to addons.

Man, I don't know if this is what you are thinking here but maybe the "keyboard shortcuts" is an addon on itself that people can enable/disable 🤯 . I'm now convinced this is the way to go here. I will open an issue for this.

@humitos
Copy link
Member Author

humitos commented Sep 9, 2023

Man, I don't know if this is what you are thinking here but maybe the "keyboard shortcuts" is an addon on itself that people can enable/disable 🤯 . I'm now convinced this is the way to go here. I will open an issue for this.

Boom! 💣 I implemented this idea in #118

@humitos
Copy link
Member Author

humitos commented Sep 9, 2023

Agreed. I imagine we can probably just document "How to disable the default search on Material for MkDocs" and "How to disable the default search on Sphinx" and other doctools giving people a chunk of code or similar and rely on that.

👍 This seems like a great place to start for now. If we do have to hack things in later to support themes/tools, I'd consider that work polish level adjustments for later in our roadmap. I bet we'll find better solutions in the meantime though.

Sphinx also catches / to focus on the search input from the navbar: https://github.com/sphinx-doc/sphinx/blob/fcc38997f1d9b728bb4ffc64fc362c7763a4ee25/sphinx/themes/basic/static/doctools.js#L146

It seems there is an option to disable it, DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS, but I didn't dig enough to know where to put that yet.

@humitos
Copy link
Member Author

humitos commented Sep 11, 2023

This PR is ready to be merged 👍🏼

@agjohnson
Copy link
Contributor

Excellent, I'm glad this approach worked out. I think your idea to split out keyboard shortcuts as it's own addon is an interesting one, definitely worth testing that more. But I like that we have a way to surface configuration of hotkeys already 👍

@humitos humitos merged commit c4e406c into main Sep 12, 2023
2 of 4 checks passed
@humitos humitos deleted the humitos/focus-search-input branch September 12, 2023 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Search: when hitting / it shows the modal but it does not focus it
2 participants