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 | import { ChevronDownIcon, ChevronRightIcon } from "@chakra-ui/icons";
import { Box, Flex, IconButton, useDisclosure } from "@chakra-ui/react";
import { FunctionComponent, useMemo } from "react";
import { NavItemWrapper } from "./NavItemWrapper";
import type { GetIsActiveItemFunction, NavItemConfig } from "./types";
import { clickEvent, eventName, useAnalytics } from "../../contexts/Analytics";
const navTreeEvent = (scope: string, event: string) =>
eventName(scope, "NavTree", event);
const iconProps = {
color: "textTertiary",
h: 4,
w: 4,
};
export interface NavItemProps extends NavItemConfig {
// The following props don't need to be explicitly defined - they are passed internally
getIsActiveItem?: GetIsActiveItemFunction;
onOpen?: () => void;
level: number;
variant?: "sm" | "md";
}
export const NavItem: FunctionComponent<NavItemProps> = ({
children,
"data-event": dataEvent,
getIsActiveItem,
id,
title,
path,
level,
onOpen,
variant,
}) => {
const { trackCustomEvent } = useAnalytics();
const defaultIsOpen = level < 2; // only show first two levels by default
const disclosure = useDisclosure({ onOpen, defaultIsOpen });
const linkIsActive =
getIsActiveItem?.({
id,
title,
path,
children,
}) ?? false;
const showToggle = (children?.length ?? 0) > 0;
const showChildren = disclosure.isOpen && showToggle;
const nestedItems = useMemo(
() =>
children?.map((item, idx) => {
return (
<NavItem
data-event={dataEvent}
{...item}
getIsActiveItem={getIsActiveItem}
key={idx}
level={level + 1}
onOpen={disclosure.onOpen}
variant={variant}
/>
);
}),
[children, dataEvent, getIsActiveItem, level, disclosure.onOpen, variant]
);
return (
<Flex direction="column">
<Flex align="center" color={linkIsActive ? "link" : "textPrimary"}>
{showToggle && (
<IconButton
aria-label="expand-toggle"
borderRadius="md"
h={4}
icon={
disclosure.isOpen ? (
<ChevronDownIcon {...iconProps} />
) : (
<ChevronRightIcon {...iconProps} />
)
}
ml={-1}
onClick={() => {
disclosure.onToggle();
if (dataEvent) {
trackCustomEvent(
clickEvent({
name: navTreeEvent(dataEvent, "Toggle"),
})
);
}
}}
size="xs"
variant="link"
w={0}
/>
)}
<NavItemWrapper
data-event={dataEvent ? navTreeEvent(dataEvent, "Link") : undefined}
path={path}
showToggle={showToggle}
title={title}
variant={variant}
>
{title}
</NavItemWrapper>
</Flex>
<Box
_before={{
// Creates a border without taking up any box space
// This is important to keep items perfectly aligned
bg: "borderColor",
bottom: 0,
content: `""`,
left: 0,
position: "absolute",
top: 0,
w: "1px",
}}
display={showChildren ? "initial" : "none"}
ml={2}
mr={2}
pl={2}
position="relative"
>
{nestedItems}
</Box>
</Flex>
);
};
|