2017-12-24 06:16:45 +00: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 15:17:10 +00:00
|
|
|
import DropdownMenu from './dropdown_menu';
|
2017-12-24 06:16:45 +00:00
|
|
|
|
|
|
|
// Utils.
|
|
|
|
import { isUserTouching } from 'flavours/glitch/util/is_mobile';
|
|
|
|
import { assignHandlers } from 'flavours/glitch/util/react_helpers';
|
|
|
|
|
2019-08-06 11:57:45 +00:00
|
|
|
// The component.
|
|
|
|
export default class ComposerOptionsDropdown extends React.PureComponent {
|
2017-12-24 06:16:45 +00:00
|
|
|
|
2019-08-06 11:57:45 +00:00
|
|
|
static propTypes = {
|
|
|
|
active: PropTypes.bool,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
icon: PropTypes.string,
|
|
|
|
items: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
icon: PropTypes.string,
|
|
|
|
meta: PropTypes.node,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
on: PropTypes.bool,
|
|
|
|
text: PropTypes.node,
|
|
|
|
})).isRequired,
|
|
|
|
onModalOpen: PropTypes.func,
|
|
|
|
onModalClose: PropTypes.func,
|
|
|
|
title: PropTypes.string,
|
|
|
|
value: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
needsModalUpdate: false,
|
|
|
|
open: false,
|
2019-08-06 12:18:09 +00:00
|
|
|
openedViaKeyboard: undefined,
|
2019-08-06 11:57:45 +00:00
|
|
|
placement: 'bottom',
|
|
|
|
};
|
2017-12-24 06:16:45 +00:00
|
|
|
|
2019-08-06 11:57:45 +00:00
|
|
|
// Toggles opening and closing the dropdown.
|
2019-08-06 12:18:09 +00:00
|
|
|
handleToggle = ({ target, type }) => {
|
2019-08-06 11:57:45 +00:00
|
|
|
const { onModalOpen } = this.props;
|
|
|
|
const { open } = this.state;
|
|
|
|
|
|
|
|
if (isUserTouching()) {
|
|
|
|
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 12:18:09 +00:00
|
|
|
if (this.state.open && this.activeElement) {
|
|
|
|
this.activeElement.focus();
|
|
|
|
}
|
2019-08-06 11:57:45 +00:00
|
|
|
this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
|
2019-08-06 12:18:09 +00:00
|
|
|
this.setState({ open: !this.state.open, openedViaKeyboard: type !== 'click' });
|
2019-08-06 11:57:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown = (e) => {
|
|
|
|
switch (e.key) {
|
2017-12-24 06:16:45 +00:00
|
|
|
case 'Escape':
|
2019-08-06 11:57:45 +00:00
|
|
|
this.handleClose();
|
2017-12-24 06:16:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-08-06 11:57:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 12:18:09 +00: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 11:57:45 +00:00
|
|
|
handleClose = () => {
|
2019-08-06 12:18:09 +00:00
|
|
|
if (this.state.open && this.activeElement) {
|
|
|
|
this.activeElement.focus();
|
|
|
|
}
|
2019-08-06 11:57:45 +00:00
|
|
|
this.setState({ open: false });
|
|
|
|
}
|
2017-12-24 06:16:45 +00:00
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
// Creates an action modal object.
|
2019-08-06 11:57:45 +00:00
|
|
|
handleMakeModal = () => {
|
2018-01-03 20:36:21 +00:00
|
|
|
const component = this;
|
2017-12-24 06:16:45 +00:00
|
|
|
const {
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
onModalOpen,
|
2018-01-03 20:36:21 +00:00
|
|
|
onModalClose,
|
2017-12-24 06:16:45 +00:00
|
|
|
value,
|
|
|
|
} = this.props;
|
2018-01-03 20:36:21 +00:00
|
|
|
|
|
|
|
// Required props.
|
|
|
|
if (!(onChange && onModalOpen && onModalClose && items)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The object.
|
|
|
|
return {
|
|
|
|
actions: items.map(
|
|
|
|
({
|
|
|
|
name,
|
|
|
|
...rest
|
|
|
|
}) => ({
|
|
|
|
...rest,
|
|
|
|
active: value && name === value,
|
|
|
|
name,
|
|
|
|
onClick (e) {
|
|
|
|
e.preventDefault(); // Prevents focus from changing
|
|
|
|
onModalClose();
|
|
|
|
onChange(name);
|
|
|
|
},
|
|
|
|
onPassiveClick (e) {
|
|
|
|
e.preventDefault(); // Prevents focus from changing
|
|
|
|
onChange(name);
|
|
|
|
component.setState({ needsModalUpdate: true });
|
|
|
|
},
|
|
|
|
})
|
|
|
|
),
|
|
|
|
};
|
2019-08-06 11:57:45 +00:00
|
|
|
}
|
2017-12-24 06:16:45 +00:00
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
// If our modal is open and our props update, we need to also update
|
|
|
|
// the modal.
|
2019-08-06 11:57:45 +00:00
|
|
|
handleUpdate = () => {
|
2018-01-03 20:36:21 +00:00
|
|
|
const { onModalOpen } = this.props;
|
|
|
|
const { needsModalUpdate } = this.state;
|
|
|
|
|
|
|
|
// Gets our modal object.
|
2019-08-06 11:57:45 +00:00
|
|
|
const modal = this.handleMakeModal();
|
2018-01-03 20:36:21 +00:00
|
|
|
|
|
|
|
// Reopens the modal with the new object.
|
|
|
|
if (needsModalUpdate && modal && onModalOpen) {
|
|
|
|
onModalOpen(modal);
|
|
|
|
}
|
2017-12-24 06:16:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
// Updates our modal as necessary.
|
|
|
|
componentDidUpdate (prevProps) {
|
|
|
|
const { items } = this.props;
|
|
|
|
const { needsModalUpdate } = this.state;
|
|
|
|
if (needsModalUpdate && items.find(
|
|
|
|
(item, i) => item.on !== prevProps.items[i].on
|
|
|
|
)) {
|
2019-08-06 11:57:45 +00:00
|
|
|
this.handleUpdate();
|
2018-01-03 20:36:21 +00:00
|
|
|
this.setState({ needsModalUpdate: false });
|
|
|
|
}
|
2017-12-24 06:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rendering.
|
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
active,
|
|
|
|
disabled,
|
|
|
|
title,
|
|
|
|
icon,
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
} = this.props;
|
2018-04-10 18:04:55 +00:00
|
|
|
const { open, placement } = this.state;
|
2017-12-24 06:16:45 +00:00
|
|
|
const computedClass = classNames('composer--options--dropdown', {
|
|
|
|
active,
|
2018-01-03 20:36:21 +00:00
|
|
|
open,
|
2018-08-19 15:14:30 +00:00
|
|
|
top: placement === 'top',
|
2017-12-24 06:16:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// The result.
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={computedClass}
|
2019-08-06 11:57:45 +00:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2017-12-24 06:16:45 +00:00
|
|
|
>
|
|
|
|
<IconButton
|
|
|
|
active={open || active}
|
|
|
|
className='value'
|
|
|
|
disabled={disabled}
|
|
|
|
icon={icon}
|
2019-08-05 12:25:48 +00:00
|
|
|
inverted
|
2019-08-06 11:57:45 +00:00
|
|
|
onClick={this.handleToggle}
|
2019-08-06 12:18:09 +00:00
|
|
|
onMouseDown={this.handleMouseDown}
|
|
|
|
onKeyDown={this.handleButtonKeyDown}
|
|
|
|
onKeyPress={this.handleKeyPress}
|
2017-12-24 06:16:45 +00:00
|
|
|
size={18}
|
|
|
|
style={{
|
|
|
|
height: null,
|
|
|
|
lineHeight: '27px',
|
|
|
|
}}
|
|
|
|
title={title}
|
|
|
|
/>
|
|
|
|
<Overlay
|
2018-01-03 20:36:21 +00:00
|
|
|
containerPadding={20}
|
2018-04-10 18:04:55 +00:00
|
|
|
placement={placement}
|
2017-12-24 06:16:45 +00:00
|
|
|
show={open}
|
|
|
|
target={this}
|
|
|
|
>
|
2019-04-21 15:17:10 +00:00
|
|
|
<DropdownMenu
|
2018-01-03 20:36:21 +00:00
|
|
|
items={items}
|
|
|
|
onChange={onChange}
|
2019-08-06 11:57:45 +00:00
|
|
|
onClose={this.handleClose}
|
2018-01-03 20:36:21 +00:00
|
|
|
value={value}
|
2019-08-06 12:18:09 +00:00
|
|
|
openedViaKeyboard={this.state.openedViaKeyboard}
|
2018-01-03 20:36:21 +00:00
|
|
|
/>
|
2017-12-24 06:16:45 +00:00
|
|
|
</Overlay>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|