Created
March 27, 2021 15:49
-
-
Save schwa/4ff3934539a1fe815b60c6a60f772322 to your computer and use it in GitHub Desktop.
Dash Snippet for SwiftUI Environments & Modifiers
This file contains hidden or 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
struct __ValueType__ { | |
} | |
struct __ValueType__Key: EnvironmentKey { | |
static var defaultValue = __ValueType__() | |
} | |
extension EnvironmentValues { | |
var __KeyName__: __ValueType__ { | |
get { | |
self[__ValueType__Key.self] | |
} | |
set { | |
self[__ValueType__Key.self] = newValue | |
} | |
} | |
} | |
struct __ValueType__Modifier: ViewModifier { | |
let value: __ValueType__ | |
func body(content: Content) -> some View { | |
content.environment(\.__KeyName__, value) | |
} | |
} | |
extension View { | |
func __KeyName__(value: __ValueType__) -> some View { | |
self.modifier(__ValueType__Modifier(value: value)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And if you prefer Xcode style snippets: