Skip to content

Commit

Permalink
Add touch backend example
Browse files Browse the repository at this point in the history
  • Loading branch information
nickclaw committed May 11, 2017
1 parent 43b99b8 commit b47a8e4
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/mobile-backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env
npm-debug.log*
yarn-debug.log*
yarn-error.log*

7 changes: 7 additions & 0 deletions examples/mobile-backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Basic Example

```bash
# in this directory
npm install
npm start
```
22 changes: 22 additions & 0 deletions examples/mobile-backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "mobile-backend",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-scripts": "0.9.5"
},
"dependencies": {
"prop-types": "^15.5.9",
"react": "^15.5.4",
"react-dnd": "^2.4.0",
"react-dnd-scrollzone": "^3.1.0",
"react-dnd-touch-backend": "^0.3.10",
"react-dom": "^15.5.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Binary file added examples/mobile-backend/public/favicon.ico
Binary file not shown.
31 changes: 31 additions & 0 deletions examples/mobile-backend/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tag above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start`.
To create a production bundle, use `npm run build`.
-->
</body>
</html>
6 changes: 6 additions & 0 deletions examples/mobile-backend/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.App {
width: 500px;
height: 500px;
overflow: scroll;
border: solid 2px black;
}
26 changes: 26 additions & 0 deletions examples/mobile-backend/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { Component } from 'react';
import TouchBackend from 'react-dnd-touch-backend';
import { DragDropContextProvider } from 'react-dnd';
import withScrolling from 'react-dnd-scrollzone';
import DragItem from './DragItem';
import DragPreview from './DragPreview';
import './App.css';

const ScrollingComponent = withScrolling('div');

const ITEMS = [1,2,3,4,5,6,7,8,9,10];

export default class App extends Component {
render() {
return (
<DragDropContextProvider backend={TouchBackend({ enableMouseEvents: true })}>
<ScrollingComponent className="App">
{ITEMS.map(n => (
<DragItem key={n} label={`Item ${n}`} />
))}
<DragPreview />
</ScrollingComponent>
</DragDropContextProvider>
);
}
}
5 changes: 5 additions & 0 deletions examples/mobile-backend/src/DragItem.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DragItem {
width: 100%;
border: solid 1px black;
padding: 30px;
}
31 changes: 31 additions & 0 deletions examples/mobile-backend/src/DragItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { DragSource } from 'react-dnd';
import './DragItem.css';

class DragItem extends PureComponent {

static propTypes = {
label: PropTypes.string.isRequired,
};

render() {
return this.props.dragSource(
<div className="DragItem">
{this.props.label}
</div>
);
}
}

export default DragSource(
'foo',
{
beginDrag() {
return {}
}
},
(connect) => ({
dragSource: connect.dragSource(),
})
)(DragItem);
16 changes: 16 additions & 0 deletions examples/mobile-backend/src/DragPreview.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.DragPreview {
position: absolute;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
pointer-events: none;
z-index: 1000;
}

.DragPreview__item {
width: 100px;
height: 100px;
background: white;
border: solid 1px black;
}
35 changes: 35 additions & 0 deletions examples/mobile-backend/src/DragPreview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { PureComponent } from 'react';
import { DragLayer } from 'react-dnd';
import './DragPreview.css';

class DragPreview extends PureComponent {

render() {
const {
item,
offset,
} = this.props;

return (
<div className="DragPreview">
{item && (
<div
className="DragPreview__item"
style={{
position: 'absolute',
top: offset.y,
left: offset.x,
}}
/>
)}
</div>
);
}
}

export default DragLayer(
monitor => ({
item: monitor.getItem(),
offset: monitor.getClientOffset(),
})
)(DragPreview);
5 changes: 5 additions & 0 deletions examples/mobile-backend/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
9 changes: 9 additions & 0 deletions examples/mobile-backend/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';

ReactDOM.render(
<App />,
document.getElementById('root')
);

0 comments on commit b47a8e4

Please sign in to comment.