macbackend/Sources/Core/View/Button.swift
david-swift 3d2ef52cc8
Some checks failed
Deploy Docs / publish (push) Successful in 3m4s
SwiftLint / SwiftLint (push) Failing after 9s
Extract core target
2024-12-04 21:17:21 +01:00

65 lines
1.6 KiB
Swift

//
// Button.swift
// MacBackend
//
// Created by david-swift on 18.09.2024.
//
import SwiftUI
/// A button widget.
public struct Button: SwiftUIWidget {
/// The button's label.
var label: String?
/// The button's icon.
var icon: Icon?
/// The button's action.
var action: () -> Void
/// Initialize a button.
/// - Parameters:
/// - label: The button's label.
/// - icon: The button's icon.
/// - action: The handler.
public init(_ label: String, icon: Icon? = nil, action: @escaping () -> Void) {
self.label = label
self.icon = icon
self.action = action
}
/// Initialize a button.
/// - Parameters:
/// - icon: The button's icon.
/// - action: The handler.
public init(icon: Icon, action: @escaping () -> Void) {
self.icon = icon
self.action = action
}
/// Get the SwiftUI view.
/// - Parameter properties: The widget data.
/// - Returns: The SwiftUI view.
public static func view(properties: Self) -> some SwiftUI.View {
if let icon = properties.icon {
SwiftUI.Button {
properties.action()
} label: {
SwiftUI.Label {
if let label = properties.label {
SwiftUI.Text(label)
}
} icon: {
icon.image
}
}
} else if let label = properties.label {
SwiftUI.Button(label) {
properties.action()
}
}
}
}