Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Hotkeys are fired when trying to type into a text field inside a custom element #123

Open
kolaente opened this issue Mar 12, 2024 · 4 comments

Comments

@kolaente
Copy link

kolaente commented Mar 12, 2024

When typing in an input (textarea, input tag, editable, …) which is itself placed inside of a custom html element, the hotkey is still fired. That's because the isFormField handler checks only the target element, which is this case is the custom element. It should instead use the explicitOriginalTarget property, since that contains the actual input element.

Here's a minimal reproduction: https://jsfiddle.net/zm7etdh3/

@kolaente
Copy link
Author

My initial attemt for a fix:

diff --git a/dist/index.js b/dist/index.js
index b6e6e0a6864cb00bc085b8d4503a705cb3bc8404..91a107f0e562f68baac0f71f83cef93f0178076d 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -368,10 +368,10 @@ const sequenceTracker = new SequenceTracker({
 function keyDownHandler(event) {
     if (event.defaultPrevented)
         return;
-    if (!(event.target instanceof Node))
+    const target = (event.explicitOriginalTarget || event.originalTarget) || event.target;
+    if (!(target instanceof Node))
         return;
-    if (isFormField(event.target)) {
-        const target = event.target;
+    if (isFormField(target)) {
         if (!target.id)
             return;
         if (!target.ownerDocument.querySelector(`[data-hotkey-scope="${target.id}"]`))
@@ -385,7 +385,6 @@ function keyDownHandler(event) {
     sequenceTracker.registerKeypress(event);
     currentTriePosition = newTriePosition;
     if (newTriePosition instanceof Leaf) {
-        const target = event.target;
         let shouldFire = false;
         let elementToFire;
         const formField = isFormField(target);

Unfortunately, this works only in Firefox, because the explicitOriginalTarget does not exist in Chromium based browser. I haven't found an equivalent.

What do you think about disabling hotkeys altogether when the keydown event originated from a custom element?

@keithamus
Copy link
Member

We could change the check to something like event.composedPath().some(isFormField) - that would work for open shadowroots, but not closed ones.

@kolaente
Copy link
Author

Isn't the shadow root open anyways when the event target is the custom element?

@keithamus
Copy link
Member

keithamus commented Mar 12, 2024

If you change the mode: 'open' to mode: 'closed' then it won’t be, and the composedPath will exclude items from the ShadowDOM, but that’s probably fine - there isn’t much we can do about it.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants