UNPKG

9.39 kBTypeScriptView Raw
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13import {Key} from '@react-types/shared';
14import {RefObject} from 'react';
15
16export interface DragDropEvent {
17 /** The x coordinate of the event, relative to the target element. */
18 x: number,
19 /** The y coordinate of the event, relative to the target element. */
20 y: number
21}
22
23export type DropOperation = 'copy' | 'link' | 'move' | 'cancel';
24
25export interface DragItem {
26 [type: string]: string
27}
28
29export interface DragStartEvent extends DragDropEvent {
30 /** The event type. */
31 type: 'dragstart'
32}
33
34export interface DragMoveEvent extends DragDropEvent {
35 /** The event type. */
36 type: 'dragmove'
37}
38
39export interface DragEndEvent extends DragDropEvent {
40 /** The event type. */
41 type: 'dragend',
42 /** The drop operation that occurred. */
43 dropOperation: DropOperation
44}
45
46export interface DropEnterEvent extends DragDropEvent {
47 /** The event type. */
48 type: 'dropenter'
49}
50
51export interface DropMoveEvent extends DragDropEvent {
52 /** The event type. */
53 type: 'dropmove'
54}
55
56export interface DropActivateEvent extends DragDropEvent {
57 /** The event type. */
58 type: 'dropactivate'
59}
60
61export interface DropExitEvent extends DragDropEvent {
62 /** The event type. */
63 type: 'dropexit'
64}
65
66export interface TextDropItem {
67 /** The item kind. */
68 kind: 'text',
69 /**
70 * The drag types available for this item.
71 * These are often mime types, but may be custom app-specific types.
72 */
73 types: Set<string>,
74 /** Returns the data for the given type as a string. */
75 getText(type: string): Promise<string>
76}
77
78export interface FileDropItem {
79 /** The item kind. */
80 kind: 'file',
81 /** The file type (usually a mime type). */
82 type: string,
83 /** The file name. */
84 name: string,
85 /** Returns the contents of the file as a blob. */
86 getFile(): Promise<File>,
87 /** Returns the contents of the file as a string. */
88 getText(): Promise<string>
89}
90
91export interface DirectoryDropItem {
92 /** The item kind. */
93 kind: 'directory',
94 /** The directory name. */
95 name: string,
96 /** Returns the entries contained within the directory. */
97 getEntries(): AsyncIterable<FileDropItem | DirectoryDropItem>
98}
99
100export type DropItem = TextDropItem | FileDropItem | DirectoryDropItem;
101
102export interface DropEvent extends DragDropEvent {
103 /** The event type. */
104 type: 'drop',
105 /** The drop operation that should occur. */
106 dropOperation: DropOperation,
107 /** The dropped items. */
108 items: DropItem[]
109}
110
111export type DropPosition = 'on' | 'before' | 'after';
112export interface RootDropTarget {
113 /** The event type. */
114 type: 'root'
115}
116
117export interface ItemDropTarget {
118 /** The drop target type. */
119 type: 'item',
120 /** The item key. */
121 key: Key,
122 /** The drop position relative to the item. */
123 dropPosition: DropPosition
124}
125
126export type DropTarget = RootDropTarget | ItemDropTarget;
127
128export interface DroppableCollectionEnterEvent extends DropEnterEvent {
129 /** The drop target. */
130 target: DropTarget
131}
132
133export interface DroppableCollectionMoveEvent extends DropMoveEvent {
134 /** The drop target. */
135 target: DropTarget
136}
137
138export interface DroppableCollectionActivateEvent extends DropActivateEvent {
139 /** The drop target. */
140 target: DropTarget
141}
142
143export interface DroppableCollectionExitEvent extends DropExitEvent {
144 /** The drop target. */
145 target: DropTarget
146}
147
148export interface DroppableCollectionDropEvent extends DropEvent {
149 /** The drop target. */
150 target: DropTarget
151}
152
153export interface DroppableCollectionInsertDropEvent {
154 /** The dropped items. */
155 items: DropItem[],
156 /** The drop operation that should occur. */
157 dropOperation: DropOperation,
158 /** The drop target. */
159 target: ItemDropTarget
160}
161
162export interface DroppableCollectionRootDropEvent {
163 /** The dropped items. */
164 items: DropItem[],
165 /** The drop operation that should occur. */
166 dropOperation: DropOperation
167}
168
169export interface DroppableCollectionOnItemDropEvent {
170 /** The dropped items. */
171 items: DropItem[],
172 /** The drop operation that should occur. */
173 dropOperation: DropOperation,
174 /** Whether the drag originated within the same collection as the drop. */
175 isInternal: boolean,
176 /** The drop target. */
177 target: ItemDropTarget
178}
179
180export interface DroppableCollectionReorderEvent {
181 /** The keys of the items that were reordered. */
182 keys: Set<Key>,
183 /** The drop operation that should occur. */
184 dropOperation: DropOperation,
185 /** The drop target. */
186 target: ItemDropTarget
187}
188
189export interface DragTypes {
190 /** Returns whether the drag contains data of the given type. */
191 has(type: string | symbol): boolean
192}
193
194export interface DropTargetDelegate {
195 /**
196 * Returns a drop target within a collection for the given x and y coordinates.
197 * The point is provided relative to the top left corner of the collection container.
198 * A drop target can be checked to see if it is valid using the provided `isValidDropTarget` function.
199 */
200 getDropTargetFromPoint(x: number, y: number, isValidDropTarget: (target: DropTarget) => boolean): DropTarget | null
201}
202
203export interface DroppableCollectionUtilityOptions {
204 /**
205 * The drag types that the droppable collection accepts. If the collection accepts directories, include `DIRECTORY_DRAG_TYPE` in your array of allowed types.
206 * @default 'all'
207 */
208 acceptedDragTypes?: 'all' | Array<string | symbol>,
209 /**
210 * Handler that is called when external items are dropped "between" items.
211 */
212 onInsert?: (e: DroppableCollectionInsertDropEvent) => void,
213 /**
214 * Handler that is called when external items are dropped on the droppable collection's root.
215 */
216 onRootDrop?: (e: DroppableCollectionRootDropEvent) => void,
217 /**
218 * Handler that is called when items are dropped "on" an item.
219 */
220 onItemDrop?: (e: DroppableCollectionOnItemDropEvent) => void,
221 /**
222 * Handler that is called when items are reordered via drag in the source collection.
223 */
224 onReorder?: (e: DroppableCollectionReorderEvent) => void,
225 /**
226 * A function returning whether a given target in the droppable collection is a valid "on" drop target for the current drag types.
227 */
228 shouldAcceptItemDrop?: (target: ItemDropTarget, types: DragTypes) => boolean
229}
230
231export interface DroppableCollectionBaseProps {
232 /** Handler that is called when a valid drag enters a drop target. */
233 onDropEnter?: (e: DroppableCollectionEnterEvent) => void,
234 /**
235 * Handler that is called after a valid drag is held over a drop target for a period of time.
236 * @private
237 */
238 onDropActivate?: (e: DroppableCollectionActivateEvent) => void,
239 /** Handler that is called when a valid drag exits a drop target. */
240 onDropExit?: (e: DroppableCollectionExitEvent) => void,
241 /**
242 * Handler that is called when a valid drag is dropped on a drop target. When defined, this overrides other
243 * drop handlers such as `onInsert`, and `onItemDrop`.
244 */
245 onDrop?: (e: DroppableCollectionDropEvent) => void,
246 /**
247 * A function returning the drop operation to be performed when items matching the given types are dropped
248 * on the drop target.
249 */
250 getDropOperation?: (target: DropTarget, types: DragTypes, allowedOperations: DropOperation[]) => DropOperation
251}
252
253export interface DroppableCollectionProps extends DroppableCollectionUtilityOptions, DroppableCollectionBaseProps {}
254
255export interface DraggableCollectionStartEvent extends DragStartEvent {
256 /** The keys of the items that were dragged. */
257 keys: Set<Key>
258}
259
260export interface DraggableCollectionMoveEvent extends DragMoveEvent {
261 /** The keys of the items that were dragged. */
262 keys: Set<Key>
263}
264
265export interface DraggableCollectionEndEvent extends DragEndEvent {
266 /** The keys of the items that were dragged. */
267 keys: Set<Key>,
268 /** Whether the drop ended within the same collection as it originated. */
269 isInternal: boolean
270}
271
272export type DragPreviewRenderer = (items: DragItem[], callback: (node: HTMLElement) => void) => void;
273
274export interface DraggableCollectionProps {
275 /** Handler that is called when a drag operation is started. */
276 onDragStart?: (e: DraggableCollectionStartEvent) => void,
277 /** Handler that is called when the drag is moved. */
278 onDragMove?: (e: DraggableCollectionMoveEvent) => void,
279 /** Handler that is called when the drag operation is ended, either as a result of a drop or a cancellation. */
280 onDragEnd?: (e: DraggableCollectionEndEvent) => void,
281 /** A function that returns the items being dragged. */
282 getItems: (keys: Set<Key>) => DragItem[],
283 /** The ref of the element that will be rendered as the drag preview while dragging. */
284 preview?: RefObject<DragPreviewRenderer>,
285 /** Function that returns the drop operations that are allowed for the dragged items. If not provided, all drop operations are allowed. */
286 getAllowedDropOperations?: () => DropOperation[]
287}