Pantrix UI

Previewing a screen's states

Why the preview of a screen written in Compose Multiplatform is drawn differently from machine to machine and from the app on a phone, how a screen's states are previewed in its windows, in the app's theme, dark and light, and how a screenshot test draws the same states. The problem is not Pantrix's own: it is seen the same way in every Compose Multiplatform project that previews its screens.

What is said of Compose was read in the sources of Compose Multiplatform 1.12.2 (Preview.kt, PreviewParameter.kt in ui-tooling-preview) or seen in a test that runs in this repository; what was not tried is written in section 6.

1. The problem

Symptom

Cause

2. The solution

PieceWhat it gives
1A preview theme around the app's theme. The platform, dark or light, the window and the clock are the preview's choiceThe preview is the same on every machine, and is the screen on the platform it says
2The states as a list. A PreviewParameterProvider counts themEach state is a preview, and the list is written once
3The windows as sizes. @Preview(widthDp, heightDp), and the window for what a size does not sayThe screen is seen where it is laid out: a phone, a tablet, a fold
4The same states in a screenshot test. The test walks the same list in PantrixTestThemeWhat is previewed is what is checked

3. With Pantrix

3.1. The preview's theme

PantrixPreviewTheme is in com.pantrixui:foundation (com.pantrixui.foundation.theme). The app's own theme goes inside it: a theme inside another keeps what the outer one sets unless it sets it itself, and its own colours are dark or light as the outer one is.

ParameterDefaultWhat it fixes
platformPlatformType.AndroidThe touch targets, the focus rings and the platform's ways; the shortcuts are written as its system writes them, Windows's on the desktop and the web
darkfalseDark or light, whatever the machine's mode
windownull: the preview's ownThe window the screen is told it is in, its size and its folds; it stands over the whole preview
clocknull: the device'sThe moment the screen reads: rememberToday(), a time picker's hour, a chart's moments
motionReducedNothing is drawn halfway
@Preview(widthDp = 390, heightDp = 844)
@Composable
private fun CheckoutOnAnIphoneDarkPreview() {
    PantrixPreviewTheme(platform = PlatformType.Ios, dark = true, clock = PreviewClock) {
        AppTheme { CheckoutScreen(state = CheckoutState.Filled) }
    }
}

/** Noon on 24 October 2026, UTC: the moment every preview and every screenshot shows. */
val PreviewInstant: Instant = Instant.parse("2026-10-24T12:00:00Z")

val PreviewClock = PantrixClock(
    object : Clock {
        override fun now(): Instant = PreviewInstant
    }
) { Duration.ZERO }

3.2. A screen's states

A PreviewParameterProvider counts the states once; a preview takes them as a parameter, and each is drawn as a preview of its own:

class CheckoutStates : PreviewParameterProvider<CheckoutState> {
    override val values = sequenceOf(CheckoutState.Empty, CheckoutState.Filled, CheckoutState.Failed)
}

@Preview(widthDp = 390, heightDp = 844)
@Composable
private fun CheckoutPreview(@PreviewParameter(CheckoutStates::class) state: CheckoutState) {
    PantrixPreviewTheme(clock = PreviewClock) { AppTheme { CheckoutScreen(state = state) } }
}

3.3. Windows

3.4. The same states in a screenshot test

The test walks the same provider, in PantrixTestTheme with the same platform, dark and clock:

@OptIn(ExperimentalTestApi::class)
class CheckoutScreenshotTest {
    @Test
    fun every_state() = CheckoutStates().values.forEachIndexed { index, state ->
        runDesktopComposeUiTest(width = 390, height = 844) {
            setContent {
                PantrixTestTheme(platform = PlatformType.Ios, dark = true, clock = TestClock(PreviewInstant)) {
                    AppTheme { CheckoutScreen(state = state) }
                }
            }
            onRoot().captureRoboImage("Checkout_$index.png")
        }
    }
}

PantrixTestTheme takes a TestClock, the preview a PantrixClock: the two are given the same moment. The screenshot setup is in the testing guide (3.6).

4. Without Pantrix

The shortest form that can be copied into a project without the library: the values a theme would read from the device, given outright around the app's theme.

@Composable
fun FixedPreview(dark: Boolean = false, content: @Composable () -> Unit) {
    CompositionLocalProvider(
        LocalAppDark provides dark,               // the app's own local, which its theme reads in place of isSystemInDarkTheme()
        LocalAppClock provides PreviewClock,      // the app's own clock, which its screens read in place of Clock.System
        LocalLayoutDirection provides LayoutDirection.Ltr,
        content = content
    )
}

Each value the app's theme reads from the device needs a local of the app's own: a theme that calls isSystemInDarkTheme() or Clock.System itself cannot be fixed from outside.

5. Pitfalls

6. Verification

Tests

WhatWhere
The platform, dark and light, the system the shortcuts are written for, reduced motion; the app's theme inside keeping the platform and taking its preset's dark colours; the clock and the window, which stands over the whole previewui/foundation/src/commonTest/.../theme/PantrixPreviewThemeTest.kt (JVM, iOS, JS, Wasm)

Looked at by hand

Not verified

7. Where it is in this repository