Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MarkdownText conflicts with `Modifier.clickable #7

Open
jber18 opened this issue Oct 14, 2024 · 5 comments
Open

MarkdownText conflicts with `Modifier.clickable #7

jber18 opened this issue Oct 14, 2024 · 5 comments

Comments

@jber18
Copy link

jber18 commented Oct 14, 2024

Description:
When using the MarkdownText composable from the library, it renders the markdown content using an AndroidView. However, I encountered an issue where adding a Modifier.clickable to the MarkdownText or any of its parent containers does not register click events properly. This behavior creates conflicts when I try to wrap the MarkdownText with clickable elements for interaction.


Steps to Reproduce:

  1. Use MarkdownText in a Jetpack Compose component.
  2. Try wrapping it inside a Box or Row with a Modifier.clickable to detect taps.
  3. Observe that the click events are not registered, and the onClick callback is never triggered.
@Composable
fun TestMessage() {
    Box(
        modifier = Modifier
            .clickable { // This never gets called
                println("Clicked!")
            }
            .padding(16.dp)
    ) {
        MarkdownText(
            markdown = "This is **markdown** text!",
            modifier = Modifier.fillMaxWidth()
        )
    }
}

Expected Behavior:

  • The Modifier.clickable should register the click event when the markdown text is tapped.
  • The onClick callback provided to the Modifier.clickable should be triggered.

Actual Behavior:

  • The clickable modifier does not work when applied to MarkdownText or its parent containers.
  • It seems that the AndroidView used internally by MarkdownText is blocking gesture events from reaching the Jetpack Compose layout.

Possible Cause:

The MarkdownText composable uses AndroidView under the hood to render markdown content via the Markwon library. Since AndroidView bridges the Android View system with Jetpack Compose, it consumes touch events internally, preventing them from propagating properly to the outer composables.

This is a known issue when mixing AndroidView with Compose gesture handlers, as the Android view hierarchy does not seamlessly cooperate with the Compose UI system.


Workaround Attempts:

I tried a few potential solutions, but none resolved the issue:

  1. Adding a clickable Modifier to the parent Box: No success; the click is never registered.
  2. Adding a transparent overlay for gesture detection: This blocked the visual content, which is not ideal for UI.
  3. Using zIndex adjustments: Had no effect, as AndroidView still intercepts the gestures.

Suggested Solutions:

  1. Expose a onClick callback directly in the MarkdownText composable. This way, the click events can be handled internally within the AndroidView.

    Example API:

    @Composable
    fun MarkdownText(
        markdown: String,
        modifier: Modifier = Modifier,
        onClick: () -> Unit = {}
    ) { ... }
  2. Alternatively, provide guidance on how to integrate MarkdownText with Compose's clickable modifier or gesture detection.


Environment:

  • Jetpack Compose version: latest
  • Library version: latest
  • Android Studio version: latest

@colintheshots
Copy link
Owner

colintheshots commented Oct 15, 2024

I deliberately didn't want clicks to get intercepted at the top-level, so URLs would be clickable with their own handlers. One usually wants clicks to be handled at the lowest-level.

What level are you expecting to be clickable? The TextView? I assume you wouldn't want to also handle clicks on URLs or anything linkified in the same click handler?

@lneugebauer
Copy link

What level are you expecting to be clickable? The TextView? I assume you wouldn't want to also handle clicks on URLs or anything linkified in the same click handler?

I'd like to add an onLongClick event to my TextView. Similar to Signal or WhatsApp, I want links to be clickable, but when the TextView is long clicked, a secondary action (In my case copying the text to the clipboard) should be triggered.

@miloszelko
Copy link

miloszelko commented Jan 15, 2025

Screenshot 2025-01-15 at 12 46 57

@lneugebauer
I had the same issue, here's workaround I used. Not the most elegant solution but it works.

@mitrejcevski
Copy link

@miloszelko how do you resolve findViewById inside the LaunchedEffect?

@colintheshots
Copy link
Owner

LocalContext.current keeps the local context inside of Compose and you can pass it in. Just be careful about passing context to long running jobs as you may not want memory leaks after the view should go away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants