Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/danielsaidi/environmentkit

EnvironmentKit is a SwiftUI SDK that let you create custom environment values with less code.
https://github.com/danielsaidi/environmentkit

swift swiftui

Last synced: about 2 months ago
JSON representation

EnvironmentKit is a SwiftUI SDK that let you create custom environment values with less code.

Awesome Lists containing this project

README

        


EnvironmentKit Logo


Version
Swift 6.0
Swift UI
MIT License
Twitter: @danielsaidi
Mastodon: @danielsaidi@mastodon.social

## About EnvironmentKit

EnvironmentKit is a SwiftUI SDK that lets you define custom SwiftUI environment values with less code.

## ‼️ Important Information

Xcode 16 added support for the new `@Entry` type, which makes it a lot easier than before to create various value types, which makes this SDK less useful than it was before.

While this SDK remains a fun experiment with how far we could push Swift to make it easier to create custom environment values, this will most probably be left as is until September 2025, at which it will be deleted.

## Installation

EnvironmentKit can be installed with the Swift Package Manager:

```
https://github.com/danielsaidi/EnvironmentKit.git
```

You can also just copy the `EnvironmentValue.swift` file to your project, instead of using the package.

## Getting Started

Without `EnvironmentKit`, you have to write this boilerplate code for each custom environment value:

```swift
public extension MyStyle {

static var standard = Self()
}

private extension MyStyle {

struct Key: EnvironmentKey {
static var defaultValue: MyStyle = .standard
}
}

public extension EnvironmentValues {

var myStyle: MyStyle {
get { self[MyStyle.Key.self] }
set { self[MyStyle.Key.self] = newValue }
}
}

public extension View {

func myStyle(_ style: MyStyle) -> some View {
environment(\.myStyle, style)
}
}
```

With `EnvironmentKit`, you just need to implement `EnvironmentValue` and add a little extra code:

```swift
struct MyStyle: EnvironmentValue {

static var keyPath: EnvironmentPath { \.myStyle }
}

extension EnvironmentValues {

var myStyle: MyStyle {
get { get() } set { set(newValue) }
}
}

extension View {

func myStyle(_ style: MyStyle) -> some View {
environment(style)
}
}
```

## Documentation

The [online documentation][Documentation] has more information about this SDK, how it works, etc.

## Support my work

You can [sponsor me][Sponsors] on GitHub Sponsors or [reach out][Email] for paid support, to help support my [open-source projects][OpenSource].

Your support makes it possible for me to put more work into these projects and make them the best they can be.

## Contact

Feel free to reach out if you have questions or want to contribute in any way:

* Website: [danielsaidi.com][Website]
* Mastodon: [@[email protected]][Mastodon]
* Twitter: [@danielsaidi][Twitter]
* E-mail: [[email protected]][Email]

## License

EnvironmentKit is available under the MIT license. See the [LICENSE][License] file for more info.

[Email]: mailto:[email protected]

[Website]: https://danielsaidi.com
[GitHub]: https://github.com/danielsaidi
[Twitter]: https://twitter.com/danielsaidi
[Mastodon]: https://mastodon.social/@danielsaidi
[OpenSource]: https://danielsaidi.com/opensource
[Sponsors]: https://github.com/sponsors/danielsaidi

[Documentation]: https://danielsaidi.github.io/EnvironmentKit
[Getting-Started]: https://danielsaidi.github.io/EnvironmentKit/documentation/environmentkit/getting-started

[License]: https://github.com/danielsaidi/EnvironmentKit/blob/master/LICENSE