From b172e06729c4d27042ca28c3f65f42ea5acca358 Mon Sep 17 00:00:00 2001 From: Jeff Verkoeyen Date: Sat, 3 Aug 2024 14:33:55 -0400 Subject: [PATCH] Add fontWeight and bold modifiers. (#61) Part of https://github.com/jverkoey/slipstream/issues/51. --- .../Views/TailwindCSS/FontSize.md | 12 ++++ .../Views/TailwindCSS/FontWeight.md | 13 ++++ .../Views/TailwindCSS/TailwindCSSClasses.md | 5 +- .../Views/TailwindCSS/TextAlignment.md | 11 +++ .../Views/TailwindCSS/fontSize.md | 5 -- .../Views/TailwindCSS/textAlignment.md | 5 -- .../Typography/View+fontWeight.swift | 64 +++++++++++++++++ .../Sites/CatalogSiteTests.swift | 3 +- .../TailwindCSS/FontSizeTests.swift | 68 +++++++++---------- .../TailwindCSS/FontWeightTests.swift | 7 ++ 10 files changed, 143 insertions(+), 50 deletions(-) create mode 100644 Sources/Slipstream/Documentation.docc/Views/TailwindCSS/FontSize.md create mode 100644 Sources/Slipstream/Documentation.docc/Views/TailwindCSS/FontWeight.md create mode 100644 Sources/Slipstream/Documentation.docc/Views/TailwindCSS/TextAlignment.md delete mode 100644 Sources/Slipstream/Documentation.docc/Views/TailwindCSS/fontSize.md delete mode 100644 Sources/Slipstream/Documentation.docc/Views/TailwindCSS/textAlignment.md create mode 100644 Sources/Slipstream/TailwindCSS/Typography/View+fontWeight.swift create mode 100644 Tests/SlipstreamTests/TailwindCSS/FontWeightTests.swift diff --git a/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/FontSize.md b/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/FontSize.md new file mode 100644 index 0000000..ac03ae3 --- /dev/null +++ b/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/FontSize.md @@ -0,0 +1,12 @@ +# Font size + +## Topics + +### Modifiers + +- ``View/fontSize(_:)-6cmd6`` +- ``View/fontSize(_:)-6mwyi`` + +### Enumerations + +- ``FontSize`` diff --git a/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/FontWeight.md b/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/FontWeight.md new file mode 100644 index 0000000..fb1be9c --- /dev/null +++ b/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/FontWeight.md @@ -0,0 +1,13 @@ +# Font weight + +## Topics + +### Modifiers + +- ``View/fontWeight(_:)-9ehtx`` +- ``View/fontWeight(_:)-2g9rx`` +- ``View/bold()`` + +### Enumerations + +- ``FontWeight`` diff --git a/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/TailwindCSSClasses.md b/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/TailwindCSSClasses.md index 1295a73..e673656 100644 --- a/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/TailwindCSSClasses.md +++ b/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/TailwindCSSClasses.md @@ -10,5 +10,6 @@ Slipstream implementations of Tailwind CSS's classes. ### Typography -- ``View/fontSize(_:)`` -- ``View/textAlignment(_:)`` +- +- +- diff --git a/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/TextAlignment.md b/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/TextAlignment.md new file mode 100644 index 0000000..5d7e90b --- /dev/null +++ b/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/TextAlignment.md @@ -0,0 +1,11 @@ +# Text alignment + +## Topics + +### Modifiers + +- ``View/textAlignment(_:)`` + +### Enumerations + +- ``TextAlignment`` diff --git a/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/fontSize.md b/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/fontSize.md deleted file mode 100644 index 3a9ac7f..0000000 --- a/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/fontSize.md +++ /dev/null @@ -1,5 +0,0 @@ -# ```View/fontSize(_:)``` - -## Topics - -- ``FontSize`` diff --git a/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/textAlignment.md b/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/textAlignment.md deleted file mode 100644 index bcc5cdd..0000000 --- a/Sources/Slipstream/Documentation.docc/Views/TailwindCSS/textAlignment.md +++ /dev/null @@ -1,5 +0,0 @@ -# ```View/textAlignment(_:)``` - -## Topics - -- ``TextAlignment`` diff --git a/Sources/Slipstream/TailwindCSS/Typography/View+fontWeight.swift b/Sources/Slipstream/TailwindCSS/Typography/View+fontWeight.swift new file mode 100644 index 0000000..7e97235 --- /dev/null +++ b/Sources/Slipstream/TailwindCSS/Typography/View+fontWeight.swift @@ -0,0 +1,64 @@ +/// Constants that specify the weight of the text. +/// +/// - SeeAlso: Tailwind CSS' [`font-weight`](https://tailwindcss.com/docs/font-weight) documentation. +@available(iOS 17.0, macOS 14.0, *) +public enum FontWeight: String { + case thin + case extraLight = "extralight" + case light + case normal + case medium + case semiBold = "semibold" + case bold + case extraBold = "extrabold" + case black +} + +extension View { + /// Set the font weight. + /// + /// - Parameter fontWeight: The font weight to apply to the modified view. + /// + /// - SeeAlso: Tailwind CSS' [`font-weight`](https://tailwindcss.com/docs/font-weight) documentation. + @available(iOS 17.0, macOS 14.0, *) + public func fontWeight(_ fontWeight: FontWeight) -> some View { + return self.modifier(ClassModifier(add: "font-" + fontWeight.rawValue)) + } + + /// Set the font weight to the closest equivalent Tailwind CSS font weight. + /// + /// - Parameter weight: A font weight value. If the weight is exactly between + /// two weight classes, then the lighter weight will be used. + /// + /// - SeeAlso: Tailwind CSS' [`font-weight`](https://tailwindcss.com/docs/font-weight) documentation. + @available(iOS 17.0, macOS 14.0, *) + public func fontWeight(_ weight: Int) -> some View { + return fontWeight(closestTailwindFontWeight(weight: weight)) + } + + /// Set the font weight to bold. + /// + /// - SeeAlso: Tailwind CSS' [`font-weight`](https://tailwindcss.com/docs/font-weight) documentation. + @available(iOS 17.0, macOS 14.0, *) + public func bold() -> some View { + return fontWeight(.bold) + } + + private func closestTailwindFontWeight(weight: Int) -> FontWeight { + // Mapping of Tailwind CSS font weight classes to their weight values. + let fontWeightMapping: [(fontWeight: FontWeight, weight: Int)] = [ + (.thin, 100), // Thin + (.extraLight, 200), // Extra Light + (.light, 300), // Light + (.normal, 400), // Normal + (.medium, 500), // Medium + (.semiBold, 600), // Semi Bold + (.bold, 700), // Bold + (.extraBold, 800), // Extra Bold + (.black, 900) // Black + ] + // Find the closest matching font weight + let closestFontWeight = fontWeightMapping.min { abs($0.weight - weight) < abs($1.weight - weight) } + return closestFontWeight?.fontWeight ?? .normal + } +} diff --git a/Tests/SlipstreamTests/Sites/CatalogSiteTests.swift b/Tests/SlipstreamTests/Sites/CatalogSiteTests.swift index 11c6ba3..6d43635 100644 --- a/Tests/SlipstreamTests/Sites/CatalogSiteTests.swift +++ b/Tests/SlipstreamTests/Sites/CatalogSiteTests.swift @@ -21,6 +21,7 @@ private struct CatalogSite: View { Text("world!") H1("Heading 1") .fontSize(.extraLarge) + .bold() .textAlignment(.leading) H2 { Text("Heading 2") @@ -54,7 +55,7 @@ struct CatalogSiteTests {
Hello
world! -

Heading 1

+

Heading 1

Heading 2

Heading 3

Heading 4

diff --git a/Tests/SlipstreamTests/TailwindCSS/FontSizeTests.swift b/Tests/SlipstreamTests/TailwindCSS/FontSizeTests.swift index 927faca..b56f7eb 100644 --- a/Tests/SlipstreamTests/TailwindCSS/FontSizeTests.swift +++ b/Tests/SlipstreamTests/TailwindCSS/FontSizeTests.swift @@ -1,44 +1,38 @@ import Testing - import Slipstream -struct FontSizeTests { - @Test func enumFontSizes() throws { - try #expect(renderHTML(Div {}.fontSize(.extraSmall)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.small)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.base)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.large)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.extraLarge)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.extraExtraLarge)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.extraExtraExtraLarge)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.fourXLarge)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.fiveXLarge)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.sixXLarge)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.sevenXLarge)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.eightXLarge)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(.nineXLarge)) == #"
"#) +struct FontWeightTests { + @Test func enumFontWeights() throws { + try #expect(renderHTML(Div {}.fontWeight(.thin)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(.extraLight)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(.light)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(.normal)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(.medium)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(.semiBold)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(.bold)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(.extraBold)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(.black)) == #"
"#) } - @Test func numericalFontSizes() throws { - try #expect(renderHTML(Div {}.fontSize(6)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(12)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(13)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(14)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(15)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(16)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(17)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(18)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(19)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(20)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(22)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(24)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(30)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(36)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(48)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(60)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(72)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(96)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(128)) == #"
"#) - try #expect(renderHTML(Div {}.fontSize(200)) == #"
"#) + @Test func numericalFontWeights() throws { + try #expect(renderHTML(Div {}.fontWeight(50)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(100)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(150)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(200)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(250)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(300)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(350)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(400)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(450)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(500)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(550)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(600)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(650)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(700)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(750)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(800)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(850)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(900)) == #"
"#) + try #expect(renderHTML(Div {}.fontWeight(950)) == #"
"#) } } diff --git a/Tests/SlipstreamTests/TailwindCSS/FontWeightTests.swift b/Tests/SlipstreamTests/TailwindCSS/FontWeightTests.swift new file mode 100644 index 0000000..9627606 --- /dev/null +++ b/Tests/SlipstreamTests/TailwindCSS/FontWeightTests.swift @@ -0,0 +1,7 @@ +// +// FontWeightTests.swift +// slipstream +// +// Created by Jeff Verkoeyen on 8/3/24. +// +