Closing an expanded element when clicked outside of?

474 views
Skip to first unread message

Jamison Dance

unread,
Feb 27, 2016, 7:39:22 AM2/27/16
to elm-d...@googlegroups.com
I'm currently building a menu element that expands when you click on it to reveal more options. It is pretty simple to make clicking on the element toggle the expand/collapse state.

However, my ideal interaction is to collapse the element when you click *anywhere* outside of it. This is pretty common in JS, but usually involves some global event handler checking clicks on `window` to see if they occur inside our element or not (see https://github.com/javierbyte/react-clickaway/blob/master/index.js for an example).

What is a good way to replicate this interaction in the Elm Architecture?

Bernd Meyer

unread,
Feb 27, 2016, 7:52:44 AM2/27/16
to elm-d...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ossi Hanhinen

unread,
Feb 28, 2016, 8:09:16 AM2/28/16
to Elm Discuss
Our straightforward solution was to add a full window translucent div (commonly referred to as a backdrop) that has an onClick handler for closing any transient dialogs/menus. That would sit at e.g. z-index 9 and the transient dialog/menu thing at z-index 10.

Jamison Dance

unread,
Mar 3, 2016, 5:00:00 PM3/3/16
to Elm Discuss
Thanks Ossi, this approach seemed to work the best for me.

One wrinkle I wasn't aware of is that z-index only applies to things that aren't `position: static`. I had to set the dialog I wanted to appear on top of the backdrop to `position: relative` for it to work properly, but things are working beautifully now.
Reply all
Reply to author
Forward
0 new messages