Skip to content

Commit

Permalink
update Collapse visual
Browse files Browse the repository at this point in the history
- move chevron to the right
- proper text style
- whole header clickable
  • Loading branch information
hrach committed Mar 29, 2023
1 parent f12f879 commit f921257
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
8 changes: 4 additions & 4 deletions docs/03-components/07-interaction/collapse.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fun Example() {
expanded = expanded,
onExpandClick = { expanded = it },
title = {
Text(text = "This the title")
Text(text = "Collapse title")
},
content = {
Text(text = "This is the collapsible content")
Expand All @@ -43,7 +43,7 @@ fun Example() {
expanded = expanded,
onExpandClick = { expanded = it },
title = {
Text(text = "This the title")
Text(text = "Collapse title")
},
content = {
Text(text = "This is the collapsible content")
Expand All @@ -64,7 +64,7 @@ composeTestRule.setContent {
expanded = expanded,
onExpandClick = { expanded = it },
title = {
Text(text = "This the title", modifier = Modifier.testTag("title"))
Text(text = "Collapse title", modifier = Modifier.testTag("title"))
},
content = {
Text(text = "This is the collapsible content", modifier = Modifier.testTag("content"))
Expand All @@ -73,7 +73,7 @@ composeTestRule.setContent {
)
}

composeTestRule.onNodeWithTag("title").assertIsDisplayed()
composeTestRule.onNodeWithTag("title", useUnmergedTree = true).assertIsDisplayed()
composeTestRule.onNodeWithTag("content").assertDoesNotExist()

composeTestRule.onNodeWithTag("collapse").performSemanticsAction(SemanticsActions.Expand)
Expand Down
46 changes: 35 additions & 11 deletions ui/src/main/java/kiwi/orbit/compose/ui/controls/Collapse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package kiwi.orbit.compose.ui.controls

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Divider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
Expand All @@ -20,8 +24,10 @@ import androidx.compose.ui.semantics.expand
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import kiwi.orbit.compose.icons.Icons
import kiwi.orbit.compose.ui.OrbitTheme
import kiwi.orbit.compose.ui.controls.internal.OrbitPreviews
import kiwi.orbit.compose.ui.controls.internal.Preview
import kiwi.orbit.compose.ui.foundation.ProvideMergedTextStyle

/**
* Hides long or complex information under a block that can be hidden.
Expand All @@ -44,6 +50,7 @@ public fun Collapse(
title: @Composable () -> Unit,
content: @Composable () -> Unit,
modifier: Modifier = Modifier,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
withSeparator: Boolean = true,
) {
Column(
Expand All @@ -68,17 +75,33 @@ public fun Collapse(
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
Row(
modifier = Modifier.fillMaxWidth(),
modifier = Modifier
.clickable(
enabled = true,
onClick = { onExpandChange(!expanded) },
indication = null,
interactionSource = interactionSource,
)
.padding(vertical = 12.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
) {
title()
CollapseArrow(expanded = expanded, onClick = { onExpandChange(!expanded) })
ProvideMergedTextStyle(OrbitTheme.typography.bodyNormalMedium) {
Box(Modifier.weight(1f)) {
title()
}
}
CollapseArrow(
expanded = expanded,
interactionSource = interactionSource,
onClick = { onExpandChange(!expanded) },
)
}
AnimatedVisibility(visible = expanded, modifier = Modifier.fillMaxWidth()) {
content()
AnimatedVisibility(visible = expanded) {
ProvideMergedTextStyle(OrbitTheme.typography.bodyNormal) {
content()
}
}

if (withSeparator) {
Divider()
}
Expand All @@ -88,16 +111,17 @@ public fun Collapse(
@Composable
private fun CollapseArrow(
expanded: Boolean,
interactionSource: MutableInteractionSource,
onClick: () -> Unit,
) {
val arrowRotationDegree by animateFloatAsState(
targetValue = if (expanded) 180f else 0f,
)

IconButton(
onClick = onClick,
modifier = Modifier
.rotate(arrowRotationDegree),
modifier = Modifier.rotate(arrowRotationDegree),
interactionSource = interactionSource,
minimumInteractiveComponentEnforcement = false,
) {
Icon(
painter = Icons.ChevronDown,
Expand All @@ -115,7 +139,7 @@ internal fun CollapsePreview() {
expanded = expanded,
onExpandChange = { expanded = it },
title = {
Text(text = "This the title")
Text(text = "Collapse title")
},
content = {
Text(text = "This is the collapsible content")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class CollapseTest {
expanded = expanded,
onExpandChange = { expanded = it },
title = {
Text(text = "This the title", modifier = Modifier.testTag("title"))
Text(text = "Collapse title", modifier = Modifier.testTag("title"))
},
content = {
Text(text = "This is the collapsible content", modifier = Modifier.testTag("content"))
Expand All @@ -38,7 +38,7 @@ internal class CollapseTest {
)
}

composeTestRule.onNodeWithTag("title").assertIsDisplayed()
composeTestRule.onNodeWithTag("title", useUnmergedTree = true).assertIsDisplayed()
composeTestRule.onNodeWithTag("content").assertDoesNotExist()

composeTestRule.onNodeWithTag("collapse").performSemanticsAction(SemanticsActions.Expand)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f921257

Please sign in to comment.