Skip to content

Commit

Permalink
Create PopupView only when it's presented.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-lyn committed Dec 11, 2021
1 parent fd524c7 commit 65388ab
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions STPopup/STPopup+SwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@ public typealias PopupStyle = STPopupStyle

@available (iOS 13.0, *)
public extension View {
func popup<ContentView>(
@ViewBuilder func popup<ContentView>(
isPresented: Binding<Bool>,
style: PopupStyle = .formSheet,
onDismiss: (() -> Void)? = nil,
@ViewBuilder contentView: @escaping () -> ContentView
) -> some View where ContentView: View {
overlay(
// We don't need a binding for now. It's exposed as `Binding` so that we can change internal implementation
// without affecting public API.
PopupView(isPresented: isPresented.wrappedValue, style: style, onDismiss: onDismiss, contentView: contentView())
.frame(
width: isPresented.wrappedValue ? Self.windowSize.width : 0,
height: isPresented.wrappedValue ? Self.windowSize.height : 0
)
)
if isPresented.wrappedValue {
overlay(
PopupView(isPresented: isPresented, style: style, onDismiss: onDismiss, contentView: contentView())
.frame(
width: isPresented.wrappedValue ? Self.windowSize.width : 0,
height: isPresented.wrappedValue ? Self.windowSize.height : 0
)
)
} else {
self
}
}

// Window size is needed to properly layout content view when `sizeThatFits` is called.
private static var windowSize: CGSize {
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
Expand All @@ -44,7 +46,7 @@ public extension View {

@available (iOS 13.0, *)
fileprivate struct PopupView<ContentView>: UIViewControllerRepresentable where ContentView: View {
let isPresented: Bool
@Binding var isPresented: Bool
let style: PopupStyle
let onDismiss: (() -> Void)?
let contentView: ContentView
Expand Down

0 comments on commit 65388ab

Please sign in to comment.