Here is a nice little extension which can be used to conditionally apply a modifier in SwiftUI. Without using extensions, we cannot do conditional application of a modifier with SwiftUI. Because of the declarative way, there is no way to specify if and else to a modifier on a View directly. In this example we will see how to apply tap gesture conditionally. I am using this in relation to a click and a drag on a list.

extension View {
    /// Attach a tap handler only when `enabled` is true.
    /// This is can be used when we want to have click on list view by default. But when drag mode is enabled, we remove the tap gesture to make the drag work.
    @ViewBuilder  // ViewBuilder is required so that the function can return different view trees.
    func onTapIf(_ enabled: Bool, perform action: @escaping () -> Void) -> some View {
        if enabled {
            self.onTapGesture(perform: action)
        } else {
            self
        }
    }
}

When we have tap gesture added to a list, it takes precedence over the drag gesture. And if we need drag to work, we need an edit list option, a drag mode, where we need to remove the tap gesture.

@State private var isDragMode = false

var body: some View {
    List(selection: $selectedProjectIds) {
        ForEach(projects) { proj in
            NameDescView(imageName: "project", name: proj.getName(), desc: proj.desc, isDisplayDragIndicator: isDragMode)
                .padding(.vertical, 6)
                .frame(maxWidth: .infinity, alignment: .leading)
                .contentShape(Rectangle())
                .tag(proj.getId())
                .onTapIf(!isDragMode) {  // Conditionally apply tap. This overrides the default selection check on the list.
                    onSelect(proj)
                }
        }
    }
}