Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ In addition to \`aside\` slot, both tree item layouts support \`actions\` slot t

The \`actions\` slot has a \`role="toolbar"\` and ensures proper horizontal navigation with the keyboard by using [\`useArrowNavigationGroup\`](https://react.fluentui.dev/?path=/docs/utilities-focus-management-usearrownavigationgroup--default).

For a controlled menu in \`actions\`, reset its \`open\` state when the slot's \`onVisibilityChange\` reports \`data.visible === false\`. Hidden actions are unmounted, but controlled state held outside the slot is preserved. See [Actions Controlled](?path=/story/components-tree--actions-controlled) for an example.

> ⚠️ Although \`actions\` are easy to navigate, they're not an expected pattern according to [WAI-ARIA](https://www.w3.org/WAI/ARIA/apg/patterns/treeview/).providing a context menu with the same functionalities as the actions is recommended to ensure your tree item is accessible.

In the example below, we compose on top of \`TreeItem\` component to include both a context menu and actions that provide the same amount of functionalities. We also provide an \`aria-description\` to the tree item to indicate that it has actions. This is a new behavior that the user might not be aware of, so you might need to explain somewhere else in the UI what does having actions refers to.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as React from 'react';
import type { JSXElement } from '@fluentui/react-components';
import {
Button,
Menu,
MenuItem,
MenuList,
MenuPopover,
MenuTrigger,
Tree,
TreeItem,
TreeItemLayout,
} from '@fluentui/react-components';
import { MoreHorizontal20Regular } from '@fluentui/react-icons';

const ItemWithControlledMenu = ({ children }: { children: string }): JSXElement => {
const [open, setOpen] = React.useState(false);

return (
<TreeItem itemType="leaf" aria-description="has actions">
Comment thread
PaulGMardling marked this conversation as resolved.
Resolved
<TreeItemLayout
actions={{
onVisibilityChange: (_, data) => {
if (!data.visible) {
setOpen(false);
}
},
children: (
<Menu open={open} onOpenChange={(_, data) => setOpen(data.open)}>
<MenuTrigger disableButtonEnhancement>
<Button
aria-label={`More options for ${children}`}
appearance="subtle"
icon={<MoreHorizontal20Regular />}
/>
</MenuTrigger>
<MenuPopover>
<MenuList>
<MenuItem>Open</MenuItem>
<MenuItem>Rename</MenuItem>
</MenuList>
</MenuPopover>
</Menu>
),
}}
>
{children}
</TreeItemLayout>
</TreeItem>
);
};

export const ActionsControlled = (): JSXElement => (
<Tree aria-label="Controlled action menus">
<ItemWithControlledMenu>item 1</ItemWithControlledMenu>
<ItemWithControlledMenu>item 2</ItemWithControlledMenu>
</Tree>
);

ActionsControlled.parameters = {
docs: {
description: {
story: `
When a \`Menu\` in the \`actions\` slot is controlled, update its \`open\` state from both the menu's \`onOpenChange\` and the slot's \`onVisibilityChange\`.
Reset \`open\` to \`false\` when \`data.visible\` is \`false\`; leave it unchanged when the actions become visible.

Hidden actions are unmounted. Unmounting a menu does not reset controlled state stored in the tree item or notify \`onOpenChange\` that the menu has closed.
Without this reset, a menu whose state is still \`open\` remounts open when the actions become visible again.
Keep separate menu state for each tree item, outside the actions subtree.

Open an item's menu, move to the other item so the first item's actions disappear, then return.
The first item's actions should reappear with its menu closed, until you open it again.
The same reset applies when focus leaves the actions.

This example focuses on controlled state. As with the [Actions example](?path=/story/components-tree--actions),
provide a context menu with the same commands so actions are also available through the standard tree interaction pattern.
`,
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export { ExpandIcon } from './TreeExpandIcon.stories';
export { IconBeforeAndAfter } from './TreeIconBeforeAndAfter.stories';
export { Aside } from './TreeAside.stories';
export { Actions } from './TreeActions.stories';
export { ActionsControlled } from './TreeActionsControlled.stories';
export { NavigationModeTreeGrid } from './TreeNavigationModeTreeGrid.stories';

// FUNCTIONAL EXAMPLES
Expand Down
Loading