-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathIonTabBar.ts
More file actions
303 lines (277 loc) · 9.3 KB
/
IonTabBar.ts
File metadata and controls
303 lines (277 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { defineCustomElement } from "@ionic/core/components/ion-tab-bar.js";
import type { VNode, Ref } from "vue";
import { h, defineComponent, getCurrentInstance, inject } from "vue";
// TODO(FW-2969): types
interface TabState {
activeTab?: string;
tabs: { [k: string]: Tab };
hasRouterOutlet?: boolean;
}
interface Tab {
originalHref: string;
currentHref: string;
ref: VNode;
}
interface TabBarData {
hasRouterOutlet: boolean;
_tabsWillChange: Function;
_tabsDidChange: Function;
}
const isTabButton = (child: any) => child.type?.name === "IonTabButton";
/**
* Checks if pathname matches the tab's href using path segment matching.
* Avoids false matches like /home2 matching /home by requiring exact match
* or a path segment boundary (/).
*/
const matchesTab = (pathname: string, href: string | undefined): boolean => {
if (href === undefined) {
return false;
}
const normalizedHref =
href.endsWith("/") && href !== "/" ? href.slice(0, -1) : href;
return (
pathname === normalizedHref || pathname.startsWith(normalizedHref + "/")
);
};
const getTabs = (nodes: VNode[]) => {
let tabs: VNode[] = [];
nodes.forEach((node: VNode) => {
if (isTabButton(node)) {
tabs.push(node);
} else if (Array.isArray(node.children) && node.children.length > 1) {
const childTabs = getTabs(node.children as VNode[]);
tabs = [...tabs, ...childTabs];
}
});
return tabs;
};
export const IonTabBar = defineComponent({
name: "IonTabBar",
data() {
return {
tabState: {
activeTab: undefined as string | undefined,
tabs: {},
/**
* Passing this prop to each tab button
* lets it be aware of the presence of
* the router outlet.
*/
hasRouterOutlet: false,
},
tabVnodes: [] as VNode[],
/* eslint-disable @typescript-eslint/no-empty-function */
_tabsWillChange: { type: Function, default: () => {} },
_tabsDidChange: { type: Function, default: () => {} },
/* eslint-enable @typescript-eslint/no-empty-function */
};
},
updated() {
this.setupTabState(inject("navManager", null));
},
methods: {
setupTabState(ionRouter: any) {
const hasRouterOutlet = this.$data.tabState.hasRouterOutlet;
/**
* For each tab, we need to keep track of its
* base href as well as any child page that
* is active in its stack so that when we go back
* to a tab from another tab, we can correctly
* show any child pages if necessary.
*/
const tabState: TabState = this.$data.tabState;
const currentInstance = getCurrentInstance();
const tabs = (this.$data.tabVnodes = getTabs(
(currentInstance.subTree.children || []) as VNode[]
));
tabs.forEach((child) => {
tabState.tabs[child.props.tab] = {
originalHref: child.props.href,
currentHref: child.props.href,
ref: child,
};
/**
* Passing this prop to each tab button
* lets it be aware of the state that
* ion-tab-bar is managing for it.
*/
child.component.props._getTabState = () => tabState;
/**
* If the router outlet is not defined, then the tabs are being used
* as a basic tab navigation without the router. In this case, the
* tabs will not emit the `ionTabsDidChange` and `ionTabsWillChange`
* events through the `checkActiveTab` method. Instead, we need to
* handle those events through the tab buttons.
*/
if (!hasRouterOutlet) {
child.component.props._onClick = (
event: CustomEvent<{
href: string;
selected: boolean;
tab: string;
}>
) => {
this.handleIonTabButtonClick(event);
};
}
});
this.checkActiveTab(ionRouter);
},
/**
* This method is called upon setup and when the
* history changes. It checks the current route
* and updates the active tab accordingly.
*
* History changes only occur when the router
* outlet is present. Due to this, the
* `ionTabsDidChange` and `ionTabsWillChange`
* events are only emitted when the router
* outlet is present. A different approach must
* be taken for tabs without a router outlet.
*
* @param ionRouter
*/
checkActiveTab(ionRouter: any) {
const hasRouterOutlet = this.$data.tabState.hasRouterOutlet;
const currentRoute = ionRouter?.getCurrentRouteInfo();
const childNodes = this.$data.tabVnodes;
const { tabs, activeTab: prevActiveTab } = this.$data.tabState;
const tabKeys = Object.keys(tabs);
let activeTab = tabKeys.find((key) => {
const href = tabs[key].originalHref;
return (
currentRoute?.pathname && matchesTab(currentRoute.pathname, href)
);
});
/**
* Tabs is being used as a basic tab navigation,
* so we need to set the first tab as active since
* `checkActiveTab` will not be called after setup.
*/
if (!activeTab && !hasRouterOutlet) {
activeTab = tabKeys[0];
}
/**
* For each tab, check to see if the
* base href has changed. If so, update
* it in the tabs state.
*/
childNodes.forEach((child: VNode) => {
const tab = tabs[child.props.tab];
if (!tab || tab.originalHref !== child.props.href) {
tabs[child.props.tab] = {
originalHref: child.props.href,
currentHref: child.props.href,
ref: child,
};
}
});
if (activeTab && prevActiveTab) {
const prevHref = this.$data.tabState.tabs[prevActiveTab].currentHref;
/**
* If the tabs change or the url changes,
* update the currentHref for the active tab.
* Ex: url changes from /tabs/tab1 --> /tabs/tab1/child
* If we went to tab2 then back to tab1, we should
* land on /tabs/tab1/child instead of /tabs/tab1.
*/
if (
activeTab !== prevActiveTab ||
prevHref !== currentRoute?.pathname
) {
/**
* By default the search is `undefined` in Ionic Vue,
* but Vue Router can set the search to the empty string.
* We check for truthy here because empty string is falsy
* and currentRoute.search cannot ever be a boolean.
*/
const search = currentRoute?.search ? `?${currentRoute.search}` : "";
tabs[activeTab] = {
...tabs[activeTab],
currentHref: currentRoute?.pathname + search,
};
}
/**
* If navigating back and the tabs change,
* set the previous tab back to its original href.
*/
if (
currentRoute?.routerAction === "pop" &&
activeTab !== prevActiveTab
) {
tabs[prevActiveTab] = {
...tabs[prevActiveTab],
currentHref: tabs[prevActiveTab].originalHref,
};
}
}
this.tabSwitch(activeTab, ionRouter);
},
handleIonTabButtonClick(
event: CustomEvent<{
href: string;
selected: boolean;
tab: string;
}>
) {
const activeTab = event.detail.tab;
this.tabSwitch(activeTab);
},
tabSwitch(activeTab: string, ionRouter?: any) {
const hasRouterOutlet = this.$data.tabState.hasRouterOutlet;
const childNodes = this.$data.tabVnodes;
const { activeTab: prevActiveTab } = this.$data.tabState;
const tabState = this.$data.tabState;
const activeChild = childNodes.find(
(child: VNode) => isTabButton(child) && child.props?.tab === activeTab
);
const tabBar = this.$refs.ionTabBar;
const tabDidChange = activeTab !== prevActiveTab;
if (tabBar) {
if (activeChild) {
tabDidChange && this.$data._tabsWillChange(activeTab);
if (hasRouterOutlet && ionRouter !== null) {
ionRouter.handleSetCurrentTab(activeTab);
}
tabBar.selectedTab = tabState.activeTab = activeTab;
tabDidChange && this.$data._tabsDidChange(activeTab);
} else {
/**
* When going to a tab that does
* not have an associated ion-tab-button
* we need to remove the selected state from
* the old tab.
*/
tabBar.selectedTab = tabState.activeTab = "";
}
}
},
},
mounted() {
const ionRouter: any = inject("navManager", null);
/**
* Tab bar can be used as a standalone component,
* so it cannot be modified directly through
* IonTabs. Instead, data will be passed through
* the provide/inject.
*/
const tabBarData = inject<Ref<TabBarData>>("tabBarData");
this.$data.tabState.hasRouterOutlet = tabBarData.value.hasRouterOutlet;
this.$data._tabsWillChange = tabBarData.value._tabsWillChange;
this.$data._tabsDidChange = tabBarData.value._tabsDidChange;
this.setupTabState(ionRouter);
ionRouter?.registerHistoryChangeListener(() =>
this.checkActiveTab(ionRouter)
);
},
setup(_, { slots }) {
defineCustomElement();
return () => {
return h(
"ion-tab-bar",
{ ref: "ionTabBar" },
slots.default && slots.default()
);
};
},
});