Contents

reorderDestination(for:in:)

Provides the destination value of a reordering operation that occurred in the container associated with this drop destination modifier.

Declaration

nonisolated func reorderDestination<Item, CollectionID>(for item: Item.Type, in collectionID: CollectionID.Type = ReorderableSingleCollectionIdentifier.self) -> ReorderDifference<Item.ID, CollectionID>.Destination? where Item : Identifiable

Mentioned in

Discussion

Use the ItemID and CollectionID types of your reorderContainer(for:in:isEnabled:move:) modifier to look up the destination value.

This value can be nil, if the container was unable to determine a placement for the items. This can happen if the user drags items into the destination but does not interact with any of the container items to determine a concrete position. You should still accept these items into the container. In this example, those values are appended to the end of the collection:

struct ContentView: View {
    @State var contacts: [Contact] = []

    var body: some View {
        VStack {
            ForEach(contacts) { contact in
                ContactView(contact)
            }
            .reorderable()
        }
        .reorderContainer(for: Contact.ID.self) { difference in
            difference.apply(to: &contacts)
        }
        .dragContainer(for: Contact.self) { itemID in
            contacts.first { $0.id == itemID }.map { [$0] } ?? []
        }
        .dropDestination(for: Contact.self) { items, session in
            if let destinationIndex = session.reorderDestination(
                for: Contact.ID.self)?.index(in: contacts)
            {
                contacts.insert(
                    contentsOf: items, at: destinationIndex)
            } else {
                contacts.append(contentsOf: items)
            }
        }
    }
}
  • item: The type of reorderable items in the container.

  • collectionID: The identifier type for collections in your container.

See Also

Reordering items