Skip to content

Commit 03594c8

Browse files
authored
Add drag and drop and overlay hooks to monopackages (#3737)
1 parent e1a5391 commit 03594c8

File tree

13 files changed

+98
-32
lines changed

13 files changed

+98
-32
lines changed

packages/@adobe/react-spectrum/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@react-spectrum/datepicker": "^3.2.0",
3535
"@react-spectrum/dialog": "^3.5.2",
3636
"@react-spectrum/divider": "^3.4.2",
37+
"@react-spectrum/dnd": "3.0.0-rc.0",
3738
"@react-spectrum/form": "^3.5.0",
3839
"@react-spectrum/icon": "^3.6.1",
3940
"@react-spectrum/illustratedmessage": "^3.3.2",

packages/@adobe/react-spectrum/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export {useAsyncList, useListData, useTreeData} from '@react-stately/data';
5757
export {VisuallyHidden} from '@react-aria/visually-hidden';
5858
export {useCollator, useDateFormatter, useFilter, useLocale, useLocalizedStringFormatter, useMessageFormatter, useNumberFormatter} from '@react-aria/i18n';
5959
export {SSRProvider} from '@react-aria/ssr';
60+
export {useDragAndDrop, DIRECTORY_DRAG_TYPE} from '@react-spectrum/dnd';
6061

6162
export type {SpectrumActionGroupProps} from '@react-spectrum/actiongroup';
6263
export type {SpectrumBadgeProps} from '@react-spectrum/badge';
@@ -71,7 +72,7 @@ export type {DialogContainerValue, SpectrumAlertDialogProps, SpectrumDialogConta
7172
export type {SpectrumDateFieldProps, SpectrumDatePickerProps, SpectrumDateRangePickerProps, SpectrumTimeFieldProps} from '@react-spectrum/datepicker';
7273
export type {SpectrumDividerProps} from '@react-spectrum/divider';
7374
export type {SpectrumFormProps} from '@react-spectrum/form';
74-
export type {IconProps} from '@react-spectrum/icon';
75+
export type {IconProps, IllustrationProps} from '@react-spectrum/icon';
7576
export type {SpectrumIllustratedMessageProps} from '@react-spectrum/illustratedmessage';
7677
export type {SpectrumImageProps} from '@react-spectrum/image';
7778
export type {DimensionValue, FlexProps, GridProps} from '@react-spectrum/layout';
@@ -101,3 +102,4 @@ export type {AsyncListData, AsyncListOptions, ListData, ListOptions, TreeData, T
101102
export type {VisuallyHiddenAria, VisuallyHiddenProps} from '@react-aria/visually-hidden';
102103
export type {DateFormatter, DateFormatterOptions, Filter, FormatMessage, Locale, LocalizedStrings} from '@react-aria/i18n';
103104
export type {SSRProviderProps} from '@react-aria/ssr';
105+
export type {DirectoryDropItem, DragAndDropHooks, DragAndDropOptions, DraggableCollectionEndEvent, DraggableCollectionMoveEvent, DraggableCollectionStartEvent, DragPreviewRenderer, DragTypes, DropItem, DropOperation, DroppableCollectionDropEvent, DroppableCollectionEnterEvent, DroppableCollectionExitEvent, DroppableCollectionInsertDropEvent, DroppableCollectionMoveEvent, DroppableCollectionOnItemDropEvent, DroppableCollectionReorderEvent, DroppableCollectionRootDropEvent, DropPosition, DropTarget, FileDropItem, ItemDropTarget, RootDropTarget, TextDropItem} from '@react-spectrum/dnd';

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ function Pasteable() {
226226

227227
### Text
228228

229-
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.TextItem.id]} /> represents textual data in one or more different formats. These may be either standard [mime types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) or custom app-specific formats. Representing data in multiple formats allows drop targets both within and outside an application to choose data in a format that they understand. For example, a complex object may be serialized in a custom format for use within an application, with fallbacks in plain text and/or rich HTML that can be used when a user drops data from an external application.
229+
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.TextDropItem.id]} /> represents textual data in one or more different formats. These may be either standard [mime types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) or custom app-specific formats. Representing data in multiple formats allows drop targets both within and outside an application to choose data in a format that they understand. For example, a complex object may be serialized in a custom format for use within an application, with fallbacks in plain text and/or rich HTML that can be used when a user drops data from an external application.
230230

231231
The example below works with the above `Copyable` example using a custom app-specific data format to transfer rich data. If no such data is available, it falls back to pasting plain text data.
232232

@@ -311,7 +311,7 @@ function Pasteable() {
311311

312312
### Files
313313

314-
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileItem.id]} /> references a file on the user's device. It includes the name and mime type of the file, and methods to read the contents as plain text, or retrieve a native [File](https://developer.mozilla.org/en-US/docs/Web/API/File) object which can be attached to form data for uploading.
314+
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileDropItem.id]} /> references a file on the user's device. It includes the name and mime type of the file, and methods to read the contents as plain text, or retrieve a native [File](https://developer.mozilla.org/en-US/docs/Web/API/File) object which can be attached to form data for uploading.
315315

316316
This example accepts JPEG and PNG image files, and renders them by creating a local [object URL](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL).
317317

@@ -337,9 +337,9 @@ function Pasteable() {
337337

338338
### Directories
339339

340-
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryItem.id]} /> references the contents of a directory on the user's device. It includes the name of the directory, as well as a method to iterate through the files and folders within the directory. The contents of any folders within the directory can be accessed recursively.
340+
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryDropItem.id]} /> references the contents of a directory on the user's device. It includes the name of the directory, as well as a method to iterate through the files and folders within the directory. The contents of any folders within the directory can be accessed recursively.
341341

342-
The `getEntries` method returns an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) object, which can be used in a `for await...of` loop. This provides each item in the directory as either a <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileItem.id]} /> or <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryItem.id]} />, and you can access the contents of each file as discussed above.
342+
The `getEntries` method returns an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) object, which can be used in a `for await...of` loop. This provides each item in the directory as either a <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileDropItem.id]} /> or <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryDropItem.id]} />, and you can access the contents of each file as discussed above.
343343

344344
This example renders the file names within a dropped directory in a grid.
345345

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function Draggable() {
149149

150150
### Text
151151

152-
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.TextItem.id]} /> represents textual data in one or more different formats. These may be either standard [mime types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) or custom app-specific formats. Representing data in multiple formats allows drop targets both within and outside an application to choose data in a format that they understand. For example, a complex object may be serialized in a custom format for use within an application, with fallbacks in plain text and/or rich HTML that can be used when a user drops data from an external application.
152+
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.TextDropItem.id]} /> represents textual data in one or more different formats. These may be either standard [mime types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) or custom app-specific formats. Representing data in multiple formats allows drop targets both within and outside an application to choose data in a format that they understand. For example, a complex object may be serialized in a custom format for use within an application, with fallbacks in plain text and/or rich HTML that can be used when a user drops data from an external application.
153153

154154
The example below finds the first available item that includes a custom app-specific type. The same draggable component as used in the above example is used here, but rather than displaying the plain text representation, the custom format is used instead.
155155

@@ -186,7 +186,7 @@ function DropTarget() {
186186

187187
### Files
188188

189-
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileItem.id]} /> references a file on the user's device. It includes the name and mime type of the file, and methods to read the contents as plain text, or retrieve a native [File](https://developer.mozilla.org/en-US/docs/Web/API/File) object which can be attached to form data for uploading.
189+
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileDropItem.id]} /> references a file on the user's device. It includes the name and mime type of the file, and methods to read the contents as plain text, or retrieve a native [File](https://developer.mozilla.org/en-US/docs/Web/API/File) object which can be attached to form data for uploading.
190190

191191
This example accepts JPEG and PNG image files, and renders them by creating a local [object URL](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL).
192192

@@ -216,9 +216,9 @@ function DropTarget() {
216216

217217
### Directories
218218

219-
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryItem.id]} /> references the contents of a directory on the user's device. It includes the name of the directory, as well as a method to iterate through the files and folders within the directory. The contents of any folders within the directory can be accessed recursively.
219+
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryDropItem.id]} /> references the contents of a directory on the user's device. It includes the name of the directory, as well as a method to iterate through the files and folders within the directory. The contents of any folders within the directory can be accessed recursively.
220220

221-
The `getEntries` method returns an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) object, which can be used in a `for await...of` loop. This provides each item in the directory as either a <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileItem.id]} /> or <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryItem.id]} />, and you can access the contents of each file as discussed above.
221+
The `getEntries` method returns an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) object, which can be used in a `for await...of` loop. This provides each item in the directory as either a <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileDropItem.id]} /> or <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryDropItem.id]} />, and you can access the contents of each file as discussed above.
222222

223223
This example renders the file names within a dropped directory in a grid.
224224

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ function Example() {
693693

694694
### Text
695695

696-
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.TextItem.id]} /> represents textual data in one or more different formats. These may be either standard [mime types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) or custom app-specific formats. Representing data in multiple formats allows drop targets both within and outside an application to choose data in a format that they understand. For example, a complex object may be serialized in a custom format for use within an application, with fallbacks in plain text and/or rich HTML that can be used when a user drops data from an external application.
696+
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.TextDropItem.id]} /> represents textual data in one or more different formats. These may be either standard [mime types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) or custom app-specific formats. Representing data in multiple formats allows drop targets both within and outside an application to choose data in a format that they understand. For example, a complex object may be serialized in a custom format for use within an application, with fallbacks in plain text and/or rich HTML that can be used when a user drops data from an external application.
697697

698698
The example below uses the `acceptedDragTypes` prop to accept items that include a custom app-specific type, which is retrieved using the item's `getText` method. The same draggable component as used in the above example is used here, but rather than displaying the plain text representation, the custom format is used instead. When `acceptedDragTypes` is specified, the dropped items are filtered to include only items that include the accepted types.
699699

@@ -740,7 +740,7 @@ function Example() {
740740

741741
### Files
742742

743-
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileItem.id]} /> references a file on the user's device. It includes the name and mime type of the file, and methods to read the contents as plain text, or retrieve a native [File](https://developer.mozilla.org/en-US/docs/Web/API/File) object which can be attached to form data for uploading.
743+
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileDropItem.id]} /> references a file on the user's device. It includes the name and mime type of the file, and methods to read the contents as plain text, or retrieve a native [File](https://developer.mozilla.org/en-US/docs/Web/API/File) object which can be attached to form data for uploading.
744744

745745
This example accepts JPEG and PNG image files, and renders them by creating a local [object URL](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL). When the list is empty, you can drop on the whole collection, and otherwise items can be inserted.
746746

@@ -819,9 +819,9 @@ function Example() {
819819

820820
### Directories
821821

822-
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryItem.id]} /> references the contents of a directory on the user's device. It includes the name of the directory, as well as a method to iterate through the files and folders within the directory. The contents of any folders within the directory can be accessed recursively.
822+
A <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryDropItem.id]} /> references the contents of a directory on the user's device. It includes the name of the directory, as well as a method to iterate through the files and folders within the directory. The contents of any folders within the directory can be accessed recursively.
823823

824-
The `getEntries` method returns an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) object, which can be used in a `for await...of` loop. This provides each item in the directory as either a <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileItem.id]} /> or <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryItem.id]} />, and you can access the contents of each file as discussed above.
824+
The `getEntries` method returns an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) object, which can be used in a `for await...of` loop. This provides each item in the directory as either a <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.FileDropItem.id]} /> or <TypeLink links={sharedDocs.links} type={sharedDocs.links[sharedDocs.exports.DirectoryDropItem.id]} />, and you can access the contents of each file as discussed above.
825825

826826
This example accepts directory drops over the whole collection, and renders the contents as items in the list. `DIRECTORY_DRAG_TYPE` is imported from `@react-aria/dnd` and included in the `acceptedDragTypes` prop to limit the accepted items to only directories.
827827

packages/@react-aria/dnd/src/index.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,39 @@ export type {DragPreviewProps} from './DragPreview';
1919
export type {DragOptions, DragResult} from './useDrag';
2020
export type {DropOptions, DropResult} from './useDrop';
2121
export type {ClipboardProps, ClipboardResult} from './useClipboard';
22-
export type {DropTargetDelegate} from '@react-types/shared';
22+
export type {
23+
DirectoryDropItem,
24+
DragEndEvent,
25+
DraggableCollectionEndEvent,
26+
DraggableCollectionMoveEvent,
27+
DraggableCollectionStartEvent,
28+
DragItem,
29+
DragMoveEvent,
30+
DragPreviewRenderer,
31+
DragStartEvent,
32+
DragTypes,
33+
DropEnterEvent,
34+
DropEvent,
35+
DropExitEvent,
36+
DropItem,
37+
DropMoveEvent,
38+
DropOperation,
39+
DroppableCollectionDropEvent,
40+
DroppableCollectionEnterEvent,
41+
DroppableCollectionExitEvent,
42+
DroppableCollectionInsertDropEvent,
43+
DroppableCollectionMoveEvent,
44+
DroppableCollectionOnItemDropEvent,
45+
DroppableCollectionReorderEvent,
46+
DroppableCollectionRootDropEvent,
47+
DropPosition,
48+
DropTarget,
49+
DropTargetDelegate,
50+
FileDropItem,
51+
ItemDropTarget,
52+
RootDropTarget,
53+
TextDropItem
54+
} from '@react-types/shared';
2355

2456
export {DIRECTORY_DRAG_TYPE} from './utils';
2557
export {useDrag} from './useDrag';

packages/@react-aria/dnd/src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
import {CUSTOM_DRAG_TYPE, DROP_OPERATION, GENERIC_TYPE, NATIVE_DRAG_TYPES} from './constants';
14-
import {DirectoryItem, DragItem, DropItem, FileItem, DragTypes as IDragTypes} from '@react-types/shared';
14+
import {DirectoryDropItem, DragItem, DropItem, FileDropItem, DragTypes as IDragTypes} from '@react-types/shared';
1515
import {DroppableCollectionState} from '@react-stately/dnd';
1616
import {getInteractionModality, useInteractionModality} from '@react-aria/interactions';
1717
import {Key, RefObject} from 'react';
@@ -270,7 +270,7 @@ function blobToString(blob: Blob): Promise<string> {
270270
});
271271
}
272272

273-
function createFileItem(file: File): FileItem {
273+
function createFileItem(file: File): FileDropItem {
274274
return {
275275
kind: 'file',
276276
type: file.type || GENERIC_TYPE,
@@ -280,15 +280,15 @@ function createFileItem(file: File): FileItem {
280280
};
281281
}
282282

283-
function createDirectoryItem(entry: any): DirectoryItem {
283+
function createDirectoryItem(entry: any): DirectoryDropItem {
284284
return {
285285
kind: 'directory',
286286
name: entry.name,
287287
getEntries: () => getEntries(entry)
288288
};
289289
}
290290

291-
async function *getEntries(item: FileSystemDirectoryEntry): AsyncIterable<FileItem | DirectoryItem> {
291+
async function *getEntries(item: FileSystemDirectoryEntry): AsyncIterable<FileDropItem | DirectoryDropItem> {
292292
let reader = item.createReader();
293293

294294
// We must call readEntries repeatedly because there may be a limit to the

packages/@react-spectrum/dnd/src/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,28 @@
1515
export type {DragAndDropOptions, DragAndDropHooks} from './useDragAndDrop';
1616
export {useDragAndDrop} from './useDragAndDrop';
1717
export {DIRECTORY_DRAG_TYPE} from '@react-aria/dnd';
18+
19+
export type {
20+
DirectoryDropItem,
21+
DraggableCollectionEndEvent,
22+
DraggableCollectionMoveEvent,
23+
DraggableCollectionStartEvent,
24+
DragPreviewRenderer,
25+
DragTypes,
26+
DropItem,
27+
DropOperation,
28+
DroppableCollectionDropEvent,
29+
DroppableCollectionEnterEvent,
30+
DroppableCollectionExitEvent,
31+
DroppableCollectionInsertDropEvent,
32+
DroppableCollectionMoveEvent,
33+
DroppableCollectionOnItemDropEvent,
34+
DroppableCollectionReorderEvent,
35+
DroppableCollectionRootDropEvent,
36+
DropPosition,
37+
DropTarget,
38+
FileDropItem,
39+
ItemDropTarget,
40+
RootDropTarget,
41+
TextDropItem
42+
} from '@react-types/shared';

0 commit comments

Comments
 (0)