Created
September 9, 2019 23:22
-
-
Save vince19972/b0cfbcf08dfb28520b47201e3f490de4 to your computer and use it in GitHub Desktop.
Demo code of building modifier by mutating function in SwiftUI
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
// buttonLabel modifier | |
struct TokenButtonLabel: View { | |
// --snip-- | |
/// State properties | |
var isHighlighted = false | |
// --snip-- | |
} | |
extension TokenButtonLabel { | |
/// Pass highlightSwitch argument bonded with @State variable in order to trigger updates | |
mutating func highlight(_ highlightSwitch: TokenButton.StateSwitch) -> Self { | |
self.isHighlighted = highlightSwitch == .on ? true : false | |
return self | |
} | |
} | |
// buttonStyle modifier | |
struct TokenButtonStyle: ButtonStyle { | |
// --snip-- | |
/// State properties | |
var isActive = true | |
// --snip-- | |
} | |
extension TokenButtonStyle { | |
// Pass highlightSwitch argument bonded with @State variable in order to trigger updates | |
mutating func activate(_ activeSwitch: TokenButton.StateSwitch) -> Self { | |
self.isActive = activeSwitch == .on ? true : false | |
return self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment