Skip to content

Commit

Permalink
Add shadow modifier.
Browse files Browse the repository at this point in the history
Part of #51.
  • Loading branch information
jverkoey committed Aug 10, 2024
1 parent 42b0de5 commit 41d84d8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ Tailwind utility | Slipstream modifier

Tailwind utility | Slipstream modifier
:----------------|:-------------------
[Box Shadow](https://tailwindcss.com/docs/box-shadow) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/51)
[Box Shadow Color](https://tailwindcss.com/docs/box-shadow-color) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/51)
[Box Shadow](https://tailwindcss.com/docs/box-shadow) | ``View/shadow(color:radius:condition:)``
[Box Shadow Color](https://tailwindcss.com/docs/box-shadow-color) | ``View/shadow(color:radius:condition:)
[Opacity](https://tailwindcss.com/docs/opacity) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/51)
[Mix Blend Mode](https://tailwindcss.com/docs/mix-blend-mode) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/51)
[Background Blend Mode](https://tailwindcss.com/docs/background-blend-mode) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/51)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Shadow

Utilities for controlling the box shadow of a view.

## Topics

### Modifiers

- ``View/shadow(color:radius:condition:)``
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Slipstream implementations of Tailwind CSS's utility classes.
- <doc:Borders-Border>
- <doc:Borders-CornerRadius>

### Effects

- <doc:Effects-Shadow>

### Filters

- <doc:Filters-BackdropBlur>
Expand Down
38 changes: 38 additions & 0 deletions Sources/Slipstream/TailwindCSS/Effects/View+shadow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
extension View {
/// Sets the shadow of the view.
///
/// - SeeAlso: Tailwind CSS' [box shadow](https://tailwindcss.com/docs/box-shadow) documentation.
/// - SeeAlso: Tailwind CSS' [box shadow color](https://tailwindcss.com/docs/box-shadow-color) documentation.
@available(iOS 17.0, macOS 14.0, *)
public func shadow(
color: Color? = nil,
radius: Double? = nil,
condition: Condition? = nil
) -> some View {
var classNames: [String] = []
if let color {
classNames.append("shadow-\(color.toTailwindColorClass())")
}
if let radius {
classNames.append("shadow" + Self.radiusToTailwindShadowClass(radius: radius))
}
return modifier(TailwindClassModifier(add: Set(classNames), condition: condition))
}

/// Maps a shadow radius to the closest Tailwind CSS shadow class.
///
/// - Parameter radius: The radius in points to be mapped.
/// - Returns: The Tailwind CSS shadow class.
private static func radiusToTailwindShadowClass(radius: Double) -> String {
let mapping: [(name: String, radius: Double)] = [
("-sm", 2),
("", 3),
("-md", 6),
("-lg", 15),
("-xl", 25),
("-2xl", 50),
]
let closestClass = mapping.min { abs($0.radius - radius) < abs($1.radius - radius) }
return closestClass?.name ?? "0"
}
}

0 comments on commit 41d84d8

Please sign in to comment.