Swiftpack.co -  HonQii/ContextMenuModifier as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
HonQii/ContextMenuModifier
Simply convert UIContextMenuInteraction to SwiftUI Modifier
.package(url: "https://github.com/HonQii/ContextMenuModifier.git", from: "1.0.0")

ContextMenuModifier

Simply convert UIContextMenuInteraction to SwiftUI Modifier

Usage

import the package in the file you would like to use it: import ContextMenuModifier

You can use it as system .contextMenu modifier

Display custom view as context menu preview

custom preview

code:

Text("Popup custom preview!")
    .previewContextMenu(preview: VStack {
        Image(systemName: "face.smiling")
            .font(.largeTitle)
            .foregroundColor(Color(UIColor.systemYellow))
        Text("This is another preview content")
    }, actions: [
        UIAction(title: "Only Title", handler: { _ in }),
        UIAction(title: "Title with image", image: UIImage(systemName: "plus"), handler: { _ in }),
        UIAction(title: "Desctructive action", attributes: .destructive, handler: { _ in })
    ])

Display destination as preview

destination view

code:

Text("Popup destination as preview!")
    .previewContextMenu(destination: Text("This is destination view"), actions: [
        UIAction(title: "Only Title", handler: { _ in }),
        UIAction(title: "Title with image", image: UIImage(systemName: "plus"), handler: { _ in }),
        UIAction(title: "Desctructive action", attributes: .destructive, handler: { _ in })
    ])

Custom context preview and destination

preview and destination

code:

Text("Popup custom preview and destination!")
    .previewContextMenu(
        destination: Text("This is custom destination view"),
        preview: VStack {
            Image(systemName: "face.smiling")
                .font(.largeTitle)
                .foregroundColor(Color(UIColor.systemYellow))
            Text("This is another preview content")
        },
        actions: [
            UIAction(title: "Only Title", handler: { _ in }),
            UIAction(title: "Title with image", image: UIImage(systemName: "plus"), handler: { _ in }),
            UIAction(title: "Desctructive action", attributes: .destructive, handler: { _ in })
        ]
    )

Custom preferred size for preview

preferred size preview
Text("Custom Preview and destination with custom size!")
    .previewContextMenu(
        destination: VStack {
            Image(systemName: "face.smiling")
                .font(.largeTitle)
                .foregroundColor(Color(UIColor.systemYellow))
            Text("This is another preview content")
        },
        preview: Text("This is another preview content"),
        preferredContentSize: .init(width: 200, height: 200),
        actions: [
            UIAction(title: "Only Title", handler: { _ in }),
            UIAction(title: "Title with image", image: UIImage(systemName: "plus"), handler: { _ in }),
            UIAction(title: "Desctructive action", attributes: .destructive, handler: { _ in })
        ]
    )

Popup destination view as sheet

sheet destination

code:

Text("Popup destination as sheet")
    .previewContextMenu(
        destination: VStack {
            Image(systemName: "face.smiling")
                .font(.largeTitle)
                .foregroundColor(Color(UIColor.systemYellow))
            Text("This is another preview content")
        },
        preview: Text("This is another preview content"),
        presentAsSheet: true,
        actions: [
            UIAction(title: "Only Title", handler: { _ in }),
            UIAction(title: "Title with image", image: UIImage(systemName: "plus"), handler: { _ in }),
            UIAction(title: "Desctructive action", attributes: .destructive, handler: { _ in })
        ]
    )

GitHub

link
Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API