Skip to content

Commit 96e9506

Browse files
Mark Pospeselkarthikyml
authored andcommitted
Add extent property to track how big shadows get
1 parent 2f749a2 commit 96e9506

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Sources/YCoreUI/Components/Elevation.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,21 @@ public struct Elevation {
8080
self.opacity = max(min(opacity, 1), 0)
8181
self.useShadowPath = useShadowPath
8282
}
83-
83+
84+
/// Computes how far from the edges of the view the shadow will extend in each direction.
85+
///
86+
/// This is determined by offset, blur, and spread, but not by opacity (unless zero).
87+
/// The rectangle enclosing the shadow should be the view's frame outset by `extent`.
88+
public var extent: UIEdgeInsets {
89+
guard opacity > 0 else { return .zero }
90+
91+
return UIEdgeInsets(
92+
top: blur + spread - yOffset,
93+
left: blur + spread - xOffset,
94+
bottom: blur + spread + yOffset,
95+
right: blur + spread + xOffset
96+
)
97+
}
8498
/// Applies elevation to a layer.
8599
///
86100
/// In most cases `cornerRadius` should match `layer.cornerRadius`.

Tests/YCoreUITests/Components/ElevationTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ final class ElevationTests: XCTestCase {
5353
XCTAssertEqual(sut.opacity, 1)
5454
}
5555

56+
func test_extent_isZeroWhenOpacityIsZero() {
57+
let sut = makeSUT(opacity: 0)
58+
59+
XCTAssertEqual(sut.extent, .zero)
60+
}
61+
62+
func test_extent_dependsOnOffsetBlurAndSpread() {
63+
let sut = makeSUT()
64+
65+
XCTAssertEqual(sut.extent.top, 2 + 3 - 1)
66+
XCTAssertEqual(sut.extent.left, 2 + 3 - 1)
67+
XCTAssertEqual(sut.extent.bottom, 2 + 3 + 1)
68+
XCTAssertEqual(sut.extent.right, 2 + 3 + 1)
69+
}
70+
5671
func test_apply_doesNotSetShadowPathWhenUseShadowPathIsFalse() {
5772
let sut = makeSUT(useShadowPath: false)
5873
let layer = makeLayer()

0 commit comments

Comments
 (0)