DEV Community

Cover image for Stop Writing 20 @Preview Functions — Build a Runtime Sandbox Instead
George Karanikolas
George Karanikolas

Posted on

Stop Writing 20 @Preview Functions — Build a Runtime Sandbox Instead

Every Compose developer hits this wall eventually. You start with one @Preview. Then one for dark mode. Then one for large fonts. Then one per locale. Before you know it, you're scrolling through two dozen nearly-identical functions just to check whether a button label wraps in German.

The real problem isn't laziness — it's that static previews don't scale. Dark × 3 locales × 4 font scales × RTL is 24 permutations for a single screen. And your IDE renders all of them every build.

So I built PreviewSandbox. Instead of multiplying preview functions, you get an interactive control panel that lets you toggle every configuration axis at runtime — no rebuilds, no system-setting changes.


What It Looks Like

The API is intentionally minimal:

@Preview
@Composable
private fun ScreenPreview() {
    PreviewSandbox(
        theme = { state, content ->
            AppTheme(darkTheme = state.isDark) { content() }
        }
    ) {
        MyScreen()
    }
}
Enter fullscreen mode Exit fullscreen mode

That's one preview. One function. A ⚙ icon appears at runtime and opens a control panel where you can toggle:

  • Dark / light theme
  • Language — cycles through your supported locales
  • Font scale — 1.0×, 1.3×, 1.5×, 1.8×, 2.0×
  • RTL layout direction
  • Background color
  • Wallpaper (for transparent UIs like widgets or overlays)
  • Dynamic Color (Material You)
  • Bold text
  • High contrast mode

The control panel only shows the toggles that are relevant to your app. Everything else stays out of the way.

Read the full deep-dive on Medium.

Top comments (0)