2017-12-24 15:16:45 +09:00
|
|
|
// Package imports.
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2023-01-12 05:58:46 +09:00
|
|
|
import Overlay from 'react-overlays/Overlay';
|
2017-12-24 15:16:45 +09:00
|
|
|
|
|
|
|
// Components.
|
|
|
|
import IconButton from 'flavours/glitch/components/icon_button';
|
2019-04-22 00:17:10 +09:00
|
|
|
import DropdownMenu from './dropdown_menu';
|
2017-12-24 15:16:45 +09:00
|
|
|
|
|
|
|
// Utils.
|
2022-10-11 17:41:15 +09:00
|
|
|
import { assignHandlers } from 'flavours/glitch/utils/react_helpers';
|
2017-12-24 15:16:45 +09:00
|
|
|
|
2019-08-06 20:57:45 +09:00
|
|
|
// The component.
|
|
|
|
export default class ComposerOptionsDropdown extends React.PureComponent {
|
2017-12-24 15:16:45 +09:00
|
|
|
|
2019-08-06 20:57:45 +09:00
|
|
|
static propTypes = {
|
2022-11-22 19:26:14 +09:00
|
|
|
isUserTouching: PropTypes.func,
|
2019-08-06 20:57:45 +09:00
|
|
|
disabled: PropTypes.bool,
|
|
|
|
icon: PropTypes.string,
|
|
|
|
items: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
icon: PropTypes.string,
|
2022-02-09 22:39:12 +09:00
|
|
|
meta: PropTypes.string,
|
2019-08-06 20:57:45 +09:00
|
|
|
name: PropTypes.string.isRequired,
|
2022-02-09 22:39:12 +09:00
|
|
|
text: PropTypes.string,
|
2019-08-06 20:57:45 +09:00
|
|
|
})).isRequired,
|
|
|
|
onModalOpen: PropTypes.func,
|
|
|
|
onModalClose: PropTypes.func,
|
|
|
|
title: PropTypes.string,
|
|
|
|
value: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
2021-02-11 08:53:12 +09:00
|
|
|
container: PropTypes.func,
|
2022-02-09 22:39:12 +09:00
|
|
|
renderItemContents: PropTypes.func,
|
|
|
|
closeOnChange: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
closeOnChange: true,
|
2019-08-06 20:57:45 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
open: false,
|
2019-08-06 21:18:09 +09:00
|
|
|
openedViaKeyboard: undefined,
|
2019-08-06 20:57:45 +09:00
|
|
|
placement: 'bottom',
|
|
|
|
};
|
2017-12-24 15:16:45 +09:00
|
|
|
|
2019-08-06 20:57:45 +09:00
|
|
|
// Toggles opening and closing the dropdown.
|
2023-01-12 05:58:46 +09:00
|
|
|
handleToggle = ({ type }) => {
|
2022-02-09 21:49:49 +09:00
|
|
|
const { onModalOpen } = this.props;
|
2019-08-06 20:57:45 +09:00
|
|
|
const { open } = this.state;
|
|
|
|
|
2022-11-22 19:26:14 +09:00
|
|
|
if (this.props.isUserTouching && this.props.isUserTouching()) {
|
2019-08-06 20:57:45 +09:00
|
|
|
if (this.state.open) {
|
|
|
|
this.props.onModalClose();
|
|
|
|
} else {
|
|
|
|
const modal = this.handleMakeModal();
|
|
|
|
if (modal && onModalOpen) {
|
|
|
|
onModalOpen(modal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-06 21:18:09 +09:00
|
|
|
if (this.state.open && this.activeElement) {
|
2020-08-21 21:14:28 +09:00
|
|
|
this.activeElement.focus({ preventScroll: true });
|
2019-08-06 21:18:09 +09:00
|
|
|
}
|
|
|
|
this.setState({ open: !this.state.open, openedViaKeyboard: type !== 'click' });
|
2019-08-06 20:57:45 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown = (e) => {
|
|
|
|
switch (e.key) {
|
2017-12-24 15:16:45 +09:00
|
|
|
case 'Escape':
|
2019-08-06 20:57:45 +09:00
|
|
|
this.handleClose();
|
2017-12-24 15:16:45 +09:00
|
|
|
break;
|
|
|
|
}
|
2019-08-06 20:57:45 +09:00
|
|
|
}
|
|
|
|
|
2019-08-06 21:18:09 +09: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 20:57:45 +09:00
|
|
|
handleClose = () => {
|
2019-08-06 21:18:09 +09:00
|
|
|
if (this.state.open && this.activeElement) {
|
2020-08-21 21:14:28 +09:00
|
|
|
this.activeElement.focus({ preventScroll: true });
|
2019-08-06 21:18:09 +09:00
|
|
|
}
|
2019-08-06 20:57:45 +09:00
|
|
|
this.setState({ open: false });
|
|
|
|
}
|
2017-12-24 15:16:45 +09:00
|
|
|
|
2022-02-09 22:39:12 +09:00
|
|
|
handleItemClick = (e) => {
|
|
|
|
const {
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
onModalClose,
|
|
|
|
closeOnChange,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const i = Number(e.currentTarget.getAttribute('data-index'));
|
|
|
|
|
2022-02-10 01:15:36 +09:00
|
|
|
const { name } = items[i];
|
2022-02-09 22:39:12 +09:00
|
|
|
|
|
|
|
e.preventDefault(); // Prevents focus from changing
|
|
|
|
if (closeOnChange) onModalClose();
|
|
|
|
onChange(name);
|
|
|
|
};
|
|
|
|
|
2018-01-04 05:36:21 +09:00
|
|
|
// Creates an action modal object.
|
2019-08-06 20:57:45 +09:00
|
|
|
handleMakeModal = () => {
|
2017-12-24 15:16:45 +09:00
|
|
|
const {
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
onModalOpen,
|
2018-01-04 05:36:21 +09:00
|
|
|
onModalClose,
|
2017-12-24 15:16:45 +09:00
|
|
|
value,
|
|
|
|
} = this.props;
|
2018-01-04 05:36:21 +09:00
|
|
|
|
|
|
|
// Required props.
|
|
|
|
if (!(onChange && onModalOpen && onModalClose && items)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The object.
|
|
|
|
return {
|
2022-02-09 22:39:12 +09:00
|
|
|
renderItemContents: this.props.renderItemContents,
|
|
|
|
onClick: this.handleItemClick,
|
2018-01-04 05:36:21 +09:00
|
|
|
actions: items.map(
|
|
|
|
({
|
|
|
|
name,
|
|
|
|
...rest
|
|
|
|
}) => ({
|
|
|
|
...rest,
|
|
|
|
active: value && name === value,
|
|
|
|
name,
|
|
|
|
})
|
|
|
|
),
|
|
|
|
};
|
2019-08-06 20:57:45 +09:00
|
|
|
}
|
2017-12-24 15:16:45 +09:00
|
|
|
|
2023-01-12 05:58:46 +09:00
|
|
|
setTargetRef = c => {
|
|
|
|
this.target = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
findTarget = () => {
|
|
|
|
return this.target;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOverlayEnter = (state) => {
|
|
|
|
this.setState({ placement: state.placement });
|
|
|
|
}
|
|
|
|
|
2017-12-24 15:16:45 +09:00
|
|
|
// Rendering.
|
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
disabled,
|
|
|
|
title,
|
|
|
|
icon,
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
value,
|
2021-02-11 08:53:12 +09:00
|
|
|
container,
|
2022-02-09 22:39:12 +09:00
|
|
|
renderItemContents,
|
|
|
|
closeOnChange,
|
2017-12-24 15:16:45 +09:00
|
|
|
} = this.props;
|
2018-04-11 03:04:55 +09:00
|
|
|
const { open, placement } = this.state;
|
2017-12-24 15:16:45 +09:00
|
|
|
|
2022-11-06 21:30:37 +09:00
|
|
|
const active = value && items.findIndex(({ name }) => name === value) === (placement === 'bottom' ? 0 : (items.length - 1));
|
|
|
|
|
2017-12-24 15:16:45 +09:00
|
|
|
return (
|
|
|
|
<div
|
2022-11-06 21:30:37 +09:00
|
|
|
className={classNames('privacy-dropdown', placement, { active: open })}
|
2019-08-06 20:57:45 +09:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2023-01-12 05:58:46 +09:00
|
|
|
ref={this.setTargetRef}
|
2017-12-24 15:16:45 +09:00
|
|
|
>
|
2022-11-06 21:30:37 +09:00
|
|
|
<div className={classNames('privacy-dropdown__value', { active })}>
|
|
|
|
<IconButton
|
|
|
|
active={open}
|
|
|
|
className='privacy-dropdown__value-icon'
|
|
|
|
disabled={disabled}
|
|
|
|
icon={icon}
|
|
|
|
inverted
|
|
|
|
onClick={this.handleToggle}
|
|
|
|
onMouseDown={this.handleMouseDown}
|
|
|
|
onKeyDown={this.handleButtonKeyDown}
|
|
|
|
onKeyPress={this.handleKeyPress}
|
|
|
|
size={18}
|
|
|
|
style={{
|
|
|
|
height: null,
|
|
|
|
lineHeight: '27px',
|
|
|
|
}}
|
|
|
|
title={title}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2017-12-24 15:16:45 +09:00
|
|
|
<Overlay
|
2018-01-04 05:36:21 +09:00
|
|
|
containerPadding={20}
|
2018-04-11 03:04:55 +09:00
|
|
|
placement={placement}
|
2017-12-24 15:16:45 +09:00
|
|
|
show={open}
|
2023-01-12 05:58:46 +09:00
|
|
|
flip
|
|
|
|
target={this.findTarget}
|
2021-02-11 08:53:12 +09:00
|
|
|
container={container}
|
2023-01-12 05:58:46 +09:00
|
|
|
popperConfig={{ strategy: 'fixed', onFirstUpdate: this.handleOverlayEnter }}
|
2017-12-24 15:16:45 +09:00
|
|
|
>
|
2023-01-12 05:58:46 +09:00
|
|
|
{({ props, placement }) => (
|
|
|
|
<div {...props} style={{ ...props.style, width: 350, maxWidth: '100vw' }}>
|
|
|
|
<div className={`dropdown-animation privacy-dropdown__dropdown ${placement}`}>
|
|
|
|
<DropdownMenu
|
|
|
|
items={items}
|
|
|
|
renderItemContents={renderItemContents}
|
|
|
|
onChange={onChange}
|
|
|
|
onClose={this.handleClose}
|
|
|
|
value={value}
|
|
|
|
openedViaKeyboard={this.state.openedViaKeyboard}
|
|
|
|
closeOnChange={closeOnChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2017-12-24 15:16:45 +09:00
|
|
|
</Overlay>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|