Не подскажешь почему onTransitionEnd не выполняется никогда?
export const useDragTouch = (target) => {
const [touchStart, setTouchStart] = useState()
const [snapping, setSnapping] = useState(false)
const [lastOffsetX, setLastOffsetX] = useState(0);
useLayoutEffect(() => {
if (snapping) {
const hide = lastOffsetX < 100;
const onTransitionEnd = () => {
console.log(1)
setSnapping(false);
target.current.style.transition = "none";
setLastOffsetX(hide ? 0 : 200);
};
target.current.addEventListener("transitionend", onTransitionEnd);
Object.assign(
target.current.style, {
transition: "transform 0.16s ease-out",
marginLeft: (hide ? 0 : 200) + 'px'
});
return () => target.current.removeEventListener("transitionend", onTransitionEnd);
}
})
useLayoutEffect(() => {
const handleTouchStart = (e) => {
setTouchStart(e.touches[0].clientX)
}
const handleTouchMove = (e) => {
if (!snapping) {
const pos = Math.min(
200,
lastOffsetX + e.touches[0].clientX - touchStart
)
target.current.style.marginLeft = pos + 'px'
}
}
const handleTouchEnd = (e) => {
setLastOffsetX(
Math.min(
200,
lastOffsetX + e.changedTouches[0].clientX - touchStart
)
)
setSnapping(true);
}
document.addEventListener('touchstart', handleTouchStart)
document.addEventListener('touchmove', handleTouchMove)
document.addEventListener('touchend', handleTouchEnd)
return () => {
document.removeEventListener('touchstart', handleTouchStart)
document.removeEventListener('touchmove', handleTouchMove)
document.removeEventListener('touchend', handleTouchEnd)
};
})
}