Contents

WebPage.BackForwardList

An observable representation of a webpage’s previously loaded resources.

Declaration

@MainActor struct BackForwardList

Overview

This type can be used to facilitate navigating to prior or subsequent loaded resources and for observing when new entries get added or removed.

In this example, the back-forward list is used to create a SwiftUI View to facilitate navigating to previous or next items:

private struct BackForwardMenuView: View {
    struct LabelConfiguration {
        let text: String
        let systemImage: String
    }

    let list: [WebPage.BackForwardList.Item]
    let label: LabelConfiguration
    let navigateToItem: (WebPage.BackForwardList.Item) -> Void

    var body: some View {
        Menu {
            ForEach(list) { item in
                Button(item.title ?? item.url.absoluteString) {
                    navigateToItem(item)
                }
            }
        } label: {
            Label(label.text, systemImage: label.systemImage)
                .labelStyle(.iconOnly)
        } primaryAction: {
            navigateToItem(list.first!)
        }
        .disabled(list.isEmpty)
    }
}

The view can then be used for both the back and forward list using a specific WebPage:

struct ContentView: some View {
    @State private var page = WebPage()

    var body: some View {
        WebView(page)
            .toolbar {
                ToolbarItemGroup {
                    ToolbarBackForwardMenuView(
                        list: page.backForwardList.backList.reversed(),
                        label: .init(text: "Backward", systemImage: "chevron.backward")
                    ) {
                        viewModel.page.load($0)
                    }

                    ToolbarBackForwardMenuView(
                        list: page.backForwardList.forwardList,
                        label: .init(text: "Forward", systemImage: "chevron.forward")
                    ) {
                        viewModel.page.load($0)
                    }
                }
            }
    }
}

Because backForwardList is an observable property, the states of these buttons are automatically updated.

Topics

Structures

Instance Properties

Subscripts

See Also

Observing navigation between webpages