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

fix: support users setting tabindex attribute for button #32003

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
chrisdholt marked this conversation as resolved.
Show resolved Hide resolved
"type": "prerelease",
"comment": "fix: support tabindex values from implementors on buttons",
"packageName": "@fluentui/web-components",
"email": "[email protected]",
"dependentChangeType": "patch"
}
18 changes: 18 additions & 0 deletions packages/web-components/src/button/button.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,24 @@ test.describe('Button', () => {
expect(ReceivedNoKeyDownEvent2).toBeTruthy();
});

test('should NOT receive focus when the `tabindex` is manually set to -1', async ({ page }) => {
const element = page.locator('fluent-button', { hasText: 'Button' });
const focusable = page.locator('fluent-button', { hasText: 'Focusable' });

await page.setContent(/* html */ `
<fluent-button>Focusable</fluent-button>
<fluent-button tabindex="-1">Button</fluent-button>
`);

await focusable.focus();

await expect(focusable).toBeFocused();

await focusable.press('Tab');

await expect(element).not.toBeFocused();
});

test('should focus the element when the `autofocus` attribute is present', async ({ page }) => {
const element = page.locator('fluent-button');

Expand Down
2 changes: 1 addition & 1 deletion packages/web-components/src/button/button.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ButtonOptions } from './button.options.js';
export function buttonTemplate<T extends Button>(options: ButtonOptions = {}): ElementViewTemplate<T> {
return html<T>`
<template
tabindex="${x => (x.disabled ? -1 : 0)}"
tabindex="${x => (x.disabled ? -1 : x.tabIndex ?? 0)}"
@click="${(x, c) => x.clickHandler(c.event as MouseEvent)}"
@keypress="${(x, c) => x.keypressHandler(c.event as KeyboardEvent)}"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { CompoundButtonOptions } from './compound-button.options.js';
*/
export function buttonTemplate<T extends CompoundButton>(options: CompoundButtonOptions = {}): ElementViewTemplate<T> {
return html<T>`
<template ?disabled="${x => x.disabled}" tabindex="${x => (x.disabled ? -1 : 0)}">
<template ?disabled="${x => x.disabled}" tabindex="${x => (x.disabled ? -1 : x.tabIndex ?? 0)}">
${startSlotTemplate(options)}
<span class="content" part="content">
<slot ${slotted('defaultSlottedContent')}></slot>
Expand Down
Loading