Skip to content

Instantly share code, notes, and snippets.

@zwaldowski
Created April 28, 2023 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwaldowski/9c7aa34c474bcbe5e72f23a86e855e1b to your computer and use it in GitHub Desktop.
Save zwaldowski/9c7aa34c474bcbe5e72f23a86e855e1b to your computer and use it in GitHub Desktop.
// TODO docs
public typealias SubviewProxy = _VariadicView.Children
// TODO docs
public struct SubviewReader<Children, Content>: View where Children: View, Content: View {
var children: Children
var content: (SubviewProxy) -> Content
// TODO docs
public init(_ children: Children, @ViewBuilder content: @escaping (SubviewProxy) -> Content) {
self.children = children
self.content = content
}
public var body: some View {
_VariadicView.Tree(Layout(content: content)) {
children
}
}
struct Layout: _VariadicView.MultiViewRoot {
var content: (SubviewProxy) -> Content
func body(children: SubviewProxy) -> Content {
content(children)
}
}
}
public struct Divided<Content>: View where Content: View {
var content: Content
public init(@ViewBuilder content: () -> Content) {
self.content = content()
}
public var body: some View {
SubviewReader(content) { children in
ForEach(children) { view in
view
if view.id != children.last?.id {
Divider()
}
}
}
}
}
struct Divided_Previews: PreviewProvider {
static var previews: some View {
VStack(alignment: .leading) {
Divided {
Text("Milk")
Text("Eggs")
Text("Cheese")
}
}
.padding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment