Skip to content

Commit 792f7b9

Browse files
committed
improve documentation
1 parent 6c99908 commit 792f7b9

5 files changed

Lines changed: 40 additions & 16 deletions

File tree

‎src/components/ContextOverlay/ContextMenu.stories.tsx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ const Template: StoryFn<typeof ContextMenu> = (args) => (
2323

2424
export const Default = Template.bind({});
2525
Default.args = {
26+
role: "menu",
2627
children: [
27-
<MenuItem key="m0" text={"First option"} />,
28-
<MenuItem key="m1" text={"Item two"}>
29-
<MenuItem key="m2" text={"First sub option"} />
30-
<MenuItem key="m3" text={"Sub item two"} />
28+
<MenuItem key="m0" roleStructure="menuitem" text={"First option"} />,
29+
<MenuItem key="m1" roleStructure="menuitem" text={"Item two"}>
30+
<MenuItem key="m2" roleStructure="menuitem" text={"First sub option"} />
31+
<MenuItem key="m3" roleStructure="menuitem" text={"Sub item two"} />
3132
</MenuItem>,
3233
],
3334
};

‎src/components/Menu/Menu.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ type SupportedMenuRole = "menu" | "navigation" | "list" | "listbox" | "none" | "
1818
export interface MenuProps extends BlueprintMenuProps {
1919
/**
2020
* Sets the ARIA role of the menu.
21-
* For `menu`, `navigation`, `list`, `listbox`, `none` and `presentation` it checks if the `MenuItems` using a correct `roleStructure`,
22-
* or set it automatically.
23-
* Other roles are set as they are, the children are not processed then.
21+
* It may become mandatory in a future version.
22+
* Setting a `role` checks whether `MenuItem` and `MenuDivider` children use a correct `roleStructure`, or sets it automatically.
23+
* The automatic checks only work if `MenuItem` and `MenuDivider` are used as direct children or inside a `Fragment`.
24+
* The checks only work for `menu`, `navigation`, `list`, `listbox`, `none` and `presentation`.
25+
* Other roles are applied as they are, and the children are not processed.
2426
*/
2527
role?: SupportedMenuRole | AriaRole;
2628
}

‎src/components/Menu/MenuItem.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ export interface MenuItemProps
3434
* Visual intent color to apply to element.
3535
*/
3636
intent?: BlueprintMenuItemProps["intent"] | "accent";
37+
/**
38+
* Defines the ARIA role structure that is used for the menu item element and its inner target element.
39+
* It is recommended to set it, even if it is optional right now.
40+
* The value is checked if a `role` is set on the parent `Menu`.
41+
* It may become mandatory in a future version.
42+
*/
43+
roleStructure?: BlueprintMenuItemProps["roleStructure"];
3744
}
3845

3946
/**
4047
* Single item, used as child inside `Menu`.
48+
* It is recommended to use it as a direct child and not to wrap it inside other elements, except `Fragment`.
4149
*/
4250
export const MenuItem = React.forwardRef<HTMLLIElement, MenuItemProps>(
4351
({ children, className = "", icon, onClick, href, text, tooltip, intent, ...restProps }, ref) => {

‎src/components/Menu/Stories/Menu.stories.tsx‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,21 @@ const MenuExample: StoryFn<typeof Menu> = (args) => (
2525

2626
export const Default = MenuExample.bind({});
2727
Default.args = {
28+
role: "menu",
2829
children: (
2930
<>
3031
{ContentMenuStory.args.children}
31-
<MenuDivider title="Second menu part" />
32+
<MenuDivider role="separator" title="Second menu part" />
3233
<MenuItem {...MenuItemStory.args} active />
3334
</>
3435
),
3536
};
3637

3738
/**
3839
* Use `role="navigation"` for a list of links, e.g. as main or sidebar navigation.
39-
* The `MenuItem` elements get `roleStructure="listitem"` automatically.
40+
* The `MenuItem` elements need `roleStructure="listitem"`, it is set automatically if missing.
41+
* `MenuDivider` elements need `role="listitem"` because `separator` is not allowed inside a list,
42+
* it is set automatically if missing. Dividers without `title` get `aria-hidden` automatically.
4043
* `Menu` does not add a `nav` element, so you need to wrap it on your own.
4144
*/
4245
const NavigationExample: StoryFn<typeof Menu> = (args) => (
@@ -51,9 +54,18 @@ export const Navigation = NavigationExample.bind({});
5154
Navigation.args = {
5255
role: "navigation",
5356
children: [
54-
<MenuItem key="1" text="Dashboard" icon="application-homepage" href="#dashboard" active aria-current="page" />,
55-
<MenuItem key="2" text="Projects" icon="artefact-project" href="#projects" />,
56-
<MenuItem key="3" text="Datasets" icon="artefact-dataset" href="#datasets" />,
57-
<MenuItem key="4" text="Settings" icon="application-useraccount" href="#settings" />,
57+
<MenuItem
58+
key="1"
59+
roleStructure="listitem"
60+
text="Dashboard"
61+
icon="application-homepage"
62+
href="#dashboard"
63+
active
64+
aria-current="page"
65+
/>,
66+
<MenuItem key="2" roleStructure="listitem" text="Projects" icon="artefact-project" href="#projects" />,
67+
<MenuItem key="3" roleStructure="listitem" text="Datasets" icon="artefact-dataset" href="#datasets" />,
68+
<MenuDivider key="d1" role="listitem" />,
69+
<MenuItem key="4" roleStructure="listitem" text="Settings" icon="application-useraccount" href="#settings" />,
5870
],
5971
};

‎src/components/Menu/Stories/MenuItem.stories.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default {
2929

3030
const MenuExample: StoryFn<typeof MenuItem> = (args) => (
3131
<OverlaysProvider>
32-
<Menu style={{ width: "200px" }}>
32+
<Menu role="menu" style={{ width: "200px" }}>
3333
<MenuItem {...args} />
3434
</Menu>
3535
</OverlaysProvider>
@@ -39,10 +39,11 @@ export const Default = MenuExample.bind({});
3939
Default.args = {
4040
children: (
4141
<>
42-
<MenuItem key="m6" text={"Sub option 1"} />
43-
<MenuItem key="m7" text={"Sub option 2"} />
42+
<MenuItem key="m6" roleStructure="menuitem" text={"Sub option 1"} />
43+
<MenuItem key="m7" roleStructure="menuitem" text={"Sub option 2"} />
4444
</>
4545
),
4646
text: "Parent option",
47+
roleStructure: "menuitem",
4748
key: "m4",
4849
};

0 commit comments

Comments
 (0)