Last active
March 20, 2023 11:51
-
-
Save michaelevensen/fb4f43f2da7256e5b48537193414d370 to your computer and use it in GitHub Desktop.
Generics in extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct InputFieldButtonStyle<Content: View>: ButtonStyle { | |
@Environment(\.colorScheme) var colorScheme | |
let mimicksDropdown: Bool | |
let placeholderContent: () -> Content? | |
init( | |
mimicksDropdown: Bool = false, | |
@ViewBuilder placeholderContent: @escaping () -> Content? = { nil } | |
) { | |
self.mimicksDropdown = mimicksDropdown | |
self.placeholderContent = placeholderContent | |
} | |
func makeBody(configuration: Configuration) -> some View { | |
VStack(alignment: .leading) { | |
configuration.label | |
.font(.defaultSubheadline) | |
HStack { | |
ZStack { | |
if let _ = placeholderContent { | |
placeholderContent() | |
} | |
else { | |
configuration.label | |
} | |
} | |
.foregroundColor(.baseNeutral) | |
Spacer() | |
if mimicksDropdown { | |
Image(systemName: "chevron.down") | |
} | |
} | |
.foregroundColor(.black) | |
// .foregroundColor(colorScheme == .dark ? .black : .white) | |
.padding(20) | |
.background(Color.baseGrey) | |
.cornerRadius(6) | |
.scaleEffect(configuration.isPressed ? 0.98 : 1) | |
.font(.defaultBody) | |
.animation( | |
.interpolatingSpring( | |
mass: 3, | |
stiffness: 60, | |
damping: 100, | |
initialVelocity: 10), | |
value: configuration.isPressed) | |
} | |
} | |
} | |
/**/ | |
extension ButtonStyle where Self == InputFieldButtonStyle<AnyView> { | |
internal static var input: InputFieldButtonStyle<AnyView> { | |
InputFieldButtonStyle() | |
} | |
internal static func input(mimicksDropdown: Bool = false) -> InputFieldButtonStyle<AnyView> { | |
InputFieldButtonStyle(mimicksDropdown: mimicksDropdown) | |
} | |
} | |
extension ButtonStyle where Self == InputFieldButtonStyle<AnyView> { | |
internal static func input<Content: View>(mimicksDropdown: Bool = false, | |
@ViewBuilder placeholderContent: @escaping () -> Content? = { nil }) -> InputFieldButtonStyle<AnyView> { | |
InputFieldButtonStyle(mimicksDropdown: mimicksDropdown, | |
placeholderContent: { | |
AnyView(placeholderContent()) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment