|
1 | 1 | 'use client' |
2 | 2 | import { Box, css, Flex, Image, Text, VStack } from '@devup-ui/react' |
3 | 3 | import Link from 'next/link' |
| 4 | +import { usePathname } from 'next/navigation' |
4 | 5 | import { Fragment, useReducer } from 'react' |
5 | 6 |
|
6 | 7 | import { URL_PREFIX } from '../../../constants' |
7 | 8 | import { MenuItemProps } from './MenuItem' |
8 | 9 |
|
9 | 10 | export function OpenMenuItem({ |
10 | | - selected, |
11 | 11 | children, |
12 | 12 | subMenu, |
13 | 13 | }: Omit<MenuItemProps, 'subMenu' | 'to'> & |
14 | 14 | Required<Pick<MenuItemProps, 'subMenu'>>) { |
15 | | - const [open, handleOpen] = useReducer((state) => !state, false) |
| 15 | + const path = usePathname() |
| 16 | + const selected = subMenu.some((item) => |
| 17 | + item.to ? path.startsWith(item.to) : false, |
| 18 | + ) |
| 19 | + const [open, handleOpen] = useReducer((state) => !state, selected) |
16 | 20 | return ( |
17 | 21 | <> |
18 | 22 | <Flex |
@@ -47,16 +51,24 @@ export function OpenMenuItem({ |
47 | 51 | <Box borderRight="1px solid var(--border, #E0E0E0)" w="10px" /> |
48 | 52 | <VStack flex="1" gap="4px"> |
49 | 53 | {subMenu.map(({ children, to }, idx) => { |
| 54 | + const selected = to ? path.startsWith(to) : false |
50 | 55 | const inner = ( |
51 | 56 | <Flex |
52 | 57 | alignItems="center" |
53 | | - bg="$menuActive" |
| 58 | + bg={selected ? '$menuActive' : undefined} |
54 | 59 | borderRadius="6px" |
55 | 60 | gap="10px" |
56 | 61 | p="10px" |
57 | 62 | > |
58 | | - <Box bg="$primary" borderRadius="100%" boxSize="8px" /> |
59 | | - <Text color="$text" flex="1" typography="buttonS"> |
| 63 | + {selected && ( |
| 64 | + <Box bg="$primary" borderRadius="100%" boxSize="8px" /> |
| 65 | + )} |
| 66 | + <Text |
| 67 | + color={selected ? '$title' : '$text'} |
| 68 | + flex="1" |
| 69 | + opacity={selected ? '1' : '0.8'} |
| 70 | + typography={selected ? 'buttonS' : 'buttonSmid'} |
| 71 | + > |
60 | 72 | {children} |
61 | 73 | </Text> |
62 | 74 | </Flex> |
|
0 commit comments