file-tree-item-menu.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { useState } from 'react'
  2. import { findDOMNode } from 'react-dom'
  3. import PropTypes from 'prop-types'
  4. import { useTranslation } from 'react-i18next'
  5. import withoutPropagation from '../../../../infrastructure/without-propagation'
  6. import { Button } from 'react-bootstrap'
  7. import Icon from '../../../../shared/components/icon'
  8. import { useFileTreeMainContext } from '../../contexts/file-tree-main'
  9. function FileTreeItemMenu({ id }) {
  10. const { t } = useTranslation()
  11. const { contextMenuCoords, setContextMenuCoords } = useFileTreeMainContext()
  12. const [dropdownTarget, setDropdownTarget] = useState()
  13. function handleClick(_ev) {
  14. const target = dropdownTarget.getBoundingClientRect()
  15. if (!contextMenuCoords) {
  16. setContextMenuCoords({
  17. top: target.top + target.height / 2,
  18. left: target.right,
  19. })
  20. } else {
  21. setContextMenuCoords(null)
  22. }
  23. }
  24. const menuButtonRef = component => {
  25. if (component) {
  26. // eslint-disable-next-line react/no-find-dom-node
  27. setDropdownTarget(findDOMNode(component))
  28. }
  29. }
  30. return (
  31. <div className="menu-button btn-group">
  32. <Button
  33. className="entity-menu-toggle"
  34. bsSize="sm"
  35. id={`menu-button-${id}`}
  36. onClick={withoutPropagation(handleClick)}
  37. ref={menuButtonRef}
  38. bsStyle={null}
  39. >
  40. <Icon type="ellipsis-v" accessibilityLabel={t('menu')} />
  41. </Button>
  42. </div>
  43. )
  44. }
  45. FileTreeItemMenu.propTypes = {
  46. id: PropTypes.string.isRequired,
  47. }
  48. export default FileTreeItemMenu