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

175 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-06-16 03:39:08 +00:00
// @flow
import { observer } from "mobx-react";
import { CollapsedIcon } from "outline-icons";
import * as React from "react";
import { withRouter, NavLink } from "react-router-dom";
import styled, { withTheme } from "styled-components";
import Flex from "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,
href?: 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
};
function SidebarLink({
icon,
children,
onClick,
to,
label,
active,
menu,
menuOpen,
hideDisclosure,
theme,
exact,
href,
depth,
...rest
}: Props) {
const [expanded, setExpanded] = React.useState(rest.expanded);
const style = React.useMemo(() => {
return {
paddingLeft: `${(depth || 0) * 16 + 16}px`,
};
}, [depth]);
2018-06-10 02:10:30 +00:00
React.useEffect(() => {
if (rest.expanded) {
setExpanded(rest.expanded);
}
}, [rest.expanded]);
const handleClick = React.useCallback(
(ev: SyntheticEvent<>) => {
ev.preventDefault();
ev.stopPropagation();
setExpanded(!expanded);
},
[expanded]
);
const handleExpand = React.useCallback(() => {
setExpanded(true);
}, []);
const showDisclosure = !!children && !hideDisclosure;
const activeStyle = {
color: theme.text,
background: theme.sidebarItemBackground,
fontWeight: 600,
...style,
2017-10-23 03:17:54 +00:00
};
return (
<Wrapper column>
<StyledNavLink
activeStyle={activeStyle}
style={active ? activeStyle : style}
onClick={onClick}
exact={exact !== false}
to={to}
as={to ? undefined : href ? "a" : "div"}
href={href}
>
{icon && <IconWrapper>{icon}</IconWrapper>}
<Label onClick={handleExpand}>
{showDisclosure && (
<Disclosure expanded={expanded} onClick={handleClick} />
)}
{label}
</Label>
{menu && <Action menuOpen={menuOpen}>{menu}</Action>}
</StyledNavLink>
{expanded && children}
</Wrapper>
);
}
// 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;
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.black05};
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
`;
export default withRouter(withTheme(observer(SidebarLink)));