2020-02-25 17:15:23 +01:00
|
|
|
import React, { FC, ReactNode, useEffect, useState } from "react";
|
2020-02-25 09:49:23 +01:00
|
|
|
import { Button } from "../buttons";
|
|
|
|
|
import styled from "styled-components";
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
label: string;
|
2019-10-20 16:59:02 +02:00
|
|
|
children?: ReactNode;
|
2020-02-25 09:49:23 +01:00
|
|
|
collapsed?: boolean;
|
|
|
|
|
onCollapse?: (newStatus: boolean) => void;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2020-02-25 17:15:23 +01:00
|
|
|
const SectionContainer = styled.div`
|
|
|
|
|
position: ${props => (props.scrollPositionY > 210 ? "fixed" : "absolute")};
|
|
|
|
|
top: ${props => props.scrollPositionY > 210 && "4.5rem"};
|
|
|
|
|
width: ${props => (props.collapsed ? "5.5rem" : "20.5rem")};
|
|
|
|
|
`;
|
|
|
|
|
|
2020-02-25 09:49:23 +01:00
|
|
|
const SmallButton = styled(Button)`
|
|
|
|
|
height: 1.5rem;
|
|
|
|
|
width: 1rem;
|
|
|
|
|
position: absolute;
|
|
|
|
|
right: 1.5rem;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const MenuLabel = styled.p`
|
|
|
|
|
min-height: 2.5rem;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const Section: FC<Props> = ({ label, children, collapsed, onCollapse }) => {
|
2020-02-25 17:15:23 +01:00
|
|
|
const [scrollPositionY, setScrollPositionY] = useState(0);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
window.addEventListener("scroll", () => setScrollPositionY(window.pageYOffset));
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener("scroll", () => setScrollPositionY(window.pageYOffset));
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2020-02-25 09:49:23 +01:00
|
|
|
const childrenWithProps = React.Children.map(children, child => React.cloneElement(child, { collapsed: collapsed }));
|
2020-02-25 17:15:23 +01:00
|
|
|
const arrowIcon = collapsed ? <i className="fas fa-caret-down" /> : <i className="fas fa-caret-right" />;
|
|
|
|
|
|
2020-02-25 09:49:23 +01:00
|
|
|
return (
|
2020-02-25 17:15:23 +01:00
|
|
|
<SectionContainer collapsed={collapsed} scrollPositionY={scrollPositionY}>
|
2020-02-25 09:49:23 +01:00
|
|
|
<MenuLabel className="menu-label">
|
|
|
|
|
{collapsed ? "" : label}
|
|
|
|
|
{onCollapse && (
|
|
|
|
|
<SmallButton color="info" className="is-small" action={onCollapse}>
|
2020-02-25 17:15:23 +01:00
|
|
|
{arrowIcon}
|
2020-02-25 09:49:23 +01:00
|
|
|
</SmallButton>
|
|
|
|
|
)}
|
|
|
|
|
</MenuLabel>
|
|
|
|
|
<ul className="menu-list">{childrenWithProps}</ul>
|
2020-02-25 17:15:23 +01:00
|
|
|
</SectionContainer>
|
2020-02-25 09:49:23 +01:00
|
|
|
);
|
|
|
|
|
};
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
export default Section;
|