Is there any way to mark a composable so that it can only be used from a preview?
z
zt
09/27/2024, 7:07 AM
I don't think there is anything to do it in compile time. You could have a common preview function that checks LocalInspectionMode and throws an error if it's not being used in a preview
t
Timo Drick
09/27/2024, 10:59 AM
Maybe you could create you own annotation. Than you have to optin when you use it. At least the other developers see a warning when using it. But it will not automatically allow this for previews.
Copy code
@RequiresOptIn(message = "Only use this in previews please!")
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.FUNCTION)
annotation class PreviewComposable
@Composable
@PreviewComposable
private fun Preview1() {
Box() {
}
}