Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ struct CustomizingComponentsView: View {

NiceButton("Over here as well", style: .secondary, rightImage: NiceButtonImage(systemIcon: "heart"), horizontalContentPadding: 20) {}

NiceButton("Leading aligned", style: .primary, contentHorizontalAlignment: .leading) {}

NiceButton(".. and trailing aligned", style: .secondary, contentHorizontalAlignment: .trailing) {}

NiceButton("and buttons with images", style: .primary, balanceImages: false) {}
.withLeftImage(
Expand Down
22 changes: 19 additions & 3 deletions Sources/NiceComponents/Button/NiceButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ public struct NiceButton: View {
/// The style configuration for the button.
let style: NiceButtonStyle

/// Padding between the button text/images and edges of the button. Default is 8.
/// Padding between the button text/images and edges of the button.
/// Default is nil, unless contentHorizontalAlignment has been set, in which case it is 16.
/// Set this to `nil` to have the button fill it's available space, like you'd set `maxWidth: .infinity`.
var horizontalContentPadding: CGFloat?

/// When set, aligns the content of the button, including images inside the button. Default is `center`.
var contentHorizontalAlignment: TextAlignment

/// An optional image to display on the left side of the button.
var leftImage: NiceButtonImage?

Expand All @@ -42,15 +46,18 @@ public struct NiceButton: View {
/// - style: The style configuration for the button.
/// - inactive: A Boolean value that determines whether the button is inactive. Defaults to `false`.
/// - balanceImages: A Boolean value indicating whether the images should be balanced. Defaults to `true`.
/// - contentHorizontalAlignment: Optionally align all content within the button. Defaults to `center`.
/// - leftImage: An optional image to display on the left side of the button.
/// - rightImage: An optional image to display on the right side of the button.
/// - horizontalContentPadding: Padding between the button content and edges of the button. Default is nil, causing the button to expand to fill all available space.
/// - horizontalContentPadding: Padding between the button content and edges of the button.
/// Default is nil, unless contentHorizontalAlignment has been set, in which case it is 16.
/// - action: The closure to execute when the button is tapped.
public init(
_ text: String,
style: NiceButtonStyle,
inactive: Bool = false,
balanceImages: Bool = true,
contentHorizontalAlignment: TextAlignment? = nil,
leftImage: NiceButtonImage? = nil,
rightImage: NiceButtonImage? = nil,
horizontalContentPadding: CGFloat? = nil,
Expand All @@ -60,9 +67,10 @@ public struct NiceButton: View {
self.style = style
self.inactive = inactive
self.balanceImages = balanceImages
self.contentHorizontalAlignment = contentHorizontalAlignment ?? .center
self.leftImage = leftImage
self.rightImage = rightImage
self.horizontalContentPadding = horizontalContentPadding
self.horizontalContentPadding = horizontalContentPadding ?? (contentHorizontalAlignment == nil ? nil : 16)
self.action = action
}

Expand All @@ -73,6 +81,10 @@ public struct NiceButton: View {
public var body: some View {
Button(action: action) {
HStack(spacing: 0) {
if contentHorizontalAlignment == .trailing {
Spacer()
}

if let leftImage = leftImage {
leftImage.image
.padding(.leading, leftImage.offset)
Expand All @@ -92,6 +104,10 @@ public struct NiceButton: View {
rightImage.image
.padding(.trailing, rightImage.offset)
}

if contentHorizontalAlignment == .leading {
Spacer()
}
}
.frame(maxWidth: horizontalContentPadding == nil ? .infinity : nil)
}
Expand Down