Skip to content

Commit fb1525e

Browse files
authored
Update RSP DropZone visual feedback example in docs (#5207)
* Update RSP DropZone visual feedback example * add more info to visual feedback example
1 parent f6ff97d commit fb1525e

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

packages/@react-spectrum/dropzone/docs/DropZone.mdx

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -357,52 +357,67 @@ function Example() {
357357

358358
A DropZone displays visual feedback to the user when a drag hovers over the drop target by passing the `getDropOperation` function. If a drop target only supports data of specific types (e.g. images, videos, text, etc.), then it should implement the `getDropOperation` prop and return `'cancel'` for types that aren't supported. This will prevent visual feedback indicating that the drop target accepts the dragged data when this is not true. [Read more about getDropOperation.](../react-aria/useDrop.html#getdropoperation)
359359

360+
In the below example, the drop zone only supports dropping JPEG images. If a JPEG is dragged over the drop zone, it will be highlighted and the operating system will display a copy cursor. If another type is dragged over the drop zone, then there is no visual feedback, indicating that a drop is not accepted.
361+
360362
```tsx example
361363
import {FileTrigger} from 'react-aria-components';
362364
import {Text} from 'react-aria-components';
363-
import {FileDropItem} from 'react-aria';
364365

365366
function Example() {
366367
let [filledSrc, setFilledSrc] = React.useState(null);
367368

368369
return (
369-
<DropZone
370-
maxWidth="size-3000"
371-
isFilled={!!filledSrc}
372-
getDropOperation={(types) => types.has('image/png') ? 'copy' : 'cancel'}
373-
onDrop={async (e) => {
374-
let item = e.items.find((item: FileDropItem) => item.kind === 'file' && item.type === 'image/png') as FileDropItem;
375-
if (item) {
376-
setFilledSrc({
377-
type: item.type,
378-
name: item.name
379-
});
380-
}
381-
}}>
382-
<IllustratedMessage>
383-
<Upload />
384-
<Heading>
385-
<Text slot="label">
386-
{filledSrc ? `${filledSrc.type} ${filledSrc.name}` : 'Drag and drop here'}
387-
</Text>
388-
</Heading>
389-
<Content>
390-
<FileTrigger
391-
acceptedFileTypes={['image/png']}
392-
onSelect={(e) => {
393-
let file = (Array.from(e)).find((file) => file.type === "image/png")
394-
if (file) {
370+
<>
371+
<Draggable />
372+
<DraggableImage />
373+
<DropZone
374+
maxWidth="size-3000"
375+
isFilled={!!filledSrc}
376+
getDropOperation={(types) => types.has('image/jpeg') ? 'copy' : 'cancel'}
377+
onDrop={async (e) => {
378+
e.items.find(async (item) => {
379+
if (item.kind === 'file') {
380+
if (item.type === 'image/jpeg') {
381+
let file = await item.getFile();
395382
setFilledSrc({
396383
type: file.type,
397384
name: file.name
398385
})
399386
}
400-
}}>
401-
<Button variant="primary">Browse</Button>
402-
</FileTrigger>
403-
</Content>
404-
</IllustratedMessage>
405-
</DropZone>
387+
} else if (item.kind === 'text') {
388+
let file = await item.getText('image/jpeg');
389+
setFilledSrc({
390+
type: 'image/jpeg',
391+
name: file
392+
})
393+
}
394+
});
395+
}}>
396+
<IllustratedMessage>
397+
<Upload />
398+
<Heading>
399+
<Text slot="label">
400+
{filledSrc ? `${filledSrc.type} ${filledSrc.name}` : 'Drag and drop here'}
401+
</Text>
402+
</Heading>
403+
<Content>
404+
<FileTrigger
405+
acceptedFileTypes={['image/jpeg']}
406+
onSelect={(e) => {
407+
let file = (Array.from(e)).find((file) => file.type === "image/jpeg")
408+
if (file) {
409+
setFilledSrc({
410+
type: file.type,
411+
name: file.name
412+
})
413+
}
414+
}}>
415+
<Button variant="primary">Browse</Button>
416+
</FileTrigger>
417+
</Content>
418+
</IllustratedMessage>
419+
</DropZone>
420+
</>
406421
);
407422
}
408423
```

0 commit comments

Comments
 (0)