-
Notifications
You must be signed in to change notification settings - Fork 91
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
Comments
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,
});
} |
This required some fun coding... MutationObserver to the rescue. I also filed an issue to support this better: nolanlawson/emoji-picker-element#470
This required some fun coding... MutationObserver to the rescue. I also filed an issue to support this better: nolanlawson/emoji-picker-element#470
This required some fun coding... MutationObserver to the rescue. I also filed an issue to support this better: nolanlawson/emoji-picker-element#470
I don't think you need a MutationObserver. If you are the one rendering the picker, then you can wait a Could you describe your usecase and why you'd like it to autofocus? |
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.
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 |
|
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. |
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.
The text was updated successfully, but these errors were encountered: