Skip to content

feat: on back navigation, close mobile sidebar - #5462

Merged
slorber merged 3 commits into
mainfrom
slorber/handle-backward-navigation
Sep 2, 2021
Merged

slorber merged 3 commits into
mainfrom
slorber/handle-backward-navigation

Conversation

@slorber

@slorber slorber commented Sep 1, 2021

Copy link
Copy Markdown
Collaborator

Motivation

Android users are used to using the hardware back button of their phone to close menus and drawers (cf Android apps, like Gmail)

Docusaurus users on Android might try that, and instead of closing the opened sidebar drawer, it would navigate to the previous page, potentially leaving the Docusaurus site. We should try to fix that UX issue.

Unfortunately, there's no browser API to easily detect or prevent backward navigation, and we have to use some tricks on top of the history package that offers a block() feature.

Still, it's not possible to distinguish easily between backward/forward navigation, so this remains a best effort.
Closing the mobile sidebar on forward navigation is not an UX issue + the probably of an user using forward navigation on mobile with the drawer open is low, so this is a safe usecase.

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

preview on browser + android

@slorber slorber added the pr: new feature This PR adds a new API or behavior. label Sep 1, 2021
@slorber
slorber requested a review from lex111 as a code owner September 1, 2021 17:01
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Sep 1, 2021
@netlify

netlify Bot commented Sep 1, 2021 •

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Sep 1, 2021 •

Copy link
Copy Markdown

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 72
🟢 Accessibility 98
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-5462--docusaurus-2.netlify.app/

@github-actions

github-actions Bot commented Sep 1, 2021 •

Copy link
Copy Markdown

Size Change: +532 B (0%)

Total Size: 822 kB

Filename Size Change
website/build/assets/css/styles.********.css 93.9 kB +160 B (0%)
website/build/assets/js/main.********.js 412 kB +234 B (0%)
ℹ️ View Unchanged
Filename Size Change
website/.docusaurus/globalData.json 36.5 kB 0 B
website/build/blog/2017/12/14/introducing-docusaurus/index.html 66.8 kB +20 B (0%)
website/build/blog/index.html 37.7 kB +20 B (0%)
website/build/docs/index.html 44.7 kB +19 B (0%)
website/build/docs/installation/index.html 52.7 kB +19 B (0%)
website/build/index.html 30.4 kB +20 B (0%)
website/build/tests/docs/index.html 25 kB +20 B (0%)
website/build/tests/docs/standalone/index.html 22.6 kB +20 B (0%)

compressed-size-action

@lex111

lex111 commented Sep 1, 2021

Copy link
Copy Markdown
Contributor

Very ambivalent feature. This is more of behavior expected from real mobile app, but we just have regular website in browser. And if user in browser presses on back button, it should go to previous page in browser history, because that's how browsers works.
Especially this feature is only available for Android, right? I don't understand, there have been so many requests for it? For me, this only introduces new extra code and complexity. Moreover, it is rare use-case, typical for mobile apps, so why need to implement this?

@slorber

slorber commented Sep 2, 2021

Copy link
Copy Markdown
Collaborator Author

This is more of behavior expected from real mobile app, but we just have regular website in browser.

Yes but also PWA

And if user in browser presses on back button, it should go to previous page in browser history, because that's how browsers works.

Browsers can do what you tell them to do if std APIs allow that. Preventing navigation is a common pattern, most used when editing a form, but can be useful in other cases too.

Especially this feature is only available for Android, right?

It would work for any mobile (at least small width device) that has back/forward navigation, which is just more physically accessible on Android

I don't understand, there have been so many requests for it?

I felt the pain of pressing the back button and leaving the page I was on, and also remember someone mentioned this issue a while ago (can't find ref).

The weird thing is that it navigates backward, but the drawer remains open, so we only see the page transition happening through the overlay.

If you use a full-screen drawer like the React-Native website, you don't really understand what is happening because your drawer remains open, and only the drawer highlighted items change.

At least I think we should close the drawer when navigating back/forward (even if the navigation is not prevented).

For me, this only introduces new extra code and complexity.

Forgot to push yesterday, my new implementation is simpler.

It doesn't feel like a huge JS/complexity addition even if the ROI is limited.

In any case, I think we should close the drawer on POP events (even if we don't prevent the transition), so similar would need to be added. I think preventing the transition in this case is a nice UX addition.


Note:

  • Gmail mobile has some kind of drawer that you close with the back button (but the drawer has its own URL)
  • Youtube mobile has an unanimated profile drawer that close with the back button (but the drawer has a hash in URL)
  • VuePress/VitePress/Nextra all close the drawer on pop but do not prevent the transition
  • On all non-SPA apps, navigating back would close the drawer by default
  • On SPA apps, closing the drawer has to be implemented manually, and popular SPA doc options to that

I'm going to move on and merge this because I think closing the drawer is what we should do, and what SPA doc competitors do too. If anyone ever complain about the prevented POP transition, we could easily remove this or make it optional (same code, it's just a matter of returning undefined instead of false), but I definitively think it adds a little advantage to Docusaurus.

@lex111

lex111 commented Sep 2, 2021

Copy link
Copy Markdown
Contributor

Well, I see, although I still remain of my opinion that such behavior inherent to mobile apps should not be implemented on regular website. Instead this, we could just fix mobile sidebar closing when pressing back button. Maybe get opinion from users (eg. on Twitter) about this before merging?

@slorber

slorber commented Sep 2, 2021

Copy link
Copy Markdown
Collaborator Author

Created a poll here:

https://twitter.com/sebastienlorber/status/1433424873955610639

I'm going to just close the drawer for now and merge, and eventually block the navigation later if people agree with my proposition

@slorber
slorber merged commit f361e89 into main Sep 2, 2021
@slorber
slorber deleted the slorber/handle-backward-navigation branch September 2, 2021 14:03
@lex111

lex111 commented Sep 2, 2021

Copy link
Copy Markdown
Contributor

That's good option for now, thanks.

mrizwanashiq pushed a commit to mrizwanashiq/docusaurus that referenced this pull request Jun 25, 2026
* On back, close mobile navbar sidebar

* more reliable code to block history pop events

* android backbutton: just close the drawer without cancellin the backward navigation
sparanoid pushed a commit to sparanoid/docusaurus that referenced this pull request Oct 12, 2026
* On back, close mobile navbar sidebar

* more reliable code to block history pop events

* android backbutton: just close the drawer without cancellin the backward navigation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants