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

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

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion addon/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { tracked } from '@glimmer/tracking';

import LinkManagerService from './services/link-manager';

const MAIN_BUTTON = 0;

export type QueryParams = Record<string, unknown>;

export function isQueryParams(
Expand Down Expand Up @@ -54,6 +56,14 @@ function freezeParams(params: LinkParams) {
return params;
}

function isUnmodifiedLeftClick(event: MouseEvent): boolean {
return event.button === MAIN_BUTTON && !event.ctrlKey && !event.metaKey;
}

function isMouseEvent(event: unknown): event is MouseEvent {
return typeof event === 'object' && event !== null && 'button' in event;
}

export default class Link {
@tracked
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -262,7 +272,8 @@ export class UILink extends Link {
private preventDefault(event?: Event | unknown) {
if (
(this._params.preventDefault ?? true) &&
typeof (event as Event)?.preventDefault === 'function'
typeof (event as Event)?.preventDefault === 'function' &&
(!isMouseEvent(event) || isUnmodifiedLeftClick(event))
) {
(event as Event).preventDefault();
}
Expand Down