This repository has been archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathindex.tsx
204 lines (189 loc) · 6.58 KB
/
index.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
// The MIT License
//
// Copyright (c) 2018 Google, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import React from 'react';
import classnames from 'classnames';
import {MDCSlidingTabIndicatorFoundation} from '@material/tab-indicator/sliding-foundation';
import {MDCFadingTabIndicatorFoundation} from '@material/tab-indicator/fading-foundation';
import {MDCTabIndicatorAdapter} from '@material/tab-indicator/adapter';
export interface TabIndicatorProps
extends React.HTMLAttributes<HTMLSpanElement> {
active?: boolean;
className?: string;
fade?: boolean;
icon?: boolean;
previousIndicatorClientRect?: ClientRect;
}
export default class TabIndicator extends React.Component<
TabIndicatorProps,
{}
> {
private tabIndicatorElement: React.RefObject<
HTMLSpanElement
> = React.createRef();
foundation!:
| MDCFadingTabIndicatorFoundation
| MDCSlidingTabIndicatorFoundation;
static defaultProps: Partial<TabIndicatorProps> = {
active: false,
className: '',
fade: false,
icon: false,
};
componentDidMount() {
if (this.props.fade) {
this.foundation = new MDCFadingTabIndicatorFoundation(this.adapter);
} else {
this.foundation = new MDCSlidingTabIndicatorFoundation(this.adapter);
}
this.foundation.init();
if (this.props.active) {
this.foundation.activate();
}
}
componentWillUnmount() {
this.foundation.destroy();
}
componentDidUpdate(prevProps: TabIndicatorProps) {
if (this.props.active !== prevProps.active) {
if (this.props.active) {
this.foundation.activate(this.props.previousIndicatorClientRect);
} else {
this.foundation.deactivate();
}
}
}
get classes() {
const {className, fade} = this.props;
return classnames('mdc-tab-indicator', className, {
'mdc-tab-indicator--fade': fade,
});
}
get contentClasses() {
const {icon} = this.props;
return classnames('mdc-tab-indicator__content', {
'mdc-tab-indicator__content--icon': icon,
'mdc-tab-indicator__content--underline': !icon,
});
}
get adapter(): MDCTabIndicatorAdapter {
return {
addClass: (className: string) => {
if (!this.tabIndicatorElement.current) return;
// since the sliding indicator depends on the FLIP method,
// our regular pattern of managing classes does not work here.
// setState is async, which does not work well with the FLIP method
// without a requestAnimationFrame, which was done in this PR:
// https://github.com/material-components
// /material-components-web/pull/3337/files#diff-683d792d28dad99754294121e1afbfb5L62
this.tabIndicatorElement.current.classList.add(className);
this.forceUpdate();
},
removeClass: (className: string) => {
if (!this.tabIndicatorElement.current) return;
this.tabIndicatorElement.current.classList.remove(className);
this.forceUpdate();
},
computeContentClientRect: this.computeContentClientRect,
// setContentStyleProperty was using setState, but due to the method's
// async nature, its not condusive to the FLIP technique
setContentStyleProperty: (prop: string, value: string) => {
const contentElement = this.getNativeContentElement() as HTMLElement;
// we need to cast prop from string (interface requirement) to CSSStyleDeclaration;
const typedProp = prop as keyof CSSStyleDeclaration;
// length and parentRule are readonly properties of CSSStyleDeclaration that
// cannot be set
if (
!contentElement ||
typedProp === 'length' ||
typedProp === 'parentRule'
) {
return;
}
// https://github.com/Microsoft/TypeScript/issues/11914
contentElement.style[typedProp] = value;
},
};
}
private getNativeContentElement = () => {
if (!this.tabIndicatorElement.current) return;
// need to use getElementsByClassName since tabIndicator could be
// a non-semantic element (span, i, etc.). This is a problem since refs to a non semantic elements
// return the instance of the component.
return this.tabIndicatorElement.current.getElementsByClassName(
'mdc-tab-indicator__content'
)[0];
};
computeContentClientRect = () => {
const contentElement = this.getNativeContentElement();
if (!(contentElement && contentElement.getBoundingClientRect)) {
// new DOMRect is not IE11 compatible
const defaultDOMRect = {
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
x: 0,
y: 0,
};
return defaultDOMRect;
}
return contentElement.getBoundingClientRect();
};
render() {
const {
/* eslint-disable */
active,
children,
className,
fade,
icon,
previousIndicatorClientRect,
/* eslint-enable */
...otherProps
} = this.props;
return (
<span
className={this.classes}
ref={this.tabIndicatorElement}
{...otherProps}
>
{this.renderContent()}
</span>
);
}
addContentClassesToChildren = () => {
const child = React.Children.only(
this.props.children
) as React.ReactElement<any>;
const className = classnames(child.props.className, this.contentClasses);
const props = Object.assign({}, child.props, {className});
return React.cloneElement(child, props);
};
renderContent() {
if (this.props.children) {
return this.addContentClassesToChildren();
}
return <span className={this.contentClasses} />;
}
}