<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/TableRowContent/contextMenu(menuItems:preview:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI15TableRowContentPAAE11contextMenu9menuItems7previewAA08ModifiedE0VyxAA08_Contextg7PreviewcD8ModifierVyqd__qd_0_GGqd__yXE_qd_0_yXEtAA4ViewRd__AaMRd_0_r0_lF"
  },
  "title" : "contextMenu(menuItems:preview:)"
}
-->

# contextMenu(menuItems:preview:)

Adds a context menu with a preview to a table row.

```
@MainActor @preconcurrency func contextMenu<M, P>(@ContentBuilder menuItems: () -> M, @ContentBuilder preview: () -> P) -> ModifiedContent<Self, _ContextMenuPreviewTableRowModifier<M, P>> where M : View, P : View
```

## Parameters

`menuItems`

A closure that produces the menu’s contents. You can
deactivate the context menu by returning nothing from the closure.

`preview`

A view that the system displays along with the menu.

## Return Value

A row that can display a context menu with a preview.

## Discussion

When you use this modifier to add a context menu to rows in a
table, the system shows a preview beside the menu.
Compose the menu by returning controls like [`Button`](/documentation/SwiftUI/Button), [`Toggle`](/documentation/SwiftUI/Toggle), and
[`Picker`](/documentation/SwiftUI/Picker) from the `menuItems` closure. You can also use [`Menu`](/documentation/SwiftUI/Menu) to
define submenus.

Define the preview by returning a view from the `preview` closure. The
system sizes the preview to match the size of its content. For example,
the following code adds a context menu with a preview to each row in a
table that people can use to send an email to the person represented by
that row:

```
Table(of: Person.self) {
    TableColumn("Given Name", value: \.givenName)
    TableColumn("Family Name", value: \.familyName)
} rows: {
    ForEach(people) { person in
        TableRow(person)
            .contextMenu {
                Button("Send Email...") { }
            } preview: {
                Image("envelope") // Loads the image from an asset catalog.
            }
    }
}
```

> Note: This view modifier produces a context menu on macOS, but that
> platform doesn’t display the preview.

If you don’t need a preview, use
[`contextMenu(menuItems:)`](/documentation/SwiftUI/TableRowContent/contextMenu(menuItems:)). If you want
to display a context menu that’s based on the current selection,
use [`contextMenu(forSelectionType:menu:primaryAction:)`](/documentation/SwiftUI/View/contextMenu(forSelectionType:menu:primaryAction:)). To add
context menus to other kinds of views, see [`contextMenu(menuItems:)`](/documentation/SwiftUI/View/contextMenu(menuItems:)).

---

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)