Skip to content

Commit 4512dad

Browse files
bozdozdanez
authored andcommitted
feat: Always set focus on tabs
BREAKING CHANGE: The tabs are now instantly focused on click. The default styling has been adapted for that, so that there is no outline shown anymore on focus. closes #272
1 parent 7ac656e commit 4512dad

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

src/components/Tabs.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,62 +64,61 @@ For more information about controlled and uncontrolled mode of react-tabs see ht
6464
* It is initialized from the prop defaultFocus, and after the first render it is reset back to false. Later it can become true again when using keys to navigate the tabs.
6565
*/
6666
const Tabs = (props) => {
67-
const [focus, setFocus] = useState(props.defaultFocus);
67+
const { children, defaultFocus, defaultIndex, onSelect } = props;
68+
69+
const [focus, setFocus] = useState(defaultFocus);
6870
const [mode] = useState(getModeFromProps(props));
6971
const [selectedIndex, setSelectedIndex] = useState(
70-
mode === MODE_UNCONTROLLED ? props.defaultIndex || 0 : null,
72+
mode === MODE_UNCONTROLLED ? defaultIndex || 0 : null,
7173
);
7274

73-
// Reset focus after initial render, see comment above
7475
useEffect(() => {
76+
// Reset focus after initial render, see comment above
7577
setFocus(false);
7678
}, []);
7779

7880
if (mode === MODE_UNCONTROLLED) {
7981
// Ensure that we handle removed tabs and don't let selectedIndex get out of bounds
82+
const tabsCount = getTabsCount(children);
8083
useEffect(() => {
8184
if (selectedIndex != null) {
82-
const maxTabIndex = Math.max(0, getTabsCount(props.children) - 1);
85+
const maxTabIndex = Math.max(0, tabsCount - 1);
8386
setSelectedIndex(Math.min(selectedIndex, maxTabIndex));
8487
}
85-
}, [getTabsCount(props.children)]);
88+
}, [tabsCount]);
8689
}
8790

8891
checkForIllegalModeChange(props, mode);
8992

9093
const handleSelected = (index, last, event) => {
91-
const { onSelect } = props;
92-
9394
// Call change event handler
9495
if (typeof onSelect === 'function') {
9596
// Check if the change event handler cancels the tab change
9697
if (onSelect(index, last, event) === false) return;
9798
}
9899

99-
if (event.type === 'keydown') {
100-
// Set focus if the change was triggered from the keyboard
101-
setFocus(true);
102-
}
100+
// Always set focus on tabs
101+
setFocus(true);
103102

104103
if (mode === MODE_UNCONTROLLED) {
105104
// Update selected index
106105
setSelectedIndex(index);
107106
}
108107
};
109108

110-
let newProps = { ...props };
111-
const { children } = props;
109+
let subProps = { ...props };
112110

113-
newProps.focus = focus;
114-
newProps.onSelect = handleSelected;
111+
subProps.focus = focus;
112+
subProps.onSelect = handleSelected;
115113

116114
if (selectedIndex != null) {
117-
newProps.selectedIndex = selectedIndex;
115+
subProps.selectedIndex = selectedIndex;
118116
}
119-
delete newProps.defaultFocus;
120-
delete newProps.defaultIndex;
121-
return <UncontrolledTabs {...newProps}>{children}</UncontrolledTabs>;
117+
delete subProps.defaultFocus;
118+
delete subProps.defaultIndex;
119+
return <UncontrolledTabs {...subProps}>{children}</UncontrolledTabs>;
122120
};
121+
123122
Tabs.propTypes = propTypes;
124123
Tabs.defaultProps = defaultProps;
125124
Tabs.tabsRole = 'Tabs';

style/react-tabs.css

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@
3232
}
3333

3434
.react-tabs__tab:focus {
35-
box-shadow: 0 0 5px hsl(208, 99%, 50%);
36-
border-color: hsl(208, 99%, 50%);
3735
outline: none;
3836
}
3937

4038
.react-tabs__tab:focus:after {
41-
content: "";
39+
content: '';
4240
position: absolute;
4341
height: 5px;
4442
left: -4px;

style/react-tabs.less

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
}
3131

3232
&:focus {
33-
box-shadow: 0 0 5px hsl(208, 99%, 50%);
34-
border-color: hsl(208, 99%, 50%);
3533
outline: none;
3634

3735
&:after {

style/react-tabs.scss

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
}
3131

3232
&:focus {
33-
box-shadow: 0 0 5px hsl(208, 99%, 50%);
34-
border-color: hsl(208, 99%, 50%);
3533
outline: none;
3634

3735
&:after {

0 commit comments

Comments
 (0)