|
16 | 16 | * specific language governing permissions and limitations |
17 | 17 | * under the License. |
18 | 18 | */ |
19 | | -import { Box, Button, Link, type ButtonProps } from "@chakra-ui/react"; |
20 | | -import type { ReactElement } from "react"; |
21 | | -import { NavLink } from "react-router-dom"; |
| 19 | +import { Box, type BoxProps, Button, Icon, type IconProps, Link, type ButtonProps } from "@chakra-ui/react"; |
| 20 | +import { type ReactNode, useMemo, type ForwardRefExoticComponent, type RefAttributes } from "react"; |
| 21 | +import type { IconType } from "react-icons"; |
| 22 | +import { Link as RouterLink, useMatch } from "react-router-dom"; |
22 | 23 |
|
23 | | -const styles = { |
24 | | - _active: { |
25 | | - bg: "brand.emphasized", |
26 | | - }, |
27 | | - // Fix inverted hover and active colors |
28 | | - _hover: { |
29 | | - bg: "brand.emphasized", // Even darker for better light mode contrast |
30 | | - }, |
31 | | - alignItems: "center", |
32 | | - borderRadius: "none", |
33 | | - colorPalette: "brand", |
34 | | - flexDir: "column", |
35 | | - height: 20, |
36 | | - variant: "ghost", |
37 | | - whiteSpace: "wrap", |
38 | | - width: 20, |
39 | | -} satisfies ButtonProps; |
| 24 | +const commonLabelProps: BoxProps = { |
| 25 | + fontSize: "2xs", |
| 26 | + overflow: "hidden", |
| 27 | + textAlign: "center", |
| 28 | + textOverflow: "ellipsis", |
| 29 | + whiteSpace: "nowrap", |
| 30 | + width: "full", |
| 31 | +}; |
40 | 32 |
|
41 | 33 | type NavButtonProps = { |
42 | | - readonly icon: ReactElement; |
| 34 | + readonly icon: ForwardRefExoticComponent<IconProps & RefAttributes<SVGSVGElement>> | IconType; |
43 | 35 | readonly isExternal?: boolean; |
44 | | - readonly title?: string; |
| 36 | + readonly pluginIcon?: ReactNode; |
| 37 | + readonly title: string; |
45 | 38 | readonly to?: string; |
46 | 39 | } & ButtonProps; |
47 | 40 |
|
48 | | -export const NavButton = ({ icon, isExternal = false, title, to, ...rest }: NavButtonProps) => |
49 | | - to === undefined ? ( |
50 | | - <Button {...styles} {...rest}> |
51 | | - <Box alignSelf="center">{icon}</Box> |
52 | | - <Box fontSize="xs">{title}</Box> |
53 | | - </Button> |
54 | | - ) : isExternal ? ( |
55 | | - <Link href={to} px={2} rel="noopener noreferrer" target="_blank"> |
56 | | - <Button {...styles} variant="ghost" {...rest}> |
57 | | - <Box alignSelf="center">{icon}</Box> |
58 | | - <Box fontSize="xs">{title}</Box> |
| 41 | +export const NavButton = ({ icon, isExternal = false, pluginIcon, title, to, ...rest }: NavButtonProps) => { |
| 42 | + // Use useMatch to determine if the current route matches the button's destination |
| 43 | + // This provides the same functionality as NavLink's isActive prop |
| 44 | + // Only applies to buttons with a to prop (but needs to be before any return statements) |
| 45 | + const match = useMatch({ |
| 46 | + end: to === "/", // Only exact match for root path |
| 47 | + path: to ?? "", |
| 48 | + }); |
| 49 | + // Only applies to buttons with a to prop |
| 50 | + const isActive = Boolean(to) ? Boolean(match) : false; |
| 51 | + |
| 52 | + const commonButtonProps = useMemo<ButtonProps>( |
| 53 | + () => ({ |
| 54 | + _expanded: isActive |
| 55 | + ? undefined |
| 56 | + : { |
| 57 | + bg: "brand.emphasized", // Even darker for better light mode contrast |
| 58 | + color: "fg", |
| 59 | + }, |
| 60 | + _focus: isActive |
| 61 | + ? undefined |
| 62 | + : { |
| 63 | + color: "fg", |
| 64 | + }, |
| 65 | + _hover: isActive |
| 66 | + ? undefined |
| 67 | + : { |
| 68 | + _active: { |
| 69 | + bg: "brand.solid", |
| 70 | + color: "white", |
| 71 | + }, |
| 72 | + bg: "brand.emphasized", // Even darker for better light mode contrast |
| 73 | + color: "fg", |
| 74 | + }, |
| 75 | + alignItems: "center", |
| 76 | + bg: isActive ? "brand.solid" : undefined, |
| 77 | + borderRadius: "md", |
| 78 | + borderWidth: 0, |
| 79 | + boxSize: 14, |
| 80 | + color: isActive ? "white" : "fg.muted", |
| 81 | + colorPalette: "brand", |
| 82 | + cursor: "pointer", |
| 83 | + flexDir: "column", |
| 84 | + gap: 0, |
| 85 | + overflow: "hidden", |
| 86 | + padding: 0, |
| 87 | + textDecoration: "none", |
| 88 | + title, |
| 89 | + transition: "background-color 0.2s ease, color 0.2s ease", |
| 90 | + variant: "plain", |
| 91 | + whiteSpace: "wrap", |
| 92 | + ...rest, |
| 93 | + }), |
| 94 | + [isActive, rest, title], |
| 95 | + ); |
| 96 | + |
| 97 | + if (to === undefined) { |
| 98 | + return ( |
| 99 | + <Button {...commonButtonProps}> |
| 100 | + {pluginIcon ?? <Icon as={icon} boxSize={5} />} |
| 101 | + <Box {...commonLabelProps}>{title}</Box> |
59 | 102 | </Button> |
60 | | - </Link> |
61 | | - ) : ( |
62 | | - <NavLink to={to}> |
63 | | - {({ isActive }: { readonly isActive: boolean }) => ( |
64 | | - <Button |
65 | | - {...styles} |
66 | | - _active={isActive ? { bg: "brand.solid" } : { bg: "brand.emphasized" }} |
67 | | - // Override styles for active state to ensure proper colors |
68 | | - _hover={isActive ? { bg: "brand.solid" } : { bg: "brand.emphasized" }} |
69 | | - variant={isActive ? "solid" : "ghost"} |
70 | | - {...rest} |
71 | | - > |
72 | | - <Box alignSelf="center">{icon}</Box> |
73 | | - <Box fontSize="xs">{title}</Box> |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + if (isExternal) { |
| 107 | + return ( |
| 108 | + <Link asChild href={to} rel="noopener noreferrer" target="_blank"> |
| 109 | + <Button {...commonButtonProps}> |
| 110 | + {pluginIcon ?? <Icon as={icon} boxSize={5} />} |
| 111 | + <Box {...commonLabelProps}>{title}</Box> |
74 | 112 | </Button> |
75 | | - )} |
76 | | - </NavLink> |
| 113 | + </Link> |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + return ( |
| 118 | + <Button as={Link} asChild {...commonButtonProps}> |
| 119 | + <RouterLink to={to}> |
| 120 | + {pluginIcon ?? <Icon as={icon} boxSize={5} />} |
| 121 | + <Box {...commonLabelProps}>{title}</Box> |
| 122 | + </RouterLink> |
| 123 | + </Button> |
77 | 124 | ); |
| 125 | +}; |
0 commit comments