Skip to content

Commit

Permalink
fix: should not transition states when alt-clicked
Browse files Browse the repository at this point in the history
In most browsers, clicking links with the Alt key has a special behavior, for example, Chrome
downloads the target resource. As with other modifier keys, the router should stop the original
navigation to avoid preventing the browser’s default behavior.

When users click a link while holding the Alt key together, the browsers behave as follows.

Windows 10:

| Browser    | Behavior                                    |
|:-----------|:--------------------------------------------|
| Chrome 84  | Download the target resource                |
| Firefox 79 | Prevent navigation and therefore do nothing |
| Edge 84    | Download the target resource                |
| IE 11      | No impact                                   |

macOS Catalina:

| Browser    | Behavior                                    |
|:-----------|:--------------------------------------------|
| Chrome 84  | Download the target resource                |
| Firefox 79 | Prevent navigation and therefore do nothing |
| Safari 13  | Download the target resource                |
  • Loading branch information
yuheiy authored and christopherthielen committed Aug 8, 2020
1 parent c1d9551 commit 8080adb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/directives/stateDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function clickHook(
const button = e.which || e.button,
target = getDef();

if (!(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || el.attr('target'))) {
if (!(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || el.attr('target'))) {
// HACK: This is to allow ng-clicks to be processed before the transition is initiated:
const transition = $timeout(function () {
if (!el.attr('disabled')) {
Expand Down
12 changes: 12 additions & 0 deletions test/stateDirectivesSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ describe('uiStateRef', function () {
expect(obj($stateParams)).toEqual({});
}));

it('should not transition states when alt-clicked', inject(function ($state, $stateParams, $q) {
expect($state.$current.name).toEqual('top');
expect(obj($stateParams)).toEqual({});

triggerClick(el, { altKey: true });
timeoutFlush();
$q.flush();

expect($state.current.name).toEqual('top');
expect(obj($stateParams)).toEqual({});
}));

it('should not transition states when middle-clicked', inject(function ($state, $stateParams, $q) {
expect($state.$current.name).toEqual('top');
expect(obj($stateParams)).toEqual({});
Expand Down

0 comments on commit 8080adb

Please sign in to comment.