Skip to content

Commit

Permalink
Fix non-dismissable bottom sheets being dismissable on backpress (#1577)
Browse files Browse the repository at this point in the history
Resolves #1549

Opportunistically cleaned up some docs and removed unnecessary default
param values for the private primary constructor.
  • Loading branch information
ZacSweers authored Aug 13, 2024
1 parent cef9c70 commit 83230f2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Changelog
- **New**: Add `CircuitPreview` helper function for composable previews that contain Circuit content.
- **Enhancement**: When running under `LocalInspectionMode`, Circuit's default `onUnavailableContent` now shows a simpler non-intrusive placeholder UI instead.
- **Enhancement**: Support secondary injected constructors in code gen.
- **Fix**: Fix non-dismissable `BottomSheetOverlay` crash when invoking back-press.

0.23.0
------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2024 Slack Technologies, LLC
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuitx.overlays

import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheetProperties

@OptIn(ExperimentalMaterial3Api::class)
internal actual fun createBottomSheetProperties(
isFocusable: Boolean,
shouldDismissOnBackPress: Boolean,
): ModalBottomSheetProperties {
return ModalBottomSheetProperties(
DEFAULT_PROPERTIES.securePolicy,
isFocusable,
shouldDismissOnBackPress,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2024 Slack Technologies, LLC
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuitx.overlays

import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheetProperties

@OptIn(ExperimentalMaterial3Api::class)
internal actual fun createBottomSheetProperties(
isFocusable: Boolean,
shouldDismissOnBackPress: Boolean,
): ModalBottomSheetProperties {
return ModalBottomSheetProperties(isFocusable, shouldDismissOnBackPress)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.BottomSheetDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.ModalBottomSheetDefaults
import androidx.compose.material3.ModalBottomSheetProperties
import androidx.compose.material3.SheetValue
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -43,31 +45,35 @@ import kotlinx.coroutines.launch
public class BottomSheetOverlay<Model : Any, Result : Any>
private constructor(
private val model: Model,
private val dismissOnTapOutside: Boolean = true,
private val onDismiss: (() -> Result)? = null,
private val sheetShape: Shape? = null,
private val sheetContainerColor: Color? = null,
private val dragHandle: (@Composable () -> Unit)? = null,
private val skipPartiallyExpandedState: Boolean = false,
private val dismissOnTapOutside: Boolean,
private val onDismiss: (() -> Result)?,
private val sheetShape: Shape?,
private val sheetContainerColor: Color?,
private val dragHandle: (@Composable () -> Unit)?,
private val skipPartiallyExpandedState: Boolean,
private val properties: ModalBottomSheetProperties,
private val content: @Composable (Model, OverlayNavigator<Result>) -> Unit,
) : Overlay<Result> {

/**
* Constructs a new [BottomSheetOverlay] that will not dismiss when tapped outside of the sheet.
* This means that only the [content] can finish the overlay. Additionally the appearance of the
* sheet can be customized
* sheet can be customized.
*
* @param sheetContainerColor - set the container color of the ModalBottomSheet
* @param dragHandle - customize the drag handle of the sheet
* @param skipPartiallyExpandedState - indicates if the Sheet should be expanded per default (if
* @param sheetContainerColor set the container color of the ModalBottomSheet
* @param dragHandle customize the drag handle of the sheet
* @param skipPartiallyExpandedState indicates if the Sheet should be expanded per default (if
* it's height exceed the partial height threshold)
* @param isFocusable corresponds to [ModalBottomSheetProperties.isFocusable] and will be passed
* on to the final sheet as such.
*/
public constructor(
model: Model,
sheetContainerColor: Color? = null,
sheetShape: Shape? = null,
dragHandle: @Composable (() -> Unit)? = null,
skipPartiallyExpandedState: Boolean = false,
isFocusable: Boolean = true,
content: @Composable (Model, OverlayNavigator<Result>) -> Unit,
) : this(
model = model,
Expand All @@ -77,18 +83,22 @@ private constructor(
sheetShape = sheetShape,
sheetContainerColor = sheetContainerColor,
skipPartiallyExpandedState = skipPartiallyExpandedState,
properties =
createBottomSheetProperties(isFocusable = isFocusable, shouldDismissOnBackPress = false),
content = content,
)

/**
* Constructs a new [BottomSheetOverlay] that will dismiss when tapped outside of the sheet.
* [onDismiss] is required in this case to offer a default value in this event. Additionally the
* appearance of the sheet can be customized
* appearance of the sheet can be customized.
*
* @param sheetContainerColor - set the container color of the ModalBottomSheet
* @param dragHandle - customize the drag handle of the sheet
* @param skipPartiallyExpandedState - indicates if the Sheet should be expanded per default (if
* @param sheetContainerColor set the container color of the ModalBottomSheet
* @param dragHandle customize the drag handle of the sheet
* @param skipPartiallyExpandedState indicates if the Sheet should be expanded per default (if
* it's height exceed the partial height threshold)
* @param properties any [ModalBottomSheetProperties]. Defaults to
* [ModalBottomSheetDefaults.properties].
*/
public constructor(
model: Model,
Expand All @@ -97,6 +107,7 @@ private constructor(
sheetShape: Shape? = null,
dragHandle: @Composable (() -> Unit)? = null,
skipPartiallyExpandedState: Boolean = false,
properties: ModalBottomSheetProperties = DEFAULT_PROPERTIES,
content: @Composable (Model, OverlayNavigator<Result>) -> Unit,
) : this(
model = model,
Expand All @@ -106,6 +117,7 @@ private constructor(
sheetShape = sheetShape,
sheetContainerColor = sheetContainerColor,
skipPartiallyExpandedState = skipPartiallyExpandedState,
properties = properties,
content = content,
)

Expand Down Expand Up @@ -157,6 +169,7 @@ private constructor(
check(dismissOnTapOutside)
navigator.finish(onDismiss!!.invoke())
},
properties = properties,
)

LaunchedEffect(model, onDismiss) {
Expand All @@ -171,9 +184,17 @@ private constructor(
}
}
LaunchedEffect(model, onDismiss) {
// TODO why doesn't this ever hit if it's after show()
hasShown = true
sheetState.show()
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
internal val DEFAULT_PROPERTIES: ModalBottomSheetProperties = ModalBottomSheetDefaults.properties()

@OptIn(ExperimentalMaterial3Api::class)
internal expect fun createBottomSheetProperties(
isFocusable: Boolean = DEFAULT_PROPERTIES.isFocusable,
shouldDismissOnBackPress: Boolean = DEFAULT_PROPERTIES.shouldDismissOnBackPress,
): ModalBottomSheetProperties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2024 Slack Technologies, LLC
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuitx.overlays

import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheetProperties

@OptIn(ExperimentalMaterial3Api::class)
internal actual fun createBottomSheetProperties(
isFocusable: Boolean,
shouldDismissOnBackPress: Boolean,
): ModalBottomSheetProperties {
return ModalBottomSheetProperties(isFocusable, shouldDismissOnBackPress)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2024 Slack Technologies, LLC
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuitx.overlays

import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheetProperties

@OptIn(ExperimentalMaterial3Api::class)
internal actual fun createBottomSheetProperties(
isFocusable: Boolean,
shouldDismissOnBackPress: Boolean,
): ModalBottomSheetProperties {
return ModalBottomSheetProperties(isFocusable, shouldDismissOnBackPress)
}

0 comments on commit 83230f2

Please sign in to comment.