keyboard-extra icon indicating copy to clipboard operation
keyboard-extra copied to clipboard

No way to do preventDefault

Open pixelprizm opened this issue 8 years ago • 5 comments

Hi, Is there a way to turn on preventDefault for a keypress (or all keypresses)?

I know there's been some discussion about preventDefault at: https://github.com/elm-lang/virtual-dom/issues/18 , but that discussion seems to be mainly about key presses that are sent to an Input component, etc.

Specifically, when I press Tab, it's handled by Keyboard.Extra, but Chrome also focuses elements on the page (including going to the URL bar); this messes up subsequent key presses being handled the right way.

pixelprizm avatar May 27 '17 00:05 pixelprizm

Can you add another (js) listener to prevent it?

document.documentElement.addEventListener('keydown', function (e) {
    if (e.which == 9) { // 9 is tab
        e.preventDefault();
    }
}, false);

loganhenson avatar May 27 '17 02:05 loganhenson

Ah, that works perfectly! I guess it's unfortunate that we need to go to JS to do it, but it doesn't involve the boilerplate of ports, so it's an easy workaround. Thanks!

pixelprizm avatar May 27 '17 19:05 pixelprizm

Awesome, are you OK to close this @egauderman ?

loganhenson avatar May 28 '17 00:05 loganhenson

Yeah, it's not possible to preventDefault on document in Elm. I suppose I could add a note in the readme, but then I'd want to point out that you shouldn't disable the accessibility features built into browsers...

ohanhi avatar May 29 '17 09:05 ohanhi

I think it would be good to make a note in the readme -- I think it'll save people some time in the future. My issue is solved, so I'm fine if you want to close the issue.

pixelprizm avatar Jun 08 '17 07:06 pixelprizm