Skip to content

Commit

Permalink
fix: Check button property and modifier key states before calling `…
Browse files Browse the repository at this point in the history
…preventDefault()` (#473)
  • Loading branch information
Turbo87 authored Nov 16, 2020
1 parent 41f93c3 commit 17122c4
Showing 1 changed file with 12 additions and 1 deletion.
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

0 comments on commit 17122c4

Please sign in to comment.