|
| 1 | +/** |
| 2 | + * Copyright IBM Corp. 2016, 2023 |
| 3 | + * |
| 4 | + * This source code is licensed under the Apache-2.0 license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import cx from 'classnames'; |
| 9 | +import PropTypes from 'prop-types'; |
| 10 | +import React, { ElementType, ForwardedRef, HTMLAttributes, Ref } from 'react'; |
| 11 | +import SideNavLinkText from './SideNavLinkText'; |
| 12 | +import Link from './Link'; |
| 13 | +import { usePrefix } from '../../internal/usePrefix'; |
| 14 | + |
| 15 | +interface SideNavMenuItemProps extends HTMLAttributes<HTMLElement> { |
| 16 | + /** |
| 17 | + * Specify the children to be rendered inside of the `SideNavMenuItem` |
| 18 | + */ |
| 19 | + children?: React.ReactNode; |
| 20 | + |
| 21 | + /** |
| 22 | + * Provide an optional class to be applied to the containing node |
| 23 | + */ |
| 24 | + className?: string; |
| 25 | + |
| 26 | + /** |
| 27 | + * Optionally specify whether the link is "active". An active link is one that |
| 28 | + * has an href that is the same as the current page. Can also pass in |
| 29 | + * `aria-current="page"`, as well. |
| 30 | + */ |
| 31 | + isActive?: boolean; |
| 32 | +} |
| 33 | + |
| 34 | +const SideNavMenuItem = React.forwardRef<HTMLElement, SideNavMenuItemProps>( |
| 35 | + function SideNavMenuItem(props, ref: ForwardedRef<HTMLElement>) { |
| 36 | + const prefix = usePrefix(); |
| 37 | + const { children, className: customClassName, isActive, ...rest } = props; |
| 38 | + const className = cx(`${prefix}--side-nav__menu-item`, customClassName); |
| 39 | + const linkClassName = cx({ |
| 40 | + [`${prefix}--side-nav__link`]: true, |
| 41 | + [`${prefix}--side-nav__link--current`]: isActive, |
| 42 | + }); |
| 43 | + |
| 44 | + return ( |
| 45 | + <li className={className}> |
| 46 | + <Link {...rest} className={linkClassName} ref={ref as Ref<ElementType>}> |
| 47 | + <SideNavLinkText>{children}</SideNavLinkText> |
| 48 | + </Link> |
| 49 | + </li> |
| 50 | + ); |
| 51 | + } |
| 52 | +); |
| 53 | + |
| 54 | +SideNavMenuItem.displayName = 'SideNavMenuItem'; |
| 55 | +SideNavMenuItem.propTypes = { |
| 56 | + /** |
| 57 | + * Specify the children to be rendered inside of the `SideNavMenuItem` |
| 58 | + */ |
| 59 | + children: PropTypes.node, |
| 60 | + |
| 61 | + /** |
| 62 | + * Provide an optional class to be applied to the containing node |
| 63 | + */ |
| 64 | + className: PropTypes.string, |
| 65 | + |
| 66 | + /** |
| 67 | + * Optionally specify whether the link is "active". An active link is one that |
| 68 | + * has an href that is the same as the current page. Can also pass in |
| 69 | + * `aria-current="page"`, as well. |
| 70 | + */ |
| 71 | + isActive: PropTypes.bool, |
| 72 | +}; |
| 73 | + |
| 74 | +export default SideNavMenuItem; |
0 commit comments