Skip to content

Popup menus ​

Use a popup menu to expose a small set of options or commands near a trigger. The SaltUI desktop screenshot shows the sort button, radio choices, a toggle, and the open Order submenu.

SaltUI desktop sort menu with the title option selected, the artist toggle enabled, and the Order submenu openSaltUI desktop sort menu with the title option selected, the artist toggle enabled, and the Order submenu open

The examples use the menu APIs in SaltUI 3.1.0-alpha02 under SaltTheme. Add @OptIn(UnstableSaltUiApi::class) where you call them.

Choose a component ​

ScenarioComponentWhat you control
A settings row that displays its current valueItemDropdown with PopupMenuItemThe value and when to call state.dismiss().
Commands, selection, submenus, or desktop keyboard navigationPopupActionMenuThe entries, expanded, dismissal, and application state.
A custom layout, such as cache information and a buttonPopupMenuThe contents and when to close.

Select a settings value ​

ItemDropdown provides the trigger, value, and chevron. Show the current setting in value, mark the matching option with selected, and dismiss in the selection callback.

kotlin
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.moriafly.salt.ui.ItemDropdown
import com.moriafly.salt.ui.UnstableSaltUiApi
import com.moriafly.salt.ui.popup.PopupMenuItem

@OptIn(UnstableSaltUiApi::class)
@Composable
fun LyricsVisibilitySetting(
    showLyrics: Boolean,
    onShowLyricsChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier
) {
    ItemDropdown(
        text = "Show lyrics",
        value = if (showLyrics) "On" else "Off",
        modifier = modifier
    ) {
        PopupMenuItem(
            onClick = {
                state.dismiss()
                onShowLyricsChange(true)
            },
            selected = showLyrics,
            text = "On"
        )
        PopupMenuItem(
            onClick = {
                state.dismiss()
                onShowLyricsChange(false)
            },
            selected = !showLyrics,
            text = "Off"
        )
    }
}

Use rememberPopupState() and pass state when another UI element must also control visibility. For migration from deprecated ItemPopup or ItemSelect, put the current value in value and reserve sub for supporting text. The slot-based PopupMenuItem overload supports custom content and disabled states. Use PopupActionMenu.radioItem for a structured choice with selection semantics and keyboard navigation.

Show an action menu ​

Place the trigger and menu in the same Box so the menu can use the trigger's layout bounds. Leave the menu call in composition when setting expanded = false so mobile can play the exit animation.

kotlin
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.moriafly.salt.ui.Button
import com.moriafly.salt.ui.Text
import com.moriafly.salt.ui.UnstableSaltUiApi
import com.moriafly.salt.ui.popup.PopupActionMenu
import com.moriafly.salt.ui.popup.popupMenuAnchor
import com.moriafly.salt.ui.popup.rememberPopupMenuAnchor

@OptIn(UnstableSaltUiApi::class)
@Composable
fun LibraryMenu(
    favoritesOnly: Boolean,
    showArtist: Boolean,
    onFavoritesOnlyChange: (Boolean) -> Unit,
    onShowArtistChange: (Boolean) -> Unit,
    onRefresh: () -> Unit,
    modifier: Modifier = Modifier
) {
    var expanded by remember { mutableStateOf(false) }
    val anchor = rememberPopupMenuAnchor()

    Box(modifier = modifier) {
        Button(
            onClick = { anchor.open { expanded = true } },
            text = "Library options",
            modifier = Modifier.popupMenuAnchor(anchor)
        )

        PopupActionMenu(
            expanded = expanded,
            onDismissRequest = { expanded = false },
            anchor = anchor
        ) {
            item(
                text = { Text(text = "Refresh library") },
                onClick = onRefresh
            )
            divider()
            submenu(
                text = { Text(text = "Show") }
            ) {
                radioItem(
                    text = { Text(text = "All songs") },
                    selected = !favoritesOnly,
                    onClick = { onFavoritesOnlyChange(false) }
                )
                radioItem(
                    text = { Text(text = "Favorites only") },
                    selected = favoritesOnly,
                    onClick = { onFavoritesOnlyChange(true) }
                )
            }
            toggleItem(
                text = { Text(text = "Show artist") },
                checked = showArtist,
                onCheckedChange = onShowArtistChange,
                closeOnClick = false
            )
        }
    }
}
EntryUse
itemRun a command, such as refresh or copy.
radioItemDerive each entry’s selected value from a single selection.
toggleItemFlip an independent setting through onCheckedChange.
submenuGroup related entries. The label becomes the page title on mobile.
dividerSeparate related command groups.

Actions, radios, and toggles call onDismissRequest before invoking the action by default. Use closeOnClick = false for repeated changes, enabled = false to disable an entry, and trailingContent for status or a shortcut label. Register shortcut actions in your app. Give decorative leading icons contentDescription = null when the label already communicates the action.

Placement and keyboard behavior ​

  • Use one rememberPopupMenuAnchor() per trigger. Add Modifier.popupMenuAnchor(anchor) and open with anchor.open { expanded = true } from the click, right-click, or long-press callback. Put it before any custom modifier that consumes pointer input.
  • Keyboard, accessibility, and programmatic activation use the layout anchor. Omit anchor when you always want layout-based placement. Use DpOffset for a small adjustment, or supply a popupPositionProvider for custom positioning. The provider receives the bounds, the window size, and the visible panel size in pixels.
  • On Desktop, submenus open beside the parent. With the default focus settings, Up and Down move between enabled entries, Home and End move to the first and last enabled entry, and Enter or Space activates an entry. The directional keys open and leave submenus according to the layout direction. Escape goes back one level or dismisses the root.
  • On Android and iOS, a submenu replaces the current menu page. Use the back arrow to return to its parent. Short touch menus support slide selection. Overflowing menus can scroll.

See the runnable PopupMenuScreen sample for pointer triggers and keyboard-friendly entries.

Use header for a text input above the scrollable entries. Keep the query in the calling composable and generate choices from matching results.

kotlin
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.moriafly.salt.ui.Button
import com.moriafly.salt.ui.ItemEdit
import com.moriafly.salt.ui.Text
import com.moriafly.salt.ui.UnstableSaltUiApi
import com.moriafly.salt.ui.popup.PopupActionMenu

data class OutputDevice(
    val id: String,
    val name: String
)

@OptIn(UnstableSaltUiApi::class)
@Composable
fun OutputDeviceMenu(
    devices: List<OutputDevice>,
    selectedDeviceId: String?,
    onDeviceSelect: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    var expanded by remember { mutableStateOf(false) }
    var query by remember { mutableStateOf("") }
    val matches = devices.filter { it.name.contains(query, ignoreCase = true) }

    Box(modifier = modifier) {
        Button(
            onClick = {
                query = ""
                expanded = true
            },
            text = "Select a device"
        )

        PopupActionMenu(
            expanded = expanded,
            onDismissRequest = { expanded = false },
            header = {
                Column {
                    ItemEdit(
                        text = query,
                        onChange = { query = it },
                        hint = "Search devices"
                    )

                    if (matches.isEmpty()) {
                        Text(text = "No matching devices")
                    }
                }
            }
        ) {
            matches.forEach { device ->
                radioItem(
                    text = { Text(text = device.name) },
                    selected = selectedDeviceId == device.id,
                    onClick = { onDeviceSelect(device.id) }
                )
            }
        }
    }
}

Show custom popup content ​

PopupMenu accepts a ColumnScope for text, controls, or PopupMenuItem. If the body owns scrolling, set scrollable = false and constrain its width and height as needed.

kotlin
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.moriafly.salt.ui.Button
import com.moriafly.salt.ui.Icon
import com.moriafly.salt.ui.Text
import com.moriafly.salt.ui.UnstableSaltUiApi
import com.moriafly.salt.ui.icons.Close
import com.moriafly.salt.ui.icons.SaltIcons
import com.moriafly.salt.ui.popup.PopupMenu
import com.moriafly.salt.ui.popup.PopupMenuDefaults
import com.moriafly.salt.ui.popup.PopupMenuItem
import com.moriafly.salt.ui.popup.PopupMenuMaterial

@OptIn(UnstableSaltUiApi::class)
@Composable
fun CacheDetails(
    sizeLabel: String,
    modifier: Modifier = Modifier
) {
    var expanded by remember { mutableStateOf(false) }

    Box(modifier = modifier) {
        Button(
            onClick = { expanded = true },
            text = "Cache details"
        )

        PopupMenu(
            expanded = expanded,
            onDismissRequest = { expanded = false },
            style = PopupMenuDefaults.style(material = PopupMenuMaterial.Blur)
        ) {
            Text(
                text = "Space used: $sizeLabel",
                modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp)
            )

            PopupMenuItem(
                text = { Text(text = "Close") },
                onClick = { expanded = false },
                leadingIcon = {
                    Icon(
                        imageVector = SaltIcons.Close,
                        contentDescription = null
                    )
                }
            )
        }
    }
}

Color and material ​

Use SaltColors.defaultLight(popup = ...) or defaultDark(popup = ...) to set a theme-wide color. To style one menu, pass PopupMenuDefaults.style(...) to PopupActionMenu, PopupMenu, or ItemDropdown. An action menu shares the style with its submenus.

OptionBehavior
materialDefaults to Glass. Choose Blur or Solid explicitly. Android 12 uses blur for Glass. Older Android versions use solid.
containerColorReads the theme’s popup color by default. Supplies the solid or fallback background.
tintColorTints sampled content. Defaults depend on the material and theme. Pass Color.Transparent to remove the tint.
borderColorDefaults to the theme stroke in light mode and 16% opaque white in dark mode.
backdropColorFills behind the captured pixels. Use its alpha to control fill, or pass Color.Transparent to preserve transparent regions.

The PopupMenu(backgroundColor = ...) overload can still set a container color. Use style to configure tint and backdrop independently.

Capture a backdrop ​

Wrap the Compose content you want to sample in PopupMenuBackdrop. Glass and blur menus use the nearest backdrop scope. Use PopupMenuBackdrop(enabled = false) to stop sampling in its scope. When a source is unavailable, the menu falls back to containerColor.

kotlin
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.moriafly.salt.ui.SaltTheme
import com.moriafly.salt.ui.UnstableSaltUiApi
import com.moriafly.salt.ui.popup.PopupMenuBackdrop

@OptIn(UnstableSaltUiApi::class)
@Composable
fun MenuHost(
    modifier: Modifier = Modifier,
    backgroundColor: Color = SaltTheme.colors.background,
    content: @Composable () -> Unit
) {
    PopupMenuBackdrop(
        modifier = modifier.fillMaxSize()
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(backgroundColor)
        ) {
            content()
        }
    }
}

For native desktop materials, configure the desktop window and keep areas where Acrylic, Mica, or Vibrancy should show through transparent or translucent. If an area remains opaque, check the Compose background and the menu's backdrop fill and tint.

Open a menu in a Sheet ​

Sheet already provides a local backdrop to its content and actions. A nested PopupMenuBackdrop lets you sample a more specific region.

kotlin
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.moriafly.salt.ui.UnstableSaltUiApi
import com.moriafly.salt.ui.sheet.Sheet

@OptIn(UnstableSaltUiApi::class)
@Composable
fun CacheSettingsSheet(
    visible: Boolean,
    sizeLabel: String,
    onDismissRequest: () -> Unit,
    modifier: Modifier = Modifier
) {
    Sheet(
        visible = visible,
        onDismissRequest = onDismissRequest,
        modifier = modifier,
        title = "Cache settings"
    ) {
        CacheDetails(sizeLabel = sizeLabel)
    }
}

Google/Android 是 Google LLC 的注册商标,Salt Player 和糖醋音乐均是寻浔科技(上海)有限公司在中华人民共和国的注册商标