joeattardi / picmo

JavaScript emoji picker. Any app, any framework.
https://picmojs.com
MIT License
1.19k stars 117 forks source link

Initial category button steals focus from an inline picker #218

Closed Tanza3D closed 2 years ago

Tanza3D commented 2 years ago

I'm spawning one of my website's custom popup objects with a picmo inside it, but the second it's created the page immediately scrolls down to the picker, even though it's completely hidden. I suspect this is due to autofocus of an element, even though in my create function I have "autoFocusSearch" set to false. I'd prefer not to use picmo's popup solution if possible, but if that is the only way forward I'm fine to use that.

Page in question: https://osekai.net/profiles/?user=2 (you can see on load it scrolls to around where picmo is) image

https://user-images.githubusercontent.com/33783503/180637745-1e121b54-7600-42e9-aebb-4b65bca0f476.mp4

if (document.getElementById("comments__emoji_container")) {
    var emoji = document.getElementById("comments__emoji_container");
    const picker = picmo.createPicker({
        rootElement: emoji,
        className: 'osekai__emoji-picker',
        autoFocusSearch: false,
    });
    picker.addEventListener('emoji:select', selection => {
        document.getElementById("comments__input").value += selection.emoji;
    });
}

Is this a bug or am I doing something wrong?

joeattardi commented 2 years ago

I think I see what's happening - when autoFocusSearch is false, the picker will still automatically set the focus to the first focusable emoji in the picker. This is left over from when PicMo was only shown in a popup where it makes sense to do this as soon as it is created, but for an inline picker like you have here it doesn't make sense.

I think currently your only options are:

There may be another way to move the focus back, but at that point your page is already scrolled.

I think I need to improve the autofocusing feature by allowing one of three actions:

I can introduce a new option called initialFocus or something that takes one of these three values. This should be easy to do, will follow up with yo soon.

joeattardi commented 2 years ago

This has been implemented and will be included in the 5.4.0 release which is coming soon, there are a few other things I want to include.

There will be a new autoFocus option that accepts one of three values:

The option is not required, so leaving it undefined will also have the same effect as none.

Tanza3D commented 2 years ago

Thank you for the help! It's really appreciated ❤️ - I'll be waiting for the new release! :3

joeattardi commented 2 years ago

I just released v5.4.0 which should fix your issue.

Tanza3D commented 2 years ago

I had to set autoFocus to none for it to work, but after doing that it works flawlessly! Thanks for the help! edit: actually, that's throwing an error.. i'll post another comment when i can get it working

Tanza3D commented 2 years ago

Leaving it undefined still scrolls down, and setting it to none ends up with an error. image

const picker = picmo.createPicker({
    rootElement: emoji,
    className: 'osekai__emoji-picker',
    autoFocus: none
});

I've updated my picmo version to 5.4.0 from the CDN (https://unpkg.com/picmo@5.4.0/dist/umd/picmo.js). Might I be doing something wrong here?

joeattardi commented 2 years ago

none should be a string: autoFocus: 'none', does it work then?

Tanza3D commented 2 years ago

afraid that has not done anything to help. the page is still scrolling down 😅

joeattardi commented 2 years ago

The CDN version you linked includes the change for autoFocus, if the value is 'none' or undefined it should not do any autofocus. Is there something else in your code that could be triggering a focus operation?

Tanza3D commented 2 years ago

if i simply comment out the picmo code, it immediately stops doing the focus/scroll. and beyond theming i haven't modified picmo at all. i can't think of any reason it'd be autoscrolling if simply removing picmo fixes it, sadly

joeattardi commented 2 years ago

There must be something else going on. Are you sure that it's a focus operation that is causing the scroll? Your site looks pretty complicated but any chance you can provide a simpler example that exhibits the problem?

I will reopen this issue since it looks like the autoFocus option did not resolve the issue.

Tanza3D commented 2 years ago

i've made a little codepen which shows off the issue, hope it helps! https://codepen.io/Hubz/pen/VwXQoZK

joeattardi commented 2 years ago

That was super helpful! I have found the issue. When setting the initial category, I was accidentally focusing the button which is what's causing your issue.

I want to do some further testing to make sure I didn't break focus elsewhere, but should have a fix ready soon.

joeattardi commented 2 years ago

Hi, I just published version 5.4.1. Can you try this out and let me know if it fixes your issue?

Tanza3D commented 2 years ago

that's fixed it, thank you so much!