Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Sources/SwiftUISupport/Components/Conditional.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import SwiftUI

extension View {
/// Applies the given transform if the given condition evaluates to `true`.
/// @see Referenced from: https://www.avanderlee.com/swiftui/conditional-view-modifier/
/// - Parameters:
/// - condition: The condition to evaluate.
/// - transform: The transform to apply to the source `View`.
/// - Returns: Either the original `View` or the modified `View` if the condition is `true`.
@ViewBuilder public func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
if condition {
transform(self)
} else {
self
}
}

@ViewBuilder
public func `do`<Content: View>(
@ViewBuilder transform: (Self) -> Content
) -> some View {
transform(self)
}
}