skip to content

In Jetpack Compose, what is a slot API, and why do components such as Scaffold and Button take @Composable lambdas instead of plain values?

level: middleimportance: should knowfreq 42%

answer

  1. an empty space the caller fills
  2. parameters typed as composable lambdas
  3. receiver scopes like RowScope
  4. nullable slot versus empty slot
  5. content goes last

basics

~20 s

A slot API exposes parameters typed as composable lambdas, such as Scaffold's topBar and content or Button's content, so callers fill a space with any UI instead of the component exposing a parameter for every option of its children.

solid answer

~50 s

A slot is a parameter typed as a composable lambda, for example `content: @Composable () -> Unit`. Material components are built on them: `Scaffold` takes `topBar`, `bottomBar`, `floatingActionButton`, `snackbarHost` and `content`, and `Button` takes `content: @Composable RowScope.() -> Unit`. The component owns placement and behaviour; the caller decides what goes in the space, so `Button` needs no separate parameters for a label, an icon and the icon's tint. A receiver such as `RowScope` gives the slot the scope's layout options, and `Scaffold`'s content receives `PaddingValues` that you must apply so the bars do not cover it. A nullable slot, as in `TextField`'s `leadingIcon: @Composable (() -> Unit)? = null`, lets the component tell an absent icon from an empty one. Material puts `modifier` as the first optional parameter and the main `content` slot last, so callers can write it as a trailing lambda.

code

kotlin · 25 lines
kotlin
@Composable
fun ProductCard(
    title: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    badge: (@Composable () -> Unit)? = null,
    actions: @Composable RowScope.() -> Unit = {},
) {
    Column(modifier) {
        Row {
            title()
            if (badge != null) badge()
        }
        Row(content = actions)
    }
}

@Composable
fun SaleProduct(name: String, onAddToCart: () -> Unit) {
    ProductCard(
        title = { Text(name) },
        badge = { Text("Sale") },
    ) {
        TextButton(onClick = onAddToCart) { Text("Add to cart") }
    }
}

go deeper

for a junior

Recognise slots in Scaffold, TopAppBar and Button, and remember to apply the PaddingValues Scaffold gives its content.

for a middle

Explain why components take composable lambdas instead of value parameters, how receiver scopes like RowScope help, and when an optional slot should be nullable.

for a senior

Design an app component's API with slots where content varies and plain parameters where the design system wants consistency, following Material's parameter order.

for a principal

Decide how much freedom the team's design-system components give callers: slots everywhere invite inconsistency, value-only APIs invite forks.

## The idea A **slot** is a parameter of a composable whose type is itself a composable lambda. The component leaves an empty space in its layout and the caller fills it with whatever UI it wants. The pattern is called a **slot API**, and Jetpack Compose's Material components use it heavily to stay flexible without an explosion of configuration parameters. Compare two ways to design a button: 1. With plain values, the button needs a parameter for the label, its colour, an icon, the icon's tint and the icon's position, and still cannot show a badge or a spinner. 2. With a slot, the button takes `content: @Composable RowScope.() -> Unit`, places it with the right padding and minimum size, and lets the caller put a `Text`, an `Icon`, both, or a progress indicator inside. `Button` in Material 3 for Compose does the second. ## Slots in Material components | Component | Slots | Note | |---|---|---| | `Scaffold` | `topBar`, `bottomBar`, `snackbarHost`, `floatingActionButton`, `content` | `content` receives `PaddingValues` | | `TopAppBar` | `title`, `navigationIcon`, `actions` | `actions` has a `RowScope` receiver | | `Button` | `content` | `RowScope` receiver | | `TextField` | `label`, `leadingIcon`, `trailingIcon` and others | optional slots are nullable | Defaults matter. `Scaffold` gives its bar slots an empty default, `= {}`, so you pass only the bars you have. `TextField` declares its optional slots as **nullable**, for example `leadingIcon: @Composable (() -> Unit)? = null`. A `null` slot says "nothing here", which the component can use to change its layout, while an empty lambda would still be called and could still take space. ## Receivers and parameters on slots A slot can carry more than "draw something here": - A **receiver scope** such as `RowScope` lets the caller use the parent's layout options inside the slot. The modifiers that scope offers are covered in the lesson on modifiers and layout. - A **parameter** passes information back. `Scaffold`'s `content: @Composable (PaddingValues) -> Unit` hands the content the space the bars occupy; the documentation says to apply it to the content root, or the top bar will cover the first rows of your screen. ## Writing your own slot API The same pattern works for an app's own components. A product card that must show a title, an optional badge and arbitrary actions can take three slots instead of a dozen strings and flags: ```kotlin @Composable fun ProductCard( title: @Composable () -> Unit, modifier: Modifier = Modifier, badge: (@Composable () -> Unit)? = null, actions: @Composable RowScope.() -> Unit = {}, ) { Column(modifier) { Row { title() if (badge != null) badge() } Row(content = actions) } } ``` Conventions worth copying from Material's own signatures: - `modifier: Modifier = Modifier` is the **first optional** parameter; - the main slot, usually `content`, comes **last**, so callers can pass it as a trailing lambda; - an optional slot is nullable when "absent" and "empty" must look different. ## What slots mean for recomposition A slot is a lambda, and the Compose compiler treats function types as **stable**. A composable lambda passed into a non-inline component can recompose on its own: if a state value is read only inside the `title` slot, a change to it can re-run that slot without re-running the caller. With strong skipping, on by default since Kotlin 2.0.20, lambdas written inside a composable are also remembered across passes, so passing a slot does not hand the component a fresh, unequal argument on every pass. The cost is indirection. A component made only of slots can push too much layout work onto every caller, and a design system usually mixes slots for the parts that vary with plain parameters for the parts that should stay consistent.

  • Why does Scaffold pass PaddingValues into its content slot, and what happens if you ignore it?
    The padding tells the content how much space the top bar, bottom bar and window insets occupy. Apply it to the content root, for example with `Modifier.padding(innerPadding)`; ignore it and the first and last rows sit underneath the bars.
  • When should an optional slot be nullable rather than default to an empty lambda?
    When the component should lay out differently if the slot is absent. `TextField` declares `leadingIcon` as `@Composable (() -> Unit)? = null`, so null means no icon. An empty-lambda default suits slots whose absence changes nothing, as with `Scaffold`'s bars.

A slot is like a picture frame sold with its hanging hardware: the frame decides the size, border and where it hangs, and you choose the picture. The frame never asks for a list of the picture's colours.

  • A slot API is just a composable with many String and Color parameters.
  • Scaffold's PaddingValues are optional decoration you can ignore.
  • An empty-lambda default and a null slot always behave the same.
  • Passing a lambda as a slot makes the component unstable and never skippable.