bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/common/menu/component.jsx

261 lines
7.6 KiB
React
Raw Normal View History

2021-07-08 19:41:03 +08:00
import React from "react";
import PropTypes from "prop-types";
import { defineMessages, injectIntl } from "react-intl";
2023-05-03 03:24:06 +08:00
import Menu from "@mui/material/Menu";
import { Divider } from "@mui/material";
2022-02-15 22:51:51 +08:00
import Icon from "/imports/ui/components/common/icon/component";
2022-03-04 04:48:56 +08:00
import { SMALL_VIEWPORT_BREAKPOINT } from '/imports/ui/components/layout/enums';
import KEY_CODES from '/imports/utils/keyCodes';
2021-07-08 19:41:03 +08:00
2021-11-06 03:59:01 +08:00
import Styled from './styles';
2021-07-08 19:41:03 +08:00
const intlMessages = defineMessages({
close: {
id: 'app.dropdown.close',
description: 'Close button label',
},
2023-01-04 23:24:39 +08:00
active: {
id: 'app.dropdown.list.item.activeLabel',
description: 'active item label',
},
2021-07-08 19:41:03 +08:00
});
class BBBMenu extends React.Component {
constructor(props) {
super(props);
this.state = {
anchorEl: null,
};
2022-05-13 21:42:19 +08:00
this.optsToMerge = {};
this.autoFocus = false;
this.handleKeyDown = this.handleKeyDown.bind(this);
2021-07-08 19:41:03 +08:00
this.handleClick = this.handleClick.bind(this);
this.handleClose = this.handleClose.bind(this);
}
componentDidUpdate() {
const { anchorEl } = this.state;
const { open } = this.props;
if (open === false && anchorEl) {
this.setState({ anchorEl: null });
} else if (open === true && !anchorEl) {
this.setState({ anchorEl: this.anchorElRef });
}
}
handleKeyDown(event) {
const { anchorEl } = this.state;
const isMenuOpen = Boolean(anchorEl);
if ([KEY_CODES.ESCAPE, KEY_CODES.TAB].includes(event.which)) {
this.handleClose();
return;
}
if (isMenuOpen && [KEY_CODES.ARROW_UP, KEY_CODES.ARROW_DOWN].includes(event.which)) {
event.preventDefault();
event.stopPropagation();
const menuItems = Array.from(document.querySelectorAll('[data-key^="menuItem-"]'));
if (menuItems.length === 0) return;
const focusedIndex = menuItems.findIndex(item => item === document.activeElement);
const nextIndex = event.which === KEY_CODES.ARROW_UP ? focusedIndex - 1 : focusedIndex + 1;
let indexToFocus = 0;
if (nextIndex < 0) {
indexToFocus = menuItems.length - 1;
} else if (nextIndex >= menuItems.length) {
indexToFocus = 0;
} else {
indexToFocus = nextIndex;
}
menuItems[indexToFocus].focus();
}
};
2021-07-08 19:41:03 +08:00
handleClick(event) {
this.setState({ anchorEl: event.currentTarget });
2021-07-08 19:41:03 +08:00
};
handleClose(event) {
2021-07-08 19:41:03 +08:00
const { onCloseCallback } = this.props;
this.setState({ anchorEl: null }, onCloseCallback());
if (event) {
event.persist();
if (event.type === 'click') {
setTimeout(() => {
document.activeElement.blur();
}, 0);
}
}
2021-07-08 19:41:03 +08:00
};
makeMenuItems() {
2023-01-04 23:45:26 +08:00
const { actions, selectedEmoji, intl } = this.props;
2021-07-08 19:41:03 +08:00
return actions?.map(a => {
const { dataTest, label, onClick, key, disabled, description, selected } = a;
2021-11-06 03:59:01 +08:00
const emojiSelected = key?.toLowerCase()?.includes(selectedEmoji?.toLowerCase());
let customStyles = {
2022-05-24 00:58:51 +08:00
paddingLeft: '16px',
paddingRight: '16px',
2022-05-23 22:45:53 +08:00
paddingTop: '12px',
paddingBottom: '12px',
marginLeft: '0px',
marginRight: '0px',
};
if (a.customStyles) {
customStyles = { ...customStyles, ...a.customStyles };
}
return [
a.dividerTop && <Divider disabled />,
2021-11-06 03:59:01 +08:00
<Styled.BBBMenuItem
2021-11-11 21:36:00 +08:00
emoji={emojiSelected ? 'yes' : 'no'}
key={label}
data-test={dataTest}
data-key={`menuItem-${dataTest}`}
disableRipple={true}
disableGutters={true}
disabled={disabled}
style={customStyles}
onClick={(event) => {
onClick();
2023-04-14 04:13:30 +08:00
const close = !key?.includes('setstatus') && !key?.includes('back');
// prevent menu close for sub menu actions
if (close) this.handleClose(event);
event.stopPropagation();
}}>
<Styled.MenuItemWrapper>
2021-07-08 19:41:03 +08:00
{a.icon ? <Icon iconName={a.icon} key="icon" /> : null}
<Styled.Option aria-describedby={`${key}-option-desc`}>{label}</Styled.Option>
2023-01-04 23:24:39 +08:00
{description && <div className="sr-only" id={`${key}-option-desc`}>{`${description}${selected ? ` - ${intl.formatMessage(intlMessages.active)}` : ''}`}</div>}
2021-11-06 03:59:01 +08:00
{a.iconRight ? <Styled.IconRight iconName={a.iconRight} key="iconRight" /> : null}
</Styled.MenuItemWrapper>
2021-11-06 03:59:01 +08:00
</Styled.BBBMenuItem>,
a.divider && <Divider disabled />
];
}) ?? [];
2021-07-08 19:41:03 +08:00
}
2021-07-08 19:41:03 +08:00
render() {
const { anchorEl } = this.state;
2023-06-27 22:08:49 +08:00
const {
trigger,
intl,
customStyles,
dataTest,
opts,
accessKey,
open,
renderOtherComponents,
customAnchorEl,
hasRoundedCorners,
overrideMobileStyles,
} = this.props;
2021-07-08 19:41:03 +08:00
const actionsItems = this.makeMenuItems();
2023-06-14 01:51:42 +08:00
const roundedCornersStyles = { borderRadius: '1.8rem' };
2023-05-16 03:21:27 +08:00
let menuStyles = { zIndex: 999 };
if (customStyles) {
menuStyles = { ...menuStyles, ...customStyles };
}
2021-07-08 19:41:03 +08:00
return (
<>
<div
onClick={(e) => {
e.persist();
2022-03-28 22:10:54 +08:00
const firefoxInputSource = !([1, 5].includes(e.nativeEvent.mozInputSource)); // 1 = mouse, 5 = touch (firefox only)
const chromeInputSource = !(['mouse', 'touch'].includes(e.nativeEvent.pointerType));
2022-05-13 21:42:19 +08:00
this.optsToMerge.autoFocus = firefoxInputSource && chromeInputSource;
this.handleClick(e);
}}
onKeyPress={(e) => {
e.persist();
2023-05-03 03:24:06 +08:00
if (e.which !== KEY_CODES.ENTER) return null;
this.handleClick(e);
}}
accessKey={accessKey}
ref={(ref) => this.anchorElRef = ref}
2023-05-03 03:24:06 +08:00
role="button"
2023-05-10 10:15:08 +08:00
tabIndex={-1}
>
{trigger}
</div>
2021-07-08 19:41:03 +08:00
<Menu
2022-05-13 21:42:19 +08:00
{...opts}
{...this.optsToMerge}
2023-06-14 01:51:42 +08:00
anchorEl={customAnchorEl ? customAnchorEl : anchorEl}
2021-07-08 19:41:03 +08:00
open={Boolean(anchorEl)}
onClose={this.handleClose}
style={menuStyles}
2022-01-29 03:52:22 +08:00
data-test={dataTest}
onKeyDownCapture={this.handleKeyDown}
2023-06-27 22:08:49 +08:00
PaperProps={{
style: hasRoundedCorners ? roundedCornersStyles : {},
className: overrideMobileStyles ? 'override-mobile-styles' : 'MuiPaper-root-mobile',
}}
2021-07-08 19:41:03 +08:00
>
{actionsItems}
{renderOtherComponents}
2023-06-27 22:08:49 +08:00
{!overrideMobileStyles && anchorEl && window.innerWidth < SMALL_VIEWPORT_BREAKPOINT &&
2021-11-06 03:59:01 +08:00
<Styled.CloseButton
2021-07-08 19:41:03 +08:00
label={intl.formatMessage(intlMessages.close)}
size="lg"
color="default"
onClick={this.handleClose}
/>
}
</Menu>
</>
2021-07-08 19:41:03 +08:00
);
}
}
BBBMenu.defaultProps = {
opts: {
id: "default-dropdown-menu",
autoFocus: false,
2021-07-08 19:41:03 +08:00
keepMounted: true,
transitionDuration: 0,
elevation: 3,
2023-05-03 03:24:06 +08:00
getcontentanchorel: null,
2021-07-08 19:41:03 +08:00
fullwidth: "true",
anchorOrigin: { vertical: 'top', horizontal: 'right' },
transformorigin: { vertical: 'top', horizontal: 'right' },
2021-07-08 19:41:03 +08:00
},
onCloseCallback: () => { },
2023-05-03 03:24:06 +08:00
dataTest: '',
2021-07-08 19:41:03 +08:00
};
BBBMenu.propTypes = {
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
trigger: PropTypes.element.isRequired,
actions: PropTypes.array.isRequired,
2021-07-08 19:41:03 +08:00
onCloseCallback: PropTypes.func,
2022-01-29 03:52:22 +08:00
dataTest: PropTypes.string,
2023-05-03 03:24:06 +08:00
open: PropTypes.bool,
customStyles: PropTypes.object,
opts: PropTypes.object,
accessKey: PropTypes.string,
2021-07-08 19:41:03 +08:00
};
2023-05-03 03:24:06 +08:00
export default injectIntl(BBBMenu);