2017-12-24 08:16:45 +02:00
|
|
|
// Package imports.
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
|
|
|
import Overlay from 'react-overlays/lib/Overlay';
|
|
|
|
|
|
|
|
// Components.
|
|
|
|
import IconButton from 'flavours/glitch/components/icon_button';
|
2019-04-21 18:17:10 +03:00
|
|
|
import DropdownMenu from './dropdown_menu';
|
2017-12-24 08:16:45 +02:00
|
|
|
|
|
|
|
// Utils.
|
2022-10-11 12:39:52 +03:00
|
|
|
import { isUserTouching } from 'flavours/glitch/is_mobile';
|
2022-10-11 11:41:15 +03:00
|
|
|
import { assignHandlers } from 'flavours/glitch/utils/react_helpers';
|
2017-12-24 08:16:45 +02:00
|
|
|
|
2019-08-06 14:57:45 +03:00
|
|
|
// The component.
|
|
|
|
export default class ComposerOptionsDropdown extends React.PureComponent {
|
2017-12-24 08:16:45 +02:00
|
|
|
|
2019-08-06 14:57:45 +03:00
|
|
|
static propTypes = {
|
|
|
|
active: PropTypes.bool,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
icon: PropTypes.string,
|
|
|
|
items: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
icon: PropTypes.string,
|
2022-02-09 15:39:12 +02:00
|
|
|
meta: PropTypes.string,
|
2019-08-06 14:57:45 +03:00
|
|
|
name: PropTypes.string.isRequired,
|
2022-02-09 15:39:12 +02:00
|
|
|
text: PropTypes.string,
|
2019-08-06 14:57:45 +03:00
|
|
|
})).isRequired,
|
|
|
|
onModalOpen: PropTypes.func,
|
|
|
|
onModalClose: PropTypes.func,
|
|
|
|
title: PropTypes.string,
|
|
|
|
value: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
2021-02-11 01:53:12 +02:00
|
|
|
container: PropTypes.func,
|
2022-02-09 15:39:12 +02:00
|
|
|
renderItemContents: PropTypes.func,
|
|
|
|
closeOnChange: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
closeOnChange: true,
|
2019-08-06 14:57:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
open: false,
|
2019-08-06 15:18:09 +03:00
|
|
|
openedViaKeyboard: undefined,
|
2019-08-06 14:57:45 +03:00
|
|
|
placement: 'bottom',
|
|
|
|
};
|
2017-12-24 08:16:45 +02:00
|
|
|
|
2019-08-06 14:57:45 +03:00
|
|
|
// Toggles opening and closing the dropdown.
|
2019-08-06 15:18:09 +03:00
|
|
|
handleToggle = ({ target, type }) => {
|
2022-02-09 14:49:49 +02:00
|
|
|
const { onModalOpen } = this.props;
|
2019-08-06 14:57:45 +03:00
|
|
|
const { open } = this.state;
|
|
|
|
|
2022-02-09 14:49:49 +02:00
|
|
|
if (isUserTouching()) {
|
2019-08-06 14:57:45 +03:00
|
|
|
if (this.state.open) {
|
|
|
|
this.props.onModalClose();
|
|
|
|
} else {
|
|
|
|
const modal = this.handleMakeModal();
|
|
|
|
if (modal && onModalOpen) {
|
|
|
|
onModalOpen(modal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const { top } = target.getBoundingClientRect();
|
2019-08-06 15:18:09 +03:00
|
|
|
if (this.state.open && this.activeElement) {
|
2020-08-21 15:14:28 +03:00
|
|
|
this.activeElement.focus({ preventScroll: true });
|
2019-08-06 15:18:09 +03:00
|
|
|
}
|
2019-08-06 14:57:45 +03:00
|
|
|
this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
|
2019-08-06 15:18:09 +03:00
|
|
|
this.setState({ open: !this.state.open, openedViaKeyboard: type !== 'click' });
|
2019-08-06 14:57:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown = (e) => {
|
|
|
|
switch (e.key) {
|
2017-12-24 08:16:45 +02:00
|
|
|
case 'Escape':
|
2019-08-06 14:57:45 +03:00
|
|
|
this.handleClose();
|
2017-12-24 08:16:45 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-08-06 14:57:45 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 15:18:09 +03:00
|
|
|
handleMouseDown = () => {
|
|
|
|
if (!this.state.open) {
|
|
|
|
this.activeElement = document.activeElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleButtonKeyDown = (e) => {
|
|
|
|
switch(e.key) {
|
|
|
|
case ' ':
|
|
|
|
case 'Enter':
|
|
|
|
this.handleMouseDown();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyPress = (e) => {
|
|
|
|
switch(e.key) {
|
|
|
|
case ' ':
|
|
|
|
case 'Enter':
|
|
|
|
this.handleToggle(e);
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 14:57:45 +03:00
|
|
|
handleClose = () => {
|
2019-08-06 15:18:09 +03:00
|
|
|
if (this.state.open && this.activeElement) {
|
2020-08-21 15:14:28 +03:00
|
|
|
this.activeElement.focus({ preventScroll: true });
|
2019-08-06 15:18:09 +03:00
|
|
|
}
|
2019-08-06 14:57:45 +03:00
|
|
|
this.setState({ open: false });
|
|
|
|
}
|
2017-12-24 08:16:45 +02:00
|
|
|
|
2022-02-09 15:39:12 +02:00
|
|
|
handleItemClick = (e) => {
|
|
|
|
const {
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
onModalClose,
|
|
|
|
closeOnChange,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const i = Number(e.currentTarget.getAttribute('data-index'));
|
|
|
|
|
2022-02-09 18:15:36 +02:00
|
|
|
const { name } = items[i];
|
2022-02-09 15:39:12 +02:00
|
|
|
|
|
|
|
e.preventDefault(); // Prevents focus from changing
|
|
|
|
if (closeOnChange) onModalClose();
|
|
|
|
onChange(name);
|
|
|
|
};
|
|
|
|
|
2018-01-03 22:36:21 +02:00
|
|
|
// Creates an action modal object.
|
2019-08-06 14:57:45 +03:00
|
|
|
handleMakeModal = () => {
|
2017-12-24 08:16:45 +02:00
|
|
|
const {
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
onModalOpen,
|
2018-01-03 22:36:21 +02:00
|
|
|
onModalClose,
|
2017-12-24 08:16:45 +02:00
|
|
|
value,
|
|
|
|
} = this.props;
|
2018-01-03 22:36:21 +02:00
|
|
|
|
|
|
|
// Required props.
|
|
|
|
if (!(onChange && onModalOpen && onModalClose && items)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The object.
|
|
|
|
return {
|
2022-02-09 15:39:12 +02:00
|
|
|
renderItemContents: this.props.renderItemContents,
|
|
|
|
onClick: this.handleItemClick,
|
2018-01-03 22:36:21 +02:00
|
|
|
actions: items.map(
|
|
|
|
({
|
|
|
|
name,
|
|
|
|
...rest
|
|
|
|
}) => ({
|
|
|
|
...rest,
|
|
|
|
active: value && name === value,
|
|
|
|
name,
|
|
|
|
})
|
|
|
|
),
|
|
|
|
};
|
2019-08-06 14:57:45 +03:00
|
|
|
}
|
2017-12-24 08:16:45 +02:00
|
|
|
|
|
|
|
// Rendering.
|
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
active,
|
|
|
|
disabled,
|
|
|
|
title,
|
|
|
|
icon,
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
value,
|
2021-02-11 01:53:12 +02:00
|
|
|
container,
|
2022-02-09 15:39:12 +02:00
|
|
|
renderItemContents,
|
|
|
|
closeOnChange,
|
2017-12-24 08:16:45 +02:00
|
|
|
} = this.props;
|
2018-04-10 21:04:55 +03:00
|
|
|
const { open, placement } = this.state;
|
2017-12-24 08:16:45 +02:00
|
|
|
const computedClass = classNames('composer--options--dropdown', {
|
|
|
|
active,
|
2018-01-03 22:36:21 +02:00
|
|
|
open,
|
2018-08-19 18:14:30 +03:00
|
|
|
top: placement === 'top',
|
2017-12-24 08:16:45 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// The result.
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={computedClass}
|
2019-08-06 14:57:45 +03:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2017-12-24 08:16:45 +02:00
|
|
|
>
|
|
|
|
<IconButton
|
|
|
|
active={open || active}
|
|
|
|
className='value'
|
|
|
|
disabled={disabled}
|
|
|
|
icon={icon}
|
2019-08-05 15:25:48 +03:00
|
|
|
inverted
|
2019-08-06 14:57:45 +03:00
|
|
|
onClick={this.handleToggle}
|
2019-08-06 15:18:09 +03:00
|
|
|
onMouseDown={this.handleMouseDown}
|
|
|
|
onKeyDown={this.handleButtonKeyDown}
|
|
|
|
onKeyPress={this.handleKeyPress}
|
2017-12-24 08:16:45 +02:00
|
|
|
size={18}
|
|
|
|
style={{
|
|
|
|
height: null,
|
|
|
|
lineHeight: '27px',
|
|
|
|
}}
|
|
|
|
title={title}
|
|
|
|
/>
|
|
|
|
<Overlay
|
2018-01-03 22:36:21 +02:00
|
|
|
containerPadding={20}
|
2018-04-10 21:04:55 +03:00
|
|
|
placement={placement}
|
2017-12-24 08:16:45 +02:00
|
|
|
show={open}
|
|
|
|
target={this}
|
2021-02-11 01:53:12 +02:00
|
|
|
container={container}
|
2017-12-24 08:16:45 +02:00
|
|
|
>
|
2019-04-21 18:17:10 +03:00
|
|
|
<DropdownMenu
|
2018-01-03 22:36:21 +02:00
|
|
|
items={items}
|
2022-02-09 15:39:12 +02:00
|
|
|
renderItemContents={renderItemContents}
|
2018-01-03 22:36:21 +02:00
|
|
|
onChange={onChange}
|
2019-08-06 14:57:45 +03:00
|
|
|
onClose={this.handleClose}
|
2018-01-03 22:36:21 +02:00
|
|
|
value={value}
|
2019-08-06 15:18:09 +03:00
|
|
|
openedViaKeyboard={this.state.openedViaKeyboard}
|
2022-02-09 15:39:12 +02:00
|
|
|
closeOnChange={closeOnChange}
|
2018-01-03 22:36:21 +02:00
|
|
|
/>
|
2017-12-24 08:16:45 +02:00
|
|
|
</Overlay>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|