|
1 | 1 | // prettier-ignore |
2 | 2 | import { makeStyles } from '@material-ui/core'; |
3 | | -import * as React from 'react'; |
4 | | -import { animated, useSpring } from 'react-spring'; |
5 | | -import { useDrag } from 'react-use-gesture'; |
| 3 | +import * as React from "react"; |
| 4 | +import { animated, useSpring } from "react-spring"; |
| 5 | +import { useDrag } from "react-use-gesture"; |
6 | 6 | interface Props { |
7 | | - children: React.ReactNode; |
8 | | - on: boolean; |
9 | | - dragable?: boolean; |
10 | | - dragRadius?: number; |
11 | | - bounceRadius?: number; |
12 | | - open?: boolean; |
13 | | - bounceOnOpen?: boolean; |
14 | | - bounceOnClose?: boolean; |
15 | | - bounceDirection?: 'TOP' | 'BOTTOM' | 'LEFT' | 'RIGHT'; |
| 7 | + children: React.ReactNode; |
| 8 | + on: boolean; |
| 9 | + dragable?: boolean; |
| 10 | + dragRadius?: number; |
| 11 | + bounceRadius?: number; |
| 12 | + open?: boolean; |
| 13 | + bounceOnOpen?: boolean; |
| 14 | + bounceOnClose?: boolean; |
| 15 | + bounceDirection?: "TOP" | "BOTTOM" | "LEFT" | "RIGHT"; |
16 | 16 | } |
17 | 17 |
|
18 | 18 | const DEFAULT_DRAG_RADIUS = 12; |
19 | 19 | const DEFAULT_BOUNCE_RADIUS = 3; |
20 | 20 |
|
21 | 21 | export function DragableContainer(props: Props) { |
22 | | - const { |
23 | | - children, |
24 | | - on, |
25 | | - dragable, |
26 | | - dragRadius, |
27 | | - bounceRadius, |
28 | | - open, |
29 | | - bounceOnOpen, |
30 | | - bounceOnClose, |
31 | | - bounceDirection, |
32 | | - } = props; |
| 22 | + const { |
| 23 | + children, |
| 24 | + on, |
| 25 | + dragable, |
| 26 | + dragRadius, |
| 27 | + bounceRadius, |
| 28 | + open, |
| 29 | + bounceOnOpen, |
| 30 | + bounceOnClose, |
| 31 | + bounceDirection, |
| 32 | + } = props; |
33 | 33 |
|
34 | | - if (on) { |
35 | | - const [{ x, y, cursor }, set] = useSpring(() => ({ |
36 | | - x: 0, |
37 | | - y: 0, |
38 | | - config: { tension: 400, friction: 7, precision: 0.1 }, |
39 | | - cursor: 'pointer', |
40 | | - })); |
| 34 | + if (on) { |
| 35 | + const [{ x, y, cursor }, set] = useSpring(() => ({ |
| 36 | + x: 0, |
| 37 | + y: 0, |
| 38 | + config: { tension: 400, friction: 7, precision: 0.1 }, |
| 39 | + cursor: "pointer", |
| 40 | + })); |
41 | 41 |
|
42 | | - const classes = useStyles(props); |
| 42 | + const classes = useStyles(props); |
43 | 43 |
|
44 | | - React.useEffect(() => { |
45 | | - if ((open && bounceOnOpen) || (!open && bounceOnClose)) { |
46 | | - const bRadius = bounceRadius ? bounceRadius : DEFAULT_BOUNCE_RADIUS; |
47 | | - let x = bRadius; |
48 | | - let y = bRadius; |
49 | | - switch (bounceDirection) { |
50 | | - case 'LEFT': |
51 | | - x = -bRadius; |
52 | | - y = 0; |
53 | | - break; |
54 | | - case 'RIGHT': |
55 | | - x = bRadius; |
56 | | - y = 0; |
57 | | - break; |
58 | | - case 'TOP': |
59 | | - x = 0; |
60 | | - y = -bRadius; |
61 | | - break; |
62 | | - case 'BOTTOM': |
63 | | - x = 0; |
64 | | - y = bRadius; |
65 | | - break; |
66 | | - } |
67 | | - set({ x, y }); |
68 | | - setTimeout(() => set({ x: 0, y: 0 }), 100); |
69 | | - } |
70 | | - }, [open]); |
| 44 | + React.useEffect(() => { |
| 45 | + if ((open && bounceOnOpen) || (!open && bounceOnClose)) { |
| 46 | + const bRadius = bounceRadius ? bounceRadius : DEFAULT_BOUNCE_RADIUS; |
| 47 | + let x = bRadius; |
| 48 | + let y = bRadius; |
| 49 | + switch (bounceDirection) { |
| 50 | + case "LEFT": |
| 51 | + x = -bRadius; |
| 52 | + y = 0; |
| 53 | + break; |
| 54 | + case "RIGHT": |
| 55 | + x = bRadius; |
| 56 | + y = 0; |
| 57 | + break; |
| 58 | + case "TOP": |
| 59 | + x = 0; |
| 60 | + y = -bRadius; |
| 61 | + break; |
| 62 | + case "BOTTOM": |
| 63 | + x = 0; |
| 64 | + y = bRadius; |
| 65 | + break; |
| 66 | + } |
| 67 | + set({ x, y }); |
| 68 | + setTimeout(() => set({ x: 0, y: 0 }), 100); |
| 69 | + } |
| 70 | + }, [open]); |
71 | 71 |
|
72 | | - // Creates a drag gesture |
73 | | - const bind = useDrag(({ down, movement: [dX, dY] }) => { |
74 | | - if (dragable) { |
75 | | - const rMax = dragRadius ? dragRadius : DEFAULT_DRAG_RADIUS; |
76 | | - const r = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2)); |
77 | | - // Find point along radius with rMax length |
78 | | - // See: https://math.stackexchange.com/q/1630886 |
79 | | - if (r > rMax) { |
80 | | - dX *= rMax / r; |
81 | | - dY *= rMax / r; |
82 | | - } |
83 | | - set({ |
84 | | - x: down ? dX : 0, |
85 | | - y: down ? dY : 0, |
86 | | - immediate: down, |
87 | | - cursor: down ? 'grabbing' : 'pointer', |
88 | | - }); |
89 | | - } |
90 | | - }); |
| 72 | + // Creates a drag gesture |
| 73 | + const bind = useDrag(({ down, movement: [dX, dY] }) => { |
| 74 | + if (dragable) { |
| 75 | + const rMax = dragRadius ? dragRadius : DEFAULT_DRAG_RADIUS; |
| 76 | + const r = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2)); |
| 77 | + // Find point along radius with rMax length |
| 78 | + // See: https://math.stackexchange.com/q/1630886 |
| 79 | + if (r > rMax) { |
| 80 | + dX *= rMax / r; |
| 81 | + dY *= rMax / r; |
| 82 | + } |
| 83 | + set({ |
| 84 | + x: down ? dX : 0, |
| 85 | + y: down ? dY : 0, |
| 86 | + immediate: down, |
| 87 | + cursor: down ? "grabbing" : "pointer", |
| 88 | + }); |
| 89 | + } |
| 90 | + }); |
91 | 91 |
|
92 | | - return ( |
93 | | - <div className={classes.root}> |
94 | | - <animated.div {...bind()} className={classes.dragable} style={{ cursor: cursor, left: x, top: y }}> |
95 | | - {children} |
96 | | - </animated.div> |
97 | | - </div> |
98 | | - ); |
99 | | - } else { |
100 | | - return <>{children}</>; |
101 | | - } |
| 92 | + return ( |
| 93 | + <div className={classes.root}> |
| 94 | + <animated.div |
| 95 | + {...bind()} |
| 96 | + className={classes.dragable} |
| 97 | + style={{ cursor: cursor, left: x, top: y }} |
| 98 | + > |
| 99 | + {children} |
| 100 | + </animated.div> |
| 101 | + </div> |
| 102 | + ); |
| 103 | + } else { |
| 104 | + return <>{children}</>; |
| 105 | + } |
102 | 106 | } |
103 | 107 |
|
104 | 108 | const useStyles = makeStyles({ |
105 | | - root: { |
106 | | - position: 'relative', |
107 | | - top: 0, |
108 | | - left: 0, |
109 | | - }, |
| 109 | + root: { |
| 110 | + top: 0, |
| 111 | + left: 0, |
| 112 | + }, |
110 | 113 |
|
111 | | - dragable: { |
112 | | - position: 'absolute', |
113 | | - }, |
| 114 | + dragable: { |
| 115 | + position: "absolute", |
| 116 | + }, |
114 | 117 | }); |
0 commit comments