<!--
{
  "availability" : [
    "iOS: 18.1.0 -",
    "iPadOS: 18.1.0 -",
    "macCatalyst: 18.1.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/documentBrowserContextMenu(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE26documentBrowserContextMenuyQrqd__Say10Foundation3URLVGSgcAaBRd__lF"
  },
  "title" : "documentBrowserContextMenu(_:)"
}
-->

# documentBrowserContextMenu(_:)

Adds to a `DocumentLaunchView` actions that accept
a list of selected files as their parameter.

```
@MainActor @preconcurrency func documentBrowserContextMenu(@ContentBuilder _ menu: @escaping ([URL]?) -> some View) -> some View
```

## Parameters

`menu`

Items representing the content of the menu.

## Discussion

Add `documentBrowserContextMenu` modifier to [`DocumentLaunchView`](/documentation/SwiftUI/DocumentLaunchView)
to provide additional actions to the document browser items’ context menu.
For example, a book editor application could have a “Favorite” button
that marks user-chosen books as their favorite. The button enables when the user
switches the browser into “Selection” mode.

```
import UniformTypeIdentifiers

struct BookEditor: View {

    var body: some View {
        DocumentLaunchView(for: [.book]) {
            NewDocumentButton("Start New Book")
        } onDocumentOpen: { url in
            BookEditor(url)
        }
        .documentBrowserContextMenu { selectedURLs in
            FavoriteBookButton(urls: selectedURLs)
        }
    }
}

struct FavoriteBookButton: View {
    var urls: [URL]?
    var body: some View {
        Button {
            updateFavorite(urls)
        } label: {
            Image(systemName: allFavorite(urls) ? "heart.fill" : "heart")
        }
    }

    func allFavorite(_ urls: [URL]?) -> Bool { ... }
    func updateFavorite(_ urls: [URL]?) { ... }
}

struct BookEditor: View {
    init(_ url: URL) { ... }
}

extension UTType {
    static let book = UTType(exportedAs: "com.example.bookEditor")
}
```

In the example above, the application stores a list of URLs to favorite books,
and does not need to access the file on disk. In cases when an application wants to read
the contents of the URL from the disk or associated metadata, it should call
`URL.startAccessingSecurityScopedResource()` to gain access to the resource,
and `URL.stopAccessingSecurityScopedResource()` to relinquish access when it is not needed.

The actions are displayed in the document browser navigation bar
when a document browser is in Select mode, and also added to
context menu for the file items.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)