Skip to content

Instantly share code, notes, and snippets.

@cbruegg
Last active November 2, 2024 23:02
Show Gist options
  • Save cbruegg/e954313c92c7fa835fe1c2ad70699876 to your computer and use it in GitHub Desktop.
Save cbruegg/e954313c92c7fa835fe1c2ad70699876 to your computer and use it in GitHub Desktop.
Compose 1.6/1.7 Compatibility Layer
plugins {
// ...
}
android {
// ...
}
dependencies {
implementation(project(":compose-compat-on17"))
implementation("androidx.compose.material3:material3:1.2.1") // Compose 1.6
}
package composecompaton16
import composecompaton17.runningOnCompose16
// omitted androidx imports
@Composable
@ExperimentalMaterial3Api
fun ModalBottomSheetCompat(
contentWindowInsets: @Composable () -> WindowInsets = { BottomSheetDefaults.windowInsets },
// ...
) {
if (runningOnCompose16) {
androidx.compose.material3.ModalBottomSheet(
// Obtain WindowInsets by just calling contentWindowInsets for its return value
windowInsets = contentWindowInsets(),
// ...
)
} else {
// We're on Compose 1.7 or higher -> Delegate to the compose-compat-on17 module
composecompaton17.ModalBottomSheetCompat(
contentWindowInsets = contentWindowInsets,
// ...
)
}
}
plugins {
// ...
}
android {
// ...
}
dependencies {
compileOnly("androidx.compose.material3:material3:1.3.0") // Compose 1.7
}
package composecompaton17
// omitted androidx imports
@Composable
@ExperimentalMaterial3Api
fun ModalBottomSheetCompat(
contentWindowInsets: @Composable () -> WindowInsets = { BottomSheetDefaults.windowInsets },
// ...
) {
// In this module the call resolves successfully!
androidx.compose.material3.ModalBottomSheet(
contentWindowInsets = contentWindowInsets(),
// ...
)
}
package composecompaton17
val runningOnCompose16: Boolean =
try {
androidx.compose.material3.ripple(color = Color.Red)
false
} catch (e: LinkageError) {
true
}
plugins {
// ...
}
android {
// ...
}
dependencies {
implementation(project(":compose-compat-on16"))
// Other Compose 1.6 dependencies here
}
import composecompaton16.ModalBottomSheetCompat
// other imports omitted
@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun MyAwesomeSdkFeature() {
ModalBottomSheetCompat(
contentWindowInsets = { /* ... */ }
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment