Hide/expand layout

Remember notifications button in facebook? When you press on it the notifications are shown, when you press on somewhere else or on the button again they’re are hidden. Is it possible to create something similar in vaadin application?

The problem occurs when you need to implement layout hide when the user clicks on some other place. It’s easy to add a listener to the button which makes something like this
layout.setVisible(!layout.isVisible());

But in this case hide/show works only on the button press.
Another solution is to use javascript to hide/expand the layout. In this case we cannot control layout’s state from the code.

Has anybody met the solution?