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

Handle Cmd / Ctrl click to open in a new tab #6

Closed
buschtoens opened this issue Mar 14, 2019 · 4 comments · Fixed by #473
Closed

Handle Cmd / Ctrl click to open in a new tab #6

buschtoens opened this issue Mar 14, 2019 · 4 comments · Fixed by #473
Labels
enhancement New feature or request

Comments

@buschtoens
Copy link
Owner

When a link is clicked with Cmd / Ctrl it should open in a new tab.

The reason it does not work right now is that we always call event.preventDefault(), if @preventDefault is true / unset.

I think there's different solutions to this.

For one, we can add another boolean: @allowNewTab
If true (default) it will check whether event.currentTarget is an <a> element and if Cmd / Ctrl are pressed, in which case it will not call event.preventDefault(), even if @preventDefault is true.

In addition to this, we can also add another action openInNewTab, that opens href in a new tab.

Big thanks to @tchak13, who reported this via Twitter:
https://twitter.com/tchak13/status/1106149903317852160

@buschtoens buschtoens added the enhancement New feature or request label Mar 14, 2019
@rtablada
Copy link

rtablada commented Mar 14, 2019

@buschtoens In our app we mimicked this code in ember-href-to that checks that the left click does not have modifiers.

https://github.com/intercom/ember-href-to/blob/master/addon/href-to.js#L35-L39

GitHub
🔗 A lightweight alternative to {{link-to}}. Contribute to intercom/ember-href-to development by creating an account on GitHub.

@buschtoens
Copy link
Owner Author

buschtoens commented Mar 15, 2019

Thanks for the link, @rtablada! 😊
I would have forgotten to handle right clicks.

@buschtoens
Copy link
Owner Author

buschtoens commented Mar 15, 2019

There's also the possibility of Cmd or Ctrl clicking a button, which would open the form submission in a new tab. Contrary to <a> I don't think that we would ever want this behavior for <form> in Ember, so I guess by default Cmd or Ctrl clicking a <button> should still be event.preventDefault()'ed.

It's probably easiest to understand, if I don't expose yet another flag and instead just bake all of this (including @allowNewTab) into @preventDefault. If users need more sophisticated behavior, they can just write their own event handler.

@buschtoens
Copy link
Owner Author

buschtoens commented Nov 16, 2020

Released in https://github.com/buschtoens/ember-link/releases/tag/v1.2.1. 🥳

GitHub
🐛 Bug Fix

#473 Check button property and modifier key states before calling preventDefault() (@Turbo87)

Committers: 1

Tobias Bieniek (@Turbo87)

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

Successfully merging a pull request may close this issue.

2 participants