-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
238 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { RefObject, useEffect, useState } from 'react' | ||
|
||
interface PositionDelta { | ||
dx: number | ||
dy: number | ||
} | ||
|
||
interface Position { | ||
x: number | ||
y: number | ||
} | ||
|
||
interface DraggableState extends PositionDelta { | ||
dragging: boolean | ||
} | ||
|
||
interface DraggableOptions { | ||
onStart?: (state: Position) => void | ||
onDrag?: (state: PositionDelta) => void | ||
onStop?: (state: PositionDelta) => void | ||
} | ||
|
||
|
||
const useDraggable = (ref: RefObject<HTMLElement>, options: DraggableOptions = {}): DraggableState => { | ||
const [{ dx, dy }, setOffset] = useState({ dx: 0, dy: 0 }) | ||
const [dragging, setDragging] = useState(false) | ||
|
||
const handleMouseDown = (e: MouseEvent) => { | ||
const startPos = { | ||
x: e.clientX - dx, | ||
y: e.clientY - dy, | ||
} | ||
options.onStart?.(startPos) | ||
|
||
const handleMouseMove = (e: MouseEvent) => { | ||
const ele = ref.current | ||
if (!ele) { | ||
return | ||
} | ||
|
||
const dx = e.clientX - startPos.x | ||
const dy = e.clientY - startPos.y | ||
setDragging(true) | ||
setOffset({ dx, dy }) | ||
options.onDrag?.({ dx, dy }) | ||
} | ||
|
||
const handleMouseUp = (e: MouseEvent) => { | ||
document.removeEventListener('mousemove', handleMouseMove) | ||
document.removeEventListener('mouseup', handleMouseUp) | ||
setDragging(false) | ||
setOffset({ dx: 0, dy: 0 }) | ||
|
||
const dx = e.clientX - startPos.x | ||
const dy = e.clientY - startPos.y | ||
options.onStop?.({ dx, dy }) | ||
} | ||
|
||
document.addEventListener('mousemove', handleMouseMove) | ||
document.addEventListener('mouseup', handleMouseUp) | ||
} | ||
|
||
const handleTouchStart = (e: TouchEvent) => { | ||
const touch = e.touches[0] | ||
const startPos = { | ||
x: touch.clientX - dx, | ||
y: touch.clientY - dy, | ||
} | ||
options.onStart?.(startPos) | ||
|
||
const handleTouchMove = (e: TouchEvent) => { | ||
const ele = ref.current | ||
if (!ele) { | ||
return | ||
} | ||
|
||
const touch = e.touches[0] | ||
const dx = touch.clientX - startPos.x | ||
const dy = touch.clientY - startPos.y | ||
setDragging(true) | ||
setOffset({ dx, dy }) | ||
options.onDrag?.({ dx, dy }) | ||
} | ||
|
||
const handleTouchEnd = (e: TouchEvent) => { | ||
document.removeEventListener('touchmove', handleTouchMove) | ||
document.removeEventListener('touchend', handleTouchEnd) | ||
setDragging(false) | ||
setOffset({ dx: 0, dy: 0 }) | ||
|
||
const touch = e.changedTouches[0] | ||
const dx = touch.clientX - startPos.x | ||
const dy = touch.clientY - startPos.y | ||
options.onStop?.({ dx, dy }) | ||
} | ||
|
||
document.addEventListener('touchmove', handleTouchMove) | ||
document.addEventListener('touchend', handleTouchEnd) | ||
} | ||
|
||
useEffect(() => { | ||
ref.current?.addEventListener('mousedown', handleMouseDown) | ||
ref.current?.addEventListener('touchstart', handleTouchStart) | ||
return () => { | ||
ref.current?.removeEventListener('mousedown', handleMouseDown) | ||
ref.current?.removeEventListener('touchstart', handleTouchStart) | ||
} | ||
}, [ref.current]) | ||
|
||
return { dx, dy, dragging } | ||
} | ||
|
||
export { | ||
useDraggable as default, | ||
type DraggableState, | ||
type Position, | ||
type PositionDelta | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
export { | ||
default as useDraggable, | ||
type DraggableState, | ||
type Position, | ||
type PositionDelta | ||
} from './drag' | ||
export { default as useWordleStore } from './store' | ||
export { useSwipe } from './swipe' | ||
export { default as useSwipe } from './swipe' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters