React で 要素をドラッグする場合のサンプルプログラムとしては、
こちらの記事が、とてもわかりやすくまとまっています。
参考サイト
しかしながら、最新のReactの記述ではないため、多少の修正が必要となります。
修正ポイントを抜粋いたしました。
sample1.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import React, { useState } from 'react'; import ReactDOM from 'react-dom'; import Dialog from './Dialog' import './sample.css' function Sample() { const [showDialog, setShowDialog] = useState(false); const _showDialog = () => { setShowDialog(!showDialog); } return ( <div className="container MainApp"> <div className='Title'>Example Dialog Popper</div> <div className='button' onClick={_showDialog.bind(this)}> Show Dialog </div> <Dialog onClose={_showDialog.bind(this)} show={showDialog}/> </div> ); } export default Sample; if (document.getElementById('sample')) { ReactDOM.render(<Sample />, document.getElementById('sample')); } |
Dialog.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import React, { useState } from 'react'; import './sample.css' function Dialog(props) { const [state, setState] = useState({ diffX: 0, diffY: 0, dragging: false, styles: {} }); const _dragStart = (e) => { setState( { ...state, diffX: e.screenX - e.currentTarget.getBoundingClientRect().left, diffY: e.screenY - e.currentTarget.getBoundingClientRect().top, dragging: true }); } const _dragging = (e) => { if(state.dragging) { var left = e.screenX - state.diffX; var top = e.screenY - state.diffY; setState({ ...state, styles: { left: left, top: top } }); } } const _dragEnd = () => { setState({ ...state, dragging: false }); } var classes = props.show ? 'Dialog' : 'Dialog hidden'; return( <div className={classes} style={state.styles} onMouseDown={_dragStart} onMouseMove={_dragging} onMouseUp={_dragEnd}> <div className='DialogTitle'>My Dialog</div> <div className='Contents'> Contents of the Dialog: - one - two - three </div> <div className='closeButton' onClick={props.onClose}> Close </div> </div> ); } export default Dialog; |
sample.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
.Title { font-weight: bold; font-size: 16px; padding-top: 30px; } .button { font-size: 12px; width: 100px; color: black; border: 2px solid black; border-radius: 30px; padding: 10px; margin: 10px; margin-left: 80%; } .MainApp { background-color: lightsalmon; height: 1000px; } .Dialog { width: 200px; height: 200px; background-color: lightblue; border-radius: 10px; border: 3px solid grey; position: absolute; cursor: move; top: 10px; left: 10px; } .hidden { display: none; } .DialogTitle { font-weight: bold; padding-top: 10px; padding-bottom: 10px; border-bottom: 3px solid grey; } .Contents { padding-top: 10px; padding-bottom: 10px; } .closeButton { font-size: 12px; width: 100px; color: black; border: 2px solid black; border-radius: 25px; padding: 10px; margin: 10px; margin-left: 40px; cursor: pointer; } |
react-beautiful-dnd
GitHub - atlassian/react-beautiful-dnd: Beautiful and accessible drag and drop for lists with React
Beautiful and accessible drag and drop for lists with React - GitHub - atlassian/react-beautiful-dnd: Beautiful and acce...