Debian Codegraph @

node/react-dropzone/examples/drag-overlay/README.md

raw
1 # isDragGlobal Example
2
3 The `isDragGlobal` state is `true` when files are being dragged anywhere on the document, before they reach the dropzone. This allows you to show visual feedback (like a full-page overlay) to indicate where files can be dropped.
4
5 ## Simple Example
6
7 ```jsx harmony
8 import React from 'react';
9 import {useDropzone} from 'react-dropzone';
10
11 function DragOverlay() {
12 const {
13 getRootProps,
14 getInputProps,
15 isDragGlobal,
16 isDragActive,
17 isDragAccept,
18 isDragReject,
19 acceptedFiles
20 } = useDropzone({
21 accept: {
22 'image/*': ['.png', '.jpg', '.jpeg', '.gif']
23 }
24 });
25
26 const files = acceptedFiles.map(file => (
27 <li key={file.path}>
28 {file.path} - {file.size} bytes
29 </li>
30 ));
31
32 return (
33 <div style={{ minHeight: '100vh', padding: '20px' }}>
34 {/* Global overlay shown when dragging anywhere on the page */}
35 {isDragGlobal && !isDragActive && (
36 <div style={{
37 position: 'fixed',
38 top: 0,
39 left: 0,
40 right: 0,
41 bottom: 0,
42 backgroundColor: 'rgba(0, 123, 255, 0.1)',
43 border: '3px dashed #007bff',
44 pointerEvents: 'none',
45 display: 'flex',
46 alignItems: 'center',
47 justifyContent: 'center',
48 zIndex: 1000,
49 }}>
50 <h2 style={{ color: '#007bff' }}>Drop files anywhere on this page...</h2>
51 </div>
52 )}
53
54 <section className="container">
55 <div {...getRootProps({
56 className: 'dropzone',
57 style: {
58 border: '2px dashed #ccc',
59 borderRadius: '8px',
60 padding: '40px',
61 textAlign: 'center',
62 backgroundColor: isDragAccept ? '#d4edda' : isDragReject ? '#f8d7da' : 'white',
63 transition: 'all 0.2s',
64 }
65 })}>
66 <input {...getInputProps()} />
67
68 {/* Status indicators */}
69 {isDragGlobal && !isDragActive && (
70 <p style={{ color: '#007bff', fontWeight: 'bold' }}>
71 🌐 Drag detected on page!
72 </p>
73 )}
74
75 {isDragActive && !isDragAccept && !isDragReject && (
76 <p style={{ color: '#6c757d' }}>Drop files here...</p>
77 )}
78
79 {isDragAccept && (
80 <p style={{ color: '#28a745', fontWeight: 'bold' }}>
81 ✅ Drop to upload these files
82 </p>
83 )}
84
85 {isDragReject && (
86 <p style={{ color: '#dc3545', fontWeight: 'bold' }}>
87 ❌ Some files will be rejected
88 </p>
89 )}
90
91 {!isDragGlobal && !isDragActive && (
92 <p>Drag 'n' drop images here, or click to select files</p>
93 )}
94 </div>
95
96 <aside>
97 <h4>Accepted files</h4>
98 <ul>{files}</ul>
99 </aside>
100 </section>
101 </div>
102 );
103 }
104
105 <DragOverlay />
106 ```
107
108 ## State Transitions
109
110 The `isDragGlobal` state provides early feedback about drag operations:
111
112 1. **`isDragGlobal: false`** - No drag operation detected
113 2. **`isDragGlobal: true`** - Files are being dragged anywhere on the document
114 - This is set when `dragenter` fires on the document with files
115 3. **`isDragActive: true`** - Files are being dragged over the dropzone
116 - Takes precedence when you want to show different feedback
117 4. **`isDragAccept: true`** / **`isDragReject: true`** - Files are validated
118 - Indicates whether the dragged files meet the dropzone criteria
119
120 ## Use Cases
121
122 - **Full-page overlays**: Show a visual indicator across the entire page when drag starts
123 - **Multi-dropzone highlighting**: Highlight all available dropzones when files are detected
124 - **Early user feedback**: Provide immediate visual feedback before users reach the target dropzone
125 - **Improved UX**: Make it clear that the application accepts drag and drop
126
127 ## Events
128
129 `isDragGlobal` is reset to `false` when:
130 - Drag leaves the document (`dragleave` on all elements)
131 - Files are dropped anywhere (`drop` event)
132 - Drag operation is cancelled (`dragend` event, e.g., user presses ESC)