Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Bug: SelectRectの選択が要素外のポインター移動を反映しない #8 #9

Merged
merged 2 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/SelectRect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function useSelectRectHandler() {
() =>
createDragHandler({
onDown: (e) => {
e.preventDefault();
const el = e.currentTarget as HTMLElement;
const rect = el.getBoundingClientRect();
return {
Expand Down Expand Up @@ -60,6 +61,7 @@ export function useSelectRectHandler() {
},
options: {
disableCapture: true,
pointerMoveOnWindow: true,
},
}),
[],
Expand Down
6 changes: 4 additions & 2 deletions src/utils/interactions/createDragHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function createDragHandler<T>({
onUp?: (e: PointerEvent, ctx?: T, moveEvent?: PointerEvent) => void;
options?: {
disableCapture?: boolean;
pointerMoveOnWindow?: boolean;
};
}) {
const handlePointerDown = (
Expand All @@ -33,6 +34,7 @@ export function createDragHandler<T>({

const result = onDown?.(downEvent);
let prevEvent: PointerEvent | undefined = undefined;
const moveTarget = options?.pointerMoveOnWindow ? window : el;
const handlePointerMove = (e: Event) => {
if (!(e instanceof PointerEvent)) return;
if (result === false) return;
Expand All @@ -50,7 +52,7 @@ export function createDragHandler<T>({
const handlePointerUp = (e: Event) => {
if (!(e instanceof PointerEvent)) return;
if (result === false) return;
el.removeEventListener("pointermove", handlePointerMove);
moveTarget.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
if (options?.disableCapture !== true) {
el.releasePointerCapture(e.pointerId);
Expand All @@ -63,7 +65,7 @@ export function createDragHandler<T>({
if (options?.disableCapture !== true) {
el.setPointerCapture(downEvent.pointerId);
}
el.addEventListener("pointermove", handlePointerMove, {
moveTarget.addEventListener("pointermove", handlePointerMove, {
passive: false,
});
window.addEventListener("pointerup", handlePointerUp);
Expand Down
Loading