import type React from 'react';
import { useState } from 'react';
import { useMove, useMovePointState } from '../../src';
import { toContentPoint } from '../utils';
import { DragContainer, DragItem } from './DragElements';
const getStateStyle = ({
colorInvert,
dragging,
}: {
colorInvert?: boolean;
dragging?: boolean;
}): React.CSSProperties => ({
...(dragging ? { borderColor: 'red' } : {}),
...(colorInvert ? { backgroundColor: 'black', color: 'white' } : {}),
});
function SimpleDrag(): React.ReactElement {
const {
moveOptions,
moving: dragging,
setPoint: setCoord,
...coord
} = useMovePointState({ x: 0, y: 0 });
const [colorInvert, setColorInvert] = useState(false);
const { moveProps } = useMove({
...moveOptions,
clickTolerance: 10,
onPureClick() {
setColorInvert((invert) => !invert);
},
});
const {
moveOptions: dragMoveOptions,
moving: dragDragging,
setPoint: setDragCoord,
...dragCoord
} = useMovePointState({ x: 100, y: 0 });
const { moveProps: dragMoveProps } = useMove(dragMoveOptions);
return (
drag click
drag only
);
}
function NestingDrag(): React.ReactElement {
const {
moveOptions: parentMoveOptions,
moving: unuseParentMoving,
setPoint: setParentCoord,
...parentCoord
} = useMovePointState({ x: 50, y: 50 });
const [parentColorInvert, setParentColorInvert] = useState(false);
const { moveProps: parentMoveProps } = useMove({
...parentMoveOptions,
clickTolerance: 10,
onPureClick(evt) {
evt.stopPropagation();
setParentColorInvert((invert) => !invert);
},
});
const {
moveOptions,
moving: dragging,
setPoint: setCoord,
...coord
} = useMovePointState({ x: 0, y: 0 });
const [colorInvert, setColorInvert] = useState(false);
const { moveProps } = useMove({
...moveOptions,
clickTolerance: 10,
moveStop(evt) {
return evt.ctrlKey || evt.metaKey;
},
onPureClick(evt) {
evt.stopPropagation();
setColorInvert((invert) => !invert);
},
});
const {
moveOptions: dragMoveOptions,
moving: dragDragging,
setPoint: setDragCoord,
...dragCoord
} = useMovePointState({ x: 100, y: 0 });
const { moveProps: dragMoveProps } = useMove(dragMoveOptions);
const [otherCoord, setOtherCoord] = useState({ x: 0, y: 100 });
const {
moveOptions: otherMoveOptions,
moving: otherDragging,
setPoint: setOtherDraggingCoord,
...otherDraggingCoord
} = useMovePointState({
...otherCoord,
onChange(evt, data) {
if (evt.type === 'pointerup' || evt.type === 'pointercancel') {
setOtherCoord(data);
}
},
});
const { moveProps: otherDraggingMoveProps } = useMove(otherMoveOptions);
return (
<>
drag click
drag only
other drag
>
);
}
function SVGDrag(): React.ReactElement {
const {
moveOptions,
moving: dragging,
setPoint: setCoord,
...coord
} = useMovePointState({
toPoint(moveData, evt) {
const elem = evt.currentTarget as SVGGraphicsElement;
const contentPoint = toContentPoint({ x: moveData.clientX, y: moveData.clientY }, elem);
const lastContentPoint = toContentPoint(
{ x: moveData.lastClientX, y: moveData.lastClientY },
elem,
);
const movementX = contentPoint.x - lastContentPoint.x;
const movementY = contentPoint.y - lastContentPoint.y;
return { x: moveData.lastX + movementX, y: moveData.lastY + movementY };
},
x: 0,
y: 0,
});
const { moveProps } = useMove(moveOptions);
return (
);
}
export function UseMovePointStateExamples(): React.ReactElement {
return (
Simple drag
Nesting drag
SVG drag
);
}