TechnologiesSwiftUIDocuments

NewDocumentButtonDataSource struct

iOSmacOStvOSwatchOSvisionOS✓ renders

Describes the source of data used to create a new document.

How it works

NewDocumentButtonDataSource describes the set of document options that a new-document button can offer when someone starts a fresh document. In a DocumentGroup scene, the system presents a button to create a new document; this type is the model behind that affordance, supplying the choices — document types, templates, or starting points — that the button surfaces. Reach for it when a single document-based app exposes more than one kind of new document and you want the system's standard new-document button to present those options rather than wiring up your own picker.

  1. Supply options to the system's new-document button

    Rather than building a custom creation flow, you describe the available starting points through NewDocumentButtonDataSource and let the document scene render the standard button. In the example, the visible Button with its Label("Create", systemImage: "plus") stands in for that system-provided control — inside a DocumentGroup scene the equivalent action is fed by the data source instead of an empty closure.

  2. Populate it within a DocumentGroup scene

    The data source is meaningful in the context of a document-based app, where a DocumentGroup owns the new-document lifecycle. The button shown here is decorated with doc.badge.plus and the headline Text("New Document") to evoke that role, but the actual options — the templates or document types the button can create — come from the NewDocumentButtonDataSource the scene consults.

  3. Drive the create action from the data source

    Where the example's Button closure is left empty with a comment, a real DocumentGroup routes the create action through the data source, so selecting an option opens a new document of that kind. The .buttonStyle(.borderedProminent) styling is incidental presentation; what NewDocumentButtonDataSource contributes is the meaning behind the tap — which document gets created.

  4. Keep the surrounding view as the host

    The VStack and its padding() here are only a stage for the control. NewDocumentButtonDataSource does not lay out or style anything itself; it plugs into the document scene's button and answers the question of what new documents are available, leaving the visual chrome to the views around it.

Try it — Replace the empty closure body in the Button { } with a placeholder that toggles between two document kinds, mirroring how a NewDocumentButtonDataSource would offer multiple new-document options for the same button.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

NewDocumentButtonDataSource.swift
struct NewDocumentButtonDataSourceDemo: View {
    var body: some View {
        VStack(spacing: 16) {
            Image(systemName: "doc.badge.plus")
                .font(.largeTitle)
                .foregroundStyle(.tint)
            Text("New Document")
                .font(.headline)
            Button {
                // In a DocumentGroup scene this is driven by a
                // NewDocumentButtonDataSource supplying document options.
            } label: {
                Label("Create", systemImage: "plus")
            }
            .buttonStyle(.borderedProminent)
        }
        .padding()
    }
}
Live preview
New Document Create
New Document Create
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →