2021-06-23 05:37:08 -04:00
|
|
|
import { useRef } from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
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.
|
2023-10-25 05:24:58 -04:00
|
|
|
function FileTreeDraggablePreviewLayer({
|
|
|
|
isOver,
|
|
|
|
isDragging,
|
|
|
|
item,
|
|
|
|
clientOffset,
|
|
|
|
}) {
|
2020-11-26 09:22:30 -05:00
|
|
|
const ref = useRef()
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
ref={ref}
|
|
|
|
className={classNames('dnd-draggable-preview-layer', {
|
2021-04-27 03:52:58 -04:00
|
|
|
'dnd-droppable-hover': isOver,
|
2020-11-26 09:22:30 -05:00
|
|
|
})}
|
|
|
|
>
|
2023-10-02 06:26:25 -04:00
|
|
|
{isDragging && item?.title && (
|
|
|
|
<div
|
|
|
|
style={getItemStyle(
|
|
|
|
clientOffset,
|
|
|
|
ref.current?.getBoundingClientRect()
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<DraggablePreviewItem title={item.title} />
|
|
|
|
</div>
|
|
|
|
)}
|
2020-11-26 09:22:30 -05:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
FileTreeDraggablePreviewLayer.propTypes = {
|
2021-04-27 03:52:58 -04:00
|
|
|
isOver: PropTypes.bool.isRequired,
|
2023-10-25 05:24:58 -04:00
|
|
|
isDragging: PropTypes.bool.isRequired,
|
|
|
|
item: PropTypes.shape({
|
|
|
|
title: PropTypes.string.isRequired,
|
|
|
|
}),
|
2023-11-06 05:54:03 -05:00
|
|
|
clientOffset: PropTypes.shape({
|
|
|
|
x: PropTypes.number,
|
|
|
|
y: PropTypes.number,
|
|
|
|
}),
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function DraggablePreviewItem({ title }) {
|
|
|
|
return <div className="dnd-draggable-preview-item">{title}</div>
|
|
|
|
}
|
|
|
|
|
|
|
|
DraggablePreviewItem.propTypes = {
|
2021-04-27 03:52:58 -04:00
|
|
|
title: PropTypes.string.isRequired,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-04-27 03:52:58 -04:00
|
|
|
display: 'none',
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
2021-04-27 03:52:58 -04:00
|
|
|
WebkitTransform: transform,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileTreeDraggablePreviewLayer
|