Last active
May 25, 2024 23:09
-
-
Save peterkos/591f90d1662dfcd44b1399999937b3ab to your computer and use it in GitHub Desktop.
Helpers for setting traits when using _VariadicView
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
// Generic version of https://gist.github.com/chriseidhof/5ff6ef8786f5635c18b20304ab9d9b01 | |
extension View { | |
/// Convenience for setting a `_ViewTraitKey` | |
func withTrait<Trait, Value>(_: Trait.Type, value: Value) -> some View | |
where Trait: _ViewTraitKey, Value == Trait.Value | |
{ | |
_trait(Trait.self, value) | |
} | |
} | |
extension _VariadicView.Children.Element { | |
/// Check if a `Trait.Value` is set on a variadic view's child. | |
func hasTrait<Trait, Value>(_: Trait.Type, value: Value) -> Bool | |
where Trait: _ViewTraitKey, Value == Trait.Value, Value: Equatable | |
{ | |
self[Trait.self] == value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment