Draggable Sticky Notes in React Without a Library
If you keep each sticky note's position (x and y) and stacking order (z) in state, and update those values from the Pointer Events received on the handle, you can move a note anywhere without a library. Calling setPointerCapture() on pointer down keeps the note following the pointer until release, even if the pointer leaves the handle.
To bring a note to the top, increase the z value used for z-index. You can also move the note to the end of the array so it is drawn last in the DOM, but in my test the drag stopped midway. Below I describe that result and possible reasons.
The test environment was Ubuntu 24.04, Node.js 22.23.4, and Chromium 153. I used React 19.3.1, TypeScript 7.0.2, and Playwright 1.63.0, and checked the behavior by operating the mouse and keyboard with Playwright. I did not test touch input, Firefox, Safari, mobile browsers, or screen reader announcements. This article also does not cover what happens when the board size changes during use or when there are a very large number of notes.

A component that holds each note's position and stacking order
Each note is represented as { id, x, y, z, text, color }. x and y are the px offsets from the board's top-left corner to the note's top-left corner, and z is the stacking order. NoteBoard receives the list of notes from its parent and passes a new list through onChange each time a note moves. onDrop is also called when a drag ends and the pointer is released, and when a note is moved with the arrow keys.
import { useEffect, useRef } from 'react';
import type { KeyboardEvent, PointerEvent } from 'react';
export type Note = { id: number; x: number; y: number; z: number; text: string; color: string };
type Drag = { id: number; offsetX: number; offsetY: number };
type Props = {
notes: Note[];
onChange: (notes: Note[]) => void;
// Called when a drag ends and the pointer is released, and when a note is moved with the arrow keys (not called during a drag)
onDrop?: (notes: Note[]) => void;
};
const clamp = (n: number, min: number, max: number) => Math.min(Math.max(n, min), max);
export function NoteBoard({ notes, onChange, onDrop }: Props) {
const boardRef = useRef<HTMLDivElement>(null);
const drag = useRef<Drag | null>(null);
// The latest list of notes. The notes prop keeps the value from the time of rendering,
// so keep it in a ref too, to pass the value just changed in the same handler to onDrop
const latest = useRef(notes);
useEffect(() => { latest.current = notes; }, [notes]);
// Adjust the note's top-left corner so it stays inside the board
function fit(x: number, y: number, el: HTMLElement) {
const board = boardRef.current!;
return {
x: Math.round(clamp(x, 0, board.clientWidth - el.offsetWidth)),
y: Math.round(clamp(y, 0, board.clientHeight - el.offsetHeight)),
};
}
function update(id: number, patch: Partial<Note>) {
const next = latest.current.map((n) => (n.id === id ? { ...n, ...patch } : n));
latest.current = next;
onChange(next);
}
// Bring to front (set z to 1 more than the current maximum)
function bringToFront(id: number) {
const top = Math.max(...latest.current.map((n) => n.z));
const note = latest.current.find((n) => n.id === id)!;
if (note.z !== top) update(id, { z: top + 1 });
}
function handlePointerDown(e: PointerEvent<HTMLDivElement>, note: Note) {
if (e.button !== 0) return;
const board = boardRef.current!.getBoundingClientRect();
// Offset between the grab point and the note's top-left corner. Keeping this offset while moving prevents the note from jumping to the pointer position
drag.current = {
id: note.id,
offsetX: e.clientX - board.left - note.x,
offsetY: e.clientY - board.top - note.y,
};
bringToFront(note.id);
// Keep sending events to this element until release, even if the pointer leaves the handle
e.currentTarget.setPointerCapture(e.pointerId);
}
function handlePointerMove(e: PointerEvent<HTMLDivElement>) {
const d = drag.current;
if (!d) return;
const board = boardRef.current!.getBoundingClientRect();
const el = e.currentTarget.parentElement!; // the whole note
update(d.id, fit(e.clientX - board.left - d.offsetX, e.clientY - board.top - d.offsetY, el));
}
function handlePointerUp() {
if (!drag.current) return;
drag.current = null;
onDrop?.(latest.current);
}
// Move 10px per arrow key press
function handleKeyDown(e: KeyboardEvent<HTMLDivElement>, note: Note) {
const step = { ArrowLeft: [-10, 0], ArrowRight: [10, 0], ArrowUp: [0, -10], ArrowDown: [0, 10] }[e.key];
if (!step) return;
e.preventDefault();
update(note.id, fit(note.x + step[0], note.y + step[1], e.currentTarget.parentElement!));
onDrop?.(latest.current);
}
return (
<div className="board" ref={boardRef}>
{notes.map((note) => (
<div
key={note.id}
className="note"
data-id={note.id}
style={{ transform: `translate(${note.x}px, ${note.y}px)`, zIndex: note.z, background: note.color }}
>
<div
className="note-handle"
tabIndex={0}
role="button"
aria-label={`付せん${note.id}を動かす(矢印キーでも動きます)`}
onPointerDown={(e) => handlePointerDown(e, note)}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerUp}
onPointerCancel={handlePointerUp}
onFocus={() => bringToFront(note.id)}
onKeyDown={(e) => handleKeyDown(e, note)}
/>
<textarea
aria-label={`付せん${note.id}の内容`}
value={note.text}
onChange={(e) => update(note.id, { text: e.target.value })}
onFocus={() => bringToFront(note.id)}
/>
</div>
))}
</div>
);
}
The drag state (which note is being dragged, and the offset between the grab point and the note's top-left corner) is kept in a ref named drag. Changing a ref does not cause a re-render, so a ref is a good place for values that are not shown on screen.
The list of notes is also kept in a ref named latest. The notes prop is the value from that render. In a case like the arrow key handler, where the position is changed and then immediately passed to onDrop, you must pass latest.current instead of notes. Otherwise, the position from before the change is saved.
Only the top bar (the handle) can be used to move a note. Pressing the textarea in the body works normally for selecting and typing text. The display position is set with transform: translate(), and fit corrects it so the note does not extend outside the board.
The styling is done with CSS. Notes are position: absolute inside .board, and .note-handle has touch-action: none.
.board {
position: relative;
height: 360px;
background: #eefafd;
border: 1px solid #9aa5ab;
border-radius: 8px;
overflow: hidden;
}
.note {
position: absolute;
left: 0;
top: 0;
width: 160px;
height: 120px;
display: flex;
flex-direction: column;
box-shadow: 0 2px 6px rgb(0 0 0 / 0.2);
}
.note-handle {
height: 22px;
flex: none;
background: rgb(0 0 0 / 0.08);
cursor: grab;
/* Prevent the page from scrolling when dragging with touch */
touch-action: none;
}
.note-handle:active { cursor: grabbing; }
.note-handle:focus-visible { outline: 3px solid #00c8e8; }
.note textarea {
flex: 1;
border: 0;
padding: 6px 8px;
background: transparent;
font: inherit;
resize: none;
}
With touch, when the browser starts scrolling or zooming, it sends pointercancel and the drag stops (MDN's explanation of touch-action). touch-action: none tells the browser not to scroll or zoom on the handle. However, I have not tested this with touch input.
An App that saves positions
The App that uses the component keeps the list of notes in state and saves it to localStorage in onDrop. When the page opens, it uses the saved list if one exists.
import { useState } from 'react';
import { NoteBoard } from './NoteBoard';
import type { Note } from './NoteBoard';
const STORAGE_KEY = 'notes';
const initialNotes: Note[] = [
{ id: 1, x: 20, y: 20, z: 1, text: '牛乳を買う', color: '#fff3a8' },
{ id: 2, x: 120, y: 70, z: 2, text: '記事の図を描く', color: '#c9f1fa' },
{ id: 3, x: 220, y: 120, z: 3, text: '本を返す', color: '#ffd6e0' },
];
// Use the saved positions if they exist
function load(): Note[] {
try {
const saved = localStorage.getItem(STORAGE_KEY);
return saved ? (JSON.parse(saved) as Note[]) : initialNotes;
} catch {
return initialNotes;
}
}
export function App() {
const [notes, setNotes] = useState<Note[]>(load);
function save(next: Note[]) {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
} catch {
// Keep the UI working even if saving fails
}
}
return (
<main>
<NoteBoard notes={notes} onChange={setNotes} onDrop={save} />
<ul className="info">
{notes.map((n) => (
<li key={n.id}>付せん{n.id}:({n.x}, {n.y}) z {n.z}</li>
))}
</ul>
</main>
);
}
Nothing is saved during a drag. The list is saved when the pointer is released, and when moving with the arrow keys, it is saved on each key press. When I dragged note 1 with 20 pointermove events, localStorage.setItem was called only once, at release. After reloading the page, the note appeared at the position where it was released.
付せん1を 20回の pointermove でドラッグ: 付せん1:(140, 210) z 7 / 付せん2:(0, 0) z 5 / 付せん3:(220, 120) z 6
localStorage.setItem の回数: ドラッグ中 0回、離したあと 1回
ページを読み込み直したあと: 付せん1:(140, 210) z 7 / 付せん2:(0, 0) z 5 / 付せん3:(220, 120) z 6
Values stored in localStorage remain even after the browser is closed. However, depending on browser settings, saving may fail, so setItem is wrapped in try. In this code, text edited in the body field does not go through onDrop, so it is not saved until the next time a note is moved. If you also want to save text, add a separate process that saves on each input.
How dragging behaves
With the board's inner area at 598×360px (when the browser's viewport width is 640px) and the notes at 160×120px, I dragged the handles with Playwright. Note positions are px from the board's top-left corner.
付せん1:(20, 20) z 1 / 付せん2:(120, 70) z 2 / 付せん3:(220, 120) z 3
付せん1のつまみの左端から30px・上端から10pxをつかみ、右へ200px・下へ130pxドラッグ: 付せん1:(220, 150) z 4 / 付せん2:(120, 70) z 2 / 付せん3:(220, 120) z 3
離したときのポインターと、付せん1のつまみの左上とのずれ: (30, 10)
ボードの (230, 160) でいちばん上の付せん: 1
付せん2を、ボードの右下の外までドラッグ: 付せん1:(220, 150) z 4 / 付せん2:(438, 240) z 5 / 付せん3:(220, 120) z 3
付せん2を、ボードの左上の外までドラッグ: 付せん1:(220, 150) z 4 / 付せん2:(0, 0) z 5 / 付せん3:(220, 120) z 3
Note 1 moved by the amount it was dragged, to (220, 150). When released, the pointer was still 30px from the left edge and 10px from the top edge of note 1's handle. Because the offset recorded at pointer down (offsetX and offsetY) is kept while moving, the grabbed point does not jump to the note's top-left corner.
Note 1's z became 4, so where it overlaps note 3, note 1 is now shown on top. In CSS, z-index places an element with a larger value above one with a smaller value.
When I dragged note 2 past the lower-right edge of the board, it stopped at (438, 240). The board's inner width of 598 minus the note's width of 160 is 438, and the inner height of 360 minus 120 is 240. When I dragged it past the upper-left edge, it stopped at (0, 0).
When I only clicked note 3's handle, its position did not change, and it just came to the top (note 3's z became 6, and it appeared above note 1 where they overlapped). When I dragged on the body text, the note did not move and the text was selected. Text typed afterward was also entered as-is.
付せん3の本文の上で、右へ110pxドラッグ: 付せん1:(220, 150) z 4 / 付せん2:(0, 0) z 5 / 付せん3:(220, 120) z 6 / 選ばれた文字: textarea の 0〜4 文字目
続けて入力: 付せん3の内容「本を返す(金曜まで)」
The handle can also be focused with the keyboard, and the arrow keys move the note 10px at a time. Pressing → twice and ↓ once on note 1's handle moved it from (220, 150) to (240, 160).
Without setPointerCapture, the note stops where the pointer leaves the handle
Pointer events are normally delivered to the element under the pointer. Calling setPointerCapture() makes events for that pointer go to the calling element until the pointer is released (MDN's explanation of Pointer Events).
I compared this against a page where setPointerCapture was replaced with a function that does nothing. Note 1 was dragged 300px right and 150px down in 10 pointermove steps. The pointer grabbed the handle 10px below its top edge, and it moved down 15px per step. Without capture, the pointer would leave the 22px-high handle at the first step.
あり: 付せん1を右へ300px・下へ150pxドラッグして離す: 付せん1:(320, 170) z 4 / 押さずに、ポインターを元の位置へ戻す: 付せん1:(320, 170) z 4
ドラッグ中にイベントが届いた要素: 付せん1のつまみ 付せん1のつまみ 付せん1のつまみ 付せん1のつまみ 付せん1のつまみ 付せん1のつまみ 付せん1のつまみ 付せん1のつまみ 付せん1のつまみ 付せん1のつまみ 離す:付せん1のつまみ
なし: 付せん1を右へ300px・下へ150pxドラッグして離す: 付せん1:(200, 110) z 4 / 押さずに、ポインターを元の位置へ戻す: 付せん1:(140, 80) z 4
ドラッグ中にイベントが届いた要素: 付せん1の本文 付せん1の本文 付せん1の本文 付せん1の本文 付せん2の本文 付せん3のつまみ 付せん1の本文 付せん1の本文 付せん1の本文 付せん1の本文 離す:付せん1の本文
With capture, all 10 pointermove events and the pointerup at release were delivered to note 1's handle. Without it, events went to the body textarea and other elements under the pointer. Note 1 moved only once, when the pointer passed over note 3's handle, and stopped at (200, 110).
The pointerup at release was also delivered to the body textarea, so the end of the drag was never communicated. After that, just moving the pointer back to its original position without pressing a button moved note 1 to (140, 80).
Changing stacking order by reordering the array stops the drag
Even without z-index, elements positioned with position: absolute and similar are drawn with later elements in the HTML on top. So I also made a version that moves the pressed note to the end of the array to bring it to front, and compared the two. I changed bringToFront as follows and removed the zIndex style.
// For comparison: leave z unchanged and move the note to the end of the array (drawn last in the DOM) to bring it to front
function bringToFront(id: number) {
const list = latest.current;
if (list[list.length - 1].id === id) return;
const next = [...list.filter((n) => n.id !== id), list.find((n) => n.id === id)!];
latest.current = next;
onChange(next);
}
I dragged note 1, the bottom note, 300px right and 150px down, the same as before.
z を変える版: 押した直後の hasPointerCapture: true
z を変える版: 付せん1を右へ300px・下へ150pxドラッグ: 付せん1:(320, 170) z 4 / gotpointercapture 1回 / 付せんの DOM の並び: 1,2,3
配列の最後へ移す版: 押した直後の hasPointerCapture: false
配列の最後へ移す版: 付せん1を右へ300px・下へ150pxドラッグ: 付せん1:(200, 110) z 1 / gotpointercapture 0回 / 付せんの DOM の並び: 2,3,1
In the version that moves the note to the end of the array, hasPointerCapture() right after pressing was false, and gotpointercapture did not fire. Note 1 stopped at (200, 110), the same behavior as when setPointerCapture() was not called.
setPointerCapture() was called, but I think the capture stopped working because React then moved note 1's element to the end of the DOM to match the array order. The MDN Pointer Events explanation also says that if an element is moved within the DOM, setPointerCapture() should be called after the move (note in the capture section).
In the version that changes z, the DOM order stayed 1, 2, 3, and note 1 followed the pointer until the end.
Change the stacking order of an element you drag with z-index, not with the order of the array. Because the element does not move in the DOM, the pointer capture is not released.





