React Draggable - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

This tutorial guides you through the following steps:

  • Add a Draggable to a page.
  • Configure Draggable options.
  • Handle events.

Each section in this tutorial covers a single configuration step. You can find the complete source code in the following GitHub repository:

View on GitHub

Create Draggable

Add DevExtreme to your React application and use the following code to create a Draggable:

App.tsx
  • import React, { JSX, useState } from 'react';
  • import { Draggable, type DraggableTypes } from 'devextreme-react/draggable';
  • import 'devextreme/dist/css/dx.fluent.blue.light.css';
  •  
  • function App(): JSX.Element {
  • return (
  • <Draggable />
  • );
  • }
  •  
  • export default App;

Draggable does not include default visual elements. Specify custom markup as follows:

App.tsx
  • import { Draggable } from 'devextreme-react/draggable';
  •  
  • function App(): JSX.Element {
  • return (
  • <Draggable id="note-1" className="note">
  • <div className="color-indicator blue"></div>
  • <div className="text-container">
  • <div className="body-text-box">Install New Router in Dev Room</div>
  • <div className="detail-text-box">Amelia Harper</div>
  • </div>
  • </Draggable>
  • );
  • }

This example defines Draggable markup within a custom component, Note.

This component configures two interfaces:

  • export interface NoteInfo: Specifies note information.
  • interface NoteProps: Defines properties of the Note component.

Create an array of NoteInfo objects to define four notes. Implement the Note component within an Array.map() callback to add these notes to the app:

Note.tsx
Note.types.tsx
App.tsx
  • import { Draggable, type DraggableTypes } from 'devextreme-react/draggable';
  • import { type NoteProps } from './Note.types';
  • // ...
  •  
  • function Note({
  • id,
  • group = 'notes',
  • boundary = '.board',
  • task,
  • assignee,
  • // ...
  • }: NoteProps) {
  • return (
  • <Draggable
  • id={id}
  • group={group}
  • boundary={boundary}
  • >
  • <div
  • className={`note ${isOverlapped ? 'overlapped' : ''}`}
  • >
  • <div className="color-indicator"></div>
  • <div className="text-container">
  • <div className="body-text-box">{task}</div>
  • <div className="detail-text-box">{assignee}</div>
  • </div>
  • </div>
  • </Draggable>
  • );
  • }
  •  
  • export default Note;
  • export interface NoteInfo {
  • id: string;
  • task: string;
  • assignee: string;
  • }
  •  
  • export interface NoteProps extends NoteInfo {
  • group?: string;
  • boundary?: string;
  • // ...
  • }
  • import Note from './components/Note/Note.tsx';
  • import { type NoteInfo } from './components/Note/Note.types.tsx';
  • // ...
  •  
  • const notes: NoteInfo[] = [{
  • id: 'note-1',
  • task: 'Install New Router in Dev Room',
  • assignee: 'Amelia Harper',
  • }, {
  • id: 'note-2',
  • task: '👨‍💻 Launch New Website',
  • assignee: 'Brett Wade',
  • }, {
  • id: 'note-3',
  • task: 'Prepare 2026 Marketing Plan',
  • assignee: 'Robert Reagan',
  • }, {
  • id: 'note-4',
  • task: '🖥️ Approve Personal Computer Upgrade Plan',
  • assignee: 'Bart Arnaz',
  • }];
  •  
  • function App(): JSX.Element {
  • return (
  • <div className="boundary-text">Dragging Boundary</div>
  • <div className="board">
  • {notes.map((note) => (
  • <Note
  • {...note}
  • key={note.id}
  • />
  • ))}
  • </div>
  • <div className="boundary-text">Dragging Boundary</div>
  • );
  • }

Configure Draggable Options

This example specifies the following Draggable properties:

  • boundary: Constrains Draggable movement inside a container (.board).
  • group: Specifies an interaction group (used in events such as onDragMove).

Handle Events

This example specifies the following event handlers to update the note's Z-index (bring to front while dragging):

This example also implements an onClick handler on the parent <div> element within the Note component:

Note.tsx
App.tsx
index.css
  • import React, {
  • useCallback, useMemo, useRef, useState,
  • } from 'react';
  • // ...
  •  
  • function Note({ ... }: NoteProps) {
  • const overlappedComponentId = useRef<string | null>(null);
  • const [currentZIndex, setCurrentZIndex] = useState(0);
  •  
  • const style = useMemo(() => ({ zIndex: currentZIndex }), [currentZIndex]);
  •  
  • const updateZIndex = useCallback(() => {
  • const zIndexToUpdate = zIndex.current + 1;
  • setCurrentZIndex(zIndexToUpdate);
  • zIndex.current = zIndexToUpdate;
  • }, []);
  •  
  • const handleDragStart = useCallback(() => {
  • updateZIndex();
  • }, []);
  •  
  • const handleDragMove = useCallback((e: DraggableTypes.DragMoveEvent) => {
  • if (e.toComponent !== e.component) {
  • const toComponentId = e.toComponent.element().id;
  • startOverlap(toComponentId);
  • overlappedComponentId.current = toComponentId;
  • } else {
  • stopOverlap();
  • overlappedComponentId.current = null;
  • }
  • }, []);
  •  
  • const onClick = useCallback(() => {
  • updateZIndex();
  • }, [updateZIndex]);
  •  
  • const handleDragEnd = useCallback(() => {
  • stopOverlap();
  • }, []);
  •  
  • return (
  • <Draggable ...
  • style={style}
  • onDragStart={handleDragStart}
  • onDragMove={handleDragMove}
  • onDragEnd={handleDragEnd}
  • >
  • <div
  • className={`note ${isOverlapped ? 'overlapped' : ''}`}
  • onClick={onClick}
  • >
  • {/* ... */}
  • </div>
  • </Draggable>
  • );
  • }
  • import { useCallback, useRef, useState } from 'react';
  • // ...
  •  
  • function App(): JSX.Element {
  • const zIndex = useRef(0);
  • const [overlappedId, setOverlappedId] = useState<string | null>(null);
  •  
  • const startOverlap = useCallback((id: string) => {
  • setOverlappedId(id);
  • }, []);
  •  
  • const stopOverlap = useCallback(() => {
  • setOverlappedId(null);
  • }, []);
  •  
  • return (
  • <div className="demo-container dx-theme-fluent-typography">
  • <div className="boundary-text">Dragging Boundary</div>
  • <div className="board">
  • {notes.map((note) => (
  • <Note
  • {...note}
  • key={note.id}
  • isOverlapped={overlappedId === note.id}
  • zIndex={zIndex}
  • startOverlap={startOverlap}
  • stopOverlap={stopOverlap}
  • />
  • ))}
  • </div>
  • <div className="boundary-text">Dragging Boundary</div>
  • </div>
  • );
  • }
  • .note.overlapped {
  • outline: 1px dashed var(--dx-color-primary);
  • }

Refer to the following repository for the complete source code of this tutorial:

View on GitHub