Skip to content

Commit 59dd313

Browse files
committed
feat(fab): prevent default
1 parent 67add9d commit 59dd313

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/fab/Fab.tsx

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Fab: React.FC<FabProps> = forwardRef((props) => {
1111
const { buttonProps, icon = null, text, onClick } = props;
1212

1313
const fabButtonRef = useRef(null);
14+
const fabRef = useRef<HTMLDivElement>(null);
1415

1516
const [fabButtonSize, setFabButtonSize] = useState({
1617
width: 0,
@@ -32,15 +33,15 @@ const Fab: React.FC<FabProps> = forwardRef((props) => {
3233
x: 16,
3334
y: 32,
3435
});
35-
const [switchPos, setSwitchPos] = useState({
36+
let switchPos = {
3637
hasMoved: false, // exclude click event
3738
x: btnSwitchPos.x, // right
3839
y: btnSwitchPos.y, // bottom
3940
startX: 0,
4041
startY: 0,
4142
endX: 0,
4243
endY: 0,
43-
});
44+
};
4445

4546
const { classPrefix } = useConfig();
4647
const name = useMemo(() => `${classPrefix}-fab`, [classPrefix]);
@@ -58,11 +59,11 @@ const Fab: React.FC<FabProps> = forwardRef((props) => {
5859
const onTouchStart = (e: React.TouchEvent<HTMLDivElement>) => {
5960
props.onDragStart?.({ e });
6061

61-
setSwitchPos({
62+
switchPos = {
6263
...switchPos,
63-
startX: e.touches[0].pageX,
64-
startY: e.touches[0].pageY,
65-
});
64+
startX: e.touches[0].clientX,
65+
startY: e.touches[0].clientY,
66+
};
6667
};
6768

6869
const getSwitchButtonSafeAreaXY = (x: number, y: number) => {
@@ -81,9 +82,17 @@ const Fab: React.FC<FabProps> = forwardRef((props) => {
8182
return [resultX, resultY];
8283
};
8384

84-
const onTouchMove = (e: React.TouchEvent<HTMLDivElement>) => {
85+
const onTouchMove = (e: TouchEvent) => {
86+
if (!switchPos.hasMoved) {
87+
switchPos = {
88+
...switchPos,
89+
startX: e.touches[0].clientX,
90+
startY: e.touches[0].clientY,
91+
};
92+
}
93+
8594
e.stopPropagation();
86-
e.preventDefault?.();
95+
e.preventDefault();
8796

8897
if (!props.draggable) {
8998
return;
@@ -92,8 +101,9 @@ const Fab: React.FC<FabProps> = forwardRef((props) => {
92101
if (e.touches.length <= 0) {
93102
return;
94103
}
95-
const offsetX = e.touches[0].pageX - switchPos.startX;
96-
const offsetY = e.touches[0].pageY - switchPos.startY;
104+
105+
const offsetX = e.touches[0].clientX - switchPos.startX;
106+
const offsetY = e.touches[0].clientY - switchPos.startY;
97107
const x = Math.floor(switchPos.x - offsetX);
98108
const y = Math.floor(switchPos.y - offsetY);
99109

@@ -110,17 +120,23 @@ const Fab: React.FC<FabProps> = forwardRef((props) => {
110120
toChangeData.y = newY;
111121
toChangePos.endY = newY;
112122
}
113-
setSwitchPos(toChangePos);
123+
switchPos = toChangePos;
114124
setBtnSwitchPos(toChangeData);
115125
};
116126

127+
useEffect(() => {
128+
const fab = fabRef.current;
129+
fab && fab.addEventListener('touchmove', onTouchMove, { passive: false });
130+
// eslint-disable-next-line react-hooks/exhaustive-deps
131+
}, []);
132+
117133
const setSwitchPosition = (switchX: number, switchY: number) => {
118134
const [newSwitchX, newSwitchY] = getSwitchButtonSafeAreaXY(switchX, switchY);
119-
setSwitchPos({
135+
switchPos = {
120136
...switchPos,
121137
x: newSwitchX,
122138
y: newSwitchY,
123-
});
139+
};
124140

125141
if (props.draggable !== 'vertical') {
126142
setBtnSwitchPos({
@@ -141,22 +157,22 @@ const Fab: React.FC<FabProps> = forwardRef((props) => {
141157
return;
142158
}
143159
props.onDragEnd?.({ e });
144-
setSwitchPos({
160+
switchPos = {
145161
...switchPos,
146162
startX: 0,
147163
startY: 0,
148164
hasMoved: false,
149-
});
165+
};
150166
setSwitchPosition(switchPos.endX, switchPos.endY);
151167
};
152168

153169
return (
154170
<div
171+
ref={fabRef}
155172
className={name}
156173
style={props.draggable && fabButtonSize.width ? { ...fabStyle } : props.style}
157174
onClick={onClickHandle}
158175
onTouchStart={onTouchStart}
159-
onTouchMove={onTouchMove}
160176
onTouchEnd={onTouchEnd}
161177
>
162178
<Button

0 commit comments

Comments
 (0)