Skip to content

Instantly share code, notes, and snippets.

@moyerr
moyerr / Updater.swift
Created April 2, 2026 19:25
An environment action for mutating state owned by an ancestor view
/// An action that updates a value stored in the environment.
///
/// ## Overview
///
/// Use an `Updater` to allow descendant views to modify a value owned by
/// an ancestor view, without passing Bindings or specific update actions for each property.
/// The updater accepts a ``WritableKeyPath`` and a new value, then applies
/// the change through a closure you provide at initialization.
///
/// ### Setting up the updater
@moyerr
moyerr / AsyncContent.swift
Last active July 16, 2026 17:55
An AsyncContent view for SwiftUI
/// The current phase of the asynchronous loading operation.
enum AsyncContentPhase<Value: Sendable> {
/// No value is loaded yet.
case empty
/// The value loaded successfully.
case success(Value)
/// The load operation failed with an error.
case failure(any Error)
/// The loaded value, if any.
@moyerr
moyerr / AdaptiveStyle.swift
Created July 16, 2026 17:52
Add ShapeStyle support to @ConentBuilder, allowing for more dynamic custom ShapeStyle implementations
/// A ShapeStyle that adapts dynamically based on the environment's ColorScheme
///
/// This exists as a basic example of how to apply a @ContentBuilder to ShapeStyles
struct AdaptiveStyle<Light: ShapeStyle, Dark: ShapeStyle>: ShapeStyle {
let light: Light
let dark: Dark
@ContentBuilder
func resolve(in environment: EnvironmentValues) -> some ShapeStyle {
if environment.colorScheme == .light {