File size: 4,486 Bytes
ae01f49 | 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 153 154 155 156 157 | import { makeStyles } from '@mui/styles';
import React, { useRef } from 'react';
import CheckIcon from '@mui/icons-material/Check';
import PropTypes from 'prop-types';
import {
MenuItem,
ControlledMenu,
applyStatics,
Menu,
SubMenu,
} from '@szhsin/react-menu';
export {MenuDivider as PgMenuDivider} from '@szhsin/react-menu';
import { shortcutToString } from './ShortcutTitle';
import clsx from 'clsx';
import CustomPropTypes from '../custom_prop_types';
const useStyles = makeStyles((theme)=>({
menu: {
'& .szh-menu': {
padding: '4px 0px',
zIndex: 1005,
backgroundColor: theme.palette.background.default,
color: theme.palette.text.primary,
border: `1px solid ${theme.otherVars.borderColor}`
},
'& .szh-menu__divider': {
margin: 0,
background: theme.otherVars.borderColor,
},
'& .szh-menu__item': {
display: 'flex',
padding: '3px 12px',
'&:after': {
right: '0.75rem',
},
'&.szh-menu__item--active, &.szh-menu__item--hover': {
backgroundColor: theme.palette.primary.main,
color: theme.palette.primary.contrastText,
},
'&.szh-menu__item--disabled':{
color: theme.palette.text.muted,
}
}
},
checkIcon: {
width: '1.3rem',
},
hideCheck: {
visibility: 'hidden',
},
shortcut: {
marginLeft: 'auto',
fontSize: '0.8em',
paddingLeft: '12px',
}
}));
export function PgMenu({open, className='', label, menuButton=null, ...props}) {
const classes = useStyles();
const state = open ? 'open' : 'closed';
props.anchorRef?.current?.setAttribute('data-state', state);
if(menuButton) {
return <Menu
{...props}
menuButton={menuButton}
className={clsx(classes.menu, className)}
aria-label={label || 'Menu'}
onContextMenu={(e)=>e.preventDefault()}
viewScroll='close'
/>;
}
return (
<ControlledMenu
state={state}
{...props}
className={clsx(classes.menu, className)}
aria-label={label || 'Menu'}
data-state={state}
onContextMenu={(e)=>e.preventDefault()}
viewScroll='close'
/>
);
}
PgMenu.propTypes = {
open: PropTypes.bool,
className: CustomPropTypes.className,
label: PropTypes.string,
anchorRef: CustomPropTypes.ref,
menuButton: PropTypes.element,
};
export const PgSubMenu = applyStatics(SubMenu)(({label, ...props})=>{
return (
<SubMenu label={label} itemProps={{'data-label': label}} {...props} />
);
});
export const PgMenuItem = applyStatics(MenuItem)(({hasCheck=false, checked=false, accesskey, shortcut, children, closeOnCheck=false, ...props})=>{
const classes = useStyles();
let onClick = props.onClick;
if(hasCheck) {
onClick = (e)=>{
e.keepOpen = !closeOnCheck;
props.onClick(e);
};
}
const dataLabel = typeof(children) == 'string' ? children : props.datalabel;
return <MenuItem {...props} onClick={onClick} data-label={dataLabel} data-checked={checked}>
{hasCheck && <CheckIcon className={classes.checkIcon} style={checked ? {} : {visibility: 'hidden'}} data-label="CheckIcon"/>}
{children}
{(shortcut || accesskey) && <div className={classes.shortcut}>({shortcutToString(shortcut, accesskey)})</div>}
</MenuItem>;
});
PgMenuItem.propTypes = {
hasCheck: PropTypes.bool,
checked: PropTypes.bool,
accesskey: PropTypes.string,
shortcut: CustomPropTypes.shortcut,
children: CustomPropTypes.children,
closeOnCheck: PropTypes.bool,
onClick: PropTypes.func,
dataLabel: PropTypes.string,
};
export function usePgMenuGroup() {
const [openMenuName, setOpenMenuName] = React.useState(null);
const prevMenuOpenIdRef = useRef(null);
const toggleMenu = React.useCallback((e)=>{
setOpenMenuName(()=>{
return prevMenuOpenIdRef.current == e.currentTarget?.name ? null : e.currentTarget?.name;
});
prevMenuOpenIdRef.current = null;
}, []);
const handleMenuClose = React.useCallback(()=>{
/* We have no way here to know if the menu was closed using menu button or not
We will keep the last menu name ref for sometime so that the menu does not
open again if menu button is clicked to close the menu */
prevMenuOpenIdRef.current = openMenuName;
setTimeout(()=>{
prevMenuOpenIdRef.current = null;
}, 300);
setOpenMenuName(null);
}, [openMenuName]);
return {
openMenuName: openMenuName,
toggleMenu: toggleMenu,
onMenuClose: handleMenuClose,
};
}
|