Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[28][25] Nice buttons #31

Merged
merged 5 commits into from
Jul 19, 2022
Merged

[28][25] Nice buttons #31

merged 5 commits into from
Jul 19, 2022

Conversation

AZielinsky95
Copy link
Contributor

For: #28 #25

NiceButton

This PR introduces NiceButton, a protocol that adds a bunch of flexibility and useful default behaviour to all button components.

Some problems addressed by NiceButton:

Firstly, every button component had the exact same view body constructed with the only difference being the button style.
The shared body is now constructed in an extension to NiceButton within a viewBuilder property called defaultBody. This allows easy conformance to NiceButton without needing to duplicate code. DefaultBody will also be helpful for the eventual next step of this component which will be to allow a viewBuilder constructor to be passed in.

Secondly, tweaking properties of button components has been quite tedious and verbose. There was pretty much no way to do this other than reconstruct the ButtonStyle object and re-setting the same properties for the most part. Also, depending on the border-style of your button, it would require you to remember to tweak other related properties. For instance if you styled your button to be pill shaped and you wanted to change the height, you would have to remember to change the cornerRadius as well in order to keep the uniform pill shape.

Changing the primary button height before:

let largeButtonStyle = ButtonStyle(
    textStyle: primaryStyle.textStyle,
    height: 56,
    surfaceColor: primaryStyle.surfaceColor,
    onSurfaceColor: primaryStyle.onSurfaceColor,
    border: BorderStyle(radius: 28) // Don't forget this! 
)
PrimaryButton("Large button", style: largeButtonStyle) { }

New way of changing button height:

PrimaryButton("Large button", height: 56) { }

The borderRadius is automatically set for you because the border theme is set to .capsule.

You can also set any other properties of buttons on the fly, without needing to reconstruct an entire ButtonStyle object.

Thirdly, I found it a bit odd that you would replace your entire button with an entirely new button for supporting disabled states. If you had a PrimaryButton you would need to explicitly code your disabled condition to instead render InActiveButton when disabled. This has changed so that you now pass your disabled flag into the button component.

PrimaryButton("Button", disabled: isDisabled) 

The styling is set by new properties in NiceButtonStyle. (disabledSurfaceColor & disabledOnSurfaceColor)

NiceBorderStyle:

BorderStyle has been replaced with an enum for easier use/initialization and extra helpful behaviour. ( StrokeStyle now supported as well )

public enum NiceBorderStyle {
    case none
    case capsule(borderWidth: CGFloat?, borderColor: Color?)
    case rounded(radius: CGFloat, borderWidth: CGFloat?, borderColor: Color?)
    case border(borderWidth: CGFloat, borderColor: Color)
    case stroke(strokeStyle: StrokeStyle)
}

You can now add Left/Right icons to button components like this:

PrimaryButton("Primary") {
    print("Tapped")
}.withRightImage(
    ResizableImage.init(systemIcon: "heart.fill", width: 14, height: 14, tintColor: .red),
    spacing: 8.0 // Default
)

Bug Fixes:

  • Fixed NiceButtonStyle crash
  • Fixed borders getting cut off from buttons

Simulator Screen Shot - iPhone 12 - 2022-07-18 at 17 10 53

- All buttons are now NiceButtons
- Example view for NiceButton
- LoadingView component added flexibility
- Changes to how button styling works
- ResizableImage supports systemIcons
Copy link
Collaborator

@brendanlensink brendanlensink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a huge improvement! 🎉

One small suggestion, then don't forget to update the version number and changelog before merging.

public init(
_ text: String,
style: NiceButtonStyle? = nil,
disabled: Bool = false,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we switch to using inactive over disabled?

@AZielinsky95 AZielinsky95 merged commit a45d0e5 into main Jul 19, 2022
@AZielinsky95 AZielinsky95 deleted the az/nice-buttons branch July 19, 2022 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants