# interactiveScrollbar

> 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.

## API Reference

### interactiveScrollbar

> Source set: Common

```kotlin
@Composable
public fun Modifier.interactiveScrollbar(
    state: ScrollIndicatorState,
    orientation: Orientation,
    onThumbDrag: (positionFraction: Float) -> Unit,
    onTrackPress: (positionFraction: Float) -> Unit,
    thickness: Dp = InteractiveScrollbarDefaults.Thickness,
    thumbMinLength: Dp = InteractiveScrollbarDefaults.ThumbMinLength,
    thumbMaxLengthFraction: 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](https://composables.com/jetpack-compose/androidx.compose.material3/material3/modifiers/nonInteractiveScrollbar/api.md).

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:

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

#### Parameters

| | |
| --- | --- |
| state | the [ScrollIndicatorState](https://composables.com/jetpack-compose/androidx.compose.foundation/foundation/interfaces/ScrollIndicatorState/api.md) that represents the scroll state. |
| orientation | the orientation of the scrollbar. |
| onThumbDrag | 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 | 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 | the thickness of the scrollbar. |
| 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 | 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 | 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 | the duration of the fade animation in milliseconds. |
| fadeDelayMillis | the delay before the scrollbar fades out in milliseconds. |
| mainAxisTrackInset | the inset to apply to the scrollbar track along the main axis. |
| crossAxisTrackInset | the inset to apply to the scrollbar track along the cross axis. |
| colors | the [InteractiveScrollbarColors](https://composables.com/jetpack-compose/androidx.compose.material3/material3/classes/InteractiveScrollbarColors/api.md) used to resolve the colors of the scrollbar thumb and track in different states. |
| thumbInteractionSource | an optional hoisted [MutableInteractionSource](https://composables.com/jetpack-compose/androidx.compose.foundation/foundation/interfaces/MutableInteractionSource/api.md) for observing and emitting [Interaction](https://composables.com/jetpack-compose/androidx.compose.foundation/foundation/interfaces/Interaction/api.md)s 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](https://composables.com/jetpack-compose/androidx.compose.foundation/foundation/interfaces/PressInteraction/api.md), [DragInteraction](https://composables.com/jetpack-compose/androidx.compose.foundation/foundation/interfaces/DragInteraction/api.md), and [HoverInteraction](https://composables.com/jetpack-compose/androidx.compose.foundation/foundation/interfaces/HoverInteraction/api.md)), not for track presses. Note that if `null` is provided, interactions will still happen internally. |

## Code Examples

### InteractiveScrollbarWithLazyColumnSample

```kotlin
@Sampled
@Composable
fun InteractiveScrollbarWithLazyColumnSample() {
    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),
            )
        }
    }
}
```

### InteractiveScrollbarWithVerticalScrollSample

```kotlin
@Sampled
@Composable
fun InteractiveScrollbarWithVerticalScrollSample() {
    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),
            )
        }
    }
}
```
