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

Descriptive aria-labels for Keyboard shortcuts which include punctuation previously ignored by screen readers #666

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
44 changes: 43 additions & 1 deletion packages/widgets/src/commandpalette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,14 @@ export namespace CommandPalette {
*/
renderItemShortcut(data: IItemRenderData): VirtualElement {
let content = this.formatItemShortcut(data);
return h.div({ className: 'lm-CommandPalette-itemShortcut' }, content);
let ariaContent = this.formatItemAria(data);
return h.div(
{
className: 'lm-CommandPalette-itemShortcut',
'aria-label': `${ariaContent}`
},
content
);
}

/**
Expand Down Expand Up @@ -973,6 +980,41 @@ export namespace CommandPalette {
return kb ? CommandRegistry.formatKeystroke(kb.keys) : null;
}

/**
* @returns The aria label content to add to the shortcut node.
*/
formatItemAria(data: IItemRenderData): h.Child {
const keyToText: { [key: string]: string } = {
']': 'Closing bracket',
'[': 'Opening bracket',
',': 'Comma',
'.': 'Full stop',
"'": 'Single quote',
'-': 'Hyphen-minus'
};
Copy link
Member

Choose a reason for hiding this comment

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

I think that we would want to make it easy to override in subclasses of the renderer so that they could reuse the logic below, but change the values on the right-hand side. In particular, JupyterLab will want to substituted the values with translated versions.

One solution would be making keyToText an option that can be passed to the renderer constructor, something like:

constructor(options: IRenderer.IOptions = {}) {
  this._keyToText = options.keyToText ?? DEFAULT_KEY_TO_TEXT;
}

But please read the next comment which tackles the deeper question on where the logic should be placed before implementing the suggestion above.

Copy link
Author

Choose a reason for hiding this comment

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

@krassowski I had tried to use aria-keyshortcuts in place of aria-label however, my screen reader (Windows Narrator) still omits some punctuation. With aria-label it is read out as expected.

I have implemented your other suggestions which should now allow translations to be substituted in JupyterLab


let kbText = data.item.keyBinding;
let result = kbText ? CommandRegistry.formatKeystroke(kbText.keys) : null;

let punctuationRegex = /\p{P}/u;
let punctuations = result?.match(punctuationRegex);
if (!punctuations) {
return [];
}
for (const punctuation of punctuations) {
if (result != null && Object.keys(keyToText).includes(punctuation)) {
const individualKeys = result.split('+');
let index = individualKeys.indexOf(punctuation);
if (index != -1) {
individualKeys[index] = keyToText[punctuation];
}
const textShortcut = individualKeys.join('+');
return textShortcut;
}
}
return kbText ? CommandRegistry.formatKeystroke(kbText.keys) : null;
e218736 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Create the render content for the item label node.
*
Expand Down
44 changes: 43 additions & 1 deletion packages/widgets/src/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,14 @@ export namespace Menu {
*/
renderShortcut(data: IRenderData): VirtualElement {
let content = this.formatShortcut(data);
return h.div({ className: 'lm-Menu-itemShortcut' }, content);
let ariaContent = this.formatShortcutText(data);
return h.div(
{
className: 'lm-Menu-itemShortcut',
'aria-label': `${ariaContent}`
},
content
);
}

/**
Expand Down Expand Up @@ -1371,6 +1378,41 @@ export namespace Menu {
let kb = data.item.keyBinding;
return kb ? CommandRegistry.formatKeystroke(kb.keys) : null;
}

/**
* @returns The aria label content to add to the shortcut node.
*/
formatShortcutText(data: IRenderData): h.Child {
const keyToText: { [key: string]: string } = {
']': 'Closing bracket',
'[': 'Opening bracket',
',': 'Comma',
'.': 'Full stop',
"'": 'Single quote',
'-': 'Hyphen-minus'
};

let kbText = data.item.keyBinding;
let result = kbText ? CommandRegistry.formatKeystroke(kbText.keys) : null;

let punctuationRegex = /\p{P}/u;
let punctuations = result?.match(punctuationRegex);
if (!punctuations) {
return [];
}
for (const punctuation of punctuations) {
if (result != null && Object.keys(keyToText).includes(punctuation)) {
const individualKeys = result.split('+');
let index = individualKeys.indexOf(punctuation);
if (index != -1) {
individualKeys[index] = keyToText[punctuation];
}
const textShortcut = individualKeys.join('+');
return textShortcut;
}
}
return kbText ? CommandRegistry.formatKeystroke(kbText.keys) : null;
Copy link
Member

Choose a reason for hiding this comment

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

All right, this makes it pretty clear that we need to have this implemented at CommandRegistry level (see earlier suggestion on creating CommandRegistry.formatKeystrokeAriaLabel)

Copy link
Author

Choose a reason for hiding this comment

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

Hi @krassowski,

All makes sense, thank you for your comments. I will revert back to draft for now whist I work on these and let you know once ready for review again.

}
}

/**
Expand Down
47 changes: 47 additions & 0 deletions packages/widgets/tests/src/commandpalette.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,20 @@ describe('@lumino/widgets', () => {
keys: ['Ctrl A'],
selector: 'body'
});
commands.addCommand('test-aria', {
label: 'Test Aria',
caption: 'A simple aria-label test',
className: 'testAriaClass',
isEnabled: () => enabledFlag,
isToggled: () => toggledFlag,
execute: () => {}
});
commands.addKeyBinding({
command: 'test-aria',
keys: ['Ctrl ,'],
selector: 'body'
});

item = palette.addItem({
command: 'test',
category: 'Test Category'
Expand Down Expand Up @@ -827,6 +841,39 @@ describe('@lumino/widgets', () => {
expect(child).to.equal('A simple test command');
});
});

describe('#formatItemShortcut()', () => {
it('should format the item shortcut text', () => {
let child = renderer.formatItemShortcut({
item,
indices: null,
active: false
});
if (Platform.IS_MAC) {
expect(child).to.equal('\u2303 A');
} else {
expect(child).to.equal('Ctrl+A');
}
});
});
describe('#formatItemAria', () => {
it('should format the item aria-label', () => {
let item = palette.addItem({
command: 'test-aria',
category: 'Test Category'
});
let child = renderer.formatItemAria({
item,
indices: null,
active: false
});
if (Platform.IS_MAC) {
expect(child).to.equal('\u2303 ,');
} else {
expect(child).to.equal('Ctrl+Comma');
}
});
});
});
});
});
31 changes: 31 additions & 0 deletions packages/widgets/tests/src/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ describe('@lumino/widgets', () => {
className: 'testClass',
mnemonic: 0
});
commands.addCommand('test-aria', {
execute: (args: JSONObject) => {
executed = 'test-aria';
},
label: 'Test Aria Label',
icon: iconRenderer,
iconClass,
caption: 'Test Caption',
className: 'testClass',
mnemonic: 0
});
commands.addCommand('test-toggled', {
execute: (args: JSONObject) => {
executed = 'test-toggled';
Expand Down Expand Up @@ -127,6 +138,11 @@ describe('@lumino/widgets', () => {
selector: 'body',
command: 'test'
});
commands.addKeyBinding({
keys: ['Ctrl ,'],
selector: 'body',
command: 'test-aria'
});
});

beforeEach(() => {
Expand Down Expand Up @@ -1591,6 +1607,21 @@ describe('@lumino/widgets', () => {
expect(child).to.equal('Ctrl+T');
}
});
describe('#formatShortcutText()', () => {
it('should format the item aria-label', () => {
let item = menu.addItem({ command: 'test-aria' });
let child = renderer.formatShortcutText({
item,
active: false,
collapsed: false
});
if (Platform.IS_MAC) {
expect(child).to.equal('\u2303 ,');
} else {
expect(child).to.equal('Ctrl+Comma');
}
});
});
});
});
});
Expand Down
4 changes: 4 additions & 0 deletions review/api/widgets.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ export namespace CommandPalette {
createItemDataset(data: IItemRenderData): ElementDataset;
formatEmptyMessage(data: IEmptyMessageRenderData): h.Child;
formatHeader(data: IHeaderRenderData): h.Child;
// (undocumented)
formatItemAria(data: IItemRenderData): h.Child;
formatItemCaption(data: IItemRenderData): h.Child;
formatItemLabel(data: IItemRenderData): h.Child;
formatItemShortcut(data: IItemRenderData): h.Child;
Expand Down Expand Up @@ -763,6 +765,8 @@ export namespace Menu {
createItemDataset(data: IRenderData): ElementDataset;
formatLabel(data: IRenderData): h.Child;
formatShortcut(data: IRenderData): h.Child;
// (undocumented)
formatShortcutText(data: IRenderData): h.Child;
renderIcon(data: IRenderData): VirtualElement;
renderItem(data: IRenderData): VirtualElement;
renderLabel(data: IRenderData): VirtualElement;
Expand Down
Loading