Skip to content

Commit 192f409

Browse files
authored
fix(ui/chat): improve ChatInput resizability; improve send button animation (#935)
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 7025aa0 commit 192f409

1 file changed

Lines changed: 36 additions & 42 deletions

File tree

  • ui/components/src/main/kotlin/com/getcode/ui/components/chat

ui/components/src/main/kotlin/com/getcode/ui/components/chat/ChatInput.kt

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
package com.getcode.ui.components.chat
22

3-
import androidx.compose.animation.AnimatedContent
3+
import androidx.compose.animation.animateContentSize
4+
import androidx.compose.animation.core.animateFloatAsState
45
import androidx.compose.animation.core.spring
5-
import androidx.compose.animation.fadeIn
6-
import androidx.compose.animation.fadeOut
7-
import androidx.compose.animation.scaleIn
8-
import androidx.compose.animation.scaleOut
9-
import androidx.compose.animation.togetherWith
106
import androidx.compose.foundation.background
117
import androidx.compose.foundation.clickable
128
import androidx.compose.foundation.layout.Arrangement
139
import androidx.compose.foundation.layout.Box
14-
import androidx.compose.foundation.layout.IntrinsicSize
1510
import androidx.compose.foundation.layout.PaddingValues
1611
import androidx.compose.foundation.layout.Row
17-
import androidx.compose.foundation.layout.Spacer
1812
import androidx.compose.foundation.layout.fillMaxWidth
19-
import androidx.compose.foundation.layout.height
2013
import androidx.compose.foundation.layout.padding
21-
import androidx.compose.foundation.layout.requiredSize
2214
import androidx.compose.foundation.layout.size
2315
import androidx.compose.foundation.text.KeyboardOptions
2416
import androidx.compose.foundation.text.input.TextFieldState
2517
import androidx.compose.foundation.text.input.rememberTextFieldState
2618
import androidx.compose.material3.Icon
27-
import androidx.compose.material.icons.Icons
28-
import androidx.compose.material.icons.rounded.ArrowUpward
2919
import androidx.compose.runtime.Composable
20+
import androidx.compose.runtime.getValue
3021
import androidx.compose.runtime.remember
3122
import androidx.compose.ui.Alignment
3223
import androidx.compose.ui.Modifier
@@ -57,13 +48,25 @@ fun ChatInput(
5748
onSendMessage: () -> Unit,
5849
) {
5950
val shape = CodeTheme.shapes.medium
51+
val sendVisible = state.text.isNotEmpty()
52+
val sendSpec = spring<Float>(dampingRatio = 0.66f, stiffness = 4000f)
53+
val sendAlpha by animateFloatAsState(
54+
targetValue = if (sendVisible) 1f else 0f,
55+
animationSpec = sendSpec,
56+
label = "send button alpha",
57+
)
58+
val sendScale by animateFloatAsState(
59+
targetValue = if (sendVisible) 1f else 0.6f,
60+
animationSpec = sendSpec,
61+
label = "send button scale",
62+
)
6063
Row(
6164
modifier = Modifier
6265
.graphicsLayer { compositingStrategy = CompositingStrategy.Offscreen }
6366
.clip(shape)
6467
.then(modifier)
6568
.fillMaxWidth()
66-
.height(IntrinsicSize.Min),
69+
.animateContentSize(),
6770
horizontalArrangement = Arrangement.spacedBy(CodeTheme.dimens.grid.x2),
6871
verticalAlignment = Alignment.Bottom
6972
) {
@@ -93,36 +96,27 @@ fun ChatInput(
9396
unfocusedBorderColor = Color.Transparent,
9497
),
9598
trailingIcon = {
96-
AnimatedContent(
97-
targetState = state.text.isNotEmpty(),
98-
label = "show/hide send button",
99-
transitionSpec = {
100-
val spec = spring<Float>(dampingRatio = 0.66f, stiffness = 4000f)
101-
(fadeIn(spec) + scaleIn(spec, initialScale = 0.6f)) togetherWith
102-
(fadeOut(spec) + scaleOut(spec, targetScale = 0.6f))
103-
}
104-
) { show ->
105-
if (show) {
106-
Icon(
107-
modifier = Modifier
108-
.padding(vertical = CodeTheme.dimens.grid.x1)
109-
.padding(end = CodeTheme.dimens.staticGrid.x1)
110-
.background(
111-
Color.White,
112-
shape = CodeTheme.shapes.extraSmall
113-
)
114-
.clip(CodeTheme.shapes.extraSmall)
115-
.clickable { onSendMessage() }
116-
.padding(CodeTheme.dimens.staticGrid.x1)
117-
.size(CodeTheme.dimens.staticGrid.x5),
118-
painter = painterResource(R.drawable.ic_arrow_up),
119-
tint = Color.Black,
120-
contentDescription = "Send message"
99+
Icon(
100+
modifier = Modifier
101+
.graphicsLayer {
102+
alpha = sendAlpha
103+
scaleX = sendScale
104+
scaleY = sendScale
105+
}
106+
.padding(vertical = CodeTheme.dimens.grid.x1)
107+
.padding(end = CodeTheme.dimens.staticGrid.x1)
108+
.background(
109+
Color.White,
110+
shape = CodeTheme.shapes.extraSmall
121111
)
122-
} else {
123-
Spacer(Modifier.requiredSize(CodeTheme.dimens.staticGrid.x9))
124-
}
125-
}
112+
.clip(CodeTheme.shapes.extraSmall)
113+
.clickable(enabled = sendVisible) { onSendMessage() }
114+
.padding(CodeTheme.dimens.staticGrid.x1)
115+
.size(CodeTheme.dimens.staticGrid.x5),
116+
painter = painterResource(R.drawable.ic_arrow_up),
117+
tint = Color.Black,
118+
contentDescription = "Send message"
119+
)
126120
}
127121
)
128122
}

0 commit comments

Comments
 (0)