2 import PropTypes
from 'prop-types';
3 import React
from 'react';
4 import spring
from 'react-motion/lib/spring';
7 import ComposerOptionsDropdownContentItem
from './item';
10 import { withPassive
} from 'flavours/glitch/util/dom_helpers';
11 import Motion
from 'flavours/glitch/util/optional_motion';
12 import { assignHandlers
} from 'flavours/glitch/util/react_helpers';
16 // When the document is clicked elsewhere, we close the dropdown.
17 handleDocumentClick ({ target
}) {
18 const { node
} = this;
19 const { onClose
} = this.props
;
20 if (onClose
&& node
&& !node
.contains(target
)) {
25 // Stores our node in `this.node`.
31 // The spring to use with our motion.
32 const springMotion
= spring(1, {
38 export default class ComposerOptionsDropdownContent
extends React
.PureComponent
{
43 assignHandlers(this, handlers
);
45 // Instance variables.
53 // On mounting, we add our listeners.
54 componentDidMount () {
55 const { handleDocumentClick
} = this.handlers
;
56 document
.addEventListener('click', handleDocumentClick
, false);
57 document
.addEventListener('touchend', handleDocumentClick
, withPassive
);
58 this.setState({ mounted: true });
61 // On unmounting, we remove our listeners.
62 componentWillUnmount () {
63 const { handleDocumentClick
} = this.handlers
;
64 document
.removeEventListener('click', handleDocumentClick
, false);
65 document
.removeEventListener('touchend', handleDocumentClick
, withPassive
);
70 const { mounted
} = this.state
;
71 const { handleRef
} = this.handlers
;
89 opacity: springMotion
,
94 {({ opacity
, scaleX
, scaleY
}) => (
95 // It should not be transformed when mounting because the resulting
96 // size will be used to determine the coordinate of the menu by
99 className
='composer--options--dropdown--content'
104 transform: mounted
? `scale(${scaleX}, ${scaleY})` : null,
112 <ComposerOptionsDropdownContentItem
113 active
={name
=== value
}
131 ComposerOptionsDropdownContent
.propTypes
= {
132 items: PropTypes
.arrayOf(PropTypes
.shape({
133 icon: PropTypes
.string
,
134 meta: PropTypes
.node
,
135 name: PropTypes
.string
.isRequired
,
137 text: PropTypes
.node
,
139 onChange: PropTypes
.func
,
140 onClose: PropTypes
.func
,
141 style: PropTypes
.object
,
142 value: PropTypes
.string
,
146 ComposerOptionsDropdownContent
.defaultProps
= { style: {} };