interactiveScrollbar

Functions summary

Modifier
@Composable
Modifier.interactiveScrollbar(
    state: ScrollIndicatorState,
    orientation: Orientation,
    onThumbDrag: (positionFraction: Float) -> Unit,
    onTrackPress: (positionFraction: Float) -> Unit,
    thickness: Dp,
    thumbMinLength: Dp,
    thumbMaxLengthFraction: @FloatRange(from = 0.0, to = 1.0) Float,
    isFadeEnabled: Boolean,
    fadeDurationMillis: Int,
    fadeDelayMillis: Int,
    mainAxisTrackInset: Dp,
    crossAxisTrackInset: Dp,
    colors: InteractiveScrollbarColors,
    thumbInteractionSource: MutableInteractionSource?
)

A visual and interactive scrollbar that represents the current scroll position of a scrolling component and allows users to scroll the component by dragging the thumb or pressing the track.

Cmn

Functions

Modifier.interactiveScrollbar

@Composable
fun Modifier.interactiveScrollbar(
    state: ScrollIndicatorState,
    orientation: Orientation,
    onThumbDrag: (positionFraction: Float) -> Unit,
    onTrackPress: (positionFraction: Float) -> Unit,
    thickness: Dp = InteractiveScrollbarDefaults.Thickness,
    thumbMinLength: Dp = InteractiveScrollbarDefaults.ThumbMinLength,
    thumbMaxLengthFraction: @FloatRange(from = 0.0, to = 1.0) Float = InteractiveScrollbarDefaults.ThumbMaxLengthFraction,
    isFadeEnabled: Boolean = true,
    fadeDurationMillis: Int = InteractiveScrollbarDefaults.ThumbFadeDurationMillis,
    fadeDelayMillis: Int = InteractiveScrollbarDefaults.ThumbFadeDelayMillis,
    mainAxisTrackInset: Dp = InteractiveScrollbarDefaults.MainAxisTrackInset,
    crossAxisTrackInset: Dp = InteractiveScrollbarDefaults.CrossAxisTrackInset,
    colors: InteractiveScrollbarColors = InteractiveScrollbarDefaults.colors(),
    thumbInteractionSource: MutableInteractionSource? = null
): Modifier

A visual and interactive scrollbar that represents the current scroll position of a scrolling component and allows users to scroll the component by dragging the thumb or pressing the track. For a visual-only scrollbar that does not respond to user input, see nonInteractiveScrollbar.

It is drawn at the end edge (respecting layout direction) for a vertically scrollable component, and at the bottom edge for a horizontally scrollable component.

The scrollbar is only visible if the content size is strictly greater than the viewport size. By default, the scrollbar is shown when scrolling is active and fades out after a delay when scrolling stops. Hovering over the scrollbar will also keep it fully visible (or fade it in if it had faded out). This fading behavior can be disabled by setting isFadeEnabled to false, in which case the scrollbar remains always visible.

To use an interactive scrollbar with a androidx.compose.foundation.lazy.LazyColumn, see:

import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Text
import androidx.compose.material3.interactiveScrollbar
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

val state = rememberLazyListState()
val coroutineScope = rememberCoroutineScope()
val scrollIndicatorState = state.scrollIndicatorState
val scrollToPositionFraction: suspend (Float) -> Unit = { positionFraction ->
    if (scrollIndicatorState != null) {
        val maxScrollOffset =
            scrollIndicatorState.contentSize - scrollIndicatorState.viewportSize
        val targetPx = positionFraction * maxScrollOffset
        val deltaPx = targetPx - scrollIndicatorState.scrollOffset
        state.scrollBy(deltaPx)
    }
}
val scrollbarModifier =
    if (scrollIndicatorState != null) {
        Modifier.interactiveScrollbar(
            state = scrollIndicatorState,
            orientation = Orientation.Vertical,
            onThumbDrag = { coroutineScope.launch { scrollToPositionFraction(it) } },
            onTrackPress = { coroutineScope.launch { scrollToPositionFraction(it) } },
        )
    } else {
        Modifier
    }

LazyColumn(state = state, modifier = scrollbarModifier) {
    items(100) { index ->
        Text(
            text = "Item $index",
            modifier = Modifier.fillMaxWidth().height(50.dp).wrapContentSize(Alignment.Center),
        )
    }
}

To use an interactive scrollbar with a androidx.compose.foundation.layout.Column that uses androidx.compose.foundation.verticalScroll, see:

import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.material3.interactiveScrollbar
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

val state = rememberScrollState()
val coroutineScope = rememberCoroutineScope()
val scrollIndicatorState = state.scrollIndicatorState
val scrollToPositionFraction: suspend (Float) -> Unit = { positionFraction ->
    if (scrollIndicatorState != null) {
        val maxScrollOffset =
            scrollIndicatorState.contentSize - scrollIndicatorState.viewportSize
        val targetPx = (positionFraction * maxScrollOffset).roundToInt()
        state.scrollTo(targetPx)
    }
}
val scrollbarModifier =
    if (scrollIndicatorState != null) {
        Modifier.interactiveScrollbar(
            state = scrollIndicatorState,
            orientation = Orientation.Vertical,
            onThumbDrag = { coroutineScope.launch { scrollToPositionFraction(it) } },
            onTrackPress = { coroutineScope.launch { scrollToPositionFraction(it) } },
        )
    } else {
        Modifier
    }

Column(
    // Chain before verticalScroll so the scrollbar doesn't scroll with content.
    modifier = scrollbarModifier.verticalScroll(state)
) {
    repeat(100) { index ->
        Text(
            text = "Item $index",
            modifier = Modifier.fillMaxWidth().height(50.dp).wrapContentSize(Alignment.Center),
        )
    }
}
Parameters
state: ScrollIndicatorState

the ScrollIndicatorState that represents the scroll state.

orientation: Orientation

the orientation of the scrollbar.

onThumbDrag: (positionFraction: Float) -> Unit

a callback invoked when the scrollbar thumb is dragged. The lambda receives the positionFraction representing the position of the thumb along the track (between 0f and 1f), which should be used to scroll the content. Note that pressing the thumb without moving the pointer does not invoke this callback, it is only invoked once the pointer moves.

onTrackPress: (positionFraction: Float) -> Unit

a callback invoked when the scrollbar track is pressed outside the thumb. The lambda receives the positionFraction representing the target position on the track (between 0f and 1f) to jump or navigate to. Note that if onTrackPress moves the thumb under the pressed position, continuous dragging is handled by onThumbDrag.

thickness: Dp = InteractiveScrollbarDefaults.Thickness

the thickness of the scrollbar.

thumbMinLength: Dp = InteractiveScrollbarDefaults.ThumbMinLength

the minimum length of the scrollbar thumb. Note that the scrollbar will not be shown if the available track length is less than this minimum length. Takes precedence over thumbMaxLengthFraction.

thumbMaxLengthFraction: @FloatRange(from = 0.0, to = 1.0) Float = InteractiveScrollbarDefaults.ThumbMaxLengthFraction

the maximum length of the scrollbar thumb as a fraction of the track length. This should be between 0f and 1f. Clamped to at least thumbMinLength.

isFadeEnabled: Boolean = true

whether the fade behavior is enabled. If enabled, the scrollbar will be shown when scrolling is active, and fades out after a delay when scrolling is idle. If disabled, the scrollbar remains always visible.

fadeDurationMillis: Int = InteractiveScrollbarDefaults.ThumbFadeDurationMillis

the duration of the fade animation in milliseconds.

fadeDelayMillis: Int = InteractiveScrollbarDefaults.ThumbFadeDelayMillis

the delay before the scrollbar fades out in milliseconds.

mainAxisTrackInset: Dp = InteractiveScrollbarDefaults.MainAxisTrackInset

the inset to apply to the scrollbar track along the main axis.

crossAxisTrackInset: Dp = InteractiveScrollbarDefaults.CrossAxisTrackInset

the inset to apply to the scrollbar track along the cross axis.

colors: InteractiveScrollbarColors = InteractiveScrollbarDefaults.colors()

the InteractiveScrollbarColors used to resolve the colors of the scrollbar thumb and track in different states.

thumbInteractionSource: MutableInteractionSource? = null

an optional hoisted MutableInteractionSource for observing and emitting Interactions for the scrollbar thumb. You can use this to change the thumb's appearance or preview it in different states. Externally emitted interactions only affect the thumb's appearance and do not affect fade visibility; set isFadeEnabled to false to preview interaction states. Interactions are only emitted for the thumb (PressInteraction, DragInteraction, and HoverInteraction), not for track presses. Note that if null is provided, interactions will still happen internally.