File size: 3,583 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import clsx from 'clsx';
import PropTypes from 'prop-types';
import { Children, createRef, useMemo, useState, useRef, useLayoutEffect } from 'react';
import TranslatableString from 'calypso/components/translatable/proptype';
import SidebarMenu from 'calypso/layout/sidebar/menu';
import HoverIntent from 'calypso/lib/hover-intent';
import { hasTouch } from 'calypso/lib/touch-detect';
import ExpandableSidebarHeading from './expandable-heading';
const isTouch = hasTouch();
function containsSelectedSidebarItem( children ) {
let selectedItemFound = false;
Children.forEach( children, ( child ) => {
if ( selectedItemFound ) {
return true;
}
if ( child?.props?.selected ) {
selectedItemFound = true;
} else {
const descendants = child?.props?.children;
if ( descendants ) {
selectedItemFound = containsSelectedSidebarItem( descendants );
}
}
} );
return selectedItemFound;
}
const offScreen = ( submenu ) => {
const rect = submenu.getBoundingClientRect();
return rect.y + rect.height > window.innerHeight;
};
export const ExpandableSidebarMenu = ( {
className,
title,
count,
onClick,
icon,
materialIcon,
materialIconStyle,
customIcon,
children,
disableFlyout,
...props
} ) => {
let { expanded } = props;
const menu = createRef(); // Needed for HoverIntent.
const submenu = useRef();
const [ submenuHovered, setSubmenuHovered ] = useState( false );
if ( submenu.current ) {
// Sets flyout to expand towards bottom.
submenu.current.style.bottom = 'auto';
submenu.current.style.top = 0;
}
if ( null === expanded ) {
expanded = containsSelectedSidebarItem( children );
}
const classes = clsx( className, {
'is-toggle-open': !! expanded,
'is-togglable': true,
hovered: submenuHovered,
} );
const onEnter = () => {
if ( disableFlyout || expanded || isTouch ) {
return;
}
setSubmenuHovered( true );
};
const onLeave = () => {
// Remove "hovered" state even if menu is expanded.
if ( isTouch ) {
return;
}
setSubmenuHovered( false );
};
const menuId = useMemo( () => 'menu' + crypto.randomUUID(), [] );
useLayoutEffect( () => {
if ( submenuHovered && offScreen( submenu.current ) ) {
// Sets flyout to expand towards top.
submenu.current.style.bottom = 0;
submenu.current.style.top = 'auto';
}
}, [ submenuHovered ] );
return (
<HoverIntent
onMouseOver={ () => onEnter() }
onMouseOut={ () => onLeave() }
sensitivity={ 7 }
interval={ 90 }
timeout={ 200 }
>
<SidebarMenu ref={ menu } className={ classes }>
<ExpandableSidebarHeading
title={ title }
count={ count }
onClick={ ( event ) => {
setSubmenuHovered( false );
onClick( event );
} }
customIcon={ customIcon }
icon={ icon }
materialIcon={ materialIcon }
materialIconStyle={ materialIconStyle }
expanded={ expanded }
menuId={ menuId }
{ ...props }
/>
<li
role="region"
ref={ submenu }
id={ menuId }
className="sidebar__expandable-content"
hidden={ ! expanded }
>
<ul>{ children }</ul>
</li>
</SidebarMenu>
</HoverIntent>
);
};
ExpandableSidebarMenu.propTypes = {
title: PropTypes.oneOfType( [ TranslatableString, PropTypes.element ] ).isRequired,
count: PropTypes.number,
onClick: PropTypes.func,
customIcon: PropTypes.node,
icon: PropTypes.string,
materialIcon: PropTypes.string,
materialIconStyle: PropTypes.string,
expanded: PropTypes.bool,
disableFlyout: PropTypes.bool,
expandableIconClick: PropTypes.func,
};
export default ExpandableSidebarMenu;
|