Gearside Design

Close mmenu using the back button with History API and popstate

Mobile apps have created a precedent of using the device’s back button to close off-canvas menus. This snippet will enhance mmenu by allowing users to close the menu with the browser’s back button using the History API. When the menu is opened, the current page is pushed into the user’s history. Then when the user presses the back button, the history state is triggered which will close the mmenu and stop propagation of the back button. Note that if the user closes the menu, the back button is returned to its normal usage.

JavaScript