jeziellago / compose-markdown

Markdown Text for Android Jetpack Compose 📋.
MIT License
508 stars 42 forks source link

Enhance Max Line Usage #38

Closed tSquaredd closed 1 year ago

tSquaredd commented 1 year ago

Hi there! Using this library and have a use case that might be useful for others.

I need to be able to set a max lines value of X and if the markdown is longer than X lines I need to display a "Show More" button.

So I need 2 things:

  1. A way to know if more than X lines is needed to display the full markdown text
  2. Ability to update max lines and have the MarkdownText update.

Example Usage with X = 3:

                    var isExpanded by remember {
                        mutableStateOf(false)
                    }
                    var showMoreButton by remember {
                        mutableStateOf(false)
                    }
                    Column {
                        MarkdownText(
                            markdown = descriptionMarkdown,
                            maxLines = if (isExpanded) Int.MAX_VALUE else 3,
                            onTextLayout = { lineCount ->
                                if (lineCount > 3) {
                                    showMoreButton = true
                                }
                            }
                        )
                        if (showMoreButton) {
                            Spacer(modifier = Modifier.height(8.dp))
                            TextButton(
                                state = TextButtonState(text = stringResource(id = R.string.show_more)),
                                onClick = { isExpanded = !isExpanded }
                            )
                        }
                    }
jeziellago commented 1 year ago

Hi, @tSquaredd! Thanks for contributing!

tSquaredd commented 1 year ago

@jeziellago no problem! Thank you for providing this library to use 🙃