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: wc menu keyboarding and aria-expanded #3496

Merged
merged 6 commits into from
Jul 14, 2020
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
Expand Up @@ -46,4 +46,14 @@ <h4>With standard elements</h4>
<div role="menuitem">Menu item 2</div>
<div role="menuitem">Menu item 3</div>
</fast-menu>

<h4>With radio buttons</h4>
<fast-menu>
<fast-menu-item>Menu item 1</fast-menu-item>
<fast-menu-item>Menu item 2</fast-menu-item>
<fast-menu-item>Menu item 3</fast-menu-item>
<fast-divider></fast-divider>
<fast-menu-item role="menuitemradio">Menu item 4</fast-menu-item>
<fast-menu-item role="menuitemradio">Menu item 5</fast-menu-item>
</fast-menu>
</fast-design-system-provider>
2 changes: 2 additions & 0 deletions packages/web-components/fast-foundation/docs/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ export type MediaQueryListListener = (this: MediaQueryList, ev?: MediaQueryListE
export class Menu extends FASTElement {
// @internal (undocumented)
connectedCallback(): void;
// @internal (undocumented)
disconnectedCallback(): void;
focus(): void;
// @internal
handleFocusOut: (e: FocusEvent) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const MenuItemTemplate = html<MenuItem>`
role="${x => x.role}"
aria-checked="${x => (x.role !== MenuItemRole.menuitem ? x.checked : void 0)}"
aria-disabled="${x => x.disabled}"
aria-expanded="${x => x.expanded}"
@keydown="${(x, c) => x.handleMenuItemKeyDown(c.event as KeyboardEvent)}"
@click="${(x, c) => x.handleMenuItemClick(c.event as MouseEvent)}"
class="${x => (x.disabled ? "disabled" : "")} ${x =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export class MenuItem extends FASTElement {
*
* @public
* @remarks
* HTML Attribute: aria-expanded
* HTML Attribute: expanded
*/
@attr({ attribute: "aria-expanded", mode: "reflect", converter: booleanConverter })
public expanded: boolean = false;
@attr({ attribute: "expanded" })
public expanded: boolean;
Comment on lines +39 to +40
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be aria-expanded or AT will not recognize it as expanded. The attr should stay the same here. I think we you'll want is to keep the boolean attribute converter but change "reflect" to "fromView".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We write to the "aria-expanded" attribute in the template, just like we do for disabled:
aria-expanded="${x => x.expanded}"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it's undefined we don't render a value, which fixes the bug

Copy link
Member

Choose a reason for hiding this comment

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

gotcha, thanks for the additional context.


/**
* The role of the element.
Expand Down
25 changes: 16 additions & 9 deletions packages/web-components/fast-foundation/src/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ export class Menu extends FASTElement {
public connectedCallback(): void {
super.connectedCallback();
this.menuItems = this.domChildren();
this.setItems();
}

/**
* @internal
*/
public disconnectedCallback(): void {
super.disconnectedCallback();
this.menuItems = [];
}

/**
Expand All @@ -65,29 +72,29 @@ export class Menu extends FASTElement {
* @internal
*/
public handleMenuKeyDown(e: KeyboardEvent): void | boolean {
if (e.defaultPrevented) {
return;
}
switch (e.keyCode) {
case keyCodeArrowDown:
case keyCodeArrowRight:
// go forward one index
e.preventDefault();
this.setFocus(this.focusIndex + 1, 1);
break;
return;
case keyCodeArrowUp:
case keyCodeArrowLeft:
// go back one index
e.preventDefault();
this.setFocus(this.focusIndex - 1, -1);
break;
return;
case keyCodeEnd:
// set focus on last item
e.preventDefault();
this.setFocus(this.domChildren().length - 1, -1);
break;
return;
case keyCodeHome:
// set focus on first item
e.preventDefault();
this.setFocus(0, 1);
break;
return;

default:
// if we are not handling the event, do not prevent default
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ export const fastMenuItemDefinition: WebComponentDefinition = {
required: false,
},
{
name: "aria-expanded",
name: "expanded",
type: DataType.boolean,
description: "The aria-expanded attribute",
description: "The expanded attribute",
default: true,
required: false,
},
{
name: "role",
type: DataType.boolean,
description: "The aria-expanded attribute",
description: "The role attribute",
default: MenuItemRole.menuitem,
values: [
{ name: MenuItemRole.menuitem },
Expand Down