Skip to content

Commit

Permalink
Add List and ListItem views.
Browse files Browse the repository at this point in the history
Part of #25.
  • Loading branch information
jverkoey committed Aug 6, 2024
1 parent 9ec8def commit 47b8b72
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/Slipstream/Documentation.docc/W3C/W3CViews.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ The complete W3C HTML elements standard can be found [here](https://html.spec.wh
- ``Paragraph``
- ``Divider``
- ``Blockquote``
- ``List``
- ``ListItem``
- ``Div``

### Text-level semantics
Expand Down
42 changes: 42 additions & 0 deletions Sources/Slipstream/W3C/Elements/GroupingContent/List.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// A view that represents a list of items.
///
/// The items can be ordered, where changing the order would
/// change the meaning of the document, or unordered, where
/// the order of the items is not important.
///
/// Lists are unordered by default.
///
/// ```swift
/// struct MySiteContent: View {
/// var body: some View {
/// Body {
/// List {
/// ListItem {
/// Text("Item 1")
/// }
/// }
/// }
/// }
/// }
/// ```
///
/// - SeeAlso: W3C [`ol`](https://html.spec.whatwg.org/multipage/grouping-content.html#the-ol-element) specification.
/// - SeeAlso: W3C [`ul`](https://html.spec.whatwg.org/multipage/grouping-content.html#the-ul-element) specification.
///
/// ## See Also
///
/// - ``ListItem``
public struct List<Content>: W3CElement where Content: View {
@_documentation(visibility: private)
public var tagName: String { ordered ? "ol" : "ul" }

/// Creates a list.
public init(ordered: Bool = false, @ViewBuilder content: @escaping () -> Content) {
self.ordered = ordered
self.content = content
}

@_documentation(visibility: private)
@ViewBuilder public let content: () -> Content
private let ordered: Bool
}
33 changes: 33 additions & 0 deletions Sources/Slipstream/W3C/Elements/GroupingContent/Listitem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// A view that represents a list item.
///
/// ```swift
/// struct MySiteContent: View {
/// var body: some View {
/// Body {
/// List {
/// ListItem {
/// Text("Item 1")
/// }
/// }
/// }
/// }
/// }
/// ```
///
/// - SeeAlso: W3C [`li`](https://html.spec.whatwg.org/multipage/grouping-content.html#the-li-element) specification.
///
/// ## See Also
///
/// - ``List``
public struct ListItem<Content>: W3CElement where Content: View {
@_documentation(visibility: private)
public let tagName: String = "li"

/// Creates a list item.
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

@_documentation(visibility: private)
@ViewBuilder public let content: () -> Content
}
39 changes: 39 additions & 0 deletions Tests/SlipstreamTests/W3C/ListTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Testing

import Slipstream

struct ListTests {
@Test func emptyBlock() throws {
try #expect(renderHTML(List {}) == "<ul></ul>")
try #expect(renderHTML(List(ordered: true) {}) == "<ol></ol>")
}

@Test func withItems() throws {
try #expect(renderHTML(List {
ListItem {
Text("Item 1")
}
ListItem {
Text("Item 2")
}
}) == """
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
""")
try #expect(renderHTML(List(ordered: true) {
ListItem {
Text("Item 1")
}
ListItem {
Text("Item 2")
}
}) == """
<ol>
<li>Item 1</li>
<li>Item 2</li>
</ol>
""")
}
}

0 comments on commit 47b8b72

Please sign in to comment.