zepio/app/components/sidebar.js

116 lines
3.4 KiB
JavaScript
Raw Normal View History

// @flow
2019-02-04 20:41:45 -08:00
2019-01-28 16:34:07 -08:00
/* eslint-disable max-len */
2018-12-12 11:48:08 -08:00
import React from 'react';
import styled, { withTheme } from 'styled-components';
import type { Location, RouterHistory } from 'react-router-dom';
import { MENU_OPTIONS } from '../constants/sidebar';
2018-11-26 18:52:47 -08:00
const Wrapper = styled.div`
display: flex;
flex-direction: column;
width: ${props => props.theme.sidebarWidth};
height: ${props => `calc(100vh - ${props.theme.headerHeight})`};
font-family: ${props => props.theme.fontFamily};
background-color: ${props => props.theme.colors.sidebarBg};
border-right: 1px solid ${props => props.theme.colors.sidebarBorderRight};
2018-12-12 11:05:19 -08:00
padding-top: 15px;
2019-01-10 05:00:52 -08:00
position: relative;
`;
2019-02-04 20:41:45 -08:00
/* eslint-disable max-len */
type StyledLinkProps = PropsWithTheme<{ isActive: boolean }>;
const StyledLink = styled.a`
color: ${(props: StyledLinkProps) => (props.isActive
? props.theme.colors.sidebarItemActive
: props.theme.colors.sidebarItem
)};
background-color: ${(props: StyledLinkProps) => (props.isActive
? props.theme.colors.sidebarItemHoveredBg
: 'transparent'
)};
font-size: ${(props: StyledLinkProps) => `${props.theme.fontSize.regular}em`};
2018-12-04 20:26:03 -08:00
text-decoration: none;
font-weight: ${(props: StyledLinkProps) => String(props.theme.fontWeight.bold)};
letter-spacing: 0.25px;
padding: 25px 20px;
2018-12-12 11:48:08 -08:00
height: 35px;
width: 100%;
2018-12-11 15:43:27 -08:00
display: flex;
align-items: center;
2018-12-11 16:45:52 -08:00
outline: none;
2019-01-10 04:38:00 -08:00
cursor: pointer;
2019-02-15 18:59:05 -08:00
outline: none;
transition: all 0.03s ${(props: StyledLinkProps) => props.theme.transitionEase};
2019-02-15 18:59:05 -08:00
border-right: ${(props: StyledLinkProps) => (props.isActive ? `3px solid ${props.theme.colors.sidebarActiveItemBorder}` : 'none')};
border-top: 1px solid ${(props: StyledLinkProps) => (props.isActive ? props.theme.colors.sidebarBorderRight : 'transparent')};
border-bottom: 1px solid ${(props: StyledLinkProps) => (props.isActive ? props.theme.colors.sidebarBorderRight : 'transparent')};
2018-12-11 15:43:27 -08:00
&:hover {
border-top: 1px solid ${props => props.theme.colors.sidebarBorderRight};
border-bottom: 1px solid ${props => props.theme.colors.sidebarBorderRight};
background-color: ${(props: StyledLinkProps) => props.theme.colors.sidebarItemHoveredBg};
color: ${(props: StyledLinkProps) => (props.isActive ? props.theme.colors.sidebarItemActive : props.theme.colors.sidebarItemHovered)}
2018-12-11 15:43:27 -08:00
}
2018-11-26 18:52:47 -08:00
`;
2018-12-12 11:05:19 -08:00
const Icon = styled.img`
width: 17px;
height: 17px;
margin-right: 13px;
opacity: ${(props: any) => (props.isActive ? '1' : '0.3')};
${StyledLink}:hover & {
opacity: 1;
}
2018-12-12 11:05:19 -08:00
`;
type MenuItem = {
route: string,
label: string,
icon: (isActive: boolean, themeMode: string) => string,
};
2018-11-26 18:52:47 -08:00
type Props = {
history: RouterHistory,
2018-11-26 18:52:47 -08:00
options?: MenuItem[],
2018-12-11 16:15:38 -08:00
location: Location,
theme: AppTheme,
2018-11-26 18:52:47 -08:00
};
export const Component = ({
options, location, history, theme,
}: Props) => (
2019-01-22 10:50:11 -08:00
<Wrapper id='sidebar'>
2018-12-12 11:48:08 -08:00
{(options || []).map((item) => {
2019-02-14 14:17:13 -08:00
const isActive = item.route === '/'
? location.pathname === item.route
: location.pathname.startsWith(item.route);
2018-12-12 11:48:08 -08:00
return (
<StyledLink
isActive={isActive}
key={item.route}
onClick={() => (isActive ? {} : history.push(item.route))}
>
2019-02-15 18:59:05 -08:00
<Icon
isActive={isActive}
src={item.icon(isActive, theme.mode)}
2019-02-20 11:50:18 -08:00
alt={`${item.route}`}
2019-02-15 18:59:05 -08:00
/>
2018-12-12 11:48:08 -08:00
{item.label}
</StyledLink>
);
})}
</Wrapper>
);
2018-12-12 11:05:19 -08:00
Component.defaultProps = {
2018-12-12 11:48:08 -08:00
options: MENU_OPTIONS,
};
export const SidebarComponent = withTheme(Component);