-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathButtonStyle+Toolkit.swift
210 lines (174 loc) · 9.78 KB
/
ButtonStyle+Toolkit.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// ButtonStyle+Toolkit.swift
//
// Made with ❤️ by Novum
//
// Copyright © Telefonica. All rights reserved.
//
import UIKit
public extension Button.Style {
private static let regularInsets: UIEdgeInsets = .init(top: 12, left: 16, bottom: 12, right: 16)
private static let smallInsets: UIEdgeInsets = .init(top: 5, left: 12, bottom: 5, right: 12)
private static let linkInsets: UIEdgeInsets = .init(top: 5, left: 12, bottom: 5, right: 12)
private static var regularFont: UIFont { .textPreset3(weight: .button) }
private static var smallFont: UIFont { .textPreset2(weight: .button) }
private static var linkFont: UIFont { .textPreset2(weight: .button) }
private static var regularMinimumWidth: CGFloat = 156
private static var smallMinimumWidth: CGFloat = 104
private static var linkMinimumWidth: CGFloat = 0
private enum ImageHeight {
static let regular: CGFloat = 24
static let small: CGFloat = 20
}
static var primary: Button.Style {
Button.Style(
allowsBleedingAlignment: false,
stateStyleByState: [
.normal: Button.StateStyle(textColor: .textButtonPrimary, backgroundColor: .buttonPrimaryBackground, borderColor: .buttonPrimaryBackground),
.selected: Button.StateStyle(textColor: .textButtonPrimary, backgroundColor: .buttonPrimaryBackgroundPressed, borderColor: .buttonPrimaryBackgroundPressed),
.disabled: Button.StateStyle(textColor: .textButtonPrimary, backgroundColor: .buttonPrimaryBackground, borderColor: .buttonPrimaryBackground),
.loading: Button.StateStyle(textColor: .textButtonPrimary, backgroundColor: .buttonPrimaryBackground, borderColor: .buttonPrimaryBackground)
]
)
}
static var secondary: Button.Style {
let backgroundColor: UIColor = .clear
return Button.Style(
allowsBleedingAlignment: false,
stateStyleByState: [
.normal: Button.StateStyle(textColor: .textButtonSecondary, backgroundColor: backgroundColor, borderColor: .buttonSecondaryBorder),
.selected: Button.StateStyle(textColor: .textButtonSecondaryPressed, backgroundColor: backgroundColor, borderColor: .buttonSecondaryBackgroundPressed),
.disabled: Button.StateStyle(textColor: .textButtonSecondary, backgroundColor: backgroundColor, borderColor: .buttonSecondaryBorder),
.loading: Button.StateStyle(textColor: .textButtonSecondary, backgroundColor: backgroundColor, borderColor: .buttonSecondaryBorder)
]
)
}
static var danger: Button.Style {
let textColor: UIColor = .textButtonPrimary
return Button.Style(
allowsBleedingAlignment: false,
stateStyleByState: [
.normal: Button.StateStyle(textColor: textColor, backgroundColor: .buttonDangerBackground, borderColor: .buttonDangerBackground),
.selected: Button.StateStyle(textColor: textColor, backgroundColor: .buttonDangerBackgroundPressed, borderColor: .buttonDangerBackgroundPressed),
.disabled: Button.StateStyle(textColor: textColor, backgroundColor: .buttonDangerBackground, borderColor: .buttonDangerBackground),
.loading: Button.StateStyle(textColor: textColor, backgroundColor: .buttonDangerBackground, borderColor: .buttonDangerBackground)
]
)
}
static var link: Button.Style {
let backgroundColor: UIColor = .clear
var style = Button.Style(
allowsBleedingAlignment: true,
stateStyleByState: [
.normal: Button.StateStyle(textColor: .textLink, backgroundColor: backgroundColor, borderColor: backgroundColor),
.selected: Button.StateStyle(textColor: .textLink, backgroundColor: .buttonLinkBackgroundPressed, borderColor: backgroundColor),
.disabled: Button.StateStyle(textColor: .textLink, backgroundColor: backgroundColor, borderColor: backgroundColor),
.loading: Button.StateStyle(textColor: .textLink, backgroundColor: backgroundColor, borderColor: backgroundColor)
]
)
style.overriddenSizes = linkOverriddenSizes
return style
}
static var linkDanger: Button.Style {
var style = Button.Style(
allowsBleedingAlignment: true,
stateStyleByState: [
.normal: Button.StateStyle(textColor: .textLinkDanger, backgroundColor: .clear, borderColor: .clear),
.selected: Button.StateStyle(textColor: .textLinkDanger, backgroundColor: .buttonLinkDangerBackgroundPressed, borderColor: .clear),
.disabled: Button.StateStyle(textColor: .textLinkDanger, backgroundColor: .clear, borderColor: .clear),
.loading: Button.StateStyle(textColor: .textLinkDanger, backgroundColor: .clear, borderColor: .clear)
]
)
style.overriddenSizes = linkOverriddenSizes
return style
}
static var linkDangerInverse: Button.Style {
var style = Button.Style(
allowsBleedingAlignment: true,
stateStyleByState: [
.normal: Button.StateStyle(textColor: .textLinkDanger, backgroundColor: .buttonLinkDangerBackgroundInverse, borderColor: .clear),
.selected: Button.StateStyle(textColor: .textLinkDanger, backgroundColor: .buttonLinkDangerBackgroundInversePressed, borderColor: .clear),
.disabled: Button.StateStyle(textColor: .textLinkDanger, backgroundColor: .clear, borderColor: .clear),
.loading: Button.StateStyle(textColor: .textLinkDanger, backgroundColor: .clear, borderColor: .clear)
]
)
style.overriddenSizes = linkOverriddenSizes
return style
}
static var primaryInverse: Button.Style {
Button.Style(
allowsBleedingAlignment: false,
stateStyleByState: [
.normal: Button.StateStyle(textColor: .textButtonPrimaryInverse, backgroundColor: .buttonPrimaryBackgroundInverse, borderColor: .buttonPrimaryBackgroundInverse),
.selected: Button.StateStyle(textColor: .textButtonPrimaryInversePressed, backgroundColor: .buttonPrimaryBackgroundInversePressed, borderColor: .buttonPrimaryBackgroundInversePressed),
.disabled: Button.StateStyle(textColor: .textButtonPrimaryInverse, backgroundColor: .buttonPrimaryBackgroundInverse, borderColor: .buttonPrimaryBackgroundInverse),
.loading: Button.StateStyle(textColor: .textButtonPrimaryInverse, backgroundColor: .buttonPrimaryBackgroundInverse, borderColor: .buttonPrimaryBackgroundInverse)
]
)
}
static var secondaryInverse: Button.Style {
Button.Style(
allowsBleedingAlignment: false,
stateStyleByState: [
.normal: Button.StateStyle(textColor: .textButtonSecondaryInverse, backgroundColor: .clear, borderColor: .buttonSecondaryBorderInverse),
.selected: Button.StateStyle(textColor: .textButtonSecondaryInversePressed, backgroundColor: .clear, borderColor: .buttonSecondaryBorderInversePressed),
.disabled: Button.StateStyle(textColor: .textButtonSecondaryInverse, backgroundColor: .clear, borderColor: .buttonSecondaryBorderInverse),
.loading: Button.StateStyle(textColor: .textButtonSecondaryInverse, backgroundColor: .clear, borderColor: .buttonSecondaryBorderInverse)
]
)
}
static var linkInverse: Button.Style {
let backgroundColor: UIColor = .clear
var style = Button.Style(
allowsBleedingAlignment: true,
stateStyleByState: [
.normal: Button.StateStyle(textColor: .textLinkInverse, backgroundColor: backgroundColor, borderColor: backgroundColor),
.selected: Button.StateStyle(textColor: .textLinkInverse, backgroundColor: .buttonLinkBackgroundInversePressed, borderColor: backgroundColor),
.disabled: Button.StateStyle(textColor: .textLinkInverse, backgroundColor: backgroundColor, borderColor: backgroundColor),
.loading: Button.StateStyle(textColor: .textLinkInverse, backgroundColor: backgroundColor, borderColor: backgroundColor)
]
)
style.overriddenSizes = linkOverriddenSizes
return style
}
func insets(isSmall: Bool) -> UIEdgeInsets {
if let overriddenSizes = overriddenSizes {
return overriddenSizes.insets
}
return isSmall ? Button.Style.smallInsets : Button.Style.regularInsets
}
func font(isSmall: Bool) -> UIFont {
if let overriddenSizes = overriddenSizes {
return overriddenSizes.font
}
return isSmall ? Button.Style.smallFont : Button.Style.regularFont
}
func minimumWidth(isSmall: Bool) -> CGFloat {
if let overriddenSizes = overriddenSizes {
return overriddenSizes.minimumWidth
}
return isSmall ? Button.Style.smallMinimumWidth : Button.Style.regularMinimumWidth
}
func leftImageHeight(isSmall: Bool) -> CGFloat {
if let leftImageHeight = overriddenSizes?.leftImageHeight {
return leftImageHeight
}
return isSmall ? Button.Style.ImageHeight.small : Button.Style.ImageHeight.regular
}
func rightImageHeight(isSmall: Bool) -> CGFloat {
if let rightImageHeight = overriddenSizes?.rightImageHeight {
return rightImageHeight
}
return isSmall ? Button.Style.ImageHeight.small : Button.Style.ImageHeight.regular
}
}
private extension Button.Style {
static var linkOverriddenSizes: OverriddenSizes {
OverriddenSizes(
insets: linkInsets,
minimumWidth: linkMinimumWidth,
font: linkFont,
rightImageHeight: Button.Style.ImageHeight.small
)
}
}