Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add textAlignment modifier. #55

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>"#)
}
}