Skip to content

Commit

Permalink
Add custom alert shadow
Browse files Browse the repository at this point in the history
  • Loading branch information
divadretlaw committed Nov 16, 2024
1 parent d9fb304 commit f1385f9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ extension CustomAlertConfiguration {
public var spacing: CGFloat
/// The alignment of the content of the alert view
public var alignment: CustomAlertAlignment
/// Optional shadow applied to the alert
public var shadow: CustomAlertShadow?

init() {
self.background = .blurEffect(.systemMaterial)
Expand All @@ -43,6 +45,7 @@ extension CustomAlertConfiguration {
self.contentFont = .footnote
self.spacing = 4
self.alignment = .center
self.shadow = nil
}

/// Create a custom configuration
Expand Down
36 changes: 36 additions & 0 deletions Sources/CustomAlert/Configuration/CustomAlertShadow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// CustomAlertShadow.swift
// CustomAlert
//
// Created by David Walter on 16.11.24.
//

import SwiftUI

/// Shadow configuration of the alert
public struct CustomAlertShadow: Sendable {
let color: Color
let radius: CGFloat
let x: CGFloat
let y: CGFloat

/// Create a custom alert shadow
///
/// - Parameters:
/// - color: The shadow's color.
/// - radius: A measure of how much to blur the shadow. Larger values
/// result in more blur.
/// - x: An amount to offset the shadow horizontally from the view.
/// - y: An amount to offset the shadow vertically from the view.
public init(
color: Color = Color(.sRGBLinear, white: 0, opacity: 0.33),
radius: CGFloat,
x: CGFloat = 0,
y: CGFloat = 0
) {
self.color = color
self.radius = radius
self.x = x
self.y = y
}
}
26 changes: 26 additions & 0 deletions Sources/CustomAlert/Helper/ShadowApplier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// ShadowApplier.swift
// CustomAlert
//
// Created by David Walter on 16.11.24.
//

import SwiftUI

extension View {
func shadow(_ shadow: CustomAlertShadow?) -> some View {
modifier(ShadowApplier(shadow: shadow))
}
}

private struct ShadowApplier: ViewModifier {
let shadow: CustomAlertShadow?

func body(content: Content) -> some View {
if let shadow {
content.shadow(color: shadow.color, radius: shadow.radius, x: shadow.x, y: shadow.y)
} else {
content
}
}
}
1 change: 1 addition & 0 deletions Sources/CustomAlert/Views/CustomAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ import SwiftUI
.frame(minWidth: minWidth, maxWidth: maxWidth)
.background(BackgroundView(background: configuration.alert.background))
.cornerRadius(configuration.alert.cornerRadius)
.shadow(configuration.alert.shadow)
.padding(configuration.padding)
.transition(configuration.transition)
.animation(.default, value: isPresented)
Expand Down

0 comments on commit f1385f9

Please sign in to comment.