overleaf/services/web/frontend/js/features/file-tree/components/file-tree-item/file-tree-item-menu.jsx
Alf Eaton b9a6e7009d Improve context menu behaviour in the file tree (#15142)
* Tidy up menu button
* Unselect all items when clicking in the file tree root
* Close the context menu when selecting a new item
* Avoid selecting multiple items with Ctrl+click on macOS

GitOrigin-RevId: b67a724909668ec13d7a6d09ffc31574cb42238f
2023-10-13 08:03:26 +00:00

45 lines
1.1 KiB
JavaScript

import { useRef } from 'react'
import PropTypes from 'prop-types'
import { useTranslation } from 'react-i18next'
import Icon from '../../../../shared/components/icon'
import { useFileTreeMainContext } from '../../contexts/file-tree-main'
function FileTreeItemMenu({ id }) {
const { t } = useTranslation()
const { contextMenuCoords, setContextMenuCoords } = useFileTreeMainContext()
const menuButtonRef = useRef()
function handleClick(event) {
event.stopPropagation()
if (!contextMenuCoords) {
const target = menuButtonRef.current.getBoundingClientRect()
setContextMenuCoords({
top: target.top + target.height / 2,
left: target.right,
})
} else {
setContextMenuCoords(null)
}
}
return (
<div className="menu-button btn-group">
<button
className="entity-menu-toggle btn btn-sm"
id={`menu-button-${id}`}
onClick={handleClick}
ref={menuButtonRef}
>
<Icon type="ellipsis-v" accessibilityLabel={t('menu')} />
</button>
</div>
)
}
FileTreeItemMenu.propTypes = {
id: PropTypes.string.isRequired,
}
export default FileTreeItemMenu