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

chore: Add proper textual name, role and state to breadcrumb last item #3258

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 18 additions & 18 deletions src/breadcrumb-group/__integ__/breadcrumb-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,29 @@
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await expect(page.getActiveElementId()).resolves.toBe('focus-target-short-text');

Check warning on line 166 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 1: BreadcrumbGroup › Focus does not go into ghost replica

expect(received).resolves.toBe(expected) // Object.is equality Expected: "focus-target-short-text" Received: "" at Object.toBe (node_modules/expect/build/index.js:174:22) at setupTest.width (src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:166:58) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at Object.<anonymous> (node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13)

Check warning on line 166 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 2: BreadcrumbGroup › Focus does not go into ghost replica

expect(received).resolves.toBe(expected) // Object.is equality Expected: "focus-target-short-text" Received: "" at Object.toBe (node_modules/expect/build/index.js:174:22) at setupTest.width (src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:166:58) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at Object.<anonymous> (node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13)

Check warning on line 166 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 3: BreadcrumbGroup › Focus does not go into ghost replica

expect(received).resolves.toBe(expected) // Object.is equality Expected: "focus-target-short-text" Received: "" at Object.toBe (node_modules/expect/build/index.js:174:22) at setupTest.width (src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:166:58) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at Object.<anonymous> (node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13)

Check failure on line 166 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

BreadcrumbGroup › Focus does not go into ghost replica

expect(received).resolves.toBe(expected) // Object.is equality Expected: "focus-target-short-text" Received: "" at Object.toBe (node_modules/expect/build/index.js:174:22) at setupTest.width (src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:166:58) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at Object.<anonymous> (node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13)
},
{ width: 1200, height: 800 }
)
);

test(
'Last item is focusable when truncated',
setupTest(
async page => {
await page.click('#focus-target-long-text');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await expect(page.isTooltipDisplayed()).resolves.toBe(true);
await page.keys('Tab');
await expect(page.isTooltipDisplayed()).resolves.toBe(false);
await expect(page.getActiveElementId()).resolves.toBe('focus-target-short-text');
},
{ width: 950, height: 800 }
)
test.each([
[{ width: 770, height: 800 }, 'when truncated'],
[{ width: 1200, height: 800 }, 'default state'],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
])('Last item should be focusable %s', (viewportSize, scenario) =>
setupTest(async page => {
await page.click('#focus-target-long-text');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await expect(page.isTooltipDisplayed()).resolves.toBe(true);

Check warning on line 185 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 1: BreadcrumbGroup › Last item should be focusable { width: 1200

expect(received).resolves.toBe(expected) // Object.is equality Expected: true Received: false at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:185:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13

Check warning on line 185 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 2: BreadcrumbGroup › Last item should be focusable { width: 1200

expect(received).resolves.toBe(expected) // Object.is equality Expected: true Received: false at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:185:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13

Check warning on line 185 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 3: BreadcrumbGroup › Last item should be focusable { width: 1200

expect(received).resolves.toBe(expected) // Object.is equality Expected: true Received: false at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:185:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13

Check failure on line 185 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

BreadcrumbGroup › Last item should be focusable { width: 1200

expect(received).resolves.toBe(expected) // Object.is equality Expected: true Received: false at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:185:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13
await page.keys('Tab');
await expect(page.isTooltipDisplayed()).resolves.toBe(false);
await expect(page.getActiveElementId()).resolves.toBe('focus-target-short-text');
}, viewportSize)()
);
});
8 changes: 7 additions & 1 deletion src/breadcrumb-group/__tests__/breadcrumb-item.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,16 @@ describe('BreadcrumbGroup Item', () => {

test('should not be a link', () => {
expect(lastLink.getElement()).not.toHaveAttribute('href');
expect(lastLink.getElement()).not.toHaveAttribute('aria-current');
expect(lastLink.getElement().tagName).toEqual('SPAN');
});

test('should have proper aria-labels and focusable', () => {
expect(lastLink.getElement()).toHaveAttribute('aria-current');
expect(lastLink.getElement()).toHaveAttribute('aria-disabled');
expect(lastLink.getElement().getAttribute('role')).toBe('link');
expect(lastLink.getElement().getAttribute('tabindex')).toBe('0');
});

test('should not trigger click event', () => {
lastLink.click();
expect(onClickSpy).not.toHaveBeenCalled();
Expand Down
30 changes: 24 additions & 6 deletions src/breadcrumb-group/item/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ interface BreadcrumbItemWithPopoverProps<T extends BreadcrumbGroupProps.Item> {
isLast: boolean;
anchorAttributes: React.AnchorHTMLAttributes<HTMLAnchorElement>;
children: React.ReactNode;
itemAttributes: React.HTMLAttributes<HTMLElement>;
}

const BreadcrumbItemWithPopover = <T extends BreadcrumbGroupProps.Item>({
item,
isLast,
anchorAttributes,
itemAttributes,
children,
}: BreadcrumbItemWithPopoverProps<T>) => {
const [showPopover, setShowPopover] = useState(false);
Expand Down Expand Up @@ -54,7 +56,7 @@ const BreadcrumbItemWithPopover = <T extends BreadcrumbGroupProps.Item>({
}}
onMouseLeave={() => setShowPopover(false)}
anchorAttributes={anchorAttributes}
{...(isLast ? { tabIndex: 0 } : {})}
{...itemAttributes}
>
{children}
</Item>
Expand All @@ -68,16 +70,17 @@ type ItemProps = React.HTMLAttributes<HTMLElement> & {
isLast: boolean;
};
const Item = React.forwardRef<HTMLElement, ItemProps>(
({ anchorAttributes, children, isLast, ...itemAttributes }, ref) =>
isLast ? (
({ anchorAttributes, children, isLast, ...itemAttributes }, ref) => {
return isLast ? (
<span ref={ref} className={styles.anchor} {...itemAttributes}>
{children}
</span>
) : (
<a ref={ref as React.Ref<HTMLAnchorElement>} className={styles.anchor} {...itemAttributes} {...anchorAttributes}>
{children}
</a>
)
);
}
);

export function BreadcrumbItem<T extends BreadcrumbGroupProps.Item>({
Expand All @@ -101,11 +104,21 @@ export function BreadcrumbItem<T extends BreadcrumbGroupProps.Item>({
const anchorAttributes: React.AnchorHTMLAttributes<HTMLAnchorElement> = {
href: item.href || '#',
onClick: isLast ? preventDefault : onClickHandler,
tabIndex: 0,
};

const itemAttributes: React.AnchorHTMLAttributes<HTMLAnchorElement> = {};
if (isGhost) {
anchorAttributes.tabIndex = -1;
}

if (isLast && !isGhost) {
itemAttributes['aria-current'] = 'page';
itemAttributes['aria-disabled'] = true;
itemAttributes.tabIndex = 0;
itemAttributes.role = 'link';
}

const breadcrumbItem = (
<FunnelBreadcrumbItem
className={styles.text}
Expand All @@ -119,11 +132,16 @@ export function BreadcrumbItem<T extends BreadcrumbGroupProps.Item>({
return (
<div className={clsx(!isGhost && styles.breadcrumb, isGhost && styles['ghost-breadcrumb'], isLast && styles.last)}>
{isTruncated && !isGhost ? (
<BreadcrumbItemWithPopover item={item} isLast={isLast} anchorAttributes={anchorAttributes}>
<BreadcrumbItemWithPopover
item={item}
isLast={isLast}
anchorAttributes={anchorAttributes}
itemAttributes={itemAttributes}
>
{breadcrumbItem}
</BreadcrumbItemWithPopover>
) : (
<Item isLast={isLast} anchorAttributes={anchorAttributes}>
<Item isLast={isLast} anchorAttributes={anchorAttributes} {...itemAttributes}>
{breadcrumbItem}
</Item>
)}
Expand Down
Loading