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

147 lines
3.6 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
2020-05-06 05:33:54 +01:00
import {
CX,
} from '../constants'
2020-02-19 23:57:07 +00:00
import Button from './button'
import Icon from './icon'
2020-04-08 02:06:59 +01:00
import Image from './image'
2020-05-06 07:24:16 +01:00
import ResponsiveClassesComponent from '../features/ui/util/responsive_classes_component'
2020-02-19 23:57:07 +00:00
class SidebarSectionItem extends React.PureComponent {
2020-04-08 02:06:59 +01:00
2020-03-31 17:04:50 +01:00
static contextTypes = {
router: PropTypes.object,
}
2020-02-19 23:57:07 +00:00
state = {
hovering: false,
}
handleOnMouseEnter = () => {
this.setState({ hovering: true })
}
handleOnMouseLeave = () => {
this.setState({ hovering: false })
}
render() {
2020-03-14 17:31:29 +00:00
const {
to,
active,
icon,
image,
title,
me,
count,
onClick,
href,
2020-04-08 02:06:59 +01:00
buttonRef,
2020-03-14 17:31:29 +00:00
} = this.props
2020-02-19 23:57:07 +00:00
const { hovering } = this.state
2020-05-01 06:50:27 +01:00
const noRouter = !this.context.router
2020-02-19 23:57:07 +00:00
const iconSize = '16px'
2020-05-01 06:50:27 +01:00
const currentPathname = noRouter ? '' : this.context.router.route.location.pathname
2020-03-31 17:04:50 +01:00
const shouldShowActive = hovering || active || currentPathname === to || currentPathname === href
2020-12-03 22:13:11 +00:00
const isHighlighted = ['/notifications', '/messages'].indexOf(to) > -1
2020-02-19 23:57:07 +00:00
2020-05-06 05:33:54 +01:00
const containerClasses = CX({
d: 1,
maxW100PC: 1,
w100PC: 1,
2020-02-19 23:57:07 +00:00
flexRow: 1,
2020-03-11 23:56:18 +00:00
py5: 1,
px10: 1,
aiCenter: 1,
2020-02-19 23:57:07 +00:00
radiusSmall: 1,
2020-03-31 17:04:50 +01:00
border1PX: 1,
2020-04-08 02:06:59 +01:00
outlineNone: 1,
2020-03-31 17:04:50 +01:00
borderColorTransparent: !shouldShowActive,
borderColorSecondary: shouldShowActive,
2020-04-29 23:32:49 +01:00
bgTransparent: !shouldShowActive,
bgPrimary: shouldShowActive,
2020-02-19 23:57:07 +00:00
})
2020-05-06 05:33:54 +01:00
const countClasses = CX({
d: 1,
2020-02-19 23:57:07 +00:00
text: 1,
2020-12-03 22:13:11 +00:00
textAlignCenter: 1,
minW20PX: 1,
2020-04-24 04:17:27 +01:00
mlAuto: 1,
2020-04-29 23:32:49 +01:00
fs12PX: 1,
2020-03-11 23:56:18 +00:00
px5: 1,
mr2: 1,
2020-02-19 23:57:07 +00:00
lineHeight15: 1,
2020-03-11 23:56:18 +00:00
ml5: 1,
2020-12-03 22:13:11 +00:00
cSecondary: !isHighlighted,
cWhite: isHighlighted,
bgRed: isHighlighted,
circle: isHighlighted,
2020-02-19 23:57:07 +00:00
})
return (
2020-03-14 17:31:29 +00:00
<Button
2020-05-01 06:50:27 +01:00
to={noRouter ? undefined : to}
href={noRouter ? (to || href) : href}
2020-03-14 17:31:29 +00:00
onClick={onClick}
noClasses
buttonRef={buttonRef}
2020-02-19 23:57:07 +00:00
onMouseEnter={() => this.handleOnMouseEnter()}
onMouseLeave={() => this.handleOnMouseLeave()}
className={[_s.d, _s.noUnderline, _s.outlineNone, _s.cursorPointer, _s.w100PC, _s.bgTransparent].join(' ')}
2020-02-19 23:57:07 +00:00
>
<div className={containerClasses}>
2020-04-29 03:24:35 +01:00
{
icon &&
<Icon id={icon} className={_s.cPrimary} size={iconSize} />
2020-04-29 03:24:35 +01:00
}
{
image &&
<Image
alt={title}
className={[_s.circle, _s.overflowHidden].join(' ')}
2020-04-29 03:24:35 +01:00
width={iconSize}
height={iconSize}
src={image}
/>
}
2020-04-11 23:29:19 +01:00
<div className={[_s.d, _s.flexNormal, _s.px10, _s.textOverflowEllipsis, _s.overflowWrapBreakWord, _s.flexRow, _s.w100PC].join(' ')}>
2020-05-06 07:24:16 +01:00
<ResponsiveClassesComponent
classNames={[_s.d, _s.fw400, _s.fs15PX, _s.text, _s.textOverflowEllipsis, _s.cPrimary].join(' ')}
classNamesSmall={[_s.d, _s.fw400, _s.fs13PX, _s.text, _s.textOverflowEllipsis, _s.cPrimary].join(' ')}
2020-05-06 07:24:16 +01:00
>
{title}
</ResponsiveClassesComponent>
</div>
2020-04-11 23:29:19 +01:00
{
count > 0 &&
2020-02-19 23:57:07 +00:00
<span className={countClasses}>
{count}
</span>
}
</div>
2020-03-14 17:31:29 +00:00
</Button>
2020-02-19 23:57:07 +00:00
)
}
2020-04-08 02:06:59 +01:00
2020-02-19 23:57:07 +00:00
}
SidebarSectionItem.propTypes = {
to: PropTypes.string,
href: PropTypes.string,
onClick: PropTypes.func,
active: PropTypes.bool,
icon: PropTypes.string,
image: PropTypes.string,
title: PropTypes.string,
me: PropTypes.bool,
suffix: PropTypes.node,
buttonRef: PropTypes.func,
}
export default SidebarSectionItem