PricingBlog

Hide options menu if open and click outside menu

  • danishnizzle-1258172839836651561

    danishnizzle

    1 year ago

    I'm working on the todo tut and trying to add a feature where you have an options menu on each list item which contains options to "delete" or "duplicate" a given task item.

    I'm using a variable called "show" to store a boolean value which dictates to hide/show the menu and have it working just fine when you click the ... , however I'm a bit confused as to how to set up a formula to close the menu when clicking outside the element.

    I'm practicing setting this "options menu" as a component so I could use it for any list items that might have options menus in the app. My thought was to bind a click event to a parent div (shown in image) and use a formula to check if the "show" variable was presently set to "true" and if so, set it to "false" but this doesn't seem to be working as I expected.
    1258172840323055707-hideonclick.gif
    1258172840763592715-image.png
    1258172841082228867-image.png
  • Tod-1258172844056252492

    Tod

    1 year ago

    Great energy @danishnizzle! Your continuous contribution to the toddle Community just made you advance to Community Level 2!
  • lucasg-1258175384541991023

    Lucas G

    1 year ago

    1258175384344596622-image.png
  • This will change the variable to the opposite without having to worry about If statements
  • lucasg-1258176070159433728

    Lucas G

    1 year ago

    Your GIF shows it to be working as intended though. It seems like the click closes the dropdown
  • danishnizzle-1258177455236186162

    danishnizzle

    1 year ago

    right - so I do have the menu opening and closing when clicking the ellipses

    what im trying to ALSO do is close it if the user clicks outside the menu
  • and the menu is open
  • the gif is not an actual toddle app
  • just an example of how i would like it to function
  • lucasg-1258178167664017508

    Lucas G

    1 year ago

    Ah, got it.
  • That makes a lot more sense lol
  • lucasg-1258178492533833919

    Lucas G

    1 year ago

    Clicking the parent div of the component wouldn't work as that would essentially be the same as clicking the ellipsis and would not record external clicks
  • lucasg-1258178765222318182

    Lucas G

    1 year ago

    To close when clicking outside, you have two main options. Make the dropdown using the Popover API or set an 'invisible', blank div that covers the page in between the dropdown and the rest of the app which sets the show variable to false when clicked
  • Does that make sense?
  • The 'old-school' method of creating modals and similar
  • danishnizzle-1258179243280695356

    danishnizzle

    1 year ago

    the blank div method is the old-school way?
  • lucasg-1258179285806747791

    Lucas G

    1 year ago

    Kinda
  • I just meant it has been used for a long time as it is very simple to set up
  • danishnizzle-1258179559950385199

    danishnizzle

    1 year ago

    kk -- I think i'll research the popover API
  • i found blog post
  • lucasg-1258179617660076114

    Lucas G

    1 year ago

    The 'starter pack' has a dropdown which uses it
  • You could use that or just clone the dropdown component, either way it would be a good start
  • danishnizzle-1258179778687533136

    danishnizzle

    1 year ago

    ok sg -- would you say this is the more modern approach?
  • or i guess more "correct" approach?
  • lucasg-1258179947177185291

    Lucas G

    1 year ago

    Techincally, more modern, yes. Popover API just became available earlier this year
  • Solved a lot of annoyances with building modals and dropdowns
  • It is still not entirely complete as it isn't simple to position the element for now, at least not until Anchor() CSS changes happen or similar
  • danishnizzle-1258181004154114179

    danishnizzle

    1 year ago

    so dumb q -- and u don't have to baby me here much longer 😛 -- in theory -- could i basically do this somehow?

    where I bind a click event to the document and have logic to check and see that I'm essentially NOT clicking on the ellipses or menu?
    1258181003957239960-image.png
  • lucasg-1258181174514290792

    Lucas G

    1 year ago

    Definitely
  • You can always bring in custom actions
  • But people always tend to favor 'native' toddle solutions so I tend not to mention them as much
  • lucasg-1258181640040087593

    Lucas G

    1 year ago

    You'd use toddle's ctx.triggerActionEvent() to pass the relevant data back to toddle to handle the variable state
  • danishnizzle-1258181854003990619

    danishnizzle

    1 year ago

    ok sweet -- thx for all your help, will look into the items you've suggested 🙂
    👍2