Skip to content

Instantly share code, notes, and snippets.

@vince19972
Created September 9, 2019 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vince19972/b0cfbcf08dfb28520b47201e3f490de4 to your computer and use it in GitHub Desktop.
Save vince19972/b0cfbcf08dfb28520b47201e3f490de4 to your computer and use it in GitHub Desktop.
Demo code of building modifier by mutating function in SwiftUI
// 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