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

172 lines
3.5 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';
2018-06-10 02:10:30 +00:00
import styled, { withTheme } from 'styled-components';
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,
onClick?: (SyntheticEvent<>) => void,
2018-05-05 23:16:08 +00:00
children?: React.Node,
icon?: React.Node,
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,
hideDisclosure?: boolean,
2017-11-10 19:45:40 +00:00
iconColor?: string,
active?: boolean,
2018-06-10 02:10:30 +00:00
theme: Object,
2018-08-10 06:14:51 +00:00
exact?: boolean,
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> {
@observable expanded: ?boolean = this.props.expanded;
2018-06-10 02:10:30 +00:00
style = {
paddingLeft: `${(this.props.depth || 0) * 16 + 16}px`,
};
2018-06-10 02:10:30 +00:00
2017-11-10 21:42:33 +00:00
componentWillReceiveProps(nextProps: Props) {
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
handleClick = (ev: SyntheticEvent<>) => {
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,
label,
active,
2018-05-06 05:45:10 +00:00
menu,
2018-05-06 07:06:36 +00:00
menuOpen,
hideDisclosure,
2018-08-10 06:14:51 +00:00
exact,
2017-12-06 05:09:15 +00:00
} = this.props;
const showDisclosure = !!children && !hideDisclosure;
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 (
<Wrapper column>
2018-11-07 05:58:32 +00:00
<StyledNavLink
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>}
<Label onClick={this.handleExpand}>
{showDisclosure && (
<Disclosure expanded={this.expanded} onClick={this.handleClick} />
)}
{label}
</Label>
{menu && <Action menuOpen={menuOpen}>{menu}</Action>}
2018-11-07 05:58:32 +00:00
</StyledNavLink>
{this.expanded && children}
2018-05-06 05:45:10 +00:00
</Wrapper>
2017-10-23 03:17:54 +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`
display: ${props => (props.menuOpen ? 'inline' : 'none')};
2018-05-06 05:45:10 +00:00
position: absolute;
top: 4px;
right: 4px;
2019-03-13 04:35:35 +00:00
color: ${props => props.theme.textTertiary};
2018-05-06 05:45:10 +00:00
svg {
opacity: 0.75;
}
&:hover {
svg {
opacity: 1;
}
}
`;
const StyledNavLink = styled(NavLink)`
display: flex;
2018-05-06 05:45:10 +00:00
position: relative;
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
&:hover {
color: ${props => props.theme.text};
2018-05-06 05:45:10 +00:00
}
&:focus {
color: ${props => props.theme.text};
background: ${props => props.theme.sidebarItemBackground};
outline: none;
}
2018-05-06 05:45:10 +00:00
&:hover {
> ${Action} {
display: inline;
}
}
`;
const Wrapper = styled(Flex)`
position: relative;
`;
const Label = styled.div`
position: relative;
2017-10-16 05:15:15 +00:00
width: 100%;
max-height: 4.8em;
line-height: 1.6;
`;
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));