Skip to content

Commit d8fba63

Browse files
committed
Add ring modifier.
Part of #51.
1 parent d5b0cb1 commit d8fba63

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

Sources/Slipstream/Documentation.docc/Guides/SlipstreamForTailwindCSSDevelopers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ Tailwind utility | Slipstream modifier
160160
[Outline Color](https://tailwindcss.com/docs/outline-color) | ``View/outline(_:width:condition:)``, ``View/outline(_:width:style:condition:)``
161161
[Outline Style](https://tailwindcss.com/docs/outline-style) | ``View/outline(_:width:style:condition:)``
162162
[Outline Offset](https://tailwindcss.com/docs/outline-offset) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/51)
163-
[Ring Width](https://tailwindcss.com/docs/ring-width) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/51)
164-
[Ring Color](https://tailwindcss.com/docs/ring-color) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/51)
163+
[Ring Width](https://tailwindcss.com/docs/ring-width) | ``View/ring(_:width:condition:)``
164+
[Ring Color](https://tailwindcss.com/docs/ring-color) | ``View/ring(_:width:condition:)``
165165
[Ring Offset Width](https://tailwindcss.com/docs/ring-offset-width) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/51)
166166
[Ring Offset Color](https://tailwindcss.com/docs/ring-offset-color) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/51)
167167

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Ring
2+
3+
Utilities for creating outline rings with CSS box-shadows.
4+
5+
## Topics
6+
7+
### Modifiers
8+
9+
- ``View/ring(_:width:condition:)``

Sources/Slipstream/Documentation.docc/TailwindCSS/TailwindCSS-Utilities.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Slipstream implementations of Tailwind CSS's utility classes.
5151
- <doc:Borders-Border>
5252
- <doc:Borders-CornerRadius>
5353
- <doc:Borders-Outline>
54+
- <doc:Borders-Ring>
5455

5556
### Effects
5657

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
extension View {
2+
/// Changes the ring of the view using a CSS box-shadow.
3+
///
4+
/// - SeeAlso: Tailwind CSS' [ring width](https://tailwindcss.com/docs/ring-width) documentation.
5+
/// - SeeAlso: Tailwind CSS' [ring color](https://tailwindcss.com/docs/ring-color) documentation.
6+
@available(iOS 17.0, macOS 14.0, *)
7+
public func ring(_ color: Color? = nil, width: Int = 1, condition: Condition? = nil) -> some View {
8+
var classNames: [String] = [
9+
"ring" + closestTailwindRingWidth(width: width)
10+
]
11+
if let color {
12+
classNames.append("ring-" + color.toTailwindColorClass())
13+
}
14+
return modifier(TailwindClassModifier(add: Set(classNames), condition: condition))
15+
}
16+
17+
private func closestTailwindRingWidth(width: Int) -> String {
18+
let mapping: [(classSuffix: String, width: Int)] = [
19+
("-0", 0),
20+
("-1", 1),
21+
("-2", 2),
22+
("", 3),
23+
("-4", 4),
24+
("-8", 8),
25+
]
26+
let closest = mapping.min { abs($0.width - width) < abs($1.width - width) }
27+
return closest?.classSuffix ?? ""
28+
}
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Testing
2+
import Slipstream
3+
4+
struct RingTests {
5+
@Test func colors() throws {
6+
try #expect(renderHTML(Div {}.ring(.black)) == #"<div class="ring-1 ring-black"></div>"#)
7+
try #expect(renderHTML(Div {}.ring(.white.opacity(0.5))) == #"<div class="ring-1 ring-white/50"></div>"#)
8+
}
9+
10+
@Test func widths() throws {
11+
try #expect(renderHTML(Div {}.ring(width: 0)) == #"<div class="ring-0"></div>"#)
12+
try #expect(renderHTML(Div {}.ring(width: 1)) == #"<div class="ring-1"></div>"#)
13+
try #expect(renderHTML(Div {}.ring(width: 2)) == #"<div class="ring-2"></div>"#)
14+
try #expect(renderHTML(Div {}.ring(width: 3)) == #"<div class="ring"></div>"#)
15+
try #expect(renderHTML(Div {}.ring(width: 4)) == #"<div class="ring-4"></div>"#)
16+
try #expect(renderHTML(Div {}.ring(width: 5)) == #"<div class="ring-4"></div>"#)
17+
try #expect(renderHTML(Div {}.ring(width: 6)) == #"<div class="ring-4"></div>"#)
18+
try #expect(renderHTML(Div {}.ring(width: 7)) == #"<div class="ring-8"></div>"#)
19+
try #expect(renderHTML(Div {}.ring(width: 8)) == #"<div class="ring-8"></div>"#)
20+
try #expect(renderHTML(Div {}.ring(width: 9)) == #"<div class="ring-8"></div>"#)
21+
try #expect(renderHTML(Div {}.ring(width: 100)) == #"<div class="ring-8"></div>"#)
22+
}
23+
}

0 commit comments

Comments
 (0)