Skip to content

Commit bcd9639

Browse files
RajdeepcRajdeep Chandra
andauthored
fix(button): double tab navigation when clicking button/action-button with href and target="_blank" (#5937)
* fix(button): fix anchorlink href double tab opening * chore: added changeset * fix: added link click on space --------- Co-authored-by: Rajdeep Chandra <rajdeepc@adobe.com>
1 parent 266f629 commit bcd9639

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@spectrum-web-components/button': patch
3+
---
4+
5+
**Fixed** issue where clicking `sp-button` or `sp-action-button` with `href` and `target="_blank"` opened two tabs in Chrome instead of one.
6+
7+
The fix checks if the anchor element is already in the click event's composed path before triggering a proxy click. When the user clicks directly on the button, the absolutely-positioned anchor naturally receives the click, so no proxy is needed. The proxy click is now only triggered for keyboard activation and VoiceOver, where the anchor isn't in the click path.

1st-gen/packages/button/src/ButtonBase.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,17 @@ export class ButtonBase extends ObserveSlotText(LikeAnchor(Focusable), '', [
114114
}
115115

116116
if (this.anchorElement) {
117+
// Check if the click already went through the anchor element.
118+
// If so, the browser will handle navigation naturally and we
119+
// don't need to proxy the click (which would cause double navigation).
120+
const path = event?.composedPath() || [];
121+
if (path.includes(this.anchorElement)) {
122+
return false;
123+
}
117124
// Click HTML anchor element by proxy, but only for non-modified clicks
118125
this.anchorElement.click();
119126
handled = true;
127+
return handled;
120128
// if the button type is `submit` or `reset`
121129
} else if (this.type !== 'button') {
122130
// create an HTML Button Element by proxy, click it, and remove it
@@ -159,11 +167,9 @@ export class ButtonBase extends ObserveSlotText(LikeAnchor(Focusable), '', [
159167
switch (code) {
160168
case 'Space':
161169
event.preventDefault();
162-
// allows button to activate when `Space` is pressed
163-
if (typeof this.href === 'undefined') {
164-
this.addEventListener('keyup', this.handleKeyup);
165-
this.active = true;
166-
}
170+
// allows button or link to activate when `Space` is pressed
171+
this.addEventListener('keyup', this.handleKeyup);
172+
this.active = true;
167173
break;
168174
default:
169175
break;

1st-gen/packages/link/src/spectrum-link.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
}
4848

4949
a {
50+
/* stylelint-disable-next-line declaration-property-value-no-unknown */
5051
text-decoration-skip: objects;
5152
color: var(--highcontrast-link-text-color, var(--mod-link-text-color, var(--mod-link-text-color-primary-default, var(--spectrum-accent-content-color-default))));
5253
text-decoration: underline;

1st-gen/packages/radio/src/spectrum-radio.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
position: fixed;
8888
inset-block-end: 100%;
8989
inset-inline-end: 100%;
90+
/* stylelint-disable-next-line property-no-deprecated */
9091
clip: rect(1px, 1px, 1px, 1px);
9192
clip-path: inset(50%);
9293
}

0 commit comments

Comments
 (0)