Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Created November 2, 2021 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sindresorhus/4e817cdac3b1f1ac7502b697e7b0d752 to your computer and use it in GitHub Desktop.
Save sindresorhus/4e817cdac3b1f1ac7502b697e7b0d752 to your computer and use it in GitHub Desktop.
Nicer `Shape` access in SwiftUI
/**
Before:
```
.clipShape(Rectangle())
```
After:
```
.clipShape(.rectangle)
```
*/
extension Shape where Self == Rectangle {
static var rectangle: Self { .init() }
}
extension Shape where Self == Circle {
static var circle: Self { .init() }
}
extension Shape where Self == Capsule {
static var capsule: Self { .init() }
}
extension Shape where Self == Ellipse {
static var ellipse: Self { .init() }
}
extension Shape where Self == ContainerRelativeShape {
static var containerRelative: Self { .init() }
}
extension Shape where Self == RoundedRectangle {
static func roundedRectangle(cornerRadius: Double, style: RoundedCornerStyle = .circular) -> Self {
.init(cornerRadius: cornerRadius, style: style)
}
static func roundedRectangle(cornerSize: CGSize, style: RoundedCornerStyle = .circular) -> Self {
.init(cornerSize: cornerSize, style: style)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment