Contents

reorderDestination(for:itemID: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, ItemID, CollectionID>(for item: Item.Type, itemID: KeyPath<Item, ItemID>, in collectionID: CollectionID.Type = ReorderableSingleCollectionIdentifier.self) -> ReorderDifference<ItemID, CollectionID>.Destination?

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 accounts: [Account] = []

    var body: some View {
        VStack {
            ForEach(accounts, id: \.uuid) { account in
                AccountView(account)
            }
            .reorderable()
        }
        .reorderContainer(for: Account.self, itemID: \.uuid) {
            (difference) in
            difference.apply(to: &accounts)
        }
        .dragContainer(for: Account.self) { userIDs in
            findAccounts(ids: userIDs)
        }
        .dropDestination(for: Account.self) { items, session in
            if let destinationIndex = session.reorderDestination(
                for: Account.self, itemID: \.uuid)?.index(
                    in: accounts)
            {
                accounts.insert(
                    contentsOf: items, at: destinationIndex)
            } else {
                accounts.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