Skip to content

Commit 5bbe4de

Browse files
authored
fix(web-components): stopImmediatePropagation on internalProxyAnchors (#36085)
1 parent 410796d commit 5bbe4de

4 files changed

Lines changed: 55 additions & 7 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "fix: stopImmediatePropagation on internalProxyAnchors",
4+
"packageName": "@fluentui/web-components",
5+
"email": "rupertdavid@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/web-components/src/anchor-button/anchor-button.base.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,13 @@ export class BaseAnchor extends FASTElement {
171171
* @internal
172172
*/
173173
public clickHandler(e: PointerEvent): boolean {
174+
if (e.composedPath()[0] === this.internalProxyAnchor) {
175+
e.stopImmediatePropagation();
176+
return true;
177+
}
178+
174179
if (this.href) {
175-
const newTab = !this.isMac ? e.ctrlKey : e.metaKey;
176-
if (newTab) {
177-
e.preventDefault();
178-
}
180+
const newTab = e.ctrlKey || e.metaKey;
179181
this.handleNavigation(newTab);
180182
}
181183

@@ -203,7 +205,7 @@ export class BaseAnchor extends FASTElement {
203205

204206
/**
205207
* Handles navigation based on input
206-
* If the metaKey is pressed, opens the href in a new window, if false, uses the click on the proxy
208+
* If a modified activation requests a new tab, opens the href in a new window.
207209
* @internal
208210
*/
209211
private handleNavigation(newTab: boolean): void {
@@ -226,8 +228,7 @@ export class BaseAnchor extends FASTElement {
226228

227229
private createProxyElement(): HTMLAnchorElement {
228230
const proxy = this.internalProxyAnchor ?? document.createElement('a');
229-
proxy.ariaHidden = 'true';
230-
proxy.tabIndex = -1;
231+
proxy.inert = true;
231232
return proxy;
232233
}
233234
}

packages/web-components/src/anchor-button/anchor-button.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,25 @@ test.describe('Anchor Button', () => {
177177
await expect(page).toHaveURL(expectedUrl);
178178
});
179179

180+
test('should emit a single click event when clicked', async ({ fastPage, page }) => {
181+
const { element } = fastPage;
182+
183+
await fastPage.setTemplate({ attributes: { href: '#foo' } });
184+
185+
await element.evaluate(anchorButton => {
186+
let clickCount = 0;
187+
anchorButton.addEventListener('click', () => {
188+
clickCount += 1;
189+
anchorButton.setAttribute('data-click-count', String(clickCount));
190+
});
191+
anchorButton.setAttribute('data-click-count', '0');
192+
});
193+
194+
await element.click();
195+
196+
await expect(element).toHaveAttribute('data-click-count', '1');
197+
});
198+
180199
test('should navigate to the provided url when clicked while pressing the `Control` key on Windows or `Meta` on Mac', async ({
181200
fastPage,
182201
context,

packages/web-components/src/link/link.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,25 @@ test.describe('Link', () => {
8080

8181
await expect(element).not.toHaveAttribute('inline');
8282
});
83+
84+
test('should emit a single click event when clicked', async ({ fastPage, page }) => {
85+
const { element } = fastPage;
86+
87+
await fastPage.setTemplate({ attributes: { href: '#foo' } });
88+
89+
await element.evaluate((link: Link) => {
90+
let clickCount = 0;
91+
link.addEventListener('click', () => {
92+
clickCount += 1;
93+
link.setAttribute('data-click-count', String(clickCount));
94+
});
95+
link.setAttribute('data-click-count', '0');
96+
});
97+
98+
await element.evaluate((node: HTMLElement) => {
99+
node.click();
100+
});
101+
102+
await expect(element).toHaveAttribute('data-click-count', '1');
103+
});
83104
});

0 commit comments

Comments
 (0)