Skip to content

Commit

Permalink
Add fontSize modifier. (#58)
Browse files Browse the repository at this point in the history
Part of #51.
  • Loading branch information
jverkoey authored Aug 3, 2024
1 parent daf02b2 commit 8a7fc4d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Slipstream implementations of Tailwind CSS's classes.

### Typography

- ``View/fontSize(_:)``
- ``View/textAlignment(_:)``
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ```View/fontSize(_:)```

## Topics

- ``FontSize``
33 changes: 33 additions & 0 deletions Sources/Slipstream/TailwindCSS/Typography/View+fontSize.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// Constants that specify the size of the text.
///
/// - SeeAlso: Tailwind CSS' [`font-size`](https://tailwindcss.com/docs/font-size) documentation.
@available(iOS 17.0, macOS 14.0, *)
public enum FontSize: String {
case extraSmall = "xs"
case small = "sm"
/// The default document font size.
case base = "base"
case large = "lg"
case extraLarge = "xl"
case extraExtraLarge = "2xl"
case extraExtraExtraLarge = "3xl"
case fourXLarge = "4xl"
case fiveXLarge = "5xl"
case sixXLarge = "6xl"
case sevenXLarge = "7xl"
case eightXLarge = "8xl"
case nineXLarge = "9xl"
}

extension View {
/// Set the font size.
///
/// - Parameter fontSize: The font size to apply to the modified view.
///
/// - SeeAlso: Tailwind CSS' [`font-size`](https://tailwindcss.com/docs/font-size) documentation.
@available(iOS 17.0, macOS 14.0, *)
public func fontSize(_ fontSize: FontSize) -> some View {
return self.modifier(ClassModifier(add: "text-" + fontSize.rawValue))
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// Constants that specify the visual alignment of the text.
@available(iOS 17.0, macOS 14.0, *)
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
Expand All @@ -21,6 +22,9 @@ extension View {
/// Control the alignment of text.
///
/// - Parameter alignment: Text alignment to be applied to text within the modified view.
///
/// - SeeAlso: Tailwind CSS' [`text-align`](https://tailwindcss.com/docs/text-align) documentation.
@available(iOS 17.0, macOS 14.0, *)
public func textAlignment(_ alignment: TextAlignment) -> some View {
return self.modifier(ClassModifier(add: "text-" + alignment.rawValue))
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/SlipstreamTests/Sites/CatalogSiteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private struct CatalogSite: View {
Linebreak()
Text("world!")
H1("Heading 1")
.fontSize(.extraLarge)
.textAlignment(.leading)
H2 {
Text("Heading 2")
Expand Down Expand Up @@ -52,7 +53,7 @@ struct CatalogSiteTests {
<div class="container">
Hello
<br />world!
<h1 class="text-start">Heading 1</h1>
<h1 class="text-xl text-start">Heading 1</h1>
<h2 class="text-center">Heading 2</h2>
<h3 class="text-end">Heading 3</h3>
<h4>Heading 4</h4>
Expand Down
24 changes: 24 additions & 0 deletions Tests/SlipstreamTests/TailwindCSS/FontSizeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Testing

import Slipstream

struct FontSizeTests {
@Test func alignments() throws {
try #expect(renderHTML(Div {}.fontSize(.extraSmall)) == #"<div class="text-xs"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.small)) == #"<div class="text-sm"></div>"#)

try #expect(renderHTML(Div {}.fontSize(.extraSmall)) == #"<div class="text-xs"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.small)) == #"<div class="text-sm"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.base)) == #"<div class="text-base"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.large)) == #"<div class="text-lg"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.extraLarge)) == #"<div class="text-xl"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.extraExtraLarge)) == #"<div class="text-2xl"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.extraExtraExtraLarge)) == #"<div class="text-3xl"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.fourXLarge)) == #"<div class="text-4xl"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.fiveXLarge)) == #"<div class="text-5xl"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.sixXLarge)) == #"<div class="text-6xl"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.sevenXLarge)) == #"<div class="text-7xl"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.eightXLarge)) == #"<div class="text-8xl"></div>"#)
try #expect(renderHTML(Div {}.fontSize(.nineXLarge)) == #"<div class="text-9xl"></div>"#)
}
}

0 comments on commit 8a7fc4d

Please sign in to comment.