-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwipeOuut.jsx
95 lines (87 loc) · 1.99 KB
/
SwipeOuut.jsx
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
83
84
85
86
87
88
89
90
91
92
93
94
95
import React, { useState, useRef } from "react";
import { useDrag } from "react-use-gesture";
import styled, { css } from "styled-components";
const StyledWrap = styled.div`
position: relative;
width: 100%;
height: 60px;
background: red;
`;
const StyledContent = styled.div`
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 60px;
background: blue;
z-index: 2;
${({ moving }) => css`
${!moving ? "transition: all 1s;" : ""}
`}
`;
const StyledRemove = styled.div`
position: absolute;
right: 0;
top: 0;
width: 100px;
height: 60px;
line-height: 60px;
background: red;
z-index: 1;
`;
const _width = 100;
const SwipeOut = React.memo(() => {
const posXRef = useRef(0);
const [moving, setMoving] = useState(false);
const [posX, setPosX] = useState(0);
const clientWRef = useRef();
const bind = useDrag(data => {
const { down, delta, offset, movement } = data;
const clientW = clientWRef.current;
const x = movement[0];
let latestMoveX = posXRef.current + x;
console.log(`x : ${latestMoveX}`);
if (latestMoveX >= 0) {
latestMoveX = 0;
}
if (down) {
setMoving(true);
if (Math.abs(x) > _width) {
setPosX(latestMoveX);
} else {
// 回到100
setPosX(latestMoveX);
}
} else {
// eslint-disable-next-line operator-assignment
// setMoveX(-100);
setMoving(false);
if (Math.abs(latestMoveX) > _width / 2) {
setPosX(-_width);
posXRef.current = -_width;
} else {
setPosX(0);
posXRef.current = 0;
}
}
});
console.log(`${posX}px`);
return (
<StyledWrap
{...bind()}
ref={element => {
if (element) {
clientWRef.current = element.clientWidth;
}
}}
>
12312312312
<StyledContent
moving={moving}
style={{ transform: `translateX(${posX}px)` }}
/>
<StyledRemove>删除</StyledRemove>
</StyledWrap>
);
});
export default SwipeOut;