Skip to content

Popup Presentation

Tomasz K. edited this page Nov 17, 2024 · 40 revisions

Dziś musi być czwartek. Nigdy nie mogłem się połapać, o co chodzi w czwartki.

Popup Presentation

Overview

Presenting a popup is as simple as using the .present() method with your defined Popup structure (see Popup Declaration page for the reference). Furthermore, there are several other methods available that can be used to better meet your specific requirements.

Available Methods

  • func present(popupStackID: PopupStackID = .shared) async
    • Displays the popup in the application window identified by popupStackID (see Setup page for the reference).
  • func setCustomID(_ id: String) async -> some Popup
    • Optional
    • Assigns a custom identifier to a popup, allowing for more precise control over individual popups, especially useful if you need to manage multiple popups of the same type.
  • func setEnvironmentObject<T: ObservableObject>(_ object: T) async -> some Popup
    • Optional
    • Supplies an observable object to a popup's view hierarchy. This allows the popup to access and observe changes in the provided object.
  • func dismissAfter(_ seconds: Double) async -> some Popup
    • Optional
    • Automatically dismisses the popup after a specified period of time.

Step-by-Step Guide

  1. Declare a popup. See Popup Declaration page for the reference

  2. Call the present() method on an instance of your custom popup view to display it

    class SettingsViewModel: ObservableObject {
        func saveSettings() {
            (...)
            Task {
                await BottomCustomPopup().present()
            }
        }
    }
  3. (Optional) The present() method can also be called with a custom popupStackID. To do so, first register the popupStackID during the setup and then call the present() method with the registered ID

    @main struct MyApp: App {
        var body: some Scene {
            WindowGroup {
                ContentView().registerPopups(id: .custom1)
            }
        }
    }
    
    extension PopupStackID {
        static let custom1: Self = .init(rawValue: "custom1")
    }
    
    class SettingsViewModel: ObservableObject {
        func saveSettings() {
            (...)
            Task {
                await BottomCustomPopup().present(popupStackID: .custom1)
            }
        }
    }
  4. (Optional) Call setCustomID() method

    class SettingsViewModel: ObservableObject {
        func saveSettings() {
            (...)
            Task {
                await BottomCustomPopup()
                    .setCustomID("customID")
                    .present()
            }
        }
    }
  5. (Optional) Call setEnvironmentObject() method

    struct ContentView: View {
        @StateObject var myObject = MyObservableObject()
    
        var body: some View {
            Button("Show Popup") {
                Task {
                    await BottomCustomPopup()
                        .setEnvironmentObject(myObject)
                        .present(popupStackID: .custom1)
                }
            }
        }
    }
  6. (Optional) Call dismissAfter() method

    class SettingsViewModel: ObservableObject {
        func saveSettings() {
            (...)
            Task {
                await BottomCustomPopup()
                    .setCustomID("customID")
                    .dismissAfter(5)
                    .present()
            }
        }
    }

See also