feat: Adds more documentation and examples.
All checks were successful
CI / Run tests. (push) Successful in 52s

This commit is contained in:
2024-12-08 18:27:31 -05:00
parent c6a269f062
commit 78a632c3e5
12 changed files with 149 additions and 7 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -1,9 +1,28 @@
/// A horizontal group of text nodes.
/// A horizontal stack of text nodes.
///
/// ### Example:
///
/// ```swift
/// let note = HStack {
/// "NOTE:".color(.cyan).bold()
/// "This is my super cool note".italic()
/// }
///
/// print(note.render())
/// ```
///
/// ![HStack example](hstack.png)
///
///
public struct HStack: TextNode {
@usableFromInline
let content: [any TextNode]
/// Create a new ``HStack`` with the given text nodes.
///
/// - Parameters:
/// - content: The content of the hstack.
@inlinable
public init(
@TextBuilder content: () -> any TextNode
@@ -18,11 +37,18 @@ public struct HStack: TextNode {
}
public extension HStack {
/// Apply the given style to a ``HStack``.
///
/// - Parameters:
/// - style: The style to apply to the ``HStack``.
func style<S: HStackStyle>(_ style: S) -> some TextNode {
style.render(content: .init(content: content))
}
/// Apply the given separator to a ``HStack``.
///
/// - Parameters:
/// - separator: The horizontal separator to use with the ``HStack``.
func separator(_ separator: Separator.Horizontal) -> some TextNode {
style(.separator(separator))
}
@@ -30,16 +56,28 @@ public extension HStack {
// MARK: - Style
/// Style a ``HStack`` by creating a type that conforms to ``HStackStyle`` and use the
/// style by calling the ``HStack/style(_:)`` method on your instance.
///
public protocol HStackStyle: TextModifier where Content == StackConfiguration {}
public extension HStackStyle where Self == HStackSeparatorStyle {
/// Apply the given separator on a ``HStack``.
///
/// - See Also: ``HStack/separator(_:)``
///
/// - Parameters:
/// - separator: The vertical separator to use with the ``HStack``.
static func separator(_ separator: Separator.Horizontal) -> Self {
HStackSeparatorStyle(separator: separator)
}
}
/// Separate items in a ``HStack`` with a given horizontal separator.
///
/// - See Also: ``HStack/separator(_:)``.
///
public struct HStackSeparatorStyle: HStackStyle {
@usableFromInline
let separator: Separator.Horizontal

View File

@@ -5,6 +5,9 @@ public struct StackConfiguration {
public let content: [any TextNode]
}
/// A helper type that removes empty text nodes, and applies a separtor between
/// the array of text nodes.
///
@usableFromInline
struct AnySeparatableStackNode<Separator: TextNode>: TextNode {

View File

@@ -1,10 +1,30 @@
/// A vertical stack of text nodes.
///
/// ### Example:
///
/// ```swift
/// let vStack = VStack {
/// "Blob Esquire"
/// .color(.yellow)
/// .bold()
/// .underline()
///
/// "Blob is a super awesome worker.".italic()
/// }
///
/// print(vStack.render())
/// ```
/// ![VStack rendered output](vstack.png)
///
///
public struct VStack: TextNode {
@usableFromInline
let content: [any TextNode]
/// Create a new ``VStack`` with the given text nodes.
///
/// - Parameters:
/// - content: The content of the vstack.
@inlinable
public init(
@TextBuilder content: () -> any TextNode
@@ -20,10 +40,18 @@ public struct VStack: TextNode {
public extension VStack {
/// Apply the given style to a ``VStack``.
///
/// - Parameters:
/// - style: The style to apply to the ``VStack``.
func style<S: VStackStyle>(_ style: S) -> some TextNode {
style.render(content: .init(content: content.removingEmptys()))
}
/// Apply the given separator to a ``VStack``.
///
/// - Parameters:
/// - separator: The vertical separator to use with the ``VStack``.
func separator(_ separator: Separator.Vertical) -> some TextNode {
style(.separator(separator))
}
@@ -31,16 +59,29 @@ public extension VStack {
// MARK: - Style
/// Style a ``VStack`` by creating a type that conforms to ``VStackStyle`` and use the
/// style by calling the ``VStack/style(_:)`` method on your instance.
///
public protocol VStackStyle: TextModifier where Content == StackConfiguration {}
public extension VStackStyle where Self == VStackSeparatorStyle {
/// Apply the given separator on a ``VStack``.
///
/// - See Also: ``VStack/separator(_:)``
///
/// - Parameters:
/// - separator: The vertical separator to use with the ``VStack``.
static func separator(_ separator: Separator.Vertical) -> Self {
VStackSeparatorStyle(separator: separator)
}
}
/// Separate items in a ``VStack`` with a given vertical separator.
///
/// - See Also: ``VStack/separator(_:)``.
///
public struct VStackSeparatorStyle: VStackStyle {
@usableFromInline
let separator: Separator.Vertical