Skip to content

Instantly share code, notes, and snippets.

@peterkos
Last active May 25, 2024 23:09
Show Gist options
  • Save peterkos/591f90d1662dfcd44b1399999937b3ab to your computer and use it in GitHub Desktop.
Save peterkos/591f90d1662dfcd44b1399999937b3ab to your computer and use it in GitHub Desktop.
Helpers for setting traits when using _VariadicView
// 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