File size: 2,140 Bytes
04e85f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07cf1c8
 
04e85f3
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
import React from 'react';
import Panel from '../../Panel/Panel';

class DraggablePanel extends React.Component {
    constructor(props) {
        super(props);
        this.panelRef = React.createRef();
    }

    state = {
        dragging: false,
        initialX: 0,
        initialY: 0,
    };

    componentDidMount() {
        const rect = this.panelRef.current.getBoundingClientRect();
        this.panelRef.current.style.right = '25px'; // Update this property
        this.panelRef.current.style.bottom = '25px'; // Update this property

        window.addEventListener('mousemove', this.onMouseMove);
        window.addEventListener('mouseup', this.onMouseUp);
    }

    componentWillUnmount() {
        window.removeEventListener('mousemove', this.onMouseMove);
        window.removeEventListener('mouseup', this.onMouseUp);
    }

    onMouseDown = (event) => {
        if (event.button !== 0) return;
        const rect = this.panelRef.current.getBoundingClientRect();
        this.setState({
            dragging: true,
            initialX: event.clientX - rect.left,
            initialY: event.clientY - rect.top,
        });
    };

    onMouseMove = (event) => {
        if (!this.state.dragging) return;
        const x = event.clientX - this.state.initialX;
        const y = event.clientY - this.state.initialY;
        this.panelRef.current.style.left = `${x}px`;
        this.panelRef.current.style.top = `${y}px`;
    };

    onMouseUp = (event) => {
        if (event.button !== 0) return;
        this.setState({ dragging: false });
    };

    render() {
        return (
            <div
                ref={this.panelRef}
                onMouseDown={this.onMouseDown}
                style={{
                    position: 'fixed',
                    zIndex: 99999,
                    cursor: 'move',
                    right: 0, // Add this property
                    bottom: 0, // Add this property
                    width: '400px',
                    height: '350px'
                }}
            >
                <Panel />
            </div>
        );
    }
}

export default DraggablePanel;