Skip to content

Commit c7e5516

Browse files
committed
Add props for styling active tabs
1 parent fcbd1e1 commit c7e5516

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

docs/components_page/components/tabs.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ You can also apply CSS classes to the tab or label using the `tabClassName` or `
2929

3030
{{example:components/tabs/style.py:tabs}}
3131

32+
To apply certain styles only to the currently active tab, you can use the `active_tab_style`, `activeTabClassName`, `active_label_style` and `activeLabelClassName` properties.
33+
3234
{{apidoc:src/components/tabs/Tabs.js}}
3335

3436
{{apidoc:src/components/tabs/Tab.js}}

src/components/tabs/Tab.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Tab = props => {
1010

1111
Tab.defaultProps = {
1212
disabled: false
13-
}
13+
};
1414

1515
Tab.propTypes = {
1616
/**
@@ -37,12 +37,24 @@ Tab.propTypes = {
3737
*/
3838
tab_style: PropTypes.object,
3939

40+
/**
41+
* Defines CSS styles which will override styles previously set. The styles
42+
* set here apply to the NavItem in the tab when it is active.
43+
*/
44+
active_tab_style: PropTypes.object,
45+
4046
/**
4147
* Defines CSS styles which will override styles previously set. The styles
4248
* set here apply to the NavLink in the tab.
4349
*/
4450
label_style: PropTypes.object,
4551

52+
/**
53+
* Defines CSS styles which will override styles previously set. The styles
54+
* set here apply to the NavLink in the tab when it is active
55+
*/
56+
active_label_style: PropTypes.object,
57+
4658
/**
4759
* Often used with CSS to style elements with common properties.
4860
*/
@@ -54,12 +66,26 @@ Tab.propTypes = {
5466
*/
5567
tabClassName: PropTypes.string,
5668

69+
/**
70+
* Often used with CSS to style elements with common properties. The classes
71+
* specified with this prop will be applied to the NavItem in the tab when it
72+
* is active.
73+
*/
74+
activeTabClassName: PropTypes.string,
75+
5776
/**
5877
* Often used with CSS to style elements with common properties. The classes
5978
* specified with this prop will be applied to the NavLink in the tab.
6079
*/
6180
labelClassName: PropTypes.string,
6281

82+
/**
83+
* Often used with CSS to style elements with common properties. The classes
84+
* specified with this prop will be applied to the NavLink in the tab when
85+
* it is active.
86+
*/
87+
activeLabelClassName: PropTypes.string,
88+
6389
/**
6490
* A unique identifier for the component, used to improve
6591
* performance by React.js while rendering components

src/components/tabs/Tabs.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,32 @@ const Tabs = props => {
7575
children.map((child, idx) => {
7676
const childProps = resolveChildProps(child);
7777
const tabId = childProps.key || childProps.tab_id || 'tab-' + idx;
78+
const active = active_tab === tabId;
7879
return (
7980
<NavItem
8081
key={tabId}
81-
style={childProps.tab_style}
82-
className={childProps.tabClassName}
82+
style={
83+
active
84+
? {...childProps.tab_style, ...childProps.active_tab_style}
85+
: childProps.tab_style
86+
}
87+
className={classnames(
88+
childProps.tabClassName,
89+
active && childProps.activeTabClassName
90+
)}
8391
>
8492
<NavLink
85-
className={classnames(childProps.labelClassName, {
86-
active: active_tab === tabId
87-
})}
93+
className={classnames(
94+
childProps.labelClassName,
95+
active && childProps.activeLabelClassName,
96+
{active}
97+
)}
8898
href="#"
89-
style={childProps.label_style}
99+
style={
100+
active
101+
? {...childProps.label_style, ...childProps.active_label_style}
102+
: childProps.label_style
103+
}
90104
disabled={childProps.disabled}
91105
onClick={() => {
92106
if (!childProps.disabled) {
@@ -110,9 +124,13 @@ const Tabs = props => {
110124
tab_id,
111125
label,
112126
tab_style,
127+
active_tab_style,
113128
label_style,
129+
active_label_style,
114130
tabClassName,
131+
activeTabClassName,
115132
labelClassName,
133+
activeLabelClassName,
116134
loading_state,
117135
...otherProps
118136
} = childProps;

0 commit comments

Comments
 (0)