import PropTypes from 'prop-types'; import { h, Component, createRef } from 'preact'; import listingPropTypes from './listingPropTypes'; import { Button } from '@crayons'; const Icon = () => ( ); class DropdownMenu extends Component { componentRef = createRef(); static propTypes = { isOwner: PropTypes.bool.isRequired, listing: listingPropTypes.isRequired, }; constructor(props) { super(props); this.state = { isOpen: false, }; } toggleMenu = () => { const { isOpen } = this.state; this.setState({ isOpen: !isOpen }, this.addOrRemoveClickOutsideHandler); }; addOrRemoveClickOutsideHandler = () => { const { isOpen } = this.state; return isOpen ? document.addEventListener('mousedown', this.handleClickOutside) : document.removeEventListener('mousedown', this.handleClickOutside); }; handleClickOutside = (e) => { if ( this.componentRef.current && !this.componentRef.current.contains(e.target) ) { this.toggleMenu(); } }; render() { const { listing, isOwner } = this.props; const { isOpen } = this.state; const { id, category, slug } = listing; const editUrl = `/listings/${id}/edit`; const reportUrl = `/report-abuse?url=https://dev.to/listings/${category}/${slug}`; return (