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).

See [Actions Controlled](?path=/story/components-tree--actions-controlled) for managing menu state when actions are hidden.

> ⚠️ 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,84 @@
import * as React from 'react';
import type { JSXElement } from '@fluentui/react-components';
import {
Button,
Menu,
MenuItem,
MenuList,
MenuPopover,
MenuTrigger,
Tree,
TreeItem,
TreeItemLayout,
useRestoreFocusTarget,
} from '@fluentui/react-components';
import { MoreHorizontal20Regular } from '@fluentui/react-icons';

const ItemWithControlledMenu = ({ children }: { children: string }): JSXElement => {
const [open, setOpen] = React.useState(false);
const focusTargetAttribute = useRestoreFocusTarget();
const treeItemRef = React.useRef<HTMLDivElement>(null);

return (
<TreeItem ref={treeItemRef} itemType="leaf" aria-description="has actions" {...focusTargetAttribute}>
<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 />}
onPointerDownCapture={() => {
if (!open) {
treeItemRef.current?.focus({ preventScroll: true });
}
}}
/>
</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: `
Hidden actions are unmounted without notifying the menu's \`onOpenChange\`. Controlled state held outside the slot survives, so the menu can reopen when actions return.
Keep separate state for each item: update it from \`onOpenChange\` and reset \`open\` when \`actions.onVisibilityChange\` reports \`data.visible === false\`.

Mark the persistent \`TreeItem\` with \`useRestoreFocusTarget\` and focus it before pointer-opening a closed menu to establish the restoration target.
Skip this handoff when the menu is open so clicking its trigger can dismiss it.

Try opening an item's menu, hovering the other item, then returning. The actions should reappear with the menu closed.

As in the [Actions example](?path=/story/components-tree--actions), also provide a context menu with the same commands for standard tree interaction.
`,
},
},
};
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