Skip to content

Commit

Permalink
Merge branch 'dismissOnBackgroundTap'
Browse files Browse the repository at this point in the history
  • Loading branch information
swiftfulthinking-llc committed May 12, 2024
2 parents 0d8b953 + b591750 commit c9c536b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
11 changes: 8 additions & 3 deletions Sources/SwiftfulRouting/Components/ModalSupportView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct AnyModalWithDestination: Identifiable, Equatable {
animation: .default,
alignment: .center,
backgroundColor: nil,
dismissOnBackgroundTap: true,
ignoreSafeArea: true
),
destination: AnyDestination(EmptyView())
Expand Down Expand Up @@ -62,9 +63,13 @@ struct ModalSupportView: View {
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
.transition(AnyTransition.opacity.animation(.easeInOut))
.onTapGesture {
onDismissModal(data)
}
// Only add backgound tap gesture if needed
.ifSatisfiesCondition(data.configuration.dismissOnBackgroundTap, transform: { content in
content
.onTapGesture {
onDismissModal(data)
}
})
.zIndex(dataIndex + 1)
} else {
EmptyView()
Expand Down
5 changes: 3 additions & 2 deletions Sources/SwiftfulRouting/Core/AnyRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ public struct AnyRouter: Router {
animation: Animation = .smooth,
alignment: Alignment = .center,
backgroundColor: Color? = nil,
dismissOnBackgroundTap: Bool = true,
ignoreSafeArea: Bool = true,
@ViewBuilder destination: @escaping () -> T) where T : View {
object.showModal(id: id, transition: transition, animation: animation, alignment: alignment, backgroundColor: backgroundColor, ignoreSafeArea: ignoreSafeArea, destination: destination)
object.showModal(id: id, transition: transition, animation: animation, alignment: alignment, backgroundColor: backgroundColor, dismissOnBackgroundTap: dismissOnBackgroundTap, ignoreSafeArea: ignoreSafeArea, destination: destination)
}

/// Convenience method for a simple modal appearing over the current Environment in the center of the screen.
Expand Down Expand Up @@ -196,7 +197,7 @@ struct MockRouter: Router {
printError()
}

func showModal<V>(id: String? = nil, transition: AnyTransition, animation: Animation, alignment: Alignment, backgroundColor: Color?, ignoreSafeArea: Bool, destination: @escaping () -> V) where V : View {
func showModal<V>(id: String? = nil, transition: AnyTransition, animation: Animation, alignment: Alignment, backgroundColor: Color?, dismissOnBackgroundTap: Bool, ignoreSafeArea: Bool, destination: @escaping () -> V) where V : View {
printError()
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftfulRouting/Core/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public protocol Router {

func dismissAlert()

func showModal<V:View>(id: String?, transition: AnyTransition, animation: Animation, alignment: Alignment, backgroundColor: Color?, ignoreSafeArea: Bool, @ViewBuilder destination: @escaping () -> V)
func showModal<V:View>(id: String?, transition: AnyTransition, animation: Animation, alignment: Alignment, backgroundColor: Color?, dismissOnBackgroundTap: Bool, ignoreSafeArea: Bool, @ViewBuilder destination: @escaping () -> V)
func dismissModal(id: String?)
func dismissAllModals()

Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftfulRouting/Core/RouterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,11 @@ extension RouterViewInternal {
animation: Animation,
alignment: Alignment,
backgroundColor: Color?,
dismissOnBackgroundTap: Bool,
ignoreSafeArea: Bool,
@ViewBuilder destination: @escaping () -> T) {

let config = ModalConfiguration(transition: transition, animation: animation, alignment: alignment, backgroundColor: backgroundColor, ignoreSafeArea: ignoreSafeArea)
let config = ModalConfiguration(transition: transition, animation: animation, alignment: alignment, backgroundColor: backgroundColor, dismissOnBackgroundTap: dismissOnBackgroundTap, ignoreSafeArea: ignoreSafeArea)
let dest = AnyDestination(destination())

self.modals.append(AnyModalWithDestination(id: id ?? UUID().uuidString, configuration: config, destination: dest))
Expand Down
21 changes: 21 additions & 0 deletions Sources/SwiftfulRouting/Extensions/View+EXT.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// File.swift
//
//
// Created by Nick Sarno on 5/12/24.
//

import Foundation
import SwiftUI

extension View {

@ViewBuilder func ifSatisfiesCondition<Content: View>(_ condition: Bool, transform: @escaping (Self) -> Content) -> some View {
if condition {
transform(self)
} else {
self
}
}

}
2 changes: 2 additions & 0 deletions Sources/SwiftfulRouting/Options/ModalConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public struct ModalConfiguration {
let animation: Animation
let alignment: Alignment
let backgroundColor: Color?
let dismissOnBackgroundTap: Bool
let ignoreSafeArea: Bool

static let `default` = ModalConfiguration(
transition: .move(edge: .bottom),
animation: .easeInOut,
alignment: .bottom,
backgroundColor: nil,
dismissOnBackgroundTap: true,
ignoreSafeArea: true
)
}

0 comments on commit c9c536b

Please sign in to comment.