This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
index.js
82 lines (79 loc) · 2.11 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { useRef } from 'react'
import { Box, Flex } from 'blockstack-ui'
import { useSpring, config, animated } from 'react-spring'
import { useDrag } from 'react-use-gesture'
import { Image } from '@components/image'
import { Video } from '@components/video'
import useComponentSize from '@rehooks/component-size'
import { useWindowSize } from 'react-use'
const Drag = ({ items, ...rest }) => {
const [{ x }, set] = useSpring(() => ({
x: 0,
config: { ...config.default, tension: 200, friction: 30 }
}))
let ref = useRef(null)
let size = useComponentSize(ref)
const { width } = useWindowSize()
const bind = useDrag(({ delta: [dx], memo = x.getValue() }) => {
if (!size || !width) return
if (dx + memo <= 0) {
const componentSize = size.width - width + 96
if (componentSize && dx + memo <= componentSize * -1) {
set({ x: componentSize * -1 })
} else {
set({ x: dx + memo })
}
} else {
set({ x: 0 })
}
return memo
})
return (
<Box
width={1}
my={[5, 5, 8]}
overflow="hidden"
cursor="grab"
maxWidth="100vw"
pl={[5, 5, 9]}
{...bind()}
{...rest}
>
<Flex
is={animated.div}
style={{
willChange: 'transform',
transform: x.interpolate((x) => `translateX(${x}px)`)
}}
>
<div style={{ display: 'flex', alignItems: 'center' }} ref={ref}>
{items.map((item, key) => (
<Box
flexShrink={0}
mr={[5, 5, 9]}
width={item.width}
key={key}
style={{
userSelect: 'none',
pointerEvents: 'none'
}}
>
{item.type === 'image' ? (
<Image src={item.src} />
) : (
<Video
noHover
hideOverlay
src={item.src}
ratio={item.ratio}
poster={item.poster}
/>
)}
</Box>
))}
</div>
</Flex>
</Box>
)
}
export { Drag }