onDrop

Defines the destination of a drag and drop operation that handles the dropped content with a closure that you specify.

View ModifieriOS 14.0+macOS 11.0+

Overloads

2 ways to call compose().onDrop(). Each is one SwiftUI overload, picked by the arguments you pass.

React
compose().onDrop({
  of: string[],
  isTargeted: Binding<boolean> | null,
  perform: { returns: boolean; perform?: (providers: string[], location: CGPoint) => void; },
})
Swift
nonisolated func onDrop(
    of supportedContentTypes: [UTType],
    isTargeted: Binding<Bool>?,
    perform action: @escaping ([NSItemProvider], CGPoint) -> Bool
) -> some View

Parameters

  • options.of*string[]
  • options.isTargeted*Binding<boolean> | null

    A binding that updates when a drag and drop operation enters or exits the drop target area. The binding's value is true when the cursor is inside the area, and false when the cursor is outside.

  • options.perform*{ returns: boolean; perform?: (providers: string[], location: CGPoint) => void; }

Return value

A view that provides a drop destination for a drag operation of the specified types.

Discussion

The drop destination is the same size and position as this view.

Make sure to start loading the contents of NSItemProvider instances within the scope of the action closure. Do not perform loading asynchronously on a different actor. Loading the contents may finish later, but it must start here. For security reasons, the drop receiver can access the dropped payload only before this closure returns.

To use Transferable types directly and access a richer feature set — including support for DropSession — use View.dropDestination(for:isEnabled:action:) instead.

See also

More in Events and lifecycle.