How to close DocumentWindow when click outside of it?

I want to create in my AU/VST3/AXX plugin some settings panel which would be opened in separated window which I can move by dragging like normal window, but I want to make it close by clicking special button or when user click outside of window.

I use for that juce::DocumentWindow. But it doesn’t work fine. Mainly I have two problems.

  1. I have no idea how to close it when user click outside of it.
  2. I would also want to pass through all keyboard events, so user can for example with spacebar start or stop playback in his DAW, but now it doesn’t work.

For any help great thanks in advance.
Best regards