cuttable

Specifies an action that moves items to the Clipboard in response to the system's Cut command.

View ModifieriOS 27.0+macOS 13.0+cuttable(for:action:) in SwiftUI

Declaration

React
compose().cuttable<T>({
  action: T[],
})
Swift
nonisolated func cuttable<T>(
    for payloadType: T.Type = T.self,
    action: @escaping () -> [T]
) -> some View
where T : Transferable

Parameters

Return value

A view that sends one or more items to the Clipboard in response to a Cut command.

Discussion

Use this modifier to remove one or more items from a collection of items and then move the items to the Clipboard when someone issues a Cut command. People issue a Cut command by choosing Edit > Cut from the app's menu, or by using the Command-X keyboard shortcut. The system enables the Cut command for your app when it detects cuttable content.

For example, the following code enables people to remove bird names from a list of birds:

struct CuttableExample: View {
    @State private var birds = ["owl", "parrot", "swift"]
    @State private var selection: Set<String> = []

    var body: some View {
        List(birds, id: \.self, selection: $selection) {
            Text($0)
        }
        .cuttable(for: String.self) {
            for bird in selection {
                birds.removeAll(where: { $0 == bird })
            }
            return Array(selection)
        }
    }
}

When someone selects "owl" and issues a Cut command, the action closure removes the selected item from the list and returns it. In response, SwiftUI moves it to the Clipboard. If you want to copy the item without removing it, use the copyable(_:) modifier instead.

context menu item --- rather than using the system Cut command, update the Clipboard directly using an doc://com.apple.documentation/documentation/appkit/nspasteboard or a doc://com.apple.documentation/documentation/uikit/uipasteboard instance.

See also

More in Drag and drop.