{"slug":"compose-multiplatform-patterns","title":"compose-multiplatform-patterns","summary":"Compose Multiplatform and Jetpack Compose patterns for KMP projects — state management, navigation, theming, performance, and platform-specific UI. Use when building Compose or Jetpack Compose UI, state, navigation, or theming in a KMP project.","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-12-02T16:53:30.956584Z","repo":{"url":"https://github.com/beginwithu/agent-skills","stars":11,"forks":2,"license":"MIT","updatedAt":"2026-12-02T16:37:11Z"},"bodyHtml":"<hr>\n<h2>name: compose-multiplatform-patterns\ndescription: Compose Multiplatform and Jetpack Compose patterns for KMP projects — state management, navigation, theming, performance, and platform-specific UI. Use when building Compose or Jetpack Compose UI, state, navigation, or theming in a KMP project.\nmetadata:\norigin: ECC</h2>\n<h1>Compose Multiplatform Patterns</h1>\n<p>Patterns for building shared UI across Android, iOS, Desktop, and Web using Compose Multiplatform and Jetpack Compose. Covers state management, navigation, theming, and performance.</p>\n<h2>When to Activate</h2>\n<ul>\n<li>Building Compose UI (Jetpack Compose or Compose Multiplatform)</li>\n<li>Managing UI state with ViewModels and Compose state</li>\n<li>Implementing navigation in KMP or Android projects</li>\n<li>Designing reusable composables and design systems</li>\n<li>Optimizing recomposition and rendering performance</li>\n</ul>\n<h2>State Management</h2>\n<h3>ViewModel + Single State Object</h3>\n<p>Use a single data class for screen state. Expose it as <code>StateFlow</code> and collect in Compose:</p>\n<pre><code>data class ItemListState(\n    val items: List&lt;Item&gt; = emptyList(),\n    val isLoading: Boolean = false,\n    val error: String? = null,\n    val searchQuery: String = \"\"\n)\n\nclass ItemListViewModel(\n    private val getItems: GetItemsUseCase\n) : ViewModel() {\n    private val _state = MutableStateFlow(ItemListState())\n    val state: StateFlow&lt;ItemListState&gt; = _state.asStateFlow()\n\n    fun onSearch(query: String) {\n        _state.update { it.copy(searchQuery = query) }\n        loadItems(query)\n    }\n\n    private fun loadItems(query: String) {\n        viewModelScope.launch {\n            _state.update { it.copy(isLoading = true) }\n            getItems(query).fold(\n                onSuccess = { items -&gt; _state.update { it.copy(items = items, isLoading = false) } },\n                onFailure = { e -&gt; _state.update { it.copy(error = e.message, isLoading = false) } }\n            )\n        }\n    }\n}\n</code></pre>\n<h3>Collecting State in Compose</h3>\n<pre><code>@Composable\nfun ItemListScreen(viewModel: ItemListViewModel = koinViewModel()) {\n    val state by viewModel.state.collectAsStateWithLifecycle()\n\n    ItemListContent(\n        state = state,\n        onSearch = viewModel::onSearch\n    )\n}\n\n@Composable\nprivate fun ItemListContent(\n    state: ItemListState,\n    onSearch: (String) -&gt; Unit\n) {\n    // Stateless composable — easy to preview and test\n}\n</code></pre>\n<h3>Event Sink Pattern</h3>\n<p>For complex screens, use a sealed interface for events instead of multiple callback lambdas:</p>\n<pre><code>sealed interface ItemListEvent {\n    data class Search(val query: String) : ItemListEvent\n    data class Delete(val itemId: String) : ItemListEvent\n    data object Refresh : ItemListEvent\n}\n\n// In ViewModel\nfun onEvent(event: ItemListEvent) {\n    when (event) {\n        is ItemListEvent.Search -&gt; onSearch(event.query)\n        is ItemListEvent.Delete -&gt; deleteItem(event.itemId)\n        is ItemListEvent.Refresh -&gt; loadItems(_state.value.searchQuery)\n    }\n}\n\n// In Composable — single lambda instead of many\nItemListContent(\n    state = state,\n    onEvent = viewModel::onEvent\n)\n</code></pre>\n<h2>Navigation</h2>\n<h3>Type-Safe Navigation (Compose Navigation 2.8+)</h3>\n<p>Define routes as <code>@Serializable</code> objects:</p>\n<pre><code>@Serializable data object HomeRoute\n@Serializable data class DetailRoute(val id: String)\n@Serializable data object SettingsRoute\n\n@Composable\nfun AppNavHost(navController: NavHostController = rememberNavController()) {\n    NavHost(navController, startDestination = HomeRoute) {\n        composable&lt;HomeRoute&gt; {\n            HomeScreen(onNavigateToDetail = { id -&gt; navController.navigate(DetailRoute(id)) })\n        }\n        composable&lt;DetailRoute&gt; { backStackEntry -&gt;\n            val route = backStackEntry.toRoute&lt;DetailRoute&gt;()\n            DetailScreen(id = route.id)\n        }\n        composable&lt;SettingsRoute&gt; { SettingsScreen() }\n    }\n}\n</code></pre>\n<h3>Dialog and Bottom Sheet Navigation</h3>\n<p>Use <code>dialog()</code> and overlay patterns instead of imperative show/hide:</p>\n<pre><code>NavHost(navController, startDestination = HomeRoute) {\n    composable&lt;HomeRoute&gt; { /* ... */ }\n    dialog&lt;ConfirmDeleteRoute&gt; { backStackEntry -&gt;\n        val route = backStackEntry.toRoute&lt;ConfirmDeleteRoute&gt;()\n        ConfirmDeleteDialog(\n            itemId = route.itemId,\n            onConfirm = { navController.popBackStack() },\n            onDismiss = { navController.popBackStack() }\n        )\n    }\n}\n</code></pre>\n<h2>Composable Design</h2>\n<h3>Slot-Based APIs</h3>\n<p>Design composables with slot parameters for flexibility:</p>\n<pre><code>@Composable\nfun AppCard(\n    modifier: Modifier = Modifier,\n    header: @Composable () -&gt; Unit = {},\n    content: @Composable ColumnScope.() -&gt; Unit,\n    actions: @Composable RowScope.() -&gt; Unit = {}\n) {\n    Card(modifier = modifier) {\n        Column {\n            header()\n            Column(content = content)\n            Row(horizontalArrangement = Arrangement.End, content = actions)\n        }\n    }\n}\n</code></pre>\n<h3>Modifier Ordering</h3>\n<p>Modifier order matters — apply in this sequence:</p>\n<pre><code>Text(\n    text = \"Hello\",\n    modifier = Modifier\n        .padding(16.dp)          // 1. Layout (padding, size)\n        .clip(RoundedCornerShape(8.dp))  // 2. Shape\n        .background(Color.White) // 3. Drawing (background, border)\n        .clickable { }           // 4. Interaction\n)\n</code></pre>\n<h2>KMP Platform-Specific UI</h2>\n<h3>expect/actual for Platform Composables</h3>\n<pre><code>// commonMain\n@Composable\nexpect fun PlatformStatusBar(darkIcons: Boolean)\n\n// androidMain\n@Composable\nactual fun PlatformStatusBar(darkIcons: Boolean) {\n    val systemUiController = rememberSystemUiController()\n    SideEffect { systemUiController.setStatusBarColor(Color.Transparent, darkIcons) }\n}\n\n// iosMain\n@Composable\nactual fun PlatformStatusBar(darkIcons: Boolean) {\n    // iOS handles this via UIKit interop or Info.plist\n}\n</code></pre>\n<h2>Performance</h2>\n<h3>Stable Types for Skippable Recomposition</h3>\n<p>Mark classes as <code>@Stable</code> or <code>@Immutable</code> when all properties are stable:</p>\n<pre><code>@Immutable\ndata class ItemUiModel(\n    val id: String,\n    val title: String,\n    val description: String,\n    val progress: Float\n)\n</code></pre>\n<h3>Use <code>key()</code> and Lazy Lists Correctly</h3>\n<pre><code>LazyColumn {\n    items(\n        items = items,\n        key = { it.id }  // Stable keys enable item reuse and animations\n    ) { item -&gt;\n        ItemRow(item = item)\n    }\n}\n</code></pre>\n<h3>Defer Reads with <code>derivedStateOf</code></h3>\n<pre><code>val listState = rememberLazyListState()\nval showScrollToTop by remember {\n    derivedStateOf { listState.firstVisibleItemIndex &gt; 5 }\n}\n</code></pre>\n<h3>Avoid Allocations in Recomposition</h3>\n<pre><code>// BAD — new lambda and list every recomposition\nitems.filter { it.isActive }.forEach { ActiveItem(it, onClick = { handle(it) }) }\n\n// GOOD — key each item so callbacks stay attached to the right row\nval activeItems = remember(items) { items.filter { it.isActive } }\nactiveItems.forEach { item -&gt;\n    key(item.id) {\n        ActiveItem(item, onClick = { handle(item) })\n    }\n}\n</code></pre>\n<h2>Theming</h2>\n<h3>Material 3 Dynamic Theming</h3>\n<pre><code>@Composable\nfun AppTheme(\n    darkTheme: Boolean = isSystemInDarkTheme(),\n    dynamicColor: Boolean = true,\n    content: @Composable () -&gt; Unit\n) {\n    val colorScheme = when {\n        dynamicColor &amp;&amp; Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.S -&gt; {\n            if (darkTheme) dynamicDarkColorScheme(LocalContext.current)\n            else dynamicLightColorScheme(LocalContext.current)\n        }\n        darkTheme -&gt; darkColorScheme()\n        else -&gt; lightColorScheme()\n    }\n\n    MaterialTheme(colorScheme = colorScheme, content = content)\n}\n</code></pre>\n<h2>Anti-Patterns to Avoid</h2>\n<ul>\n<li>Using <code>mutableStateOf</code> in ViewModels when <code>MutableStateFlow</code> with <code>collectAsStateWithLifecycle</code> is safer for lifecycle</li>\n<li>Passing <code>NavController</code> deep into composables — pass lambda callbacks instead</li>\n<li>Heavy computation inside <code>@Composable</code> functions — move to ViewModel or <code>remember {}</code></li>\n<li>Using <code>LaunchedEffect(Unit)</code> as a substitute for ViewModel init — it re-runs on configuration change in some setups</li>\n<li>Creating new object instances in composable parameters — causes unnecessary recomposition</li>\n</ul>\n<h2>References</h2>\n<p>See skill: <code>android-clean-architecture</code> for module structure and layering.\nSee skill: <code>kotlin-coroutines-flows</code> for coroutine and Flow patterns.</p>\n","files":[{"path":"LICENSE","sizeBytes":1071,"isText":false},{"path":"SKILL.md","sizeBytes":8229,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-12-02T16:56:13.22719Z","sha256":"062851BBD7E1AFE568167F6E993A3F833C249A8D38A1E978D94EFC9197CD9EDF","sizeBytes":3833},"review":null,"source":{"repositoryUrl":"https://github.com/beginwithu/agent-skills","path":"skills/compose-multiplatform-patterns","license":"MIT","commit":"a960304c52212f2e845067047f5cefc0c594d787","subtreeSha":"89D968D9B7027AC7840C850BE0C4B848BCB3E2EE48FC80BFECBD9A1A9CFE4953","lastSyncedAt":"2026-12-02T16:52:59.20206Z"},"reviewedAt":"2026-12-02T17:01:34.659359Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/beginwithu/agent-skills/tree/main/skills/compose-multiplatform-patterns"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install beginwithu-agent-skills@llmmart"},{"target":"git","command":"git clone https://github.com/beginwithu/agent-skills.git"}]}