I have a list in my macOS app’s sidebar. When I add the .onDrag
modifier, dragging NSItemProvider
works correctly, but clicking/selecting the item to trigger the NavigationLink
gets blocked.
struct ContentView: View {
var body: some View {
NavigationView {
List(Data.items) { item in
NavigationLink(
destination: Text(item.text),
label: {
Text(item.text)
.lineLimit(5)
// Enabling dragging with .onDrag {} disables click and selection:
.onDrag { return NSItemProvider(object: item.url as NSURL) }
})
}
Text("Placeholder")
}
}
}
How can I add the drag behavior while keeping the normal list selection behavior intact?
Here ist the rest of the Minimal Reproducible Example:
import SwiftUI
@main
struct ListDragExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct Data {
struct Item: Identifiable {
let id = UUID()
let text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
let url = URL(string: "http://example.com")!
}
static let items = [
Item(),
Item(),
Item()
]
}