2018-11-23 15:46:06 -08:00
|
|
|
// @flow
|
|
|
|
|
2018-12-12 11:05:19 -08:00
|
|
|
import React, { Component } from 'react';
|
2018-11-23 15:46:06 -08:00
|
|
|
import styled from 'styled-components';
|
2018-12-11 16:15:38 -08:00
|
|
|
import { Link, type Location } from 'react-router-dom';
|
2018-11-28 14:00:25 -08:00
|
|
|
import { MENU_OPTIONS } from '../constants/sidebar';
|
2018-11-23 15:46:06 -08:00
|
|
|
|
2018-11-26 18:52:47 -08:00
|
|
|
const Wrapper = styled.div`
|
2018-11-28 14:00:25 -08:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2018-12-12 11:05:19 -08:00
|
|
|
width: ${props => props.theme.sidebarWidth};
|
|
|
|
height: ${props => `calc(100vh - ${props.theme.headerHeight})`};
|
2018-12-04 20:26:03 -08:00
|
|
|
font-family: ${props => props.theme.fontFamily}
|
2018-12-12 11:05:19 -08:00
|
|
|
background-color: ${props => props.theme.colors.sidebarBg};
|
|
|
|
padding-top: 15px;
|
2018-11-28 14:00:25 -08:00
|
|
|
`;
|
|
|
|
|
|
|
|
const StyledLink = styled(Link)`
|
2018-12-11 15:43:27 -08:00
|
|
|
color: ${props => (props.isActive ? props.theme.colors.sidebarItemActive : props.theme.colors.sidebarItem)};
|
2018-12-12 11:05:19 -08:00
|
|
|
font-size: ${props => `${props.theme.fontSize.text}em`};
|
2018-12-04 20:26:03 -08:00
|
|
|
text-decoration: none;
|
2018-12-12 11:05:19 -08:00
|
|
|
font-weight: ${props => (props.isActive ? props.theme.fontWeight.bold : props.theme.fontWeight.default)};
|
2018-12-11 15:43:27 -08:00
|
|
|
padding: 15px 20px;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2018-12-11 16:45:52 -08:00
|
|
|
outline: none;
|
2018-12-12 11:05:19 -08:00
|
|
|
border-right: ${props => (props.isActive ? `1px solid ${props.theme.colors.sidebarItemActive}` : 'none')};
|
2018-12-11 15:43:27 -08:00
|
|
|
|
|
|
|
&:hover {
|
|
|
|
color: ${props => props.theme.colors.sidebarItemActive};
|
2018-12-12 11:05:19 -08:00
|
|
|
|
|
|
|
svg {
|
|
|
|
color: ${props => props.theme.colors.sidebarItemActive};
|
|
|
|
}
|
2018-12-11 15:43:27 -08:00
|
|
|
}
|
2018-11-26 18:52:47 -08:00
|
|
|
`;
|
2018-11-23 15:46:06 -08:00
|
|
|
|
2018-12-12 11:05:19 -08:00
|
|
|
const Icon = styled.img`
|
|
|
|
width: 20px;
|
|
|
|
height: 20px;
|
|
|
|
margin-right: 15px;
|
|
|
|
`;
|
|
|
|
|
2018-11-28 20:14:44 -08:00
|
|
|
type MenuItem = {
|
|
|
|
route: string,
|
|
|
|
label: string,
|
2018-12-12 11:05:19 -08:00
|
|
|
icon: (isActive: boolean) => string,
|
2018-11-28 20:14:44 -08:00
|
|
|
};
|
2018-11-26 18:52:47 -08:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
options?: MenuItem[],
|
2018-12-11 16:15:38 -08:00
|
|
|
location: Location,
|
2018-11-26 18:52:47 -08:00
|
|
|
};
|
|
|
|
|
2018-12-12 11:05:19 -08:00
|
|
|
type State = {
|
|
|
|
currentHovered: string | null,
|
2018-11-26 18:52:47 -08:00
|
|
|
};
|
2018-12-12 11:05:19 -08:00
|
|
|
|
|
|
|
export class SidebarComponent extends Component<Props, State> {
|
|
|
|
static defaultProps = {
|
|
|
|
options: MENU_OPTIONS,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
currentHovered: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { options, location } = this.props;
|
|
|
|
const { currentHovered } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Wrapper>
|
|
|
|
{(options || []).map((item) => {
|
|
|
|
const isActive = location.pathname === item.route;
|
|
|
|
return (
|
|
|
|
<StyledLink
|
|
|
|
onMouseEnter={() => this.setState(() => ({ currentHovered: item.route }))}
|
|
|
|
onMouseLeave={() => this.setState(() => ({ currentHovered: null }))}
|
|
|
|
isActive={isActive}
|
|
|
|
key={item.route}
|
|
|
|
to={item.route}
|
|
|
|
>
|
|
|
|
<Icon src={item.icon(currentHovered === item.route || isActive)} alt={`Sidebar Icon ${item.route}`} />
|
|
|
|
{item.label}
|
|
|
|
</StyledLink>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Wrapper>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|