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/app/components/Sidebar/components/SidebarLink.js

162 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-06-16 03:39:08 +00:00
// @flow
2018-05-05 23:16:08 +00:00
import * as React from 'react';
2017-10-23 03:17:54 +00:00
import { observable, action } from 'mobx';
import { observer } from 'mobx-react';
import { withRouter, NavLink } from 'react-router-dom';
2018-05-03 04:51:39 +00:00
import { CollapsedIcon } from 'outline-icons';
2017-10-27 05:42:08 +00:00
import { color, fontWeight } from 'shared/styles/constants';
2017-06-16 03:39:08 +00:00
import styled from 'styled-components';
import Flex from 'shared/components/Flex';
2017-09-27 07:21:40 +00:00
2017-06-16 03:39:08 +00:00
const activeStyle = {
color: color.black,
2017-11-08 06:02:07 +00:00
fontWeight: fontWeight.medium,
2017-06-16 03:39:08 +00:00
};
2017-10-21 21:05:20 +00:00
const StyledGoTo = styled(CollapsedIcon)`
2017-10-21 23:21:43 +00:00
margin-bottom: -4px;
2017-11-01 04:35:17 +00:00
margin-left: 1px;
margin-right: -3px;
2017-11-10 22:14:30 +00:00
${({ 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;
`;
2017-10-31 07:03:17 +00:00
const StyledNavLink = styled(NavLink)`
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-12-06 05:09:15 +00:00
margin-left: ${({ iconVisible }) => (iconVisible ? '-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-10-31 07:03:17 +00:00
const StyledDiv = StyledNavLink.withComponent('div');
2017-10-21 21:05:20 +00:00
type Props = {
to?: string,
2018-05-05 23:16:08 +00:00
onClick?: (SyntheticEvent<*>) => *,
children?: React.Node,
icon?: React.Node,
2017-10-23 03:17:54 +00:00
expand?: boolean,
2018-05-05 23:16:08 +00:00
expandedContent?: React.Node,
2018-05-06 05:45:10 +00:00
menu?: React.Node,
2018-05-06 07:06:36 +00:00
menuOpen?: boolean,
2017-12-06 05:09:15 +00:00
hideExpandToggle?: boolean,
2017-11-10 19:45:40 +00:00
iconColor?: string,
active?: boolean,
2017-10-21 21:05:20 +00:00
};
@withRouter
2017-11-10 22:14:30 +00:00
@observer
2018-05-05 23:16:08 +00:00
class SidebarLink extends React.Component<Props> {
2017-10-31 07:03:17 +00:00
@observable expanded: boolean = false;
2017-10-23 03:17:54 +00:00
componentDidMount() {
if (this.props.expand) this.handleExpand();
}
2017-11-10 21:42:33 +00:00
componentWillReceiveProps(nextProps: Props) {
2017-10-23 03:17:54 +00:00
if (nextProps.expand) this.handleExpand();
}
2017-11-10 22:14:30 +00:00
@action
2018-05-05 23:16:08 +00:00
handleClick = (event: SyntheticEvent<*>) => {
2017-10-23 03:17:54 +00:00
event.preventDefault();
event.stopPropagation();
this.expanded = !this.expanded;
};
2017-11-10 22:14:30 +00:00
@action
handleExpand = () => {
2017-10-23 03:17:54 +00:00
this.expanded = true;
};
render() {
2017-12-06 05:09:15 +00:00
const {
icon,
children,
onClick,
to,
expandedContent,
expand,
active,
2018-05-06 05:45:10 +00:00
menu,
2018-05-06 07:06:36 +00:00
menuOpen,
2017-12-06 05:09:15 +00:00
hideExpandToggle,
} = this.props;
2017-11-10 22:11:02 +00:00
const Component = to ? StyledNavLink : StyledDiv;
2017-12-06 05:09:15 +00:00
const showExpandIcon = expandedContent && !hideExpandToggle;
2017-10-23 03:17:54 +00:00
return (
2018-05-06 07:06:36 +00:00
<Wrapper menuOpen={menuOpen} column>
2017-10-23 03:17:54 +00:00
<Component
2017-12-06 05:09:15 +00:00
iconVisible={showExpandIcon}
2017-10-23 03:17:54 +00:00
activeStyle={activeStyle}
style={active ? activeStyle : undefined}
2017-11-10 22:11:02 +00:00
onClick={onClick}
to={to}
exact
2017-10-23 03:17:54 +00:00
>
{icon && <IconWrapper>{icon}</IconWrapper>}
2017-12-06 05:09:15 +00:00
{showExpandIcon && (
2017-11-10 22:14:30 +00:00
<StyledGoTo expanded={this.expanded} onClick={this.handleClick} />
)}
2017-10-23 03:17:54 +00:00
<Content onClick={this.handleExpand}>{children}</Content>
</Component>
2017-12-06 05:09:15 +00:00
{/* Collection */ expand && hideExpandToggle && expandedContent}
{/* Document */ this.expanded && !hideExpandToggle && expandedContent}
2018-05-06 05:45:10 +00:00
{menu && <Action>{menu}</Action>}
</Wrapper>
2017-10-23 03:17:54 +00:00
);
}
}
2018-05-06 05:45:10 +00:00
const Action = styled.span`
position: absolute;
right: 0;
2018-05-06 07:06:36 +00:00
top: 2px;
2018-05-06 05:45:10 +00:00
color: ${color.slate};
svg {
opacity: 0.75;
}
&:hover {
svg {
opacity: 1;
}
}
`;
const Wrapper = styled(Flex)`
position: relative;
> ${Action} {
display: ${props => (props.menuOpen ? 'inline' : 'none')};
}
&:hover {
> ${Action} {
display: inline;
}
}
`;
2017-10-16 05:15:15 +00:00
const Content = styled.div`
width: 100%;
`;
export default SidebarLink;