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.
Supply options to the system's new-document button
Rather than building a custom creation flow, you describe the available starting points through
NewDocumentButtonDataSourceand let the document scene render the standard button. In the example, the visibleButtonwith itsLabel("Create", systemImage: "plus")stands in for that system-provided control — inside aDocumentGroupscene the equivalent action is fed by the data source instead of an empty closure.Populate it within a DocumentGroup scene
The data source is meaningful in the context of a document-based app, where a
DocumentGroupowns the new-document lifecycle. The button shown here is decorated withdoc.badge.plusand the headlineText("New Document")to evoke that role, but the actual options — the templates or document types the button can create — come from theNewDocumentButtonDataSourcethe scene consults.Drive the create action from the data source
Where the example's
Buttonclosure is left empty with a comment, a realDocumentGrouproutes 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; whatNewDocumentButtonDataSourcecontributes is the meaning behind the tap — which document gets created.Keep the surrounding view as the host
The
VStackand itspadding()here are only a stage for the control.NewDocumentButtonDataSourcedoes 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.
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.
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()
}
}