connectDragPreview remains a mystery
See original GitHub issueI’ve been reviewing the documentation and github issues for the past hour and connectDragPreview
still remains a mystery to me. I would like to define a custom preview for how the item appears when it is being dragged (just like the tutorial example with the horse image). Here is my code:
export const tokenCollector: DragSourceCollector = (connect, monitor) => {
return {
connectDragSource: connect.dragSource(),
connectDragPreview: connect.dragPreview(),
isDragging: monitor.isDragging()
};
};
class TokenClass extends React.Component<TokenProps> {
componentDidMount() {
const { connectDragPreview } = this.props;
const img = new Image();
img.src = '<encoded image>';
img.onload = () => connectDragPreview(<div>{img}</div>);
}
render() {
const { connectDragSource, isDragging, children } = this.props;
return connectDragSource(
<div style={{ opacity: isDragging ? 0.5 : 1 }}>
{children}
</div>
);
}
}
const dragSource = DragSource(DropType.Token, tokenSpec, tokenCollector)(TokenClass);
export { dragSource as Token };
The standard preview appears with this code.
I then tried to wrap my connectDragSource
in my component’s render()
method with connectDragPreview
, but that only appears to change the drag source where it was picked up from, not how it appears as it’s being dragged.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:11 (1 by maintainers)
Top Results From Across the Web
How does react-dnd's connectDragPreview() work?
I've been reviewing the documentation and github issues and connectDragPreview still remains a mystery to me.
Read more >DragPreviewImage - React DnD
New to React DnD? Read the overview before jumping into the docs. DragPreviewImage. A Component to render an HTML Image element as a...
Read more >Sota (@Sotq_17) / Twitter
I've been reviewing the documentation and github issues for the past hour and connectDragPreview still remains a mystery to me.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
After wrestling with this problem myself, I found this was due to a limitation in the HTML5 Drag-and-Drop API.
You cannot define arbitrary, detached DOM to write up a drag preview. You have to use an image.
To make this easier, we’ve added the DragPreviewImage component in the top-level API.
Like @augbog said, under the hood all that React DnD is doing is using setDragImage. CustomDragLayer tries to get around this by setting the drag preview image to an empty image. This is explicitly stated in the docs:
This is accomplished by using the
getEmptyImage
utility function that’s included in thereact-dnd-html5-backend
package. You can see this in action in the CustomDragLayer example provided:You are then responsible for translating the CustomDragLayer yourself. This can be seen in the following code excerpt (taken from this file in the same example as earlier):
The CustomDragLayer is also not configured for a specific item type (like DragSource is), so you have to specifically check which item type is being dragged and then decide whether or not the CustomDragLayer should be displayed (following is from same file as above):
Kind of a mess if you ask me. I’ve had a bit more success in the past by using the dom-to-image library to turn the DOM node that I want to use as the drag preview into an image and then passing the resulting image to the
connectDragPreview
function. Something like: