On Close Event to perform an action

Hello, is there a way to capture the chat closing event to perform an action before closing it, for example show a confirmation modal and if you accept it do the chat closure ?