1

I am trying to build a multi-level ActionMenu. A single level ActionMenu is easy following the documents, such as ActionMenu["Single Level", {"1" :> Print[1], "2" :> Print[2], "3" :> Print[3]}] However, I don't know how to expand this to multi-level selection. A naive code such as ActionMenu["Multi-Level", {"1" :> ActionMenu["Level 2", {"a" :> Print[a], "b" :> Print[b]}], "2" :> Print[2], "3" :> Print[3]}] didn't work. Please help...

bakerryd123
  • 427
  • 2
  • 8

1 Answers1

1

You may use AttachCell with EvaluationBox. AttachCell is an experimental function in version 12.2 so behaviour may change.

I AttachCell a new ActionMenu below the evaluating ActionMenu using EvaluationBox. The attached menu deletes itself and performs the action. The other options also delete the attached menu before performing their actions.

Module[
 {acell}
 , ActionMenu[
  "Multi-Level"
  , {
   "1" :> (
     NotebookDelete[acell]
     ; acell =
      AttachCell[
       EvaluationBox[]
       , ActionMenu[
        "Level 2"
        , {
         "a" :> (NotebookDelete[acell]; Print[a])
         , "b" :> (NotebookDelete[acell]; Print[b])
         }
        ]
       , {Left, Bottom}, Automatic, {Left, Top}
       ]
     )
   , "2" :> (NotebookDelete[acell]; Print[2])
   , "3" :> (NotebookDelete[acell]; Print[3])
   }
  ]
 ]

enter image description here

Hope this helps.

Edmund
  • 42,267
  • 3
  • 51
  • 143
  • Thanks for the suggestion, this is a cool solution. I didn't know the AttachCell function previously, seems like a pretty new function. However, I am trying to implement this on a palettes, not sure how it will work out. – bakerryd123 May 19 '21 at 12:58