gab-social/app/javascript/gabsocial/components/popover/popover_base.js

149 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-02-28 15:20:47 +00:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
2020-03-11 23:56:18 +00:00
import { Manager, Reference, Popper } from 'react-popper'
2020-02-28 15:20:47 +00:00
import classnames from 'classnames/bind'
import Overlay from 'react-overlays/lib/Overlay'
import spring from 'react-motion/lib/spring'
import Motion from '../../features/ui/util/optional_motion'
import { openPopover, closePopover } from '../../actions/popover'
import { openModal, closeModal } from '../../actions/modal'
import { isUserTouching } from '../../utils/is_mobile'
const cx = classnames.bind(_s)
let id = 0
const mapStateToProps = state => ({
isModalOpen: state.get('modal').modalType === 'ACTIONS',
popoverPlacement: state.getIn(['popover', 'placement']),
openPopoverType: state.getIn(['popover', 'popoverType']),
})
const mapDispatchToProps = (dispatch, { status, items }) => ({
onOpen(id, onItemClick, popoverPlacement, keyboard) {
2020-03-24 04:39:12 +00:00
// dispatch(isUserTouching() ? openModal('ACTIONS', {
// status,
// actions: items,
// onClick: onItemClick,
// }) : openPopover(id, popoverPlacement, keyboard))
2020-02-28 15:20:47 +00:00
},
onClose(id) {
dispatch(closeModal())
dispatch(closePopover(id))
},
})
export default
@connect(mapStateToProps, mapDispatchToProps)
class PopoverBase extends ImmutablePureComponent {
static contextTypes = {
router: PropTypes.object,
}
static propTypes = {
title: PropTypes.string,
disabled: PropTypes.bool,
status: ImmutablePropTypes.map,
isUserTouching: PropTypes.func,
isModalOpen: PropTypes.bool.isRequired,
onOpen: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
2020-03-11 23:56:18 +00:00
position: PropTypes.string,
2020-02-28 15:20:47 +00:00
openPopoverType: PropTypes.number,
visible: PropTypes.bool,
2020-03-11 23:56:18 +00:00
targetRef: PropTypes.node,
2020-03-14 17:31:29 +00:00
innerRef: PropTypes.node,
2020-02-28 15:20:47 +00:00
}
static defaultProps = {
title: 'Menu',
2020-03-11 23:56:18 +00:00
position: 'bottom',
2020-02-28 15:20:47 +00:00
}
state = {
id: id++,
}
handleClose = () => {
this.props.onClose(this.state.id)
}
handleKeyDown = e => {
switch (e.key) {
case 'Escape':
this.handleClose()
break
}
}
handleItemClick = e => {
const i = Number(e.currentTarget.getAttribute('data-index'))
const { action, to } = this.props.items[i]
this.handleClose()
if (typeof action === 'function') {
e.preventDefault()
action()
} else if (to) {
e.preventDefault()
this.context.router.history.push(to)
}
}
setTargetRef = c => {
this.target = c
}
findTarget = () => {
return this.target
}
componentWillUnmount = () => {
if (this.state.id === this.props.openPopoverType) {
this.handleClose()
}
}
render() {
2020-03-11 23:56:18 +00:00
const {
children,
visible,
position,
openPopoverType,
targetRef,
2020-03-14 17:31:29 +00:00
innerRef,
2020-03-11 23:56:18 +00:00
} = this.props
2020-02-28 15:20:47 +00:00
const open = this.state.id === openPopoverType
const containerClasses = cx({
default: 1,
z4: 1,
displayNone: !visible,
})
2020-03-25 03:08:43 +00:00
console.log("targetRef:", targetRef)
2020-02-28 15:20:47 +00:00
return (
2020-03-11 23:56:18 +00:00
<Manager>
<Popper
placement={position}
referenceElement={targetRef}
>
{({ ref, style, placement, arrowProps }) => (
2020-03-14 17:31:29 +00:00
<div ref={ref} style={style} data-placement={placement} className={[_s.my5, _s.boxShadow2].join(' ')}>
2020-03-11 23:56:18 +00:00
<div ref={arrowProps.ref} style={arrowProps.style} />
2020-03-14 17:31:29 +00:00
<div ref={innerRef} data-popover='true' onKeyDown={this.handleKeyDown} className={containerClasses}>
{children}
2020-03-11 23:56:18 +00:00
</div>
</div>
)}
</Popper>
</Manager>
2020-02-28 15:20:47 +00:00
)
}
}