diff --git a/Sources/Slipstream/TailwindCSS/Edge.swift b/Sources/Slipstream/TailwindCSS/Edge.swift index 602b47d..2018948 100644 --- a/Sources/Slipstream/TailwindCSS/Edge.swift +++ b/Sources/Slipstream/TailwindCSS/Edge.swift @@ -74,6 +74,8 @@ public enum Edge: Int8, CaseIterable { /// - Parameter size: The size in points to be mapped. /// - Returns: The Tailwind CSS spacing class. static func pointToTailwindSpacingClass(ptLength: Double) -> String { + let ptLength = abs(ptLength) + // Tailwind spacing classes and their corresponding sizes in points. let mapping: [(name: String, ptLength: Double)] = [ ("0", 0), // 0pt diff --git a/Sources/Slipstream/TailwindCSS/Spacing/View+margin.swift b/Sources/Slipstream/TailwindCSS/Spacing/View+margin.swift index 0c37a8c..3eb1e6d 100644 --- a/Sources/Slipstream/TailwindCSS/Spacing/View+margin.swift +++ b/Sources/Slipstream/TailwindCSS/Spacing/View+margin.swift @@ -23,6 +23,15 @@ public struct MarginValue { } fileprivate let storage: Storage + var isNegative: Bool { + switch storage { + case .rawValue(let value): + return value < 0 + case .auto: + return false + } + } + /// Returns the margin value as the closest Tailwind CSS spacing class. /// /// - Returns: The Tailwind CSS spacing class string. @@ -75,6 +84,9 @@ extension View { } } } + if length.isNegative { + classes = classes.map { "-" + $0 } + } return modifier(TailwindClassModifier(add: classes.joined(separator: " "), condition: condition)) } diff --git a/Tests/SlipstreamTests/TailwindCSS/Spacing/MarginTests.swift b/Tests/SlipstreamTests/TailwindCSS/Spacing/MarginTests.swift index ddbd655..a7882a0 100644 --- a/Tests/SlipstreamTests/TailwindCSS/Spacing/MarginTests.swift +++ b/Tests/SlipstreamTests/TailwindCSS/Spacing/MarginTests.swift @@ -52,6 +52,19 @@ struct MarginTests { try #expect(renderHTML(Div {}.margin(.top, 0).margin(.top, 4)) == #"
"#) } + @Test func negativeMargins() throws { + try #expect(renderHTML(Div {}.margin(-8)) == #""#) + try #expect(renderHTML(Div {}.margin(.all, -16)) == #""#) + try #expect(renderHTML(Div {}.margin(.horizontal, -8)) == #""#) + try #expect(renderHTML(Div {}.margin(.vertical, -12)) == #""#) + try #expect(renderHTML(Div {}.margin([.top, .left], -24)) == #""#) + + try #expect(renderHTML(Div {}.margin(.top, -0)) == #""#) + try #expect(renderHTML(Div {}.margin(.left, -4)) == #""#) + try #expect(renderHTML(Div {}.margin(.bottom, -32)) == #""#) + try #expect(renderHTML(Div {}.margin(.right, -64)) == #""#) + } + @Test func condition() throws { try #expect(renderHTML(Div {}.margin(8, condition: .init(startingAt: .large))) == #""#) try #expect(renderHTML(Div {}.margin(8, condition: .within(Breakpoint.small..