Skip to content

Text selection in StructuredText doesn't work with navigation #72

Description

@zhufucdev

Description

If a StructuredText is nested inside a NavigationLink:destination, on its first appearance text selection does not work properly.

Checklist

  • I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

A text selection box is overlaid at the right position

Actual behavior

There does not appear to be a box of any sort

Steps to reproduce

struct ContentView: View {
    var body: some View {
        NavigationStack {
            NavigationLink {
                StructuredText(markdown: "enim tempor cillum excepteur dolor Lorem est laboris consequat sunt aliquip et commodo excepteur id adipisicing exercitation in irure deserunt et duis proident enim proident labore deserunt proident enim nostrud Lorem sit laborum sit incididunt nostrud ullamco excepteur aute consequat")
                    .textual.textSelection(.enabled)
                    .padding()
            } label: {
                Text("Go to body")
            }
        }
    }
}
ScreenRecording_06-30-2026.11-51-04_1.MP4

Noticing how it corrected itself after a phone rotation, here's a potential workaround:

struct ContentView: View {
    @State var text: String = ""
    var body: some View {
        NavigationStack {
            NavigationLink {
                StructuredText(markdown: text)
                    .textual.textSelection(.enabled)
                    .padding()
                    .onAppear {
                        text = "enim tempor cillum excepteur dolor Lorem est laboris consequat sunt aliquip et commodo excepteur id adipisicing exercitation in irure deserunt et duis proident enim proident labore deserunt proident enim nostrud Lorem sit laborum sit incididunt nostrud ullamco excepteur aute consequat"
                    }
            } label: {
                Text("Go to body")
            }
        }
    }
}

It must have something to do with the layout engine. Also under extreme circumstances (Transmission's DestinationLink) the app crashes when selecting, with an index out of bounds error (file: TextLayoutCollection+Positioning, L78, where layouts is empty)

Textual version information

0.5.0

Destination operating system

iOS 27 DP 2

Xcode version information

Version 27.0 beta (27A5194q)

Swift Compiler version information

swift-driver version: 1.167 Apple Swift version 6.4 (swiftlang-6.4.0.20.104 clang-2100.3.20.102)
Target: arm64-apple-macosx27.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions