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';
|
2018-05-08 05:08:47 +00:00
|
|
|
import { withRouter, NavLink } from 'react-router-dom';
|
2018-05-03 04:51:39 +00:00
|
|
|
import { CollapsedIcon } from 'outline-icons';
|
2018-06-10 02:10:30 +00:00
|
|
|
import styled, { withTheme } from 'styled-components';
|
2017-10-29 22:02:24 +00:00
|
|
|
import Flex from 'shared/components/Flex';
|
2017-09-27 07:21:40 +00:00
|
|
|
|
2017-10-21 21:05:20 +00:00
|
|
|
type Props = {
|
2018-05-13 04:01:17 +00:00
|
|
|
to?: string | Object,
|
2019-08-09 06:09:09 +00:00
|
|
|
onClick?: (SyntheticEvent<>) => void,
|
2018-05-05 23:16:08 +00:00
|
|
|
children?: React.Node,
|
|
|
|
icon?: React.Node,
|
2019-01-07 00:12:07 +00:00
|
|
|
expanded?: boolean,
|
|
|
|
label?: React.Node,
|
2018-05-06 05:45:10 +00:00
|
|
|
menu?: React.Node,
|
2018-05-06 07:06:36 +00:00
|
|
|
menuOpen?: boolean,
|
2019-01-07 00:12:07 +00:00
|
|
|
hideDisclosure?: boolean,
|
2017-11-10 19:45:40 +00:00
|
|
|
iconColor?: string,
|
2018-02-28 06:41:12 +00:00
|
|
|
active?: boolean,
|
2018-06-10 02:10:30 +00:00
|
|
|
theme: Object,
|
2018-08-10 06:14:51 +00:00
|
|
|
exact?: boolean,
|
2019-01-07 00:12:07 +00:00
|
|
|
depth?: number,
|
2017-10-21 21:05:20 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@observer
|
2018-05-05 23:16:08 +00:00
|
|
|
class SidebarLink extends React.Component<Props> {
|
2019-01-07 00:12:07 +00:00
|
|
|
@observable expanded: boolean;
|
2018-06-10 02:10:30 +00:00
|
|
|
|
2019-01-07 00:12:07 +00:00
|
|
|
style = {
|
|
|
|
paddingLeft: `${(this.props.depth || 0) * 16 + 16}px`,
|
|
|
|
};
|
2018-06-10 02:10:30 +00:00
|
|
|
|
2017-10-23 03:17:54 +00:00
|
|
|
componentDidMount() {
|
2019-01-07 00:12:07 +00:00
|
|
|
if (this.props.expanded) this.handleExpand();
|
2017-10-23 03:17:54 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 21:42:33 +00:00
|
|
|
componentWillReceiveProps(nextProps: Props) {
|
2019-01-07 00:12:07 +00:00
|
|
|
if (nextProps.expanded !== undefined) {
|
|
|
|
this.expanded = nextProps.expanded;
|
|
|
|
}
|
2017-10-23 03:17:54 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2019-08-09 06:09:09 +00:00
|
|
|
handleClick = (ev: SyntheticEvent<>) => {
|
2019-01-07 00:12:07 +00:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2017-10-23 03:17:54 +00:00
|
|
|
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,
|
2019-01-07 00:12:07 +00:00
|
|
|
label,
|
2018-02-28 06:41:12 +00:00
|
|
|
active,
|
2018-05-06 05:45:10 +00:00
|
|
|
menu,
|
2018-05-06 07:06:36 +00:00
|
|
|
menuOpen,
|
2019-01-07 00:12:07 +00:00
|
|
|
hideDisclosure,
|
2018-08-10 06:14:51 +00:00
|
|
|
exact,
|
2017-12-06 05:09:15 +00:00
|
|
|
} = this.props;
|
2019-01-07 00:12:07 +00:00
|
|
|
const showDisclosure = !!children && !hideDisclosure;
|
2019-04-20 22:19:35 +00:00
|
|
|
const activeStyle = {
|
|
|
|
color: this.props.theme.text,
|
|
|
|
background: this.props.theme.sidebarItemBackground,
|
|
|
|
fontWeight: 600,
|
|
|
|
...this.style,
|
|
|
|
};
|
2017-10-23 03:17:54 +00:00
|
|
|
|
|
|
|
return (
|
2019-07-13 17:15:38 +00:00
|
|
|
<Wrapper column>
|
2018-11-07 05:58:32 +00:00
|
|
|
<StyledNavLink
|
2019-04-20 22:19:35 +00:00
|
|
|
activeStyle={activeStyle}
|
|
|
|
style={active ? activeStyle : this.style}
|
2017-11-10 22:11:02 +00:00
|
|
|
onClick={onClick}
|
2018-08-10 06:14:51 +00:00
|
|
|
exact={exact !== false}
|
2018-11-07 05:58:32 +00:00
|
|
|
to={to}
|
|
|
|
as={to ? undefined : 'div'}
|
2017-10-23 03:17:54 +00:00
|
|
|
>
|
|
|
|
{icon && <IconWrapper>{icon}</IconWrapper>}
|
2019-01-07 00:12:07 +00:00
|
|
|
<Label onClick={this.handleExpand}>
|
|
|
|
{showDisclosure && (
|
|
|
|
<Disclosure expanded={this.expanded} onClick={this.handleClick} />
|
|
|
|
)}
|
|
|
|
{label}
|
|
|
|
</Label>
|
2019-07-13 17:15:38 +00:00
|
|
|
{menu && <Action menuOpen={menuOpen}>{menu}</Action>}
|
2018-11-07 05:58:32 +00:00
|
|
|
</StyledNavLink>
|
2019-01-07 00:12:07 +00:00
|
|
|
{this.expanded && children}
|
2018-05-06 05:45:10 +00:00
|
|
|
</Wrapper>
|
2017-10-23 03:17:54 +00:00
|
|
|
);
|
|
|
|
}
|
2017-09-18 06:45:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 00:12:07 +00:00
|
|
|
// accounts for whitespace around icon
|
|
|
|
const IconWrapper = styled.span`
|
|
|
|
margin-left: -4px;
|
|
|
|
margin-right: 4px;
|
|
|
|
height: 24px;
|
|
|
|
`;
|
|
|
|
|
2018-05-06 05:45:10 +00:00
|
|
|
const Action = styled.span`
|
2019-07-13 17:15:38 +00:00
|
|
|
display: ${props => (props.menuOpen ? 'inline' : 'none')};
|
2018-05-06 05:45:10 +00:00
|
|
|
position: absolute;
|
2019-01-07 00:12:07 +00:00
|
|
|
top: 4px;
|
|
|
|
right: 4px;
|
2019-03-13 04:35:35 +00:00
|
|
|
color: ${props => props.theme.textTertiary};
|
2019-01-07 00:12:07 +00:00
|
|
|
|
2018-05-06 05:45:10 +00:00
|
|
|
svg {
|
|
|
|
opacity: 0.75;
|
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
svg {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2019-07-13 17:15:38 +00:00
|
|
|
const StyledNavLink = styled(NavLink)`
|
|
|
|
display: flex;
|
2018-05-06 05:45:10 +00:00
|
|
|
position: relative;
|
2019-07-13 17:15:38 +00:00
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
padding: 4px 16px;
|
|
|
|
border-radius: 4px;
|
|
|
|
color: ${props => props.theme.sidebarText};
|
|
|
|
font-size: 15px;
|
|
|
|
cursor: pointer;
|
2018-05-06 05:45:10 +00:00
|
|
|
|
2019-07-13 17:15:38 +00:00
|
|
|
&:hover {
|
|
|
|
color: ${props => props.theme.text};
|
2018-05-06 05:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
> ${Action} {
|
|
|
|
display: inline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2019-07-13 17:15:38 +00:00
|
|
|
const Wrapper = styled(Flex)`
|
|
|
|
position: relative;
|
|
|
|
`;
|
|
|
|
|
2019-01-07 00:12:07 +00:00
|
|
|
const Label = styled.div`
|
|
|
|
position: relative;
|
2017-10-16 05:15:15 +00:00
|
|
|
width: 100%;
|
2019-01-07 00:12:07 +00:00
|
|
|
max-height: 4.4em;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Disclosure = styled(CollapsedIcon)`
|
|
|
|
position: absolute;
|
|
|
|
left: -24px;
|
|
|
|
|
|
|
|
${({ expanded }) => !expanded && 'transform: rotate(-90deg);'};
|
2017-10-16 05:15:15 +00:00
|
|
|
`;
|
|
|
|
|
2018-06-10 02:10:30 +00:00
|
|
|
export default withRouter(withTheme(SidebarLink));
|