import SwiftUI

public extension View {
    func clearButton(value: Binding<String>) -> some View {
        modifier(ClearButtonModifier(value: value))
    }
}

private struct ClearButtonModifier: ViewModifier {
    @Binding var value: String

    func body(content: Content) -> some View {
        let show = !value.isEmpty

        HStack(spacing: 0) {
            content

            Button(
                action: { value = "" },
                label: {
                    Image(systemName: "xmark.circle")
                        .tint(Color.SemanticV1.textLink)
                }
            )
            .opacity(show ? 1 : 0)
            .animation(.default, value: show)
            .padding(.leading, 8)
        }
    }
}
