Skip to content

Commit af2356f

Browse files
dev : BottomSheet state hoisting variant added
1 parent bd38494 commit af2356f

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

  • compose-android/src/main/java/com/streamliners/compose/android/comp

compose-android/src/main/java/com/streamliners/compose/android/comp/BottomSheet.kt

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,54 @@ import androidx.compose.ui.platform.LocalSoftwareKeyboardController
2828
import androidx.compose.ui.unit.dp
2929
import com.streamliners.compose.comp.FilledIconButtonSmall
3030

31-
@OptIn(ExperimentalMaterialApi::class)
3231
@Composable
3332
fun BottomSheet(
3433
title: String,
3534
state: MutableState<Boolean>,
3635
content: @Composable () -> Unit
36+
) {
37+
BottomSheet(
38+
title = title,
39+
visible = state.value,
40+
onCloseRequest = { state.value = false },
41+
content = content
42+
)
43+
}
44+
45+
@OptIn(ExperimentalMaterialApi::class)
46+
@Composable
47+
fun BottomSheet(
48+
title: String,
49+
visible: Boolean,
50+
onCloseRequest: () -> Unit,
51+
content: @Composable () -> Unit
3752
) {
3853
val bottomSheetState =
3954
rememberModalBottomSheetState(
4055
initialValue = ModalBottomSheetValue.Hidden,
4156
confirmValueChange = {
4257
if(it == ModalBottomSheetValue.Hidden)
43-
state.value = false
58+
onCloseRequest()
4459
true
4560
}
4661
)
4762

48-
LaunchedEffect(key1 = state.value) {
49-
if (state.value) {
63+
LaunchedEffect(key1 = visible) {
64+
if (visible) {
5065
bottomSheetState.show()
5166
} else {
5267
bottomSheetState.hide()
5368
}
5469
}
5570

56-
BackHandler(enabled = state.value) { state.value = false }
71+
BackHandler(enabled = visible) { onCloseRequest() }
5772

5873
ModalBottomSheetLayout(
5974
modifier = Modifier.fillMaxWidth(),
6075
sheetShape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp),
6176
sheetState = bottomSheetState,
6277
sheetContent = {
63-
Content(title, state, content)
78+
Content(title, visible, onCloseRequest, content)
6479
},
6580
content = { },
6681
)
@@ -70,7 +85,8 @@ fun BottomSheet(
7085
@Composable
7186
private fun Content(
7287
title: String,
73-
state: MutableState<Boolean>,
88+
visible: Boolean,
89+
onCloseRequest: () -> Unit,
7490
content: @Composable () -> Unit,
7591
) {
7692
Surface(
@@ -101,7 +117,7 @@ private fun Content(
101117
val controller = LocalSoftwareKeyboardController.current
102118
FilledIconButtonSmall(
103119
onClick = {
104-
state.value = false
120+
onCloseRequest()
105121
controller?.hide()
106122
},
107123
icon = Icons.Default.Close,

0 commit comments

Comments
 (0)