Dragging Elements

Right now, Markes and Labels can be dragged around the screen when enabled. Both components accept a position and onChange callback prop.

We also provide a useCoordState hook, that's just a wrapper on useState with a few convienience features.

It automatically normalizes's any Vec2ish value passed as initial prop and types it accordingly.

You could, however, just use useState and pass a Vec2 directly if you prefer.

With useCoordState

  • tsx
const [point, setPoint] = useCoordState(Vec2.of(0, 0)); // ✅ Vec2(0, 0)
<Marker position={point} onChange={setPoint} label={"đŸ”Ĩ"} />;


const [point, setPoint] = useCoordState([0, 0]); // ✅ Vec2(0, 0)
<Marker position={point} onChange={setPoint} label={"đŸ”Ĩ"} />;


const [point, setPoint] = useCoordState({ x: 0, y: 0 }); // ✅ Vec2(0, 0)
<Marker position={point} onChange={setPoint} label={"đŸ”Ĩ"} />;

With useState

  • tsx
const [point, setPoint] = useState(Vec2.of(0, 0)); // ✅ Vec2(0, 0)
<Marker position={point} onChange={setPoint} label={"đŸ”Ĩ"} />;


const [point, setPoint] = useState<[number, number]>([0, 0]); // ❌ [0, 0]
<Marker position={point} onChange={setPoint} label={"đŸ”Ĩ"} />;


const [point, setPoint] = useState({ x: 0, y: 0 }); // ❌ { x: 0, y: 0 }
<Marker position={point} onChange={setPoint} label={"đŸ”Ĩ"} />;

Note that even in the ❌ cases, the Marker will still probably work, but the point state will change to a Vec2 after the first onChange callback So if you're using Typescript, it will complain about the type mismatch.

You could solve make it type safe again by doing something like this. (is it worth it though? 🤔)

  • tsx
const [point, setPoint] = useState<Vec2ish>([0, 0]); // ✅ [0, 0]
<Marker position={point} onChange={setPoint} label={"đŸ”Ĩ"} />;


//  after onChange is triggered
point; // ✅ Vec2(0, 0)

Or this

  • tsx
const [point, setPoint] = useState<[number, number]>([0, 0]); // ✅ [0, 0]
<Marker position={point} onChange={({ x, y }) => [x, y]} label={"đŸ”Ĩ"} />;


//  after onChange is triggered
point; // ✅ [0, 0]

Example

For finer control, you can also leverage other HTML native events, such as onMouse*, onPointer*, etc.