onMenuItemHighlight(perform:)
Adds an action to perform when the highlight state of a menu item changes.
Declaration
nonisolated func onMenuItemHighlight(perform action: @escaping (Bool) -> Void) -> some View
Parameters
- action:
The action to perform whenever the highlight state of the menu item changes. If the menu item is highlighted, the
actionclosure passestrueas a parameter; otherwise,false.
Return Value
A view that triggers action when the highlight state changes.
Discussion
A state change is triggered as a menu item goes from idle to highlighted and vice versa. Highlighting in this context includes hovering, touching, keyboard navigating, and focusing.
Use this modifier on views inside a menu to observe when the user highlights an item. For example, a photo browser can dim a thumbnail while the user hovers over the Delete action:
@State private var previewingDelete = false
let photo: Photo
PhotoThumbnail(photo: photo)
.opacity(previewingDelete ? 0.4 : 1)
.contextMenu {
Button("Copy") { ... }
Button("Delete", role: .destructive) {
...
}
.onMenuItemHighlight { highlighted in
previewingDelete = highlighted
}
}