๐Ÿ’พview modifier (template)

swift โŸฉ custom โŸฉ snippets โŸฉ view modifier (template)

import SwiftUI

// 1. (internal) view modifier
struct MyViewModifier: ViewModifier {
    
    // โญ๏ธ 2. new states (optional)
    @State private var blur = true              
    
    // body(content:) - ViewModifier requirement
    func body(content: Content) -> some View {
        // โญ๏ธ 3. modify input `content`
        content
            .blur(radius: blur ? 20 : 0)
            .clipped()
            .onTapGesture { 
                withAnimation {
                    self.blur.toggle()
                }
            }
    }
}

// โญ๏ธ 4. public helper
extension View {
    public func myModifier() -> some View { 
        modifier(MyViewModifier()) 
    }
}

Last updated