Skip to content

Commit 3a68252

Browse files
add docs for isDisabled in useDrag, useDrop, and useClipboard (#6306)
Co-authored-by: Robert Snow <rsnow@adobe.com>
1 parent 313cc05 commit 3a68252

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

packages/@react-aria/dnd/docs/useClipboard.mdx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,60 @@ function Pasteable() {
432432

433433
</details>
434434

435+
## Disabling copy and paste
436+
437+
If you need to temporarily disable copying and pasting, you can pass the `isDisabled` option to `useClipboard`. This will prevent copying and pasting on the element until it is re-enabled.
438+
439+
```tsx example
440+
import type {TextDropItem} from '@react-aria/dnd';
441+
import {useClipboard} from '@react-aria/dnd';
442+
443+
function Copyable() {
444+
let {clipboardProps} = useClipboard({
445+
getItems() {
446+
return [{
447+
'text/plain': 'Hello world'
448+
}];
449+
},
450+
/*- begin highlight -*/
451+
isDisabled: true
452+
/*- end highlight -*/
453+
});
454+
455+
return (
456+
<div role="textbox" tabIndex={0} {...clipboardProps}>
457+
Hello world
458+
<kbd>⌘C</kbd>
459+
</div>
460+
);
461+
}
462+
463+
function Pasteable() {
464+
let [pasted, setPasted] = React.useState(null);
465+
let {clipboardProps} = useClipboard({
466+
async onPaste(items) {
467+
let pasted = await Promise.all(
468+
items
469+
.filter((item) =>
470+
item.kind === 'text' && item.types.has('text/plain')
471+
)
472+
.map((item: TextDropItem) => item.getText('text/plain'))
473+
);
474+
setPasted(pasted.join('\n'));
475+
},
476+
/*- begin highlight -*/
477+
isDisabled: true
478+
/*- end highlight -*/
479+
});
480+
481+
return (
482+
<div role="textbox" tabIndex={0} {...clipboardProps}>
483+
{pasted || 'Paste here'}
484+
<kbd>⌘V</kbd>
485+
</div>
486+
);
487+
}
488+
489+
<Copyable />
490+
<Pasteable />
491+
```

packages/@react-aria/dnd/docs/useDrag.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,32 @@ function Draggable() {
375375
<Draggable />
376376
<DropTarget />
377377
```
378+
379+
## Disabling dragging
380+
381+
If you need to temporarily disable dragging, you can pass the `isDisabled` option to `useDrag`. This will prevent dragging an element until it is re-enabled.
382+
383+
```tsx example
384+
import {useDrag} from '@react-aria/dnd';
385+
386+
function Draggable() {
387+
let {dragProps, isDragging} = useDrag({
388+
getItems() {
389+
return [{
390+
'text/plain': 'hello world'
391+
}];
392+
},
393+
/*- begin highlight -*/
394+
isDisabled: true
395+
/*- end highlight -*/
396+
});
397+
398+
return (
399+
<div {...dragProps} role="button" tabIndex={0} className={`draggable ${isDragging ? 'dragging' : ''}`}>
400+
Drag me
401+
</div>
402+
);
403+
}
404+
405+
<Draggable />
406+
```

packages/@react-aria/dnd/docs/useDrop.mdx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,40 @@ function DropTarget() {
437437
<Draggable />
438438
<DropTarget />
439439
```
440+
441+
## Disabling dropping
442+
443+
If you need to temporarily disable dropping, you can pass the `isDisabled` option to `useDrop`. This will prevent the drop target from accepting any drops until it is re-enabled.
444+
445+
```tsx example
446+
import type {TextDropItem} from '@react-aria/dnd';
447+
import {useDrop} from '@react-aria/dnd';
448+
449+
function DropTarget() {
450+
let [dropped, setDropped] = React.useState(null);
451+
let ref = React.useRef(null);
452+
let {dropProps, isDropTarget} = useDrop({
453+
ref,
454+
async onDrop(e) {
455+
let items = await Promise.all(
456+
e.items
457+
.filter(item => item.kind === 'text' && item.types.has('text/plain'))
458+
.map((item: TextDropItem) => item.getText('text/plain'))
459+
);
460+
setDropped(items.join('\n'));
461+
},
462+
/*- begin highlight -*/
463+
isDisabled: true
464+
/*- end highlight -*/
465+
});
466+
467+
return (
468+
<div {...dropProps} role="button" tabIndex={0} ref={ref} className={`droppable ${isDropTarget ? 'target' : ''}`}>
469+
{dropped || 'Drop here'}
470+
</div>
471+
);
472+
}
473+
474+
<Draggable />
475+
<DropTarget />
476+
```

0 commit comments

Comments
 (0)