Skip to content

Instantly share code, notes, and snippets.

@Debdut
Created April 30, 2020 15:44
Show Gist options
  • Save Debdut/6dd4bf6dd32ea32a060edb90f9e21c38 to your computer and use it in GitHub Desktop.
Save Debdut/6dd4bf6dd32ea32a060edb90f9e21c38 to your computer and use it in GitHub Desktop.
.box {
background: #fff;
box-sizing: border-box;
border: 1px solid #999;
float: left;
}
import React from 'react'
import Draggable from 'react-draggable'
import './DraggableRecursion.css'
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
depth: 2
}
}
addWindow = () => {
this.setState({ depth : this.state.depth + 1})
}
render() {
return (
<div>
<button onClick={this.addWindow}>Add Window</button>
<div style={{ width: '100vw', height: '100vh', position: 'absolute'}}>
<Box depth={this.state.depth} unitSize={100}/>
</div>
</div>
)
}
}
const Box = ({depth, unitSize}) => (
<Draggable cancel={`#box-${depth - 1}`} bounds="parent">
<div className="box" id={`box-${depth}`} style={{ height: unitSize * depth, width: unitSize * depth }}>
<span>{depth}</span>
{(depth > 1) ? <Box depth={depth - 1} unitSize={unitSize} /> : null}
</div>
</Draggable>
)
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment