-
Notifications
You must be signed in to change notification settings - Fork 740
/
drag.tsx
55 lines (51 loc) · 1.82 KB
/
drag.tsx
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
import { motion } from "@framer"
import * as React from "react"
function getValueParam(params: any, name: string) {
const param = params.get(name) as string | undefined
if (!param) return 0
if (param.endsWith("%")) {
return param
} else {
return parseFloat(param)
}
}
// It's important for this test to only trigger a single rerender while dragging (in response to onDragStart) of draggable component.
export const App = () => {
const params = new URLSearchParams(window.location.search)
const axis = params.get("axis")
const lock = params.get("lock")
const top = parseFloat(params.get("top")) || undefined
const left = parseFloat(params.get("left")) || undefined
const right = parseFloat(params.get("right")) || undefined
const bottom = parseFloat(params.get("bottom")) || undefined
const snapToOrigin = Boolean(params.get("return"))
const x = getValueParam(params, "x")
const y = getValueParam(params, "y")
const layout = params.get("layout") || undefined
// We do this to test when scroll position isn't 0/0
React.useLayoutEffect(() => {
window.scrollTo(0, 100)
}, [])
return (
<div style={{ height: 2000, paddingTop: 100 }}>
<motion.div
id="box"
data-testid="draggable"
drag={axis ? axis : true}
dragElastic={0}
dragMomentum={false}
dragConstraints={{ top, left, right, bottom }}
dragSnapToOrigin={snapToOrigin}
dragDirectionLock={!!lock}
layout={layout}
initial={{
width: 50,
height: 50,
background: "red",
x,
y,
}}
/>
</div>
)
}