-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #51.
- Loading branch information
Showing
4 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
Sources/Slipstream/Documentation.docc/TailwindCSS/Effects/Effects-Shadow.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:)`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |