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

RFE: Autofocus the search input #470

Open
arv opened this issue Oct 29, 2024 · 5 comments
Open

RFE: Autofocus the search input #470

arv opened this issue Oct 29, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@arv
Copy link

arv commented Oct 29, 2024

What is the best way to autofocus the text input today?

In the future it would be nice to have an option to autofocus the input.

@arv
Copy link
Author

arv commented Oct 29, 2024

Here is my current workaround:

// The emoji-picker-element does not allow auto focusing the search input
// when it's first rendered. We can work around this by observing the
// shadow DOM and focusing the search input when it's added to the DOM.
if (el.shadowRoot) {
  const m = new MutationObserver(records => {
    for (const record of records) {
      for (const node of record.addedNodes) {
        if (node.nodeType === Node.ELEMENT_NODE) {
          const search = (node as Element).querySelector('#search');
          if (search) {
            (search as HTMLElement).focus();
            m.disconnect();
          }
          return;
        }
      }
    }
  });

  m.observe(el.shadowRoot, {
    subtree: true,
    childList: true,
  });
}

arv added a commit to rocicorp/mono that referenced this issue Oct 29, 2024
This required some fun coding... MutationObserver to the
rescue.

I also filed an issue to support this better:

nolanlawson/emoji-picker-element#470
arv added a commit to rocicorp/mono that referenced this issue Oct 29, 2024
This required some fun coding... MutationObserver to the
rescue.

I also filed an issue to support this better:

nolanlawson/emoji-picker-element#470
arv added a commit to rocicorp/mono that referenced this issue Oct 29, 2024
This required some fun coding... MutationObserver to the
rescue.

I also filed an issue to support this better:

nolanlawson/emoji-picker-element#470
@nolanlawson
Copy link
Owner

I don't think you need a MutationObserver. If you are the one rendering the picker, then you can wait a requestAnimationFrame and then focus the input.

Could you describe your usecase and why you'd like it to autofocus?

@nolanlawson nolanlawson added the enhancement New feature or request label Oct 30, 2024
@arv
Copy link
Author

arv commented Oct 30, 2024

I don't think you need a MutationObserver. If you are the one rendering the picker, then you can wait a requestAnimationFrame and then focus the input.

Maybe, it would be simpler but I do not generally like waiting for things. I think it would still involve a "loop" until the input is found.

Could you describe your usecase and why you'd like it to autofocus?

I'm showing the emoji picker in a popover. When the popover is shown I focus the text input so I can search right away.

Screen.Recording.2024-10-30.at.16.01.12.mov

@nolanlawson
Copy link
Owner

requestAnimationFrame is not really a wait. The browser hasn't rendered anything yet, so it's a good time to do a focus. You could probably also wait a Promise.resolve() tick or two.

@arv
Copy link
Author

arv commented Oct 31, 2024

It is next frame which should be totally fine for focus since it does not need to change the dom.

I'll try it out but I still wish I didn't have to drill into the shadow dom like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants