Is there a way to draw multi-level menus with `Dro...
# compose
j
Is there a way to draw multi-level menus with
DropdownMenu
(i.e. have `DropdownMenuItem`s open a sub-
DropdownMenu
) ?
c
Not sure if this is what you're asking for, but something like this in Compose would be cool. Cc @saket https://github.com/saket/cascade
j
Yeah that would be cool, even without fancy animation, just like https://material.io/components/menus
Oh it’s called “Cascading menu” and Material spec says it should be desktop only 😕
z
👀 looks at Google Docs’ app menus…
👀 1
c
***installs google docs***
z
This is iOS, but last time I looked it did the same thing on Android.
j
Yepp, still there on Android too. Cool 🙂 So Compose material package “must” have an implementation… 🤣
Pasting here a concoction I came up with:
Copy code
enum class SomeState {
  HIDDEN,
  MENU,
  SUBMENU,
}

@Composable
fun MenuWithSubmenu(someState: MutableState<SomeState>) {
  var state by someState
  DropdownMenu(
    expanded = state != SomeState.HIDDEN,
    onDismissRequest = { state = SomeState.HIDDEN }
  ) {
    Crossfade(targetState = state) {
      Column {
        when (it) {
          SomeState.HIDDEN -> {}
          <http://SomeState.MENU|SomeState.MENU> -> {
            DropdownMenuItem(onClick = { state = SomeState.SUBMENU }) {
              Text(text = "Something deep...")
            }
          }
          SomeState.SUBMENU -> {
            DropdownMenuItem(onClick = { TODO() }) {
              Text(text = "Click me!")
            }
          }
        }
      }
    }
  }
}
102 Views