Skip to content

Commit

Permalink
Merge pull request #24 from payan-app/button
Browse files Browse the repository at this point in the history
refactor: improve button style
  • Loading branch information
juandahurt authored Jul 8, 2022
2 parents fe34a54 + 8690c59 commit 9a3a40d
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions Sources/Purace/Views/Basic/Button/PuraceButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,58 +24,57 @@ public struct PuraceButtonView: View {
self.onTap = onTap
}

private func getTextColor() -> Color {
switch type {
case .loud:
return .white
case .quiet:
return PuraceStyle.Color.G1
case .custom(_, _, let textColor):
return textColor
public var body: some View {
Button(title) {
onTap?()
}
.font(PuraceStyle.Font.get(size: CGFloat(fontSize), weight: .medium))
.buttonStyle(PuraceButtonStyle(type: type))
}
}

struct PuraceButtonStyle: ButtonStyle {
let type: PuraceButtonType

private func getBackgroundColor() -> Color {
switch type {
case .loud:
return PuraceStyle.Color.G2
case .quiet:
return PuraceStyle.Color.G6
return .black.opacity(0.05)
case .custom(let backgroundColor, _, _):
return backgroundColor
}
}

private func getTextColor() -> Color {
switch type {
case .loud:
return .white
case .quiet:
return PuraceStyle.Color.G1
case .custom(_, _, let textColor):
return textColor
}
}

private func getOnPressedBackgroundColor() -> Color {
switch type {
case .loud:
return PuraceStyle.Color.G1
case .quiet:
return PuraceStyle.Color.G5
return .black.opacity(0.15)
case .custom(_, let onPressedColor, _):
return onPressedColor
}
}

public var body: some View {
PuraceTextView(title, fontSize: fontSize, textColor: getTextColor(), weight: .medium)
.onTapGesture {
onTap?()
}
.simultaneousGesture(
DragGesture(minimumDistance: 0)
.onChanged({ _ in
withAnimation(.easeIn(duration: 0.1)) {
isBeingPressed = true
}
})
.onEnded({ _ in
isBeingPressed = false
})
)
.padding(.horizontal)
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding(.horizontal, 10)
.padding(.vertical, 5)
.background(isBeingPressed ? getOnPressedBackgroundColor() : getBackgroundColor())
.cornerRadius(4)
.background(configuration.isPressed ? getOnPressedBackgroundColor() : getBackgroundColor())
.foregroundColor(getTextColor())
.clipShape(RoundedRectangle(cornerRadius: 5))
}
}

0 comments on commit 9a3a40d

Please sign in to comment.