-
Notifications
You must be signed in to change notification settings - Fork 0
Efficient Compose Previews
To avoid writing repetitive @Preview annotations for every component, this project includes a set of Multi-Preview Annotations in the :core:design-system module. These allow you to instantly visualize your components across different Languages (English/Spanish) and Themes (Light/Dark).
Standard @Preview annotations only configure the IDE's environment (e.g., "Set UI mode to Night"). They do not automatically apply your app's theme colors.
You must wrap your preview content in PreviewThemeWrapper.
@PreviewThemes
@Composable
fun MyComponentPreview() {
PreviewThemeWrapper { // <--- REQUIRED!
// It detects the environment (Light/Dark) and applies the correct colors
MyComponent()
}
}
Use case: Text-heavy components where you need to verify translations and layout constraints (e.g., if Spanish text overflows).
- Generates: 2 Previews (English, Spanish).
-
Source:
esvsenlocales.
@PreviewLocales
@Composable
private fun TextPreview() {
PreviewThemeWrapper {
Text(text = stringResource(id = R.string.welcome_message))
}
}
Use case: Icons, Buttons, or Cards where checking color contrast is critical.
- Generates: 2 Previews (Light Mode, Dark Mode).
-
Note: Relies on
PreviewThemeWrapperto readisSystemInDarkTheme().
@PreviewThemes
@Composable
private fun IconPreview() {
PreviewThemeWrapper {
Icon(imageVector = Icons.Default.Home, contentDescription = null)
}
}
Use case: Full Screens or complex "Feature" components. This is the "Golden Master" check.
- Generates: 4 Previews (EN-Light, EN-Dark, ES-Light, ES-Dark).
@PreviewComplete
@Composable
private fun FullScreenPreview() {
PreviewThemeWrapper {
LoginScreen(...)
}
}
-
Don't Overuse
@PreviewComplete: It generates 4x renderings per composable. For simple atoms (like a spacer or divider), a standard preview or@PreviewThemesis faster for the IDE to render. - Mock Data: Always provide dummy data to your components so the preview renders immediately without needing a running ViewModel.
-
Backgrounds: All custom annotations have
showBackground = trueenabled by default, so you can clearly see the component's bounds.