gab-social/app/javascript/gabsocial/components/tab_bar_item.js

111 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-03-26 03:11:32 +00:00
import { withRouter } from 'react-router-dom'
2020-02-22 23:26:23 +00:00
import classNames from 'classnames/bind'
2020-03-26 03:11:32 +00:00
import Button from './button'
import Icon from './icon'
2020-02-22 23:26:23 +00:00
import Text from './text'
const cx = classNames.bind(_s)
export default
@withRouter
class TabBarItem extends PureComponent {
static propTypes = {
location: PropTypes.object.isRequired,
title: PropTypes.string,
2020-03-26 03:11:32 +00:00
onClick: PropTypes.func,
icon: PropTypes.string,
2020-02-22 23:26:23 +00:00
to: PropTypes.string,
2020-02-28 15:20:47 +00:00
large: PropTypes.bool,
2020-03-26 03:11:32 +00:00
active: PropTypes.bool,
2020-02-22 23:26:23 +00:00
}
state = {
2020-04-07 02:53:23 +01:00
isCurrent: -1,
2020-02-22 23:26:23 +00:00
}
componentDidUpdate(prevProps) {
if (this.props.location !== prevProps.location) {
const isCurrent = this.props.to === this.props.location.pathname
2020-04-07 02:53:23 +01:00
if (this.state.isCurrent !== isCurrent) {
this.setState({ isCurrent })
2020-02-22 23:26:23 +00:00
}
}
}
render() {
2020-03-26 03:11:32 +00:00
const {
title,
to,
onClick,
location,
large,
icon,
2020-04-07 02:53:23 +01:00
active
2020-03-26 03:11:32 +00:00
} = this.props
2020-04-07 02:53:23 +01:00
const { isCurrent } = this.state
2020-02-22 23:26:23 +00:00
2020-04-07 02:53:23 +01:00
const isActive = active || (isCurrent === -1 ? to === location.pathname : false)
2020-02-22 23:26:23 +00:00
const containerClasses = cx({
default: 1,
height53PX: 1,
noUnderline: 1,
text: 1,
displayFlex: 1,
alignItemsCenter: 1,
justifyContentCenter: 1,
borderBottom2PX: 1,
2020-03-11 23:56:18 +00:00
py5: 1,
2020-04-03 02:05:49 +01:00
cursorPointer: 1,
2020-03-26 03:11:32 +00:00
backgroundTransparent: 1,
2020-04-07 02:53:23 +01:00
borderColorTransparent: !isActive,
borderColorBrand: isActive,
2020-03-11 23:56:18 +00:00
mr5: large,
2020-02-28 15:20:47 +00:00
})
const textParentClasses = cx({
default: 1,
height100PC: 1,
alignItemsCenter: 1,
justifyContentCenter: 1,
radiusSmall: 1,
2020-03-11 23:56:18 +00:00
px10: !large,
px15: large,
2020-04-07 02:53:23 +01:00
backgroundSubtle2Dark_onHover: !isActive,
2020-02-22 23:26:23 +00:00
})
const textOptions = {
2020-02-28 15:20:47 +00:00
size: !!large ? 'normal' : 'small',
2020-04-07 02:53:23 +01:00
color: isActive ? 'brand' : large ? 'secondary' : 'primary',
weight: isActive ? 'bold' : large ? 'medium' : 'normal',
2020-02-22 23:26:23 +00:00
}
2020-03-26 03:11:32 +00:00
const iconOptions = {
id: icon,
width: !!large ? 20 : 14,
height: !!large ? 20 : 14,
}
2020-02-22 23:26:23 +00:00
return (
2020-03-26 03:11:32 +00:00
<Button
onClick={onClick}
className={containerClasses}
2020-04-07 02:53:23 +01:00
to={to || undefined}
2020-03-26 03:11:32 +00:00
noClasses
>
2020-02-28 15:20:47 +00:00
<span className={textParentClasses}>
2020-03-26 03:11:32 +00:00
{ !!title &&
2020-02-28 15:20:47 +00:00
<Text {...textOptions}>
{title}
</Text>
2020-03-26 03:11:32 +00:00
}
{ !!icon &&
<Icon {...iconOptions} />
}
2020-02-28 15:20:47 +00:00
</span>
2020-03-26 03:11:32 +00:00
</Button>
2020-02-22 23:26:23 +00:00
)
}
}