DropZone
A drop zone is an area into which one or multiple objects can be dragged and dropped.
Drop or paste text or images here
Example
DropZone.tsx
DropZone.css
Example
DropZone.tsx
DropZone.css
Example
DropZone.tsx
DropZone.css
import {DropZone} from './DropZone';
import {Text} from 'react-aria-components';
import {useState} from 'react';
function Example() {
let [content, setContent] = useState(null);
return (
<DropZone
// Determine whether dragged content should be accepted.
getDropOperation={types => (
['text/plain', 'image/jpeg', 'image/png', 'image.gif'].some(t => types.has(t))
? 'copy'
: 'cancel'
)}
onDrop={async (event) => {
// Find the first accepted item.
let item = event.items.find(item => (
(item.kind === 'text' && item.types.has('text/plain')) ||
(item.kind === 'file' && item.type.startsWith('image/'))
));
if (item?.kind === 'text') {
let text = await item.getText('text/plain');
setContent(text);
} else if (item?.kind === 'file') {
let file = await item.getFile();
let url = URL.createObjectURL(file);
setContent(<img src={url} alt={item.name} style={{maxHeight: 100, maxWidth: '100%'}} />)
}
}}>
<Text slot="label">
{content || "Drop or paste text or images here"}
</Text>
</DropZone>
);
}
API
<DropZone>
<Text slot="label" />
</DropZone>
DropZone
Name | Type | |
---|---|---|
getDropOperation |
| |
A function returning the drop operation to be performed when items matching the given types are dropped on the drop target. | ||
isDisabled | boolean | |
Whether the drop target is disabled. If true, the drop target will not accept any drops. | ||
children | ChildrenOrFunction | |
The children of the component. A function may be provided to alter the children based on component state. | ||
Default className: react-aria-DropZone
Render Prop | CSS Selector |
---|---|
isHovered | CSS Selector: [data-hovered]
|
Whether the dropzone is currently hovered with a mouse. | |
isFocused | CSS Selector: [data-focused]
|
Whether the dropzone is focused, either via a mouse or keyboard. | |
isFocusVisible | CSS Selector: [data-focus-visible]
|
Whether the dropzone is keyboard focused. | |
isDropTarget | CSS Selector: [data-drop-target]
|
Whether the dropzone is the drop target. | |
isDisabled | CSS Selector: [data-disabled]
|
Whether the dropzone is disabled. |