|
1 | 1 | 'use client' |
2 | 2 |
|
| 3 | +import { Badge } from '../Badges' |
| 4 | +import { Icon } from '../Icon' |
3 | 5 | import React from 'react' |
| 6 | +import { Ripple } from '../Ripple' |
4 | 7 | import clsx from 'clsx' |
5 | 8 | import styles from './navigation-rail.module.css' |
| 9 | +import { useControllableState } from '../../hooks' |
6 | 10 |
|
7 | | -interface NavigationRailProps extends React.ComponentProps<'div'> {} |
| 11 | +interface Item extends React.ComponentProps<'button'> { |
| 12 | + label: string |
| 13 | + icon: React.ReactNode |
| 14 | + badge?: number | string |
| 15 | + isActive?: boolean |
| 16 | +} |
| 17 | + |
| 18 | +interface NavigationRailProps extends React.ComponentProps<'div'> { |
| 19 | + items: Item[] |
| 20 | + menu?: React.ReactNode |
| 21 | + value?: number |
| 22 | + defaultValue?: number |
| 23 | + setValue?: (index: number) => void |
| 24 | +} |
| 25 | + |
| 26 | +const Item = (props: Item) => { |
| 27 | + const { icon, label, badge, isActive, ...rest } = props |
| 28 | + |
| 29 | + return ( |
| 30 | + <button |
| 31 | + {...rest} |
| 32 | + className={clsx(styles['item'], isActive && styles['active'])} |
| 33 | + > |
| 34 | + <Ripple /> |
| 35 | + <Badge value={badge}> |
| 36 | + {typeof icon == 'string' ? <Icon>{icon}</Icon> : icon} |
| 37 | + </Badge> |
| 38 | + <label className={styles['label']}>{label}</label> |
| 39 | + </button> |
| 40 | + ) |
| 41 | +} |
8 | 42 |
|
9 | 43 | const NavigationRail = (props: NavigationRailProps) => { |
10 | | - const { children, className, ...rest } = props |
| 44 | + const { value, defaultValue, setValue } = props |
| 45 | + const [activeIndex, setActiveIndex] = useControllableState({ |
| 46 | + value, |
| 47 | + defaultValue, |
| 48 | + onChange: setValue |
| 49 | + }) |
| 50 | + |
| 51 | + const { children, className, menu, items = [], ...rest } = props |
11 | 52 | return ( |
12 | 53 | <div {...rest} className={clsx(styles['root'], className)}> |
13 | | - {children} |
| 54 | + {/* Menu icon (optional) */} |
| 55 | + {menu} |
| 56 | + {/* Navigation Items */} |
| 57 | + <nav className={styles['items']}> |
| 58 | + {items.map((item, index) => { |
| 59 | + const { icon, label, badge } = item |
| 60 | + const isActive = index == activeIndex |
| 61 | + return ( |
| 62 | + <Item |
| 63 | + key={index} |
| 64 | + icon={icon} |
| 65 | + label={label} |
| 66 | + badge={badge} |
| 67 | + isActive={isActive} |
| 68 | + onClick={() => setActiveIndex(index)} |
| 69 | + /> |
| 70 | + ) |
| 71 | + })} |
| 72 | + </nav> |
14 | 73 | </div> |
15 | 74 | ) |
16 | 75 | } |
|
0 commit comments