How to add window close handler?

I have some project, and I want to add some logic. I need to open my modal when user close app, and have some unsaved changes.

What are the ways in retool to add window close handler ???

I find solution, that I can use custom component, but I don`t understand how to use it?