-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Tab.tsx
153 lines (131 loc) · 3.79 KB
/
Tab.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
import * as React from 'react';
import {classNames} from '@shopify/css-utilities';
import {focusFirstFocusableNode} from '@shopify/javascript-utilities/focus';
import UnstyledLink from '../../../UnstyledLink';
import {withAppProvider, WithAppProviderProps} from '../../../AppProvider';
import {handleMouseUpByBlurring} from '../../../../utilities/focus';
import styles from '../../Tabs.scss';
export interface Props {
id: string;
focused?: boolean;
siblingTabHasFocus?: boolean;
selected?: boolean;
panelID?: string;
children?: React.ReactNode;
url?: string;
measuring?: boolean;
accessibilityLabel?: string;
onClick?(id: string): void;
}
export type CombinedProps = Props & WithAppProviderProps;
export class Tab extends React.PureComponent<CombinedProps, never> {
private node: HTMLElement | null = null;
// A tab can start selected when it is moved from the disclosure dropdown
// into the main list, so we need to send focus from the tab to the panel
// on mount and update
componentDidMount() {
const {id, measuring, selected, panelID, focused} = this.props;
if (measuring) {
return;
}
// Because of timing issues with the render, we may still have the old,
// in-disclosure version of the tab that has focus. Check for this
// as a second indicator of focus
const itemHadFocus =
focused || (document.activeElement && document.activeElement.id === id);
// If we just check for selected, the panel for the active tab will
// be focused on page load, which we don’t want
if (itemHadFocus && selected && panelID != null) {
focusPanelID(panelID);
}
}
componentDidUpdate(previousProps: Props) {
const {selected: wasSelected} = previousProps;
const {focused, measuring, selected, panelID} = this.props;
if (measuring) {
return;
}
if (selected && !wasSelected && panelID != null) {
focusPanelID(panelID);
} else if (focused && this.node != null) {
focusFirstFocusableNode(this.node);
}
}
render() {
const {
id,
focused,
siblingTabHasFocus,
children,
onClick,
selected,
url,
panelID,
measuring,
accessibilityLabel,
} = this.props;
const handleClick = onClick && onClick.bind(null, id);
const className = classNames(
styles.Tab,
selected && styles['Tab-selected'],
);
let tabIndex: 0 | -1;
if (selected && !siblingTabHasFocus && !measuring) {
tabIndex = 0;
} else if (focused && !measuring) {
tabIndex = 0;
} else {
tabIndex = -1;
}
const markup = url ? (
<UnstyledLink
id={id}
url={url}
role="tab"
tabIndex={tabIndex}
onClick={handleClick}
className={className}
aria-selected={selected}
aria-controls={panelID}
aria-label={accessibilityLabel}
onMouseUp={handleMouseUpByBlurring}
>
<span className={styles.Title}>{children}</span>
</UnstyledLink>
) : (
<button
id={id}
role="tab"
type="button"
tabIndex={tabIndex}
className={className}
onClick={handleClick}
aria-selected={selected}
aria-controls={panelID}
aria-label={accessibilityLabel}
onMouseUp={handleMouseUpByBlurring}
>
<span className={styles.Title}>{children}</span>
</button>
);
return (
<li
role="presentation"
className={styles.TabContainer}
ref={this.setNode}
>
{markup}
</li>
);
}
private setNode = (node: HTMLElement | null) => {
this.node = node;
};
}
function focusPanelID(panelID: string) {
const panel = document.getElementById(panelID);
if (panel) {
panel.focus();
}
}
export default withAppProvider<Props>()(Tab);