Skip to content

ericlewis/Splunk

Repository files navigation

Splunk

Go diving in SwiftUI's EnvironmentValues or use common values exposed for you.

Read First

This uses reflection under the hood. That means this is:

  • Probably incredibly unstable
  • Will almost definitely change
  • May behave incorrectly on different versions of iOS

Currently, this has been tested on iOS 15.0 -> iOS 15.2.

Examples

Splunk can be used to locate any values in Environment, there are a few ways to do that:

Basic Usage

import Splunk
import SwiftUI

struct ContentView: View {
  @Environment(\.self)
  private var env
  
  var body: some View {
    Text("Hello!")
      .foregroundColor(env.value(of: "ForegroundColorKey"))
  }
}

Colors Extension

import Splunk
import SwiftUI

struct ContentView: View {
  @Environment(\.colors.foregroundColor)
  private var foregroundColor
  
  var body: some View {
    Text("Hello!")
      .foregroundColor(foregroundColor)
  }
}

Advanced Usage

import Splunk
import SwiftUI

struct ContentView: View {
  var body: some View {
    InnerView()
      .buttonBorderShape(.capsule)
  }
}

struct InnerView: View {
  @Environment(\.buttonBorderShape)
  private var buttonBorderShape
  
  var body: some View {
    if let shape = buttonBorderShape, shape == .capsule {
      Text("Never Capsule!")
    } else {
      Button("Okay, capsule is cool.") {
        // do something amazing.
      }
    }
  }
}

extension EnvironmentValues {
  var buttonBorderShape: ButtonBorderShape? {
    get { value(of: "ButtonBorderShapeKey") }
  }
}

License

Splunk is released under the MIT license. See LICENSE for details.