Improve scrollbar a11y

This commit is contained in:
FooIbar committed 2024-11-06 23:44:48 +08:00
1 parent 72e28b8e04
commit e71dd1d576
2 files changed
+12 -6

No files matched your search

@@ -28,6 +28,7 @@ import androidx.compose.foundation.interaction.DragInteraction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsHoveredAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.shape.RoundedCornerShape
@@ -78,6 +79,7 @@ val LocalScrollbarStyle = staticCompositionLocalOf { defaultScrollbarStyle() }
data class ScrollbarStyle(
val minimalHeight: Dp,
val thickness: Dp,
val padding: Dp,
val shape: Shape,
val hoverDurationMillis: Int,
val unhoverColor: Color,
@@ -90,6 +92,7 @@ data class ScrollbarStyle(
fun defaultScrollbarStyle() = ScrollbarStyle(
minimalHeight = 16.dp,
thickness = 8.dp,
padding = 0.dp,
shape = RoundedCornerShape(4.dp),
hoverDurationMillis = 300,
unhoverColor = Color.Black.copy(alpha = 0.12f),
@@ -239,7 +242,7 @@ private fun Scrollbar(
)
}
val scrollThickness = style.thickness.roundToPx()
val scrollThickness = (style.thickness + style.padding * 2).roundToPx()
val measurePolicy = if (isVertical) {
remember(sliderAdapter, scrollThickness) {
verticalMeasurePolicy(sliderAdapter, { containerSize = it }, scrollThickness)
@@ -261,7 +264,11 @@ private fun Scrollbar(
Layout(
{
Box(
Modifier
if (isVertical) {
Modifier.padding(horizontal = style.padding)
} else {
Modifier.padding(vertical = style.padding)
}
.background(if (isVisible) color else Color.Transparent, style.shape)
.scrollbarDrag(
interactionSource = interactionSource,
@@ -56,9 +56,7 @@ fun VerticalScrollbar(
adapter = adapter,
modifier = scrollbarModifier
.padding(
start = HorizontalPadding,
top = contentPadding.calculateTopPadding(),
end = HorizontalPadding,
bottom = contentPadding.calculateBottomPadding(),
)
.graphicsLayer { alpha = scrollbarAlpha.value },
@@ -70,13 +68,14 @@ fun VerticalScrollbar(
fun scrollbarStyle(color: Color) = ScrollbarStyle(
minimalHeight = ThumbLength,
thickness = ThumbThickness,
hoverDurationMillis = 300,
padding = ThumbPadding,
shape = ThumbShape,
hoverDurationMillis = 300,
unhoverColor = color,
hoverColor = color,
)
private val HorizontalPadding = 8.dp
private val ThumbPadding = 8.dp
private val ThumbLength = 48.dp
private val ThumbThickness = 8.dp
private val ThumbShape = RoundedCornerShape(ThumbThickness / 2)