# All files / src/layout useLayoutTree.ts

100% Statements 202/202
100% Branches 8/8
100% Functions 3/3
100% Lines 202/202

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    2031x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    2x
    2x
    1x
    1x
    1x
    1x
    1x
    9x
    9x
    9x
    9x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    6x
    6x
    6x
    6x
    6x
    6x
    6x
    6x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
     "use client";
    import { useEffect, useMemo } from "react";
    import type { TreeProps } from "../tree/Tree.js";
    import type {
      DefaultTreeItemNode,
      TreeData,
      TreeItemDefaultIds,
      TreeItemNode,
    } from "../tree/types.js";
    import type { TreeImplementation } from "../tree/useTree.js";
    import { useTreeExpansion } from "../tree/useTreeExpansion.js";
    import { getTreeItemsFrom } from "../tree/utils.js";

    const noop = (): void => {
      // do nothing
    };

    /**
     * @internal
     */
    const getParentIds = (
      itemId: string,
      navItems: TreeData<TreeItemNode>
    ): readonly string[] =>
      getTreeItemsFrom(navItems, itemId).map(({ itemId }) => itemId);

    /**
     * @remarks \@since 6.0.0 Removed the `linkComponent` option.
     */
    export interface LayoutTreeOptions<
      T extends TreeItemNode = DefaultTreeItemNode,
    > {
      /**
       * The current pathname which is used as the tree `itemId`.
       */
      pathname: string;

      /**
       * @example
       * ```tsx
       * const navItems = {
       *   "/": {
       *     itemId: "/",
       *     parentId: null,
       *     children: "Home",
       *     leftAddon: <HomeIcon />,
       *     to: "/",
       *   },
       *   "/route-1": {
       *     itemId: "/route-1",
       *     parentId: null,
       *     children: "Route 1",
       *     leftAddon: <TvIcon />,
       *     to: "/route-1",
       *   },
       *   "/route-2": {
       *     itemId: "/route-2",
       *     parentId: null,
       *     children: "Route 2",
       *     leftAddon: <AppsIcon />,
       *     to: "/route-2",
       *   },
       *   "/route-3": {
       *     itemId: "/route-3",
       *     parentId: null,
       *     children: "Route 3",
       *     leftAddon: <BookIcon />,
       *     to: "/route-3",
       *   },
       * } satisfies TreeData;
       * ```
       */
      navItems: TreeData<T>;

      /**
       * @defaultValue `getParentIds(pathname, navItems)`
       */
      defaultExpandedIds?: TreeItemDefaultIds;
    }

    /**
     * @remarks \@since 6.0.0 Renamed from `LayoutNavigationState`, removed the
     * `linkComponent`, and remap `navItems` to `data` so it can be passed to the
     * `Tree` component.
     */
    export interface LayoutTreeImplementation<
      T extends TreeItemNode = DefaultTreeItemNode,
    > extends Pick<TreeProps<T>, "data">,
        TreeImplementation {}

    /**
     * Before considering to use a `Tree` for site navigation, it is important to
     * understand:
     * - There is a lot of functionality required to implement the tree widget so
     *   your bundle size will increase
     * - A pattern more suited for typical site navigation with expandable groups of
     *   links is the disclosure pattern.
     * - @see https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/
     *
     * @see https://www.w3.org/WAI/ARIA/apg/patterns/treeview/examples/treeview-navigation/
     *
     * This is a pretty reasonable default implementation for having a navigation
     * tree within the Layout component. The way it'll work is that the current
     * route will be the only selected item within the tree. When the pathname
     * changes, the selectedIds will be updated to only be the current pathname once
     * again.
     *
     * This means that you can use whatever routing library or history provider that
     * ensures that your layout re-renders on a path change.
     *
     * @example
     * ```tsx
     * import { Tree, useLayoutTree } from "@react-md/core";
     * import type { TreeData } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * // choose your routing library...
     * import { Link, useLocation } from "react-router";
     *
     * const navItems = {
     *   "/": {
     *     itemId: "/",
     *     parentId: null,
     *     children: "Home",
     *     leftAddon: <HomeIcon />,
     *     to: "/",
     *   },
     *   "/route-1": {
     *     itemId: "/route-1",
     *     parentId: null,
     *     children: "Route 1",
     *     leftAddon: <TvIcon />,
     *     to: "/route-1",
     *   },
     *   "/route-2": {
     *     itemId: "/route-2",
     *     parentId: null,
     *     children: "Route 2",
     *     leftAddon: <AppsIcon />,
     *     to: "/route-2",
     *   },
     *   "/route-3": {
     *     itemId: "/route-3",
     *     parentId: null,
     *     children: "Route 3",
     *     leftAddon: <BookIcon />,
     *     to: "/route-3",
     *   },
     * } satisfies TreeData;
     *
     * function Example(): ReactElement {
     *   const { pathname } = useLocation();
     *   const tree = useLayoutTree({
     *     navItems,
     *     pathname,
     *   });
     *
     *   return (
     *     <Tree
     *       {...tree}
     *       aria-label="Navigation"
     *       linkComponent={Link}
     *     />
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0 Renamed from `useLayoutNavigation`.
     */
    export function useLayoutTree(
      options: LayoutTreeOptions
    ): LayoutTreeImplementation {
      const { defaultExpandedIds, navItems, pathname } = options;

      const selectedIds = useMemo(() => new Set([pathname]), [pathname]);
      const { expandedIds, expandMultipleTreeItems, toggleTreeItemExpansion } =
        useTreeExpansion(
          defaultExpandedIds ?? (() => getParentIds(pathname, navItems))
        );

      useEffect(() => {
        expandMultipleTreeItems((prevExpandedIds) => {
          const nextExpandedIds = new Set([
            ...prevExpandedIds,
            ...getParentIds(pathname, navItems),
          ]);

          return nextExpandedIds;
        });
      }, [expandMultipleTreeItems, pathname, navItems]);

      return {
        data: navItems,
        multiSelect: false,
        selectedIds,
        expandedIds,
        expandMultipleTreeItems,
        toggleTreeItemExpansion,
        toggleTreeItemSelection: noop,
        selectMultipleTreeItems: noop,
      };
    }