This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/frontend/components/Layout/components/SidebarLink.js

80 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-06-16 03:39:08 +00:00
// @flow
import React from 'react';
import { NavLink } from 'react-router-dom';
2017-09-25 03:03:03 +00:00
import { color, fontWeight } from 'styles/constants';
2017-06-16 03:39:08 +00:00
import styled from 'styled-components';
2017-09-27 07:21:40 +00:00
import Flex from 'components/Flex';
2017-10-21 21:05:20 +00:00
import CollapsedIcon from 'components/Icon/CollapsedIcon';
2017-09-27 07:21:40 +00:00
2017-06-16 03:39:08 +00:00
const activeStyle = {
color: color.black,
fontWeight: fontWeight.semiBold,
2017-06-16 03:39:08 +00:00
};
2017-09-24 21:25:14 +00:00
// This is a hack for `styleComponent()` as NavLink fails to render without `to` prop
const StyleableDiv = props => <div {...props} />;
2017-10-21 21:05:20 +00:00
const StyledGoTo = styled(CollapsedIcon)`
2017-10-21 23:21:43 +00:00
margin-bottom: -4px;
margin-right: 0;
${({ expanded }) => !expanded && 'transform: rotate(-90deg);'}
2017-10-21 21:05:20 +00:00
`;
const IconWrapper = styled.span`
margin-left: -4px;
margin-right: 4px;
height: 24px;
`;
const styleComponent = component => styled(component)`
2017-10-16 01:43:26 +00:00
display: flex;
2017-09-27 07:21:40 +00:00
width: 100%;
2017-10-21 21:05:20 +00:00
position: relative;
overflow: hidden;
text-overflow: ellipsis;
2017-10-21 21:05:20 +00:00
padding: 4px 0;
2017-09-27 07:21:40 +00:00
margin-left: ${({ hasChildren }) => (hasChildren ? '-20px;' : '0')};
color: ${color.slateDark};
2017-09-04 15:32:31 +00:00
font-size: 15px;
cursor: pointer;
&:hover {
color: ${color.text};
2017-06-16 03:39:08 +00:00
}
2017-09-27 07:21:40 +00:00
2017-10-21 21:05:20 +00:00
&.active {
svg {
fill: ${activeStyle.color}
}
2017-09-27 07:21:40 +00:00
}
2017-06-16 03:39:08 +00:00
`;
2017-10-21 21:05:20 +00:00
type Props = {
to?: string,
onClick?: SyntheticEvent => *,
children?: React$Element<*>,
icon?: React$Element<*>,
hasChildren?: boolean,
expanded?: boolean,
};
function SidebarLink({ icon, children, expanded, ...rest }: Props) {
const Component = styleComponent(rest.to ? NavLink : StyleableDiv);
2017-09-27 07:21:40 +00:00
return (
<Flex>
2017-10-21 21:05:20 +00:00
<Component exact activeStyle={activeStyle} {...rest}>
{icon && <IconWrapper>{icon}</IconWrapper>}
{rest.hasChildren && <StyledGoTo expanded={expanded} />}
<Content>{children}</Content>
2017-09-27 07:21:40 +00:00
</Component>
</Flex>
);
}
2017-10-16 05:15:15 +00:00
const Content = styled.div`
width: 100%;
`;
export default SidebarLink;