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

ENH Better use of ARIA attributes #264

Merged
merged 1 commit into from
Apr 4, 2024
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
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion client/src/components/LinkField/tests/LinkField-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,27 @@ test('LinkField will render disabled state if disabled is true', async () => {
}) });
await screen.findByText('Page title');
const linkPicker = container.querySelectorAll('#link-picker__link-123');
expect(linkPicker[0]).toHaveAttribute('aria-disabled');
expect(linkPicker[0].getAttribute('aria-disabled')).toBe('true');
expect(linkPicker[0]).toHaveClass('link-picker__link--disabled');
});

test('LinkField will have aria-disabled true if readonly is true', async () => {
const { container } = render(<LinkField {...makeProps({
ownerID: 1,
readonly: true
})}
/>);
await doResolve({ json: () => ({
123: {
title: 'Page title',
typeKey: 'mylink',
}
}) });
await screen.findByText('Page title');
const linkPicker = container.querySelectorAll('#link-picker__link-123');
expect(linkPicker[0].getAttribute('aria-disabled')).toBe('true');
});

test('Empty disabled LinkField will display cannot edit message', async () => {
const { container } = render(<LinkField {...makeProps({
ownerID: 1,
Expand Down
24 changes: 9 additions & 15 deletions client/src/components/LinkPicker/LinkPickerTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ const LinkPickerTitle = ({
event.preventDefault();
};

const handleIconKeyDown = (event) => {
if (!['Enter', 'Space'].includes(event.code)) {
return;
}
const el = event.target;
const newVal = el.getAttribute('aria-pressed') === 'true' ? 'false' : 'true';
el.setAttribute('aria-pressed', newVal);
};

const style = {
transform: CSS.Transform.toString(transform),
transition,
Expand All @@ -112,18 +103,22 @@ const LinkPickerTitle = ({
if ([versionStates.draft, versionStates.modified].includes(versionState)) {
onUnpublishedVersionedState();
}
// Remove the default tabindex="0" attribute from the sortable element because we're going to manually
// add this to the drag handle instead
delete attributes.tabIndex;

// dndkit will not set aria-pressed to undefined by default meaning it won't be an
// html attribute. it's a little weird so set it to default to false if it's not set
if (!attributes.hasOwnProperty('aria-pressed')) {
attributes['aria-pressed'] = false;
}

const idAttr = `link-picker__link-${id}`;
const Tag = isMulti ? 'li' : 'div';
return <Tag
className={className}
ref={setNodeRef}
style={style}
{...attributes}
{...listeners}
id={idAttr}
aria-disabled={readonly || disabled}
>
<Button
aria-label={ariaLabel}
Expand Down Expand Up @@ -160,12 +155,11 @@ const LinkPickerTitle = ({
}
</Button>
{ (isMulti && !readonly && !disabled) && <div className="link-picker__drag-handle"
{...attributes}
tabIndex="0"
role="button"
aria-pressed="false"
aria-controls={idAttr}
aria-label="Sort Links"
onKeyDown={handleIconKeyDown}
>
<i
className="font-icon-drag-handle"
Expand Down
13 changes: 1 addition & 12 deletions client/src/components/LinkPicker/tests/LinkPickerTitle-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global jest, test */

import React, { createRef } from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import { LinkFieldContext } from 'components/LinkField/LinkField';
Expand Down Expand Up @@ -182,14 +182,3 @@ test('dnd handler is not displayed if link field is not MultiLinkField', () => {
</LinkFieldContext.Provider>);
expect(container.querySelectorAll('.link-picker__drag-handle')).toHaveLength(0);
});

test('keydown on dnd handler', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this test because the aria-pressed attribute is now controlled by dndkit's attributes, not with custom code. In a jest context this fails though real-life it works fine

const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}>
<LinkPickerTitle {...makeProps({ isMulti: true })}/>
</LinkFieldContext.Provider>);
expect(container.querySelectorAll('.link-picker__drag-handle')).toHaveLength(1);
container.querySelector('.link-picker__drag-handle').focus();
fireEvent.keyDown(document.activeElement || document.body, { key: 'Enter', code: 'Enter', charCode: 13 });
expect(container.querySelector('.link-picker__drag-handle').getAttribute('aria-pressed')).toBe('true');
expect(container.querySelector('.link-picker__drag-handle').getAttribute('aria-label')).toBe('Sort Links');
});
Loading