mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
7c97f8ab6e
* Use new JSX runtime and update Babel Node target * Update .eslintrc * Remove React imports GitOrigin-RevId: 559de0267f8f2934c56a860ea8701bb522aa861a
72 lines
1.9 KiB
JavaScript
72 lines
1.9 KiB
JavaScript
import { useRef } from 'react'
|
|
import PropTypes from 'prop-types'
|
|
import { useDragLayer } from 'react-dnd'
|
|
import classNames from 'classnames'
|
|
|
|
// a custom component rendered on top of a draggable area that renders the
|
|
// dragged item. See
|
|
// https://react-dnd.github.io/react-dnd/examples/drag-around/custom-drag-layer
|
|
// for more details.
|
|
// Also used to display a container border when hovered.
|
|
function FileTreeDraggablePreviewLayer({ isOver }) {
|
|
const { isDragging, item, clientOffset } = useDragLayer(monitor => ({
|
|
isDragging: monitor.isDragging(),
|
|
item: monitor.getItem(),
|
|
clientOffset: monitor.getClientOffset(),
|
|
}))
|
|
const ref = useRef()
|
|
|
|
const containerOffset = ref.current
|
|
? ref.current.getBoundingClientRect()
|
|
: null
|
|
|
|
if (!isDragging) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<div
|
|
ref={ref}
|
|
className={classNames('dnd-draggable-preview-layer', {
|
|
'dnd-droppable-hover': isOver,
|
|
})}
|
|
>
|
|
<div style={getItemStyle(clientOffset, containerOffset)}>
|
|
<DraggablePreviewItem title={item.title} />
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
FileTreeDraggablePreviewLayer.propTypes = {
|
|
isOver: PropTypes.bool.isRequired,
|
|
}
|
|
|
|
function DraggablePreviewItem({ title }) {
|
|
return <div className="dnd-draggable-preview-item">{title}</div>
|
|
}
|
|
|
|
DraggablePreviewItem.propTypes = {
|
|
title: PropTypes.string.isRequired,
|
|
}
|
|
|
|
// makes the preview item follow the cursor.
|
|
// See https://react-dnd.github.io/react-dnd/docs/api/drag-layer-monitor
|
|
function getItemStyle(clientOffset, containerOffset) {
|
|
if (!containerOffset || !clientOffset) {
|
|
return {
|
|
display: 'none',
|
|
}
|
|
}
|
|
const { x: containerX, y: containerY } = containerOffset
|
|
const { x: clientX, y: clientY } = clientOffset
|
|
const posX = clientX - containerX - 15
|
|
const posY = clientY - containerY - 15
|
|
const transform = `translate(${posX}px, ${posY}px)`
|
|
return {
|
|
transform,
|
|
WebkitTransform: transform,
|
|
}
|
|
}
|
|
|
|
export default FileTreeDraggablePreviewLayer
|