-
Notifications
You must be signed in to change notification settings - Fork 152
/
tab-bar.tsx
232 lines (207 loc) · 7.69 KB
/
tab-bar.tsx
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
import * as RMWC from '@rmwc/types';
import * as React from 'react';
import { componentFactory, FoundationComponent } from '@rmwc/base';
import { MDCTabBarFoundation } from '@material/tab-bar';
import { MDCTabInteractionEvent } from '@material/tab';
import { TabScroller } from './tab-scroller';
import { Tab } from './tab';
import { TabBarContext, TabBarContextT } from './tab-bar-context';
/************************************************************
* TabBar
************************************************************/
export type TabBarOnActivateEventT = RMWC.CustomEventT<{
index: number;
}>;
/** The TabBar component */
export interface TabBarProps {
/** Callback when the active tab changes. Receives event as an argument with event.target.value set to the activeTabIndex. evt.detail = { index: number; } */
onActivate?: (evt: TabBarOnActivateEventT) => void;
/** The index of the active tab. */
activeTabIndex?: number;
/** Specifies whether the indicator should slide or fade. Defaults to slide. */
indicatorTransition?: 'slide' | 'fade';
}
export const TabBarRoot = componentFactory<TabBarProps>({
displayName: 'TabBarRoot',
tag: 'nav',
classNames: (props: TabBarProps & { isTabScroller?: boolean }) => [
'mdc-tab-bar',
{
'mdc-tab-scroller__scroll-frame__tabs': props.isTabScroller
}
],
consumeProps: ['isTabScroller']
});
/** The TabBar component */
export class TabBar extends FoundationComponent<
MDCTabBarFoundation,
TabBarProps
> {
static displayName = 'TabBar';
private root = this.createElement('root');
private currentActiveTabIndex = this.props.activeTabIndex || 0;
tabScroller: TabScroller | null = null;
tabList: any[] = [];
contextApi: TabBarContextT = {
onTabInteraction: (evt: MDCTabInteractionEvent) =>
this.handleTabInteraction(evt),
registerTab: (tab: typeof Tab) => this.tabList.push(tab),
unregisterTab: (tab: typeof Tab) =>
this.tabList.splice(this.tabList.indexOf(tab), 1),
indicatorTransition: 'slide'
};
constructor(props: TabBarProps) {
super(props);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleTabInteraction = this.handleTabInteraction.bind(this);
}
componentDidMount() {
super.componentDidMount();
// This corrects an issue where passing in 0 or no activeTabIndex
// causes the first tab of the set to not be active
// to make this even more annoying, Tabs focus by default
// restore the focus and scroll position after we activate the tab
const activeElement: any = window.document.activeElement;
const [scrollX, scrollY] = [window.scrollX, window.scrollY];
//activate the tab
(this.foundation as any).adapter_.activateTabAtIndex(
this.props.activeTabIndex || 0,
(this.foundation as any).adapter_.getTabIndicatorClientRectAtIndex(
undefined
)
);
this.foundation.scrollIntoView(this.props.activeTabIndex || 0);
// restore focus and scroll
activeElement && activeElement.focus();
window.scrollTo(scrollX, scrollY);
}
activateTab(index: number) {
const foundation = this.foundation as any;
this.currentActiveTabIndex = index;
const previousActiveIndex = foundation.adapter_.getPreviousActiveTabIndex();
if (!foundation.indexIsInRange_(index) || index === previousActiveIndex) {
return;
}
foundation.adapter_.notifyTabActivated(index);
setTimeout(() => {
if (
this.props.activeTabIndex === index ||
this.props.activeTabIndex === undefined
) {
foundation.adapter_.deactivateTabAtIndex(previousActiveIndex);
foundation.adapter_.activateTabAtIndex(
index,
foundation.adapter_.getTabIndicatorClientRectAtIndex(
previousActiveIndex
)
);
foundation.scrollIntoView(index);
} else {
// reset the currentActiveTab index because we didnt actually change
this.currentActiveTabIndex = previousActiveIndex;
}
});
}
getDefaultFoundation() {
return new MDCTabBarFoundation(
/** @type {!MDCTabBarAdapter} */ {
scrollTo: (scrollX: number) => {
this.tabScroller && this.tabScroller.scrollTo(scrollX);
},
incrementScroll: (scrollXIncrement: number) =>
this.tabScroller &&
this.tabScroller.incrementScroll(scrollXIncrement),
getScrollPosition: () =>
this.tabScroller ? this.tabScroller.getScrollPosition() : 0,
getScrollContentWidth: () =>
this.tabScroller ? this.tabScroller.getScrollContentWidth() : 0,
getOffsetWidth: () => (this.root.ref ? this.root.ref.offsetWidth : 0),
isRTL: () =>
!!this.root.ref &&
window
.getComputedStyle(this.root.ref)
.getPropertyValue('direction') === 'rtl',
setActiveTab: (index: number) => this.activateTab(index),
activateTabAtIndex: (index: number, clientRect: ClientRect) => {
this.tabList[index] && this.tabList[index].activate(clientRect);
},
deactivateTabAtIndex: (index: number) =>
this.tabList[index] && this.tabList[index].deactivate(),
focusTabAtIndex: (index: number) => this.tabList[index].focus(),
getTabIndicatorClientRectAtIndex: (index: number) =>
this.tabList[index] &&
this.tabList[index].computeIndicatorClientRect(),
getTabDimensionsAtIndex: (index: number) =>
this.tabList[index] && this.tabList[index].computeDimensions(),
getPreviousActiveTabIndex: () => {
for (let i = 0; i < this.tabList.length; i++) {
if (this.tabList[i].active) {
return i;
}
}
return -1;
},
getFocusedTabIndex: () => {
const tabElements = this.getTabElements();
const activeElement = document.activeElement as Element;
return tabElements ? tabElements.indexOf(activeElement) : -1;
},
getIndexOfTabById: (id: string) => {
for (let i = 0; i < this.tabList.length; i++) {
if (this.tabList[i].id === id) {
return i;
}
}
return -1;
},
getTabListLength: () => this.tabList.length,
notifyTabActivated: (index: number) =>
this.emit('onActivate', { index }, true)
}
);
}
sync(props: TabBarProps, prevProps: TabBarProps) {
// this will re-activate the appropriate tabs if they get-rendered
if (
props.activeTabIndex !== prevProps.activeTabIndex &&
props.activeTabIndex !== this.currentActiveTabIndex
) {
typeof props.activeTabIndex === 'number' &&
this.activateTab(props.activeTabIndex);
}
}
getTabElements(): Element[] | null {
return [].slice.call(
this.root.ref &&
this.root.ref.querySelectorAll(MDCTabBarFoundation.strings.TAB_SELECTOR)
);
}
handleTabInteraction(evt: MDCTabInteractionEvent) {
this.foundation.handleTabInteraction(evt);
}
handleKeyDown(evt: React.KeyboardEvent | KeyboardEvent) {
this.props.onKeyDown && this.props.onKeyDown(evt as React.KeyboardEvent);
this.foundation.handleKeyDown(evt as KeyboardEvent);
}
render() {
const { children, activeTabIndex, onActivate, ...rest } = this.props;
return (
<TabBarContext.Provider
value={{
...this.contextApi,
indicatorTransition: this.props.indicatorTransition || 'slide'
}}
>
<TabBarRoot
{...rest}
ref={this.root.setRef}
onKeyDown={this.handleKeyDown}
>
<TabScroller ref={(api: TabScroller) => (this.tabScroller = api)}>
{children}
</TabScroller>
</TabBarRoot>
</TabBarContext.Provider>
);
}
}