Skip to content

Commit

Permalink
Add textAlignment modifier. (#55)
Browse files Browse the repository at this point in the history
Part of #51.
  • Loading branch information
jverkoey authored Aug 3, 2024
1 parent 9b79bb5 commit f4d4d20
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ conforms to the ``View`` protocol can act as a view in your website.

- ``ViewModifier``
- ``AttributeModifier``
- ``ClassModifier``

### Supporting view types

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ Slipstream implementations of Tailwind CSS views.
### Layout

- ``Container``

### Typography

- ``TextAlignment``
- ``View/textAlignment(_:)``
27 changes: 27 additions & 0 deletions Sources/Slipstream/TailwindCSS/Typography/View+textAlignment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// Constants that specify the visual alignment of the text.
public enum TextAlignment: String {
/// Aligns text to the left edge of the text container in
/// left-to-right (LTR) languages, and to the right edge
/// in right-to-left (RTL) languages..
case leading = "start"

/// Aligns text to the center of the text container.
case center

/// Aligns text to the right edge of the text container in
/// left-to-right (LTR) languages, and to the left edge
/// in right-to-left (RTL) languages..
case trailing = "end"

/// Text will fill the container, adding spacing between words as needed.
case justify
}

extension View {
/// Control the alignment of text.
///
/// - Parameter alignment: Text alignment to be applied to text within the modified view.
public func textAlignment(_ alignment: TextAlignment) -> some View {
return self.modifier(ClassModifier(add: "text-" + alignment.rawValue))
}
}
9 changes: 6 additions & 3 deletions Tests/SlipstreamTests/Sites/CatalogSiteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ private struct CatalogSite: View {
Linebreak()
Text("world!")
H1("Heading 1")
.textAlignment(.leading)
H2 {
Text("Heading 2")
}
.textAlignment(.center)
H3("Heading 3")
.textAlignment(.trailing)
H4("Heading 4")
H5("Heading 5")
H6("Heading 6")
Expand All @@ -49,9 +52,9 @@ struct CatalogSiteTests {
<div class="container">
Hello
<br />world!
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h1 class="text-start">Heading 1</h1>
<h2 class="text-center">Heading 2</h2>
<h3 class="text-end">Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
Expand Down
12 changes: 12 additions & 0 deletions Tests/SlipstreamTests/Views/TailwindCSS/TextAlignmentTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Testing

import Slipstream

struct TextAlignmentTests {
@Test func alignments() throws {
try #expect(renderHTML(Div {}.textAlignment(.leading)) == #"<div class="text-start"></div>"#)
try #expect(renderHTML(Div {}.textAlignment(.center)) == #"<div class="text-center"></div>"#)
try #expect(renderHTML(Div {}.textAlignment(.trailing)) == #"<div class="text-end"></div>"#)
try #expect(renderHTML(Div {}.textAlignment(.justify)) == #"<div class="text-justify"></div>"#)
}
}

0 comments on commit f4d4d20

Please sign in to comment.