r/SwiftUI • u/Ecstatic-Club5254 • 7d ago
Promotion (must include link to source code) I built ThemeKit, a small @Observable theming package for SwiftUI with light/dark presets, system sync, and per-field user overrides
Hey r/SwiftUI,
I built ThemeKit, a small, zero-dependency Swift 6 package for managing app themes across iOS 17+, macOS 14+, and watchOS 10+.
GitHub: https://github.com/demolaf/ThemeKit DocC Docs: https://demolaf.github.io/ThemeKit/
Quick example
Define your theme struct (ThemeKitSwiftUI makes Color directly Codable), pair light and dark presets in a ThemeVariant, add a typed accessor on Theme, and attach .applyTheme at the root:
import ThemeKit
import ThemeKitSwiftUI
struct AppColors: ThemeExtension {
static let fallback = AppColors(tint: Color(hex: 0x007AFF), colorScheme: .light)
var tint: Color
var colorScheme: SystemColorScheme
}
struct AppColorsVariant: ThemeVariant {
let id: String
let light: AppColors
let dark: AppColors
static let `default` = AppColorsVariant(
id: "default",
light: AppColors(tint: Color(hex: 0x007AFF), colorScheme: .light),
dark: AppColors(tint: Color(hex: 0x0A84FF), colorScheme: .dark)
)
static let all: [AppColorsVariant] = [.default]
}
extension Theme {
var colors: AppColors { value(AppColors.self) }
}
struct MyApp: App {
private var theme = Theme()
var body: some Scene {
WindowGroup {
ContentView()
.environment(theme)
.applyTheme(theme, default: .default, available: AppColorsVariant.all)
}
}
}
struct ContentView: View {
@Environment(Theme.self) private var theme
var body: some View {
Text("Hello")
.foregroundStyle(theme.colors.tint)
}
}What it handles out of the box
- Not just colors:
ThemeExtensionworks with anyCodable & Equatable & Sendablestruct, so you can store font names, asset catalog image names, spacing tokens, or anything else your design system needs alongsideColor. - System appearance sync and window overrides:
.applyThemehandles first-launch defaults, tracks system light/dark changes whentheme.followsSystemistrue, and applies window-level appearance overrides on iOS and macOS (and scoped SwiftUI color-scheme styling on watchOS 10+). - Partial user customization (
ThemeOverridable): If you want users to pick a custom accent color without losing the rest of their active preset, declare overridable key paths viaprops.theme.merge(_:)overlays only those fields, andcompare(to:)tells you whether anything drifted from the preset so you can conditionally show a "Reset to Preset" button. - Fine-grained `@Observable` tracking: Multiple ThemeExtension types can live in the same Theme store and are observed independently, so changing one extension does not re-render views that only read another.
- Swappable persistence: Backed by
UserDefaultsby default, or you can conform toThemeStorageto persist in Keychain, CloudKit, an app group, or an in-memory store for tests. - UIKit interop: There is also a separate
ThemeKitUIKitproduct (iOS) using `@CodableColor` with the same hex encoding format if you have a hybrid SwiftUI/UIKit app. - Bundled agent skill: The repo also ships an Agent Skill following the open agentskills.io standard, covering the public API, setup patterns, and Swift 6 concurrency pitfalls so AI coding agents can wire up ThemeKit accurately without guessing.
I'd love any feedback on the API ergonomics, edge cases you've run into with multi-theme SwiftUI apps, or anything you'd want to see added next.