Created
March 30, 2020 07:10
-
-
Save maximkrouk/1eac0ad7b8e0c5e23b2f9f6285403631 to your computer and use it in GitHub Desktop.
Functional view modifier
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
import SwiftUI | |
struct _ViewModifier<Input: View, Output: View>: ViewModifier { | |
typealias Body = Output | |
typealias Content = _ViewModifier_Content<_ViewModifier<Input, Output>> | |
var modification: (Content) -> Output | |
func body(content: Content) -> Output { | |
modification(content) | |
} | |
} | |
extension View { | |
func modifier<T: View>(_ viewModifier: _ViewModifier<Self, T>) | |
-> ModifiedContent<Self, _ViewModifier<Self, T>> { | |
_modifier(viewModifier) | |
} | |
private func _modifier<T: ViewModifier>(_ viewModifier: T) | |
-> ModifiedContent<Self, T> { | |
modifier(viewModifier) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
I use it with this extension:
And modifier declaration now looks like this:
instead of this
Back to index