Skip to content

Commit

Permalink
ENH Better use of ARIA attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Mar 28, 2024
1 parent bc1f7c9 commit 0efdfdd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
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
20 changes: 5 additions & 15 deletions client/src/components/LinkPicker/LinkPickerTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,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 @@ -116,25 +107,24 @@ 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;

const idAttr = `link-picker__link-${id}`;
return <div
className={className}
ref={setNodeRef}
style={style}
{...attributes}
{...listeners}
id={idAttr}
aria-disabled={readonly || disabled}
>
{ (isMulti && !readonly && !disabled) && <div className="link-picker__drag-handle"
tabIndex="0"
role="button"
aria-pressed="false"
aria-pressed={attributes['aria-pressed']}
aria-describedby={attributes['aria-describedby']}
aria-roledescription={attributes['aria-roledescription']}
aria-controls={idAttr}
aria-label="Sort Links"
onKeyDown={handleIconKeyDown}
>
<i
className="font-icon-drag-handle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,5 @@ test('keydown on dnd handler', async () => {
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');
});

0 comments on commit 0efdfdd

Please sign in to comment.