collectModulePreviews
Provides a delegate that collects @Preview functions from this module only.
The Compose Preview Lab compiler plugin replaces the body at compile time so that the returned delegate holds the actual preview list. If the compiler plugin is not applied, accessing the value throws an IllegalStateException.
The returned PreviewExport functions as a property delegate for a List<CollectedPreview>. It wraps a Lazy so that @Composable lambdas inside CollectedPreview are initialized on first access rather than at class-load time, avoiding ExceptionInInitializerError. The same wrapper acts as a marker type that downstream modules pick up automatically — see collectAllModulePreviews.
This stable no-arg overload delegates to the experimental scope-aware overload with the default-scope sentinel, so consumers do not have to opt in unless they need explicit scope filtering. See the scope-aware overload for the full scope semantics.
Example — single-module preview collection:
val myPreviews by collectModulePreviews()Example — used in a library module with public visibility:
// uiLib/src/commonMain/kotlin/Previews.kt
val uiLibPreviews by collectModulePreviews()Return
a PreviewExport delegate wrapping the collected preview list; the body is replaced by the compiler plugin
See also
Scope-aware variant of collectModulePreviews that limits the result to previews whose @ComposePreviewLabOption(collectScopes = [...]) array contains scope.
Experimental: the scope feature is still stabilizing. Consumers must opt in with @OptIn(ExperimentalComposePreviewLabApi::class) (or @file:OptIn(...)) to call this overload. The no-arg collectModulePreviews overload remains stable for callers that do not need explicit scope filtering.
Example:
@file:OptIn(me.tbsten.compose.preview.lab.ExperimentalComposePreviewLabApi::class)
val buttonPreviews by collectModulePreviews(scope = "buttons")Return
a PreviewExport delegate wrapping the collected preview list; the body is replaced by the compiler plugin
Parameters
The collection scope to draw from. Only previews annotated with the matching @ComposePreviewLabOption(collectScopes = ["..."]) end up in the result. The literal value "default" (= ComposePreviewLabOption.DefaultCollectScope) acts as a sentinel: it does not mean a literal "default" bucket — the compiler plugin substitutes it with the module's composePreviewLab.collectPreviews.defaultCollectScope Gradle DSL value (which itself defaults to "default"). So a library that pins every preview to its own bucket via defaultCollectScope = "acme_ui" automatically reads back the same bucket here.
The argument must reach the compiler plugin's IR pass as a compile-time string constant — an inline string literal or a const val reference both work, because both end up as an IrConst<String> before our pass runs. Non-const vals, string concatenations, and other expressions that produce an IrCall / IrStringConcatenation are reported as compile errors. The resolved value must also match [A-Za-z0-9_]+ because the plugin embeds it into the synthetic hint function name.