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

148 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-02-19 18:57:07 -05:00
import classNames from 'classnames/bind'
import Button from './button'
import Icon from './icon'
2020-04-07 21:06:59 -04:00
import Image from './image'
2020-02-19 18:57:07 -05:00
const cx = classNames.bind(_s)
export default class SidebarSectionItem extends PureComponent {
2020-04-07 21:06:59 -04:00
2020-03-31 12:04:50 -04:00
static contextTypes = {
router: PropTypes.object,
}
2020-02-19 18:57:07 -05:00
static propTypes = {
to: PropTypes.string,
2020-03-14 13:31:29 -04:00
href: PropTypes.string,
onClick: PropTypes.func,
2020-02-19 18:57:07 -05:00
active: PropTypes.bool,
icon: PropTypes.string,
image: PropTypes.string,
title: PropTypes.string,
me: PropTypes.bool,
suffix: PropTypes.node,
2020-03-14 13:31:29 -04:00
buttonRef: PropTypes.func,
2020-02-19 18:57:07 -05:00
}
state = {
hovering: false,
}
handleOnMouseEnter = () => {
this.setState({ hovering: true })
}
handleOnMouseLeave = () => {
this.setState({ hovering: false })
}
render() {
2020-03-14 13:31:29 -04:00
const {
to,
active,
icon,
image,
title,
me,
count,
onClick,
href,
2020-04-07 21:06:59 -04:00
buttonRef,
2020-03-14 13:31:29 -04:00
} = this.props
2020-02-19 18:57:07 -05:00
const { hovering } = this.state
const iconSize = '16px'
2020-04-30 00:34:50 -04:00
const currentPathname = this.context.router ? this.context.router.route.location.pathname : undefined
2020-03-31 12:04:50 -04:00
const shouldShowActive = hovering || active || currentPathname === to || currentPathname === href
2020-02-19 18:57:07 -05:00
const isNotifications = to === '/notifications'
const containerClasses = cx({
default: 1,
maxWidth100PC: 1,
width100PC: 1,
flexRow: 1,
2020-03-11 19:56:18 -04:00
py5: 1,
px10: 1,
2020-02-19 18:57:07 -05:00
alignItemsCenter: 1,
radiusSmall: 1,
2020-03-31 12:04:50 -04:00
border1PX: 1,
2020-04-07 21:06:59 -04:00
outlineNone: 1,
2020-03-31 12:04:50 -04:00
borderColorTransparent: !shouldShowActive,
borderColorSecondary: shouldShowActive,
2020-04-29 18:32:49 -04:00
bgTransparent: !shouldShowActive,
bgPrimary: shouldShowActive,
2020-02-19 18:57:07 -05:00
})
const textClasses = cx({
default: 1,
fontWeightNormal: 1,
2020-04-29 18:32:49 -04:00
fs15PX: 1,
2020-02-19 18:57:07 -05:00
text: 1,
textOverflowEllipsis: 1,
2020-04-22 01:00:11 -04:00
colorPrimary: 1,
2020-02-19 18:57:07 -05:00
})
const iconClasses = cx({
2020-04-29 18:32:49 -04:00
fillPrimary: 1,
2020-02-19 18:57:07 -05:00
})
const countClasses = cx({
default: 1,
text: 1,
2020-04-23 23:17:27 -04:00
mlAuto: 1,
2020-04-29 18:32:49 -04:00
fs12PX: 1,
2020-03-11 19:56:18 -04:00
px5: 1,
mr2: 1,
2020-02-19 18:57:07 -05:00
lineHeight15: 1,
2020-03-11 19:56:18 -04:00
ml5: 1,
2020-02-19 18:57:07 -05:00
colorSecondary: !isNotifications,
colorWhite: isNotifications,
2020-04-29 18:32:49 -04:00
bgBrand: isNotifications,
2020-02-19 18:57:07 -05:00
radiusSmall: isNotifications,
})
return (
2020-03-14 13:31:29 -04:00
<Button
2020-02-19 18:57:07 -05:00
to={to}
2020-03-14 13:31:29 -04:00
href={href}
onClick={onClick}
noClasses
buttonRef={buttonRef}
2020-02-19 18:57:07 -05:00
onMouseEnter={() => this.handleOnMouseEnter()}
onMouseLeave={() => this.handleOnMouseLeave()}
2020-04-29 18:32:49 -04:00
className={[_s.default, _s.noUnderline, _s.outlineNone, _s.cursorPointer, _s.width100PC, _s.bgTransparent].join(' ')}
2020-02-19 18:57:07 -05:00
>
<div className={containerClasses}>
2020-04-28 22:24:35 -04:00
{
icon &&
<Icon id={icon} className={iconClasses} size={iconSize} />
}
{
image &&
<Image
alt={title}
className={_s.circle}
width={iconSize}
height={iconSize}
src={image}
/>
}
2020-04-11 18:29:19 -04:00
2020-03-11 19:56:18 -04:00
<div className={[_s.default, _s.flexNormal, _s.px10, _s.textOverflowEllipsis, _s.overflowWrapBreakWord, _s.flexRow, _s.width100PC].join(' ')}>
2020-02-19 18:57:07 -05:00
<span className={textClasses}>{title}</span>
</div>
2020-04-11 18:29:19 -04:00
{
count > 0 &&
2020-02-19 18:57:07 -05:00
<span className={countClasses}>
{count}
</span>
}
</div>
2020-03-14 13:31:29 -04:00
</Button>
2020-02-19 18:57:07 -05:00
)
}
2020-04-07 21:06:59 -04:00
2020-02-19 18:57:07 -05:00
}