dragContainerSelection

Provides multiple item selection support for drag containers.

View ModifieriOS 27.0+macOS 26.0+visionOS 27.0+dragContainerSelection(_:) in SwiftUI

Declaration

React
compose().dragContainerSelection<ItemID>(
  selection: ItemID[],
  options?: {
    containerNamespace?: string | null;
  }
)
Swift
nonisolated func dragContainerSelection<ItemID>(
    _ selection: @autoclosure @escaping () -> Array<ItemID>,
    containerNamespace: Namespace.ID? = nil
) -> some View
where ItemID : Hashable, ItemID : Sendable

Parameters

  • selection*ItemID[]

    A closure that provides identifiers of selected items.

  • options.containerNamespacestring | null

    An optional namespace of the drag container.

Discussion

A drag container finds the nearest enclosing dragContainerSelection(_:containerNamespace:) with the same item identifier type and same namespace, if specified. Drag container uses the provided selected item identifiers to determine what the drag payload should be.

If the dragged view is associated with a selected identifier, the payload should contain all the selected items. If the dragged view is not selected, the payload should not contain the whole selection, just the dragged item. With dragContainerSelection(_:containerNamespace:), you get fine-grained control over what items are included in the drag payload.

 struct FruitContainer: View {
      @State private var fruits: [Fruit]
      @State private var selection: [Fruit.ID]

      var body: some View {
          VStack {
              ForEach(fruits) { fruit in
                  FruitView(fruit)
                      .draggable(containerItemID: fruit.id)
               }
           }
          .dragContainer(for: Fruit.self) { ids in
              fruits(with: ids)
          }
          .dragContainerSelection(selection)
      }

    func fruits(with ids: [Fruit.ID]) -> [Fruit] { ... }

    struct Fruit: Transferable, Identifiable {
        let id: String
        ...
    }

    struct FruitView: View {
        init(_ fruit: Fruit) { ... }
    }
}

See also

More in Gestures and input.