Skip to content

Instantly share code, notes, and snippets.

@nicholaslythall
Created January 9, 2024 23:16
Show Gist options
  • Save nicholaslythall/11ae936e4d868f95ca04a6e1d805f185 to your computer and use it in GitHub Desktop.
Save nicholaslythall/11ae936e4d868f95ca04a6e1d805f185 to your computer and use it in GitHub Desktop.
DynamicShapeStyle
struct DynamicShapeStyle: ShapeStyle {
let light: AnyShapeStyle
let dark: AnyShapeStyle
init<Light: ShapeStyle, Dark: ShapeStyle>(light: Light, dark: Dark) {
self.light = AnyShapeStyle(light)
self.dark = AnyShapeStyle(dark)
}
func resolve(in environment: EnvironmentValues) -> some ShapeStyle {
switch environment.colorScheme {
case .light:
light
case .dark:
dark
@unknown default:
light
}
}
}
extension ShapeStyle where Self == DynamicShapeStyle {
static func `dynamic`<Light: ShapeStyle, Dark: ShapeStyle>(light: Light, dark: Dark) -> DynamicShapeStyle {
return .init(light: light, dark: dark)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment