-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:(SwiftUI+Conditional) add conditional view extension
- Loading branch information
Build Pipeline
committed
Jan 18, 2023
1 parent
ff6d791
commit 1f5b822
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// SwiftUI+Conditional.swift | ||
// PlantUMLApp | ||
// | ||
// Created by Bartolomeo Sorrentino on 18/01/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
// | ||
// This extension lets us add the .if modifier to our Views and will only apply the modifiers we add if the condition is met. | ||
// | ||
// inspired by [Conditional modifier](https://designcode.io/swiftui-handbook-conditional-modifier) | ||
extension View { | ||
|
||
@ViewBuilder func `if`<Content: View>(_ condition: Bool, then transform: (Self) -> Content) -> some View { | ||
if condition { | ||
transform(self) | ||
} else { | ||
self | ||
} | ||
} | ||
|
||
@ViewBuilder func `if`<Content: View>(_ condition: Bool, then transformThen: (Self) -> Content, else transformElse: (Self) -> Content ) -> some View { | ||
if condition { | ||
transformThen(self) | ||
} else { | ||
transformElse(self) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters