Skip to content

Instantly share code, notes, and snippets.

@G710
Created August 15, 2018 18:59
Show Gist options
  • Save G710/6f85869b73ff08ce95ca93e31ed510f8 to your computer and use it in GitHub Desktop.
Save G710/6f85869b73ff08ce95ca93e31ed510f8 to your computer and use it in GitHub Desktop.
Quick blueprint / react-dnd implementation
// Let's make <Card text='Write the docs' /> draggable!
import * as React from "react";
import { DragSource } from "react-dnd";
/**
* Implements the drag source contract.
*/
const cardSource = {
beginDrag(props) {
return {
text: props.text
};
}
};
/**
* Specifies the props to inject into your component.
*/
function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
};
}
class Card extends React.Component {
render() {
const { isDragging, connectDragSource, text } = this.props;
return connectDragSource(
<div style={{ opacity: isDragging ? 0.5 : 1 }}>{text}</div>
);
}
}
// Export the wrapped component:
export default DragSource("CARD", cardSource, collect)(Card);
.example-card {
margin: 10px 0px;
}
.example-header {
font-size: 24px;
font-weight: bold;
border-bottom: 1px solid #e1e8ed;
padding-bottom: 5px;
margin-bottom: 10px;
}
import * as React from "react";
import { render } from "react-dom";
import { Tree } from "@blueprintjs/core";
import Card from "./Card";
import HTML5Backend from "react-dnd-html5-backend";
import { DragDropContext } from "react-dnd";
@DragDropContext(HTML5Backend, /* optional */ { window })
class App extends React.Component {
render() {
return (
<div>
<Tree
contents={[
{ id: 1, label: <Card text="First" /> },
{ id: 2, label: <Card text="Second" /> }
]}
/>
</div>
);
}
}
render(<App />, document.getElementById("root"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment