Hopefully a fix for #12364

* register the callback before invoking the toggling
* listen for `hidden` instead of `hide`
* account for the possibility of the associated `show` event being preventDefault-ed

Adds relevant unit tests.
Merge pull request #13627 from twbs/maybe-fix-12364

Hopefully fixes trigger focus restoration on modal close