Skip to content

Instantly share code, notes, and snippets.

@ddaddy
Created June 25, 2022 15:09
Show Gist options
  • Save ddaddy/010054740df06ecdfab5bc75abf1b54b to your computer and use it in GitHub Desktop.
Save ddaddy/010054740df06ecdfab5bc75abf1b54b to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
SidebarView()
SidebarView()
}
}
}
struct SidebarView: View {
var body: some View {
ThemedList {
ThemedSection("Header") {
Text("Row")
Text("Row")
Text("Row")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct ThemedList<Content>: View where Content: View {
@ViewBuilder var content: () -> Content
var body: some View {
List {
content()
.listRowBackground(Color.red)
}
}
}
struct ThemedSection<Header, Content, Footer> {
@ViewBuilder var header: () -> Header
@ViewBuilder var footer: () -> Footer
@ViewBuilder var content: () -> Content
}
extension ThemedSection : View where Header : View, Content : View, Footer : View {
var body: some View {
Section {
content()
} header: {
header()
.listRowBackground(Color.clear)
} footer: {
footer()
.listRowBackground(Color.clear)
}
}
}
extension ThemedSection where Header == Text, Content : View, Footer == EmptyView {
init<S>(_ title: S, @ViewBuilder content: @escaping () -> Content) where S : StringProtocol {
self.content = content
self.header = { Text(title) }
self.footer = { EmptyView() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment