Skip to content

Instantly share code, notes, and snippets.

@marlonjames71
Last active March 29, 2021 00:19
Show Gist options
  • Save marlonjames71/63dc5461235ac3d88e7542ce42788ea1 to your computer and use it in GitHub Desktop.
Save marlonjames71/63dc5461235ac3d88e7542ce42788ea1 to your computer and use it in GitHub Desktop.
DropDownMenu Code Snippets
extension Color {
static let bg = Color("mainBG")
static let secondaryBG = Color("secondaryBG")
static let greyText = Color("greyText")
}
import SwiftUI
struct DropDownMenuButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.background(configView(config: configuration))
}
@ViewBuilder
private func configView(config: ButtonStyleConfiguration) -> some View {
if config.isPressed {
SelectionHighlightView(opacity: 0.15)
}
}
}
extension View {
@ViewBuilder func embedInScrollView(_ shouldEmbed: Bool = true,
axis: Axis.Set = .vertical,
showsIndicators: Bool = true) -> some View {
if shouldEmbed {
ScrollView(axis, showsIndicators: showsIndicators) {
self
}
} else {
self
}
}
}
import SwiftUI
struct SelectionHighlightView: View {
let color: Color
let opacity: Double
let padding: Bool
init(_ color: Color = .blue,
opacity: Double,
padding: Bool = true) {
self.color = color
self.opacity = opacity
self.padding = padding
}
var body: some View {
color
.opacity(opacity)
.mask(RoundedRectangle(cornerRadius: 10.0))
.padding(.horizontal, padding ? 6 : 0)
}
}
import SwiftUI
struct SmoothRoundedCorners: ViewModifier {
let cornerRadius: CGFloat
init(_ cornerRadius: CGFloat) {
self.cornerRadius = cornerRadius
}
func body(content: Content) -> some View {
content
.clipShape(RoundedRectangle(cornerRadius: cornerRadius, style: .continuous))
}
}
extension View {
func smoothRoundedCorners(_ cornerRadius: CGFloat = 10.0) -> some View {
modifier(SmoothRoundedCorners(cornerRadius))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment