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

249 lines
7.3 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";
import Menu from "@material-ui/core/Menu";
import { Divider } from "@material-ui/core";
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 { ENTER } from "/imports/utils/keyCodes";
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;
const { trigger, intl, customStyles, dataTest, opts, accessKey } = this.props;
2021-07-08 19:41:03 +08:00
const actionsItems = this.makeMenuItems();
let menuStyles = { zIndex: 9999 };
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();
if (e.which !== ENTER) return null;
this.handleClick(e);
}}
accessKey={accessKey}
ref={(ref) => this.anchorElRef = ref}
>
{trigger}
</div>
2021-07-08 19:41:03 +08:00
<Menu
2022-05-13 21:42:19 +08:00
{...opts}
{...this.optsToMerge}
2021-07-08 19:41:03 +08:00
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={this.handleClose}
style={menuStyles}
2022-01-29 03:52:22 +08:00
data-test={dataTest}
onKeyDownCapture={this.handleKeyDown}
2021-07-08 19:41:03 +08:00
>
{actionsItems}
2022-03-04 04:48:56 +08:00
{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
);
}
}
export default injectIntl(BBBMenu);
BBBMenu.defaultProps = {
opts: {
id: "default-dropdown-menu",
autoFocus: false,
2021-07-08 19:41:03 +08:00
keepMounted: true,
transitionDuration: 0,
elevation: 3,
getContentAnchorEl: null,
fullwidth: "true",
anchorOrigin: { vertical: 'top', horizontal: 'right' },
transformorigin: { vertical: 'top', horizontal: 'right' },
2021-07-08 19:41:03 +08:00
},
onCloseCallback: () => { },
2021-07-08 19:41:03 +08:00
};
BBBMenu.propTypes = {
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
trigger: PropTypes.element.isRequired,
actions: PropTypes.arrayOf(PropTypes.shape({
key: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
onClick: PropTypes.func,
2021-07-08 19:41:03 +08:00
icon: PropTypes.string,
iconRight: PropTypes.string,
disabled: PropTypes.bool,
2021-07-08 19:41:03 +08:00
divider: PropTypes.bool,
dividerTop: PropTypes.bool,
2021-08-09 01:22:01 +08:00
accessKey: PropTypes.string,
dataTest: PropTypes.string,
2021-07-08 19:41:03 +08:00
})).isRequired,
onCloseCallback: PropTypes.func,
2022-01-29 03:52:22 +08:00
dataTest: PropTypes.string,
2021-07-08 19:41:03 +08:00
};