| 1 |
import { |
| 2 |
cloneElement, |
| 3 |
Fragment, |
| 4 |
isValidElement, |
| 5 |
useContext, |
| 6 |
useEffect, |
| 7 |
useRef, |
| 8 |
useState, |
| 9 |
forwardRef, |
| 10 |
MutableRefObject |
| 11 |
} from 'react'; |
| 12 |
import styles from '@patternfly/react-styles/css/components/Nav/nav'; |
| 13 |
import menuStyles from '@patternfly/react-styles/css/components/Menu/menu'; |
| 14 |
import dividerStyles from '@patternfly/react-styles/css/components/Divider/divider'; |
| 15 |
import { css } from '@patternfly/react-styles'; |
| 16 |
import { NavContext, NavSelectClickHandler } from './Nav'; |
| 17 |
import { PageSidebarContext } from '../Page/PageSidebar'; |
| 18 |
import { useOUIAProps, OUIAProps } from '../../helpers'; |
| 19 |
import { Popper } from '../../helpers/Popper/Popper'; |
| 20 |
import RhMicronsCaretRightIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-right-icon'; |
| 21 |
|
| 22 |
export interface NavItemProps extends Omit<React.HTMLProps<HTMLAnchorElement>, 'onClick'>, OUIAProps { |
| 23 |
/** Content rendered inside the nav item. */ |
| 24 |
children?: React.ReactNode; |
| 25 |
/** Whether to set className on children when React.isValidElement(children) */ |
| 26 |
styleChildren?: boolean; |
| 27 |
/** Additional classes added to the nav item */ |
| 28 |
className?: string; |
| 29 |
/** Target navigation link. Should not be used if the flyout prop is defined. */ |
| 30 |
to?: string; |
| 31 |
/** Flag indicating whether the item is active */ |
| 32 |
isActive?: boolean; |
| 33 |
/** Group identifier, will be returned with the onToggle and onSelect callback passed to the Nav component */ |
| 34 |
groupId?: string | number | null; |
| 35 |
/** Item identifier, will be returned with the onToggle and onSelect callback passed to the Nav component */ |
| 36 |
itemId?: string | number | null; |
| 37 |
/** If true prevents the default anchor link action to occur. Set to true if you want to handle navigation yourself. */ |
| 38 |
preventDefault?: boolean; |
| 39 |
/** Callback for item click */ |
| 40 |
onClick?: NavSelectClickHandler; |
| 41 |
/** Component used to render NavItems if React.isValidElement(children) is false */ |
| 42 |
component?: React.ElementType<any> | React.ComponentType<any>; |
| 43 |
/** Flyout of a nav item. This should be a Menu component. Should not be used if the to prop is defined. */ |
| 44 |
flyout?: React.ReactElement<any>; |
| 45 |
/** Callback when flyout is opened or closed */ |
| 46 |
onShowFlyout?: () => void; |
| 47 |
/** z-index of the flyout nav item */ |
| 48 |
zIndex?: number; |
| 49 |
/** Icon added before the nav item children. */ |
| 50 |
icon?: React.ReactNode; |
| 51 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 52 |
ouiaId?: number | string; |
| 53 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 54 |
ouiaSafe?: boolean; |
| 55 |
/** React ref for the anchor element within the nav item. */ |
| 56 |
anchorRef?: React.Ref<HTMLAnchorElement>; |
| 57 |
/** @hide Forwarded ref */ |
| 58 |
innerRef?: React.Ref<HTMLLIElement>; |
| 59 |
} |
| 60 |
|
| 61 |
const NavItemBase: React.FunctionComponent<NavItemProps> = ({ |
| 62 |
children, |
| 63 |
styleChildren = true, |
| 64 |
className, |
| 65 |
to, |
| 66 |
isActive = false, |
| 67 |
groupId = null as string, |
| 68 |
itemId = null as string, |
| 69 |
preventDefault = false, |
| 70 |
onClick, |
| 71 |
component = 'a', |
| 72 |
flyout, |
| 73 |
onShowFlyout, |
| 74 |
ouiaId, |
| 75 |
ouiaSafe, |
| 76 |
zIndex = 9999, |
| 77 |
icon, |
| 78 |
innerRef, |
| 79 |
anchorRef, |
| 80 |
...props |
| 81 |
}: NavItemProps) => { |
| 82 |
const { flyoutRef, setFlyoutRef, navRef } = useContext(NavContext); |
| 83 |
const { isSidebarOpen } = useContext(PageSidebarContext); |
| 84 |
const [flyoutTarget, setFlyoutTarget] = useState(null); |
| 85 |
const [isHovered, setIsHovered] = useState(false); |
| 86 |
const _ref = useRef<HTMLLIElement>(undefined); |
| 87 |
const ref = (innerRef as MutableRefObject<HTMLLIElement>) || _ref; |
| 88 |
const flyoutVisible = ref === flyoutRef; |
| 89 |
const popperRef = useRef<HTMLDivElement>(undefined); |
| 90 |
const hasFlyout = flyout !== undefined; |
| 91 |
const Component = hasFlyout ? 'button' : (component as any); |
| 92 |
|
| 93 |
// A NavItem should not be both a link and a flyout |
| 94 |
if (to && hasFlyout) { |
| 95 |
// eslint-disable-next-line no-console |
| 96 |
console.error('NavItem cannot have both "to" and "flyout" props.'); |
| 97 |
} |
| 98 |
|
| 99 |
const showFlyout = (show: boolean, override?: boolean) => { |
| 100 |
if ((!flyoutVisible || override) && show) { |
| 101 |
setFlyoutRef(ref); |
| 102 |
} else if ((flyoutVisible || override) && !show) { |
| 103 |
setFlyoutRef(null); |
| 104 |
} |
| 105 |
|
| 106 |
onShowFlyout && show && onShowFlyout(); |
| 107 |
}; |
| 108 |
|
| 109 |
const onMouseOver = (event: React.MouseEvent) => { |
| 110 |
const evtContainedInFlyout = (event.target as HTMLElement).closest(`.${styles.navItem}.pf-m-flyout`); |
| 111 |
if (hasFlyout && !flyoutVisible) { |
| 112 |
showFlyout(true); |
| 113 |
} else if (flyoutRef !== null && !evtContainedInFlyout) { |
| 114 |
setFlyoutRef(null); |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
const onFlyoutClick = (event: MouseEvent) => { |
| 119 |
const target = event.target; |
| 120 |
const closestItem = (target as HTMLElement).closest('.pf-m-flyout'); |
| 121 |
if (!closestItem) { |
| 122 |
if (hasFlyout) { |
| 123 |
showFlyout(false, true); |
| 124 |
} else if (flyoutRef !== null) { |
| 125 |
setFlyoutRef(null); |
| 126 |
} |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
const handleFlyout = (event: KeyboardEvent) => { |
| 131 |
const key = event.key; |
| 132 |
const target = event.target as HTMLElement; |
| 133 |
|
| 134 |
if ((key === ' ' || key === 'Enter' || key === 'ArrowRight') && hasFlyout && ref?.current?.contains(target)) { |
| 135 |
event.stopPropagation(); |
| 136 |
event.preventDefault(); |
| 137 |
if (!flyoutVisible) { |
| 138 |
showFlyout(true); |
| 139 |
setFlyoutTarget(target as HTMLElement); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// We only want the NavItem to handle closing a flyout menu if only the first level flyout is open. |
| 144 |
// Otherwise, MenuItem should handle closing its flyouts |
| 145 |
if ( |
| 146 |
(key === 'Escape' || key === 'ArrowLeft') && |
| 147 |
popperRef?.current?.querySelectorAll(`.${menuStyles.menu}`).length === 1 |
| 148 |
) { |
| 149 |
if (flyoutVisible) { |
| 150 |
event.stopPropagation(); |
| 151 |
event.preventDefault(); |
| 152 |
showFlyout(false); |
| 153 |
} |
| 154 |
} |
| 155 |
}; |
| 156 |
|
| 157 |
useEffect(() => { |
| 158 |
if (hasFlyout) { |
| 159 |
window.addEventListener('click', onFlyoutClick); |
| 160 |
} |
| 161 |
return () => { |
| 162 |
if (hasFlyout) { |
| 163 |
window.removeEventListener('click', onFlyoutClick); |
| 164 |
} |
| 165 |
}; |
| 166 |
}, []); |
| 167 |
|
| 168 |
useEffect(() => { |
| 169 |
if (flyoutTarget) { |
| 170 |
if (flyoutVisible) { |
| 171 |
const flyoutItems = Array.from( |
| 172 |
(popperRef.current as HTMLElement).getElementsByTagName('UL')[0].children |
| 173 |
).filter((el) => !(el.classList.contains('pf-m-disabled') || el.classList.contains(dividerStyles.divider))); |
| 174 |
(flyoutItems[0].firstChild as HTMLElement).focus(); |
| 175 |
} else { |
| 176 |
flyoutTarget.focus(); |
| 177 |
} |
| 178 |
} |
| 179 |
}, [flyoutVisible, flyoutTarget]); |
| 180 |
|
| 181 |
const flyoutButton = ( |
| 182 |
<span className={css(styles.navToggle)}> |
| 183 |
<span className={css(styles.navToggleIcon)}> |
| 184 |
<RhMicronsCaretRightIcon /> |
| 185 |
</span> |
| 186 |
</span> |
| 187 |
); |
| 188 |
|
| 189 |
const ariaFlyoutProps = { |
| 190 |
'aria-haspopup': 'menu', |
| 191 |
'aria-expanded': flyoutVisible |
| 192 |
}; |
| 193 |
|
| 194 |
const tabIndex = isSidebarOpen ? null : -1; |
| 195 |
|
| 196 |
const renderDefaultLink = (context: any): React.ReactNode => { |
| 197 |
const preventLinkDefault = preventDefault || !to; |
| 198 |
return ( |
| 199 |
<Component |
| 200 |
ref={anchorRef} |
| 201 |
href={to} |
| 202 |
onClick={(e: any) => context.onSelect(e, groupId, itemId, to, preventLinkDefault, onClick)} |
| 203 |
className={css( |
| 204 |
styles.navLink, |
| 205 |
isActive && styles.modifiers.current, |
| 206 |
isHovered && styles.modifiers.hover, |
| 207 |
className |
| 208 |
)} |
| 209 |
aria-current={isActive ? 'page' : null} |
| 210 |
tabIndex={tabIndex} |
| 211 |
{...(hasFlyout && { ...ariaFlyoutProps })} |
| 212 |
{...props} |
| 213 |
> |
| 214 |
{icon && <span className={css(styles.navLinkIcon)}>{icon}</span>} |
| 215 |
<span className={css(`${styles.nav}__link-text`)}>{children}</span> |
| 216 |
{flyout && flyoutButton} |
| 217 |
</Component> |
| 218 |
); |
| 219 |
}; |
| 220 |
|
| 221 |
const renderClonedChild = (context: any, child: React.ReactElement<any>): React.ReactNode => |
| 222 |
cloneElement(child, { |
| 223 |
onClick: (e: MouseEvent) => context.onSelect(e, groupId, itemId, to, preventDefault, onClick), |
| 224 |
'aria-current': isActive ? 'page' : null, |
| 225 |
...(styleChildren && { |
| 226 |
className: css(styles.navLink, isActive && styles.modifiers.current, child.props && child.props.className) |
| 227 |
}), |
| 228 |
tabIndex: child.props.tabIndex || tabIndex, |
| 229 |
ref: anchorRef, |
| 230 |
children: hasFlyout ? ( |
| 231 |
<Fragment> |
| 232 |
{child.props.children} |
| 233 |
{flyoutButton} |
| 234 |
</Fragment> |
| 235 |
) : ( |
| 236 |
child.props.children |
| 237 |
) |
| 238 |
}); |
| 239 |
|
| 240 |
const ouiaProps = useOUIAProps(NavItem.displayName, ouiaId, ouiaSafe); |
| 241 |
|
| 242 |
const handleMouseEnter = () => { |
| 243 |
setIsHovered(true); |
| 244 |
}; |
| 245 |
|
| 246 |
const handleMouseLeave = () => { |
| 247 |
setIsHovered(false); |
| 248 |
}; |
| 249 |
|
| 250 |
const flyoutPopper = ( |
| 251 |
<Popper |
| 252 |
triggerRef={ref} |
| 253 |
popper={ |
| 254 |
<div ref={popperRef} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave}> |
| 255 |
{flyout} |
| 256 |
</div> |
| 257 |
} |
| 258 |
popperRef={popperRef} |
| 259 |
placement="right-start" |
| 260 |
isVisible={flyoutVisible} |
| 261 |
onDocumentKeyDown={handleFlyout} |
| 262 |
zIndex={zIndex} |
| 263 |
appendTo={navRef?.current} |
| 264 |
/> |
| 265 |
); |
| 266 |
|
| 267 |
const navItem = ( |
| 268 |
<> |
| 269 |
<li |
| 270 |
onMouseOver={onMouseOver} |
| 271 |
className={css(styles.navItem, hasFlyout && styles.modifiers.flyout, className)} |
| 272 |
ref={ref} |
| 273 |
{...ouiaProps} |
| 274 |
> |
| 275 |
<NavContext.Consumer> |
| 276 |
{(context) => |
| 277 |
isValidElement(children) |
| 278 |
? renderClonedChild(context, children as React.ReactElement<any>) |
| 279 |
: renderDefaultLink(context) |
| 280 |
} |
| 281 |
</NavContext.Consumer> |
| 282 |
</li> |
| 283 |
{flyout && flyoutPopper} |
| 284 |
</> |
| 285 |
); |
| 286 |
|
| 287 |
return navItem; |
| 288 |
}; |
| 289 |
|
| 290 |
export const NavItem = forwardRef<HTMLLIElement, NavItemProps>((props, ref) => ( |
| 291 |
<NavItemBase {...props} innerRef={ref} /> |
| 292 |
)); |
| 293 |
|
| 294 |
NavItem.displayName = 'NavItem'; |