@tarojs/router 4.0.0-beta.1 → 4.0.0-beta.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +17 -3
  2. package/dist/api.js +31 -22
  3. package/dist/events/resize.d.ts +1 -1
  4. package/dist/events/resize.js +15 -7
  5. package/dist/events/scroll.d.ts +1 -1
  6. package/dist/events/scroll.js +4 -1
  7. package/dist/history.d.ts +22 -1
  8. package/dist/history.js +20 -8
  9. package/dist/index.cjs.js +793 -339
  10. package/dist/index.d.ts +6 -1
  11. package/dist/index.esm.js +753 -311
  12. package/dist/index.js +52 -4
  13. package/dist/navigationBar.d.ts +2 -0
  14. package/dist/navigationBar.js +44 -0
  15. package/dist/router/index.js +7 -3
  16. package/dist/router/mpa.d.ts +2 -1
  17. package/dist/router/mpa.js +29 -19
  18. package/dist/router/multi-page.d.ts +5 -2
  19. package/dist/router/multi-page.js +27 -43
  20. package/dist/router/navigation-bar.d.ts +36 -0
  21. package/dist/router/navigation-bar.js +252 -0
  22. package/dist/router/page.d.ts +11 -4
  23. package/dist/router/page.js +62 -59
  24. package/dist/router/spa.d.ts +2 -1
  25. package/dist/router/spa.js +81 -43
  26. package/dist/router/stack.d.ts +1 -1
  27. package/dist/router/stack.js +2 -1
  28. package/dist/style.d.ts +6 -1
  29. package/dist/style.js +106 -7
  30. package/dist/tabbar.d.ts +2 -1
  31. package/dist/tabbar.js +4 -3
  32. package/dist/utils/index.d.ts +1 -8
  33. package/dist/utils/index.js +5 -20
  34. package/dist/utils/navigate.d.ts +9 -5
  35. package/dist/utils/navigate.js +24 -37
  36. package/package.json +28 -27
  37. package/types/api.d.ts +5 -0
  38. package/types/component.d.ts +5 -0
  39. package/types/taro.d.ts +8 -0
  40. package/dist/index.cjs.d.ts +0 -22
  41. package/dist/index.cjs.js.map +0 -1
  42. package/dist/index.esm.d.ts +0 -22
  43. package/dist/index.esm.js.map +0 -1
@@ -1,15 +1,20 @@
1
- import { PageInstance } from '@tarojs/runtime';
1
+ import NavigationBarHandler from './navigation-bar';
2
+ import type { PageInstance } from '@tarojs/runtime';
2
3
  import type { PageConfig, RouterAnimate } from '@tarojs/taro';
4
+ import type { History } from 'history';
3
5
  import type { Route, SpaRouterConfig } from '../../types/router';
4
6
  export default class PageHandler {
5
- protected config: SpaRouterConfig;
7
+ history: History;
8
+ config: SpaRouterConfig;
6
9
  protected readonly defaultAnimation: RouterAnimate;
7
10
  protected unloadTimer: ReturnType<typeof setTimeout> | null;
8
11
  protected hideTimer: ReturnType<typeof setTimeout> | null;
9
12
  protected lastHidePage: HTMLElement | null;
10
13
  protected lastUnloadPage: PageInstance | null;
14
+ protected navigationBarHandler: NavigationBarHandler;
11
15
  homePage: string;
12
- constructor(config: SpaRouterConfig);
16
+ originHomePage: string;
17
+ constructor(config: SpaRouterConfig, history: History);
13
18
  get currentPage(): string;
14
19
  get appId(): string;
15
20
  get router(): import("../../types/router").Router;
@@ -23,9 +28,11 @@ export default class PageHandler {
23
28
  get animationDuration(): number;
24
29
  set pathname(p: string);
25
30
  get pathname(): string;
31
+ get originPathname(): string;
26
32
  get basename(): string;
27
33
  get pageConfig(): Route | undefined;
28
34
  isTabBar(pathname: string): boolean;
35
+ isDefaultNavigationStyle(): boolean;
29
36
  isSamePage(page?: PageInstance | null): boolean;
30
37
  get search(): string;
31
38
  get usingWindowScroll(): boolean;
@@ -37,7 +44,7 @@ export default class PageHandler {
37
44
  load(page: PageInstance, pageConfig: Route | undefined, stampId: string, pageNo?: number): void;
38
45
  unload(page?: PageInstance | null, delta?: number, top?: boolean): void;
39
46
  show(page?: PageInstance | null, pageConfig?: Route, pageNo?: number): void;
40
- hide(page?: PageInstance | null): void;
47
+ hide(page?: PageInstance | null, animation?: boolean): void;
41
48
  addAnimation(pageEl?: HTMLElement | null, first?: boolean): void;
42
49
  getPageContainer(page?: PageInstance | null): HTMLElement | null;
43
50
  getScrollingElement(page?: PageInstance | null): (Window & typeof globalThis) | HTMLElement;
@@ -1,19 +1,23 @@
1
- /* eslint-disable dot-notation */
2
- import { Current, eventCenter, requestAnimationFrame } from '@tarojs/runtime';
1
+ import { getHomePage, getCurrentPage, addLeadingSlash, stripBasename, stripTrailing, requestAnimationFrame, eventCenter, Current } from '@tarojs/runtime';
3
2
  import queryString from 'query-string';
4
- import { bindPageResize } from '../events/resize';
5
- import { bindPageScroll } from '../events/scroll';
6
- import { history, setHistoryMode } from '../history';
7
- import { loadAnimateStyle, loadRouterStyle } from '../style';
8
- import { initTabbar } from '../tabbar';
9
- import { addLeadingSlash, getCurrentPage, getHomePage, routesAlias, stripBasename, stripTrailing } from '../utils';
10
- import stacks from './stack';
11
- export default class PageHandler {
12
- constructor(config) {
3
+ import { bindPageResize } from '../events/resize.js';
4
+ import { bindPageScroll } from '../events/scroll.js';
5
+ import { setHistory, history } from '../history.js';
6
+ import { loadAnimateStyle, loadRouterStyle } from '../style.js';
7
+ import { routesAlias } from '../utils/index.js';
8
+ import NavigationBarHandler from './navigation-bar.js';
9
+ import stacks from './stack.js';
10
+
11
+ /* eslint-disable dot-notation */
12
+ class PageHandler {
13
+ constructor(config, history) {
14
+ this.history = history;
13
15
  this.defaultAnimation = { duration: 300, delay: 50 };
14
16
  this.config = config;
15
17
  this.homePage = getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
18
+ this.originHomePage = this.config.entryPagePath || this.routes[0].path || this.basename;
16
19
  this.mount();
20
+ this.navigationBarHandler = new NavigationBarHandler(this);
17
21
  }
18
22
  get currentPage() {
19
23
  const routePath = getCurrentPage(this.routerMode, this.basename);
@@ -45,14 +49,14 @@ export default class PageHandler {
45
49
  }
46
50
  set pathname(p) { this.router.pathname = p; }
47
51
  get pathname() { return this.router.pathname; }
52
+ // Note: 把 pathname 转换为原始路径,主要是处理 customRoutes 和 basename
53
+ get originPathname() { return routesAlias.getOrigin(addLeadingSlash(stripBasename(this.pathname, this.basename))); }
48
54
  get basename() { return this.router.basename || ''; }
49
55
  get pageConfig() {
50
- const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
51
56
  const homePage = addLeadingSlash(this.homePage);
52
57
  return this.routes.find(r => {
53
- var _a;
54
58
  const pagePath = addLeadingSlash(r.path);
55
- return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
59
+ return [pagePath, homePage].includes(this.originPathname);
56
60
  });
57
61
  }
58
62
  isTabBar(pathname) {
@@ -69,6 +73,14 @@ export default class PageHandler {
69
73
  })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
70
74
  return !!pagePath && this.tabBarList.some(t => stripTrailing(t.pagePath) === pagePath);
71
75
  }
76
+ isDefaultNavigationStyle() {
77
+ var _a, _b;
78
+ let style = (_a = this.config.window) === null || _a === void 0 ? void 0 : _a.navigationStyle;
79
+ if (typeof ((_b = this.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationStyle) === 'string') {
80
+ style = this.pageConfig.navigationStyle;
81
+ }
82
+ return style !== 'custom';
83
+ }
72
84
  isSamePage(page) {
73
85
  const routePath = stripBasename(this.pathname, this.basename);
74
86
  const pagePath = stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
@@ -107,40 +119,11 @@ export default class PageHandler {
107
119
  return Object.assign(Object.assign({}, query), options);
108
120
  }
109
121
  mount() {
110
- setHistoryMode(this.routerMode, this.router.basename);
122
+ setHistory(this.history, this.basename);
111
123
  this.pathname = history.location.pathname;
124
+ // Note: 注入页面样式
112
125
  this.animation && loadAnimateStyle(this.animationDuration);
113
- loadRouterStyle(this.usingWindowScroll);
114
- const appId = this.appId;
115
- let app = document.getElementById(appId);
116
- let isPosition = true;
117
- if (!app) {
118
- app = document.createElement('div');
119
- app.id = appId;
120
- isPosition = false;
121
- }
122
- const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
123
- app.classList.add('taro_router');
124
- if (this.tabBarList.length > 1) {
125
- const container = document.createElement('div');
126
- container.classList.add('taro-tabbar__container');
127
- container.id = 'container';
128
- const panel = document.createElement('div');
129
- panel.classList.add('taro-tabbar__panel');
130
- panel.appendChild(app.cloneNode(true));
131
- container.appendChild(panel);
132
- if (!isPosition) {
133
- appWrapper.appendChild(container);
134
- }
135
- else {
136
- appWrapper.replaceChild(container, app);
137
- }
138
- initTabbar(this.config);
139
- }
140
- else {
141
- if (!isPosition)
142
- appWrapper.appendChild(app);
143
- }
126
+ loadRouterStyle(this.tabBarList.length > 1, this.usingWindowScroll);
144
127
  }
145
128
  onReady(page, onLoad = true) {
146
129
  var _a;
@@ -175,19 +158,24 @@ export default class PageHandler {
175
158
  if (pageEl) {
176
159
  pageEl.classList.remove('taro_page_shade');
177
160
  this.isTabBar(this.pathname) && pageEl.classList.add('taro_tabbar_page');
161
+ this.isDefaultNavigationStyle() && pageEl.classList.add('taro_navigation_page');
178
162
  this.addAnimation(pageEl, pageNo === 0);
179
163
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
164
+ this.navigationBarHandler.load();
180
165
  this.bindPageEvents(page, pageConfig);
181
166
  this.triggerRouterChange();
182
167
  }
183
168
  else {
169
+ // FIXME 在 iOS 端快速切换页面时,可能不会执行回调注入对应类名导致 TabBar 白屏
184
170
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
185
171
  var _a;
186
172
  pageEl = this.getPageContainer(page);
187
173
  this.isTabBar(this.pathname) && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
174
+ this.isDefaultNavigationStyle() && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_navigation_page'));
188
175
  this.addAnimation(pageEl, pageNo === 0);
189
- this.onReady(page, true);
190
176
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
177
+ this.navigationBarHandler.load();
178
+ this.onReady(page, true);
191
179
  this.bindPageEvents(page, pageConfig);
192
180
  this.triggerRouterChange();
193
181
  });
@@ -241,6 +229,7 @@ export default class PageHandler {
241
229
  pageEl.classList.remove('taro_page_shade');
242
230
  this.addAnimation(pageEl, pageNo === 0);
243
231
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
232
+ this.navigationBarHandler.load();
244
233
  this.bindPageEvents(page, pageConfig);
245
234
  this.triggerRouterChange();
246
235
  }
@@ -249,31 +238,43 @@ export default class PageHandler {
249
238
  var _a;
250
239
  pageEl = this.getPageContainer(page);
251
240
  this.addAnimation(pageEl, pageNo === 0);
252
- this.onReady(page, false);
253
241
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
242
+ this.navigationBarHandler.load();
243
+ this.onReady(page, false);
254
244
  this.bindPageEvents(page, pageConfig);
255
245
  this.triggerRouterChange();
256
246
  });
257
247
  }
258
248
  }
259
- hide(page) {
260
- var _a;
249
+ hide(page, animation = false) {
250
+ var _a, _b, _c, _d, _e, _f, _g;
261
251
  if (!page)
262
252
  return;
263
253
  // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
264
254
  const pageEl = this.getPageContainer(page);
265
255
  if (pageEl) {
266
- if (this.hideTimer) {
267
- clearTimeout(this.hideTimer);
268
- this.hideTimer = null;
269
- pageEl.classList.add('taro_page_shade');
256
+ if (animation) {
257
+ if (this.hideTimer) {
258
+ clearTimeout(this.hideTimer);
259
+ this.hideTimer = null;
260
+ (_c = (_b = (_a = this.lastHidePage) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.add) === null || _c === void 0 ? void 0 : _c.call(_b, 'taro_page_shade');
261
+ }
262
+ this.lastHidePage = pageEl;
263
+ this.hideTimer = setTimeout(() => {
264
+ this.hideTimer = null;
265
+ pageEl.classList.add('taro_page_shade');
266
+ }, this.animationDuration + this.animationDelay);
267
+ (_d = page.onHide) === null || _d === void 0 ? void 0 : _d.call(page);
270
268
  }
271
- this.lastHidePage = pageEl;
272
- this.hideTimer = setTimeout(() => {
273
- this.hideTimer = null;
269
+ else {
270
+ if (this.hideTimer) {
271
+ clearTimeout(this.hideTimer);
272
+ this.hideTimer = null;
273
+ (_g = (_f = (_e = this.lastHidePage) === null || _e === void 0 ? void 0 : _e.classList) === null || _f === void 0 ? void 0 : _f.add) === null || _g === void 0 ? void 0 : _g.call(_f, 'taro_page_shade');
274
+ }
274
275
  pageEl.classList.add('taro_page_shade');
275
- }, this.animationDuration + this.animationDelay);
276
- (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
276
+ this.lastHidePage = pageEl;
277
+ }
277
278
  }
278
279
  else {
279
280
  setTimeout(() => this.hide(page), 0);
@@ -334,3 +335,5 @@ export default class PageHandler {
334
335
  }, 0);
335
336
  }
336
337
  }
338
+
339
+ export { PageHandler as default };
@@ -1,3 +1,4 @@
1
1
  import type { AppInstance } from '@tarojs/runtime';
2
+ import type { History } from 'history';
2
3
  import type { SpaRouterConfig } from '../../types/router';
3
- export declare function createRouter(app: AppInstance, config: SpaRouterConfig, framework?: string): () => void;
4
+ export declare function createRouter(history: History, app: AppInstance, config: SpaRouterConfig, framework?: string): () => void;
@@ -1,31 +1,26 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- /* eslint-disable dot-notation */
11
- import { createPageConfig, Current, eventCenter, hooks, incrementId, stringify, } from '@tarojs/runtime';
12
- import { Action as LocationAction } from 'history';
1
+ import { __awaiter } from 'tslib';
2
+ import { incrementId, addLeadingSlash, eventCenter, stripBasename, Current, safeExecute, createPageConfig, hooks, stringify } from '@tarojs/runtime';
3
+ import { Action } from 'history';
13
4
  import UniversalRouter from 'universal-router';
14
- import { history, prependBasename } from '../history';
15
- import { addLeadingSlash, routesAlias, stripBasename } from '../utils';
16
- import { setTitle } from '../utils/navigate';
17
- import { RouterConfig } from '.';
18
- import PageHandler from './page';
19
- import stacks from './stack';
5
+ import { prependBasename } from '../history.js';
6
+ import { routesAlias } from '../utils/index.js';
7
+ import { RouterConfig } from './index.js';
8
+ import PageHandler from './page.js';
9
+ import stacks from './stack.js';
10
+
20
11
  const createStampId = incrementId();
21
12
  let launchStampId = createStampId();
22
- export function createRouter(app, config, framework) {
13
+ function createRouter(history, app, config, framework) {
23
14
  var _a, _b;
24
15
  if (typeof app.onUnhandledRejection === 'function') {
25
16
  window.addEventListener('unhandledrejection', app.onUnhandledRejection);
26
17
  }
27
18
  RouterConfig.config = config;
28
- const handler = new PageHandler(config);
19
+ const handler = new PageHandler(config, history);
20
+ // Note: 弱网情况下,快速切换 tab,会造成同个页面实例被多次挂在到页面上,原因是资源请求是异步的,短时间内发起多个请求,
21
+ // 会在资源加载完成后,同时走到挂载的逻辑,造成 pageStampId 更新不及时,两个 page 的Id 相同,后面很多操作是通过 getElementById 来进行的
22
+ // 所以需要加一个锁来应对这个情况
23
+ const pageLock = {};
29
24
  routesAlias.set(handler.router.customRoutes);
30
25
  const basename = handler.router.basename;
31
26
  const routes = handler.routes.map(route => {
@@ -47,29 +42,35 @@ export function createRouter(app, config, framework) {
47
42
  eventCenter.trigger('__taroRouterLaunch', launchParam);
48
43
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
49
44
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
50
- const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
51
- var _c, _d, _e, _f, _g, _h;
52
- handler.pathname = decodeURI(location.pathname);
53
- if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
45
+ const render = (_c) => __awaiter(this, [_c], void 0, function* ({ location, action }) {
46
+ var _d, _e, _f, _g, _h, _j, _k, _l;
47
+ // Note: 由于下面有异步加载操作 先不要在这里去设置 handler.pathname
48
+ const currentPathname = decodeURI(location.pathname);
49
+ if ((_d = window.__taroAppConfig) === null || _d === void 0 ? void 0 : _d.usingWindowScroll)
54
50
  window.scrollTo(0, 0);
55
51
  eventCenter.trigger('__taroRouterChange', {
56
52
  toLocation: {
57
- path: handler.pathname
53
+ path: currentPathname
58
54
  }
59
55
  });
60
- let element, params;
56
+ let element, context, params;
57
+ const routerPath = handler.router.forcePath || currentPathname;
58
+ pageLock[routerPath] = typeof pageLock[routerPath] === 'number' ? pageLock[routerPath] + 1 : 1;
59
+ const currentLock = pageLock[routerPath];
60
+ let postLock;
61
61
  try {
62
- const result = yield router.resolve(handler.router.forcePath || handler.pathname);
63
- [element, , params] = yield Promise.all(result);
62
+ const result = yield router.resolve(routerPath);
63
+ [element, context, params] = yield Promise.all(result);
64
+ postLock = pageLock[routerPath];
64
65
  }
65
66
  catch (error) {
66
67
  if (error.status === 404) {
67
68
  const notFoundEvent = {
68
69
  isEntryPage: stacks.length === 0,
69
- path: handler.pathname,
70
+ path: currentPathname,
70
71
  query: handler.getQuery(createStampId()),
71
72
  };
72
- (_d = app.onPageNotFound) === null || _d === void 0 ? void 0 : _d.call(app, notFoundEvent);
73
+ (_e = app.onPageNotFound) === null || _e === void 0 ? void 0 : _e.call(app, notFoundEvent);
73
74
  eventCenter.trigger('__taroRouterNotFound', notFoundEvent);
74
75
  }
75
76
  else if (/Loading hot update .* failed./.test(error.message)) {
@@ -77,22 +78,36 @@ export function createRouter(app, config, framework) {
77
78
  window.location.reload();
78
79
  }
79
80
  else {
80
- throw new Error(error);
81
+ throw error;
81
82
  }
82
83
  }
83
- if (!element)
84
+ if (!element || currentLock !== postLock)
84
85
  return;
85
- const pageConfig = handler.pageConfig;
86
- let enablePullDownRefresh = ((_e = config === null || config === void 0 ? void 0 : config.window) === null || _e === void 0 ? void 0 : _e.enablePullDownRefresh) || false;
86
+ // Note: 异步结束后,在设置 handler.pathname
87
+ // context.pathname universal-router 被处理过了,是发起资源请求的时候传入的 pathname,即 await router.resolve(routerPath) 这个 routerPath
88
+ handler.pathname = context.pathname;
89
+ const { pathname, pageConfig } = handler;
90
+ let enablePullDownRefresh = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.enablePullDownRefresh) || false;
91
+ let navigationStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationStyle) || 'default';
92
+ let navigationBarTextStyle = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarTextStyle) || 'white';
93
+ let navigationBarBackgroundColor = ((_j = config === null || config === void 0 ? void 0 : config.window) === null || _j === void 0 ? void 0 : _j.navigationBarBackgroundColor) || '#000000';
87
94
  if (pageConfig) {
88
- setTitle((_f = pageConfig.navigationBarTitleText) !== null && _f !== void 0 ? _f : document.title);
89
95
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
90
96
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
91
97
  }
98
+ if (typeof pageConfig.navigationStyle === 'string') {
99
+ navigationStyle = pageConfig.navigationStyle;
100
+ }
101
+ if (typeof pageConfig.navigationBarTextStyle === 'string') {
102
+ navigationBarTextStyle = pageConfig.navigationBarTextStyle;
103
+ }
104
+ if (typeof pageConfig.navigationBarBackgroundColor === 'string') {
105
+ navigationBarBackgroundColor = pageConfig.navigationBarBackgroundColor;
106
+ }
92
107
  }
108
+ eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
93
109
  const currentPage = Current.page;
94
- const pathname = handler.pathname;
95
- const methodName = (_g = stacks.method) !== null && _g !== void 0 ? _g : '';
110
+ const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
96
111
  const cacheTabs = stacks.getTabs();
97
112
  let shouldLoad = false;
98
113
  stacks.method = '';
@@ -107,10 +122,11 @@ export function createRouter(app, config, framework) {
107
122
  }
108
123
  shouldLoad = true;
109
124
  }
110
- else if (currentPage && handler.isTabBar(handler.pathname)) {
125
+ else if (currentPage && handler.isTabBar(pathname)) {
111
126
  if (handler.isSamePage(currentPage))
112
127
  return;
113
128
  if (handler.isTabBar(currentPage.path)) {
129
+ // NOTE: 从 tabBar 页面切换到 tabBar 页面
114
130
  handler.hide(currentPage);
115
131
  stacks.pushTab(currentPage.path.split('?')[0]);
116
132
  }
@@ -124,8 +140,8 @@ export function createRouter(app, config, framework) {
124
140
  handler.unload(currentPage, stacks.length, true);
125
141
  }
126
142
  }
127
- if (cacheTabs[handler.pathname]) {
128
- stacks.popTab(handler.pathname);
143
+ if (cacheTabs[pathname]) {
144
+ stacks.popTab(pathname);
129
145
  return handler.show(stacks.getItem(0), pageConfig, 0);
130
146
  }
131
147
  shouldLoad = true;
@@ -154,11 +170,11 @@ export function createRouter(app, config, framework) {
154
170
  shouldLoad = true;
155
171
  }
156
172
  else if (action === 'PUSH') {
157
- handler.hide(currentPage);
173
+ handler.hide(currentPage, true);
158
174
  shouldLoad = true;
159
175
  }
160
176
  if (shouldLoad || stacks.length < 1) {
161
- const el = (_h = element.default) !== null && _h !== void 0 ? _h : element;
177
+ const el = (_l = element.default) !== null && _l !== void 0 ? _l : element;
162
178
  const loadConfig = Object.assign({}, pageConfig);
163
179
  const stacksIndex = stacks.length;
164
180
  delete loadConfig['path'];
@@ -181,7 +197,29 @@ export function createRouter(app, config, framework) {
181
197
  if (routePath === '/') {
182
198
  history.replace(prependBasename(handler.homePage + history.location.search));
183
199
  }
184
- render({ location: history.location, action: LocationAction.Push });
200
+ render({ location: history.location, action: Action.Push });
185
201
  (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
202
+ window.addEventListener('visibilitychange', () => {
203
+ var _a, _b, _c, _d, _e, _f, _g;
204
+ const currentPath = ((_a = Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
205
+ const path = currentPath.substring(0, currentPath.indexOf('?'));
206
+ const param = {};
207
+ // app的 onShow/onHide 生命周期的路径信息为当前页面的路径
208
+ Object.assign(param, launchParam, { path });
209
+ if (document.visibilityState === 'visible') {
210
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
211
+ // 单页面app显示后一刻会触发当前 page.onShow 生命周期函数
212
+ (_d = (_c = Current.page) === null || _c === void 0 ? void 0 : _c.onShow) === null || _d === void 0 ? void 0 : _d.call(_c);
213
+ }
214
+ else {
215
+ // 单页面app隐藏前一刻会触发当前 page.onHide 生命周期函数
216
+ if ((_e = Current.page) === null || _e === void 0 ? void 0 : _e.path) {
217
+ safeExecute((_f = Current.page) === null || _f === void 0 ? void 0 : _f.path, 'onHide');
218
+ }
219
+ (_g = app.onHide) === null || _g === void 0 ? void 0 : _g.call(app, param);
220
+ }
221
+ });
186
222
  return history.listen(render);
187
223
  }
224
+
225
+ export { createRouter };
@@ -1,4 +1,4 @@
1
- import { PageInstance } from '@tarojs/runtime';
1
+ import type { PageInstance } from '@tarojs/runtime';
2
2
  declare class Stacks {
3
3
  stacks: PageInstance[];
4
4
  backDelta: number;
@@ -78,4 +78,5 @@ class Stacks {
78
78
  }
79
79
  }
80
80
  const stacks = new Stacks();
81
- export default stacks;
81
+
82
+ export { stacks as default };
package/dist/style.d.ts CHANGED
@@ -5,4 +5,9 @@ export declare function loadAnimateStyle(ms?: number): void;
5
5
  /**
6
6
  * 插入路由相关样式
7
7
  */
8
- export declare function loadRouterStyle(usingWindowScroll: boolean): void;
8
+ export declare function loadRouterStyle(enableTabBar: boolean, enableWindowScroll: boolean): void;
9
+ /**
10
+ * 插入导航栏相关的样式
11
+ */
12
+ export declare function loadNavigationBarStyle(): void;
13
+ export declare function addStyle(css: any): void;
package/dist/style.js CHANGED
@@ -1,8 +1,12 @@
1
1
  /**
2
2
  * 插入页面动画需要的样式
3
3
  */
4
- export function loadAnimateStyle(ms = 300) {
4
+ function loadAnimateStyle(ms = 300) {
5
5
  const css = `
6
+ body {
7
+ /* 防止 iOS 页面滚动 */
8
+ overflow: hidden;
9
+ }
6
10
  .taro_router > .taro_page {
7
11
  position: absolute;
8
12
  left: 0;
@@ -18,6 +22,7 @@ export function loadAnimateStyle(ms = 300) {
18
22
  .taro_router > .taro_page.taro_tabbar_page,
19
23
  .taro_router > .taro_page.taro_page_show.taro_page_stationed {
20
24
  transform: none;
25
+ transition: none;
21
26
  }
22
27
 
23
28
  .taro_router > .taro_page.taro_page_show {
@@ -29,7 +34,7 @@ export function loadAnimateStyle(ms = 300) {
29
34
  /**
30
35
  * 插入路由相关样式
31
36
  */
32
- export function loadRouterStyle(usingWindowScroll) {
37
+ function loadRouterStyle(enableTabBar, enableWindowScroll) {
33
38
  const css = `
34
39
  .taro_router {
35
40
  position: relative;
@@ -40,13 +45,13 @@ export function loadRouterStyle(usingWindowScroll) {
40
45
  .taro_page {
41
46
  width: 100%;
42
47
  height: 100%;
43
- ${usingWindowScroll ? '' : `
48
+ ${enableWindowScroll ? '' : `
44
49
  overflow-x: hidden;
45
50
  overflow-y: scroll;
46
51
  max-height: 100vh;
47
- `}
52
+ `}
48
53
  }
49
-
54
+ ${enableTabBar ? `
50
55
  .taro-tabbar__container > .taro-tabbar__panel {
51
56
  overflow: hidden;
52
57
  }
@@ -56,10 +61,102 @@ export function loadRouterStyle(usingWindowScroll) {
56
61
  max-height: calc(100vh - var(--taro-tabbar-height) - env(safe-area-inset-bottom));
57
62
  }
58
63
 
59
- .taro_page_shade,
60
- .taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child) {
64
+ ` : ''}
65
+ .taro_page_shade:has(+.taro_page_stationed),
66
+ .taro_page_shade.taro_tabbar_page,
67
+ .taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child):has(+.taro_page_stationed) {
68
+ display: none;
69
+ }
70
+ `;
71
+ addStyle(css);
72
+ }
73
+ /**
74
+ * 插入导航栏相关的样式
75
+ */
76
+ function loadNavigationBarStyle() {
77
+ const css = `
78
+ .taro-navigation-bar-show {
79
+ display: flex;
80
+ background: white;
81
+ position: sticky;
82
+ z-index: 500;
83
+ top: 0;
84
+ padding-bottom: 8px;
85
+ padding-top: calc(env(safe-area-inset-top) + 8px);
86
+ justify-content: center;
87
+ align-items: center;
88
+ }
89
+
90
+ .taro-navigation-bar-hide {
91
+ display: none;
92
+ }
93
+
94
+ .taro-navigation-bar-title-wrap {
95
+ display: flex;
96
+ height: 24px;
97
+ }
98
+
99
+ .taro-navigation-bar-title-wrap > .taro-navigation-bar-loading {
100
+ display: none;
101
+ animation: loading 2s linear infinite;
102
+ }
103
+
104
+ .taro-navigation-bar-title-wrap .taro-navigation-bar-loading.taro-navigation-bar-loading-show {
105
+ display: flex;
106
+ }
107
+
108
+ .taro-navigation-bar-title-wrap > .taro-navigation-bar-title {
109
+ font-size: 24px;
110
+ height: 24px;
111
+ line-height: 24px;
112
+ max-width: 100px;
113
+ white-space: nowrap;
114
+ overflow: hidden;
115
+ line-height: 24px;
116
+ text-overflow: ellipsis;
117
+ }
118
+
119
+ @keyframes loading {
120
+ from {
121
+ transform: rotate(0deg);
122
+ }
123
+ to {
124
+ transform: rotate(360deg);
125
+ }
126
+ }
127
+
128
+ @keyframes loading {
129
+ from {
130
+ transform: rotate(0deg);
131
+ }
132
+ to {
133
+ transform: rotate(360deg);
134
+ }
135
+ }
136
+
137
+ .taro-navigation-bar-no-icon > .taro-navigation-bar-home {
61
138
  display: none;
62
139
  }
140
+
141
+ .taro-navigation-bar-no-icon > .taro-navigation-bar-back {
142
+ display: none;
143
+ }
144
+
145
+ .taro-navigation-bar-home-icon > .taro-navigation-bar-home {
146
+ display: flex;
147
+ left: 8px;
148
+ position: absolute;
149
+ width: 24px;
150
+ height: 24px;
151
+ }
152
+
153
+ .taro-navigation-bar-back-icon > .taro-navigation-bar-back {
154
+ display: flex;
155
+ left: 8px;
156
+ position: absolute;
157
+ width: 24px;
158
+ height: 24px;
159
+ }
63
160
  `;
64
161
  addStyle(css);
65
162
  }
@@ -70,3 +167,5 @@ function addStyle(css) {
70
167
  style.innerHTML = css;
71
168
  document.getElementsByTagName('head')[0].appendChild(style);
72
169
  }
170
+
171
+ export { addStyle, loadAnimateStyle, loadNavigationBarStyle, loadRouterStyle };
package/dist/tabbar.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import { AppConfig } from '@tarojs/taro';
2
- export declare function initTabbar(config: AppConfig): void;
2
+ import type { History } from 'history';
3
+ export declare function initTabbar(config: AppConfig, history: History): void;