Repository navigation
feat(h5): support hideHomeButton on H5 #19691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
42cc76c
213c091
22a8469
315e90b
e835be4
48f9315
9832376
27bb7ca
11b4d97
ef7cfd9
abaa436
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { navigateBack, reLaunch } from '../api' | |
| import { isDingTalk } from '../utils' | ||
| import stacks from './stack' | ||
|
|
||
| import type { PageInstance } from '@tarojs/runtime' | ||
| import type PageHandler from './page' | ||
|
|
||
| interface NavigationBarCache { | ||
|
|
@@ -19,6 +20,8 @@ export default class NavigationBarHandler { | |
| navigationBarElement: HTMLElement | ||
| cache: Record<string, NavigationBarCache> | ||
| isLoadDdEntry = false | ||
| // 按页面实例记录已隐藏首页按钮的页面,页面实例销毁后状态随之失效 | ||
| homeButtonHiddenPages = new WeakSet<PageInstance>() | ||
|
|
||
| constructor (pageContext: PageHandler) { | ||
| this.cache = {} | ||
|
|
@@ -38,6 +41,10 @@ export default class NavigationBarHandler { | |
|
|
||
| if (typeof frontColor === 'string') this.setNavigationBarTextStyle(frontColor) | ||
| }) | ||
|
|
||
| eventCenter.on('__taroH5HideHomeButton', () => { | ||
| this.hideHomeButton() | ||
| }) | ||
| } | ||
|
|
||
| private toHomeFn () { | ||
|
|
@@ -102,13 +109,26 @@ export default class NavigationBarHandler { | |
| this.fnBtnToggleToNone() | ||
| } else if (stacks.length > 1) { | ||
| this.fnBtnToggleToBack() | ||
| } else if (this.pageContext.homePage === currentRouter) { | ||
| } else if (this.pageContext.homePage === currentRouter || this.isHomeButtonHidden()) { | ||
| this.fnBtnToggleToNone() | ||
| } else { | ||
| this.fnBtnToggleToHome() | ||
| } | ||
| } | ||
|
|
||
| private isHomeButtonHidden () { | ||
| const page = stacks.last | ||
| return !!page && this.homeButtonHiddenPages.has(page) | ||
| } | ||
|
|
||
| hideHomeButton () { | ||
| // Note: 页面通常在 onShow 中调用,此时 load 尚未执行,但页面已入栈,直接记录在页面实例上,确保 load 时不会重新展示首页按钮 | ||
| const page = stacks.last | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reran the unload case with the API call deferred by one microtask and could still reproduce the cross-page update.
On base The existing test calls the API synchronously inside
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 9832376. The unload guard now stays active until the macrotask after onUnload, so a call deferred by a microtask no longer reaches A, and onShow of the page shown right after the unload is exempt. Added a test that waits for the deferred call and A's return, then checks A still shows its Return to Home button.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for fixing the microtask case. I verified that B's deferred There are two cases that still need coverage:
Could you add regression tests using the real
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed the onShow case in 27bb7ca. The router no longer uses a timed unload window and instead ignores a call unless Current.page is the stack top, so A's own onShow call applies after back or reLaunch and B's microtask call from onUnload is still ignored. Added tests with the real createPageConfig, with and without animation. The setTimeout(30) one I left alone: by then Current.page is A, so a call from B can't be told apart from one A makes itself. |
||
| if (!page) return | ||
| this.homeButtonHiddenPages.add(page) | ||
| this.setFnBtnState() | ||
| } | ||
|
|
||
| shiftLoadingState (show: boolean) { | ||
| if (!this.loadingElement) return | ||
| if (show) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -122,7 +122,8 @@ declare module '../../index' { | |||||
| hideNavigationBarLoading(option?: hideNavigationBarLoading.Option): void | ||||||
|
|
||||||
| /** 隐藏返回首页按钮。微信7.0.7版本起,当用户打开的小程序最底层页面是非首页时,默认展示“返回首页”按钮,开发者可在页面 onShow 中调用 hideHomeButton 进行隐藏。 | ||||||
| * @supported weapp, tt, harmony | ||||||
| * @supported weapp, tt, h5, harmony | ||||||
| * @h5 隐藏当前页面实例 H5 导航栏中的返回首页按钮,不影响返回按钮;通过 reLaunch 等方式重新打开的页面不受影响 | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we recommend calling
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| * @see https://developers.weixin.qq.com/miniprogram/dev/api/ui/navigation-bar/wx.hideHomeButton.html | ||||||
| */ | ||||||
| hideHomeButton(option?: hideHomeButton.Option): Promise<TaroGeneral.CallbackResult> | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
现有用例都是在栈深为 1 的情况下切换页面,没覆盖最常见的 navigateTo 再 navigateBack 回来的流程。补一个用例确认中间出现返回按钮后,回到该页面时首页按钮仍保持隐藏,这个行为不管缓存最终按路由还是按页面实例都应该成立。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the navigate-away-and-back case in 213c091. It checks the back button shows on the intermediate page and the home button is still hidden after returning.