| 1 |
import { createRef, forwardRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Menu/menu'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { MenuContext } from './MenuContext'; |
| 5 |
import cssHeight from '@patternfly/react-tokens/dist/esm/c_menu__content_Height'; |
| 6 |
import cssMaxHeight from '@patternfly/react-tokens/dist/esm/c_menu__content_MaxHeight'; |
| 7 |
|
| 8 |
export interface MenuContentProps extends React.HTMLProps<HTMLElement> { |
| 9 |
/** Items within group */ |
| 10 |
children?: React.ReactNode; |
| 11 |
/** @hide Forwarded ref */ |
| 12 |
innerRef?: React.Ref<any>; |
| 13 |
/** Height of the menu content */ |
| 14 |
menuHeight?: string; |
| 15 |
/** Maximum height of menu content */ |
| 16 |
maxMenuHeight?: string; |
| 17 |
/** Callback to return the height of the menu content */ |
| 18 |
getHeight?: (height: string) => void; |
| 19 |
} |
| 20 |
|
| 21 |
export const MenuContent = forwardRef((props: MenuContentProps, ref: React.Ref<HTMLDivElement>) => { |
| 22 |
const { getHeight, children, menuHeight, maxMenuHeight, ...rest } = props; |
| 23 |
const menuContentRef = createRef<HTMLDivElement>(); |
| 24 |
const refCallback = (el: HTMLElement, menuId: string, onGetMenuHeight: (menuId: string, height: number) => void) => { |
| 25 |
if (el) { |
| 26 |
let clientHeight = el.clientHeight; |
| 27 |
|
| 28 |
// if this menu is a submenu, we need to account for the root menu list's padding and root menu content's border. |
| 29 |
let rootMenuList = null; |
| 30 |
let parentEl = el.closest(`.${styles.menuList}`); |
| 31 |
while (parentEl !== null && parentEl.nodeType === 1) { |
| 32 |
if (parentEl.classList.contains(styles.menuList)) { |
| 33 |
rootMenuList = parentEl; |
| 34 |
} |
| 35 |
parentEl = parentEl.parentElement; |
| 36 |
} |
| 37 |
|
| 38 |
if (rootMenuList) { |
| 39 |
const rootMenuListStyles = getComputedStyle(rootMenuList); |
| 40 |
const rootMenuListPaddingOffset = |
| 41 |
parseFloat(rootMenuListStyles.getPropertyValue('padding-top').replace(/px/g, '')) + |
| 42 |
parseFloat(rootMenuListStyles.getPropertyValue('padding-bottom').replace(/px/g, '')) + |
| 43 |
parseFloat( |
| 44 |
getComputedStyle(rootMenuList.parentElement).getPropertyValue('border-bottom-width').replace(/px/g, '') |
| 45 |
); |
| 46 |
clientHeight = clientHeight + rootMenuListPaddingOffset; |
| 47 |
} |
| 48 |
|
| 49 |
onGetMenuHeight && onGetMenuHeight(menuId, clientHeight); |
| 50 |
getHeight && getHeight(clientHeight.toString()); |
| 51 |
} |
| 52 |
return ref || menuContentRef; |
| 53 |
}; |
| 54 |
return ( |
| 55 |
<MenuContext.Consumer> |
| 56 |
{({ menuId, onGetMenuHeight }) => ( |
| 57 |
<div |
| 58 |
{...rest} |
| 59 |
className={css(styles.menuContent, props.className)} |
| 60 |
ref={(el) => { |
| 61 |
refCallback(el, menuId, onGetMenuHeight); |
| 62 |
}} |
| 63 |
style={ |
| 64 |
{ |
| 65 |
...(menuHeight && { [cssHeight.name]: menuHeight }), |
| 66 |
...(maxMenuHeight && { [cssMaxHeight.name]: maxMenuHeight }) |
| 67 |
} as React.CSSProperties |
| 68 |
} |
| 69 |
> |
| 70 |
{children} |
| 71 |
</div> |
| 72 |
)} |
| 73 |
</MenuContext.Consumer> |
| 74 |
); |
| 75 |
}); |
| 76 |
MenuContent.displayName = 'MenuContent'; |