Skip to content

Commit

Permalink
Fix missing dropdown menu when top app bar overflows (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
boswelja authored Dec 6, 2023
1 parent 9316725 commit a32a128
Showing 1 changed file with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import androidx.compose.animation.togetherWith
import androidx.compose.foundation.layout.Row
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
Expand Down Expand Up @@ -43,9 +45,13 @@ public fun TopAppBarMenuItems(
}
Row(modifier) {
if (hasOverflow) {
val visibleItemRange by remember(menuItems, maxVisibleActions) {
derivedStateOf { 0..<maxVisibleActions }
val visibleItemRange = remember(menuItems, maxVisibleActions) {
0..<maxVisibleActions
}
val overflowItemRange = remember(menuItems, maxVisibleActions) {
maxVisibleActions..<menuItems.size
}

visibleItemRange.forEach {
TopAppBarAction(menuItems[it])
}
Expand All @@ -61,6 +67,14 @@ public fun TopAppBarMenuItems(
),
modifier = Modifier.menuAnchor()
)
ExposedDropdownMenu(
expanded = menuExpanded,
onDismissRequest = { menuExpanded = false }
) {
overflowItemRange.forEach {
DropdownMenuItem(menuItems[it])
}
}
}
} else {
menuItems.forEach { menuItem ->
Expand All @@ -82,4 +96,21 @@ internal fun TopAppBarAction(
) {
Icon(menuItem.imageVector, menuItem.label)
}
}

@Composable
internal fun DropdownMenuItem(
menuItem: MenuItem,
modifier: Modifier = Modifier,
) {
DropdownMenuItem(
text = {
Text(menuItem.label)
},
leadingIcon = {
Icon(menuItem.imageVector, null)
},
onClick = menuItem.onClick,
modifier = modifier,
)
}

0 comments on commit a32a128

Please sign in to comment.