/
Sidebar.tsx
61 lines (56 loc) · 1.46 KB
/
Sidebar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/** @jsx jsx */
import React, { FC, useContext, ReactText } from 'react';
import { jsx, Box, BoxProps, Heading } from 'theme-ui';
import { get } from '@theme-ui/css';
import { useTheme } from '../ThemeContext';
import { SidebarContext } from './SidebarContext';
export interface SidebarProps {
/**
* Title string or any react node
*/
title?: React.ReactNode;
/**
* Whether the sidebar can be collapsed
*/
collapsible?: boolean;
/**
children content elements to be displayed in Sidebar
*/
children: React.ReactNode;
}
/**
* Collapsible side bar component
*/
export const Sidebar: FC<SidebarProps & BoxProps> = ({
title,
children,
variant,
...rest
}) => {
const toggleContext = useContext(SidebarContext);
const { collapsed, responsive, setCollapsed } = toggleContext || {};
const theme = useTheme();
return collapsed ? null : (
<Box
variant={responsive ? 'sidebar.responsive' : 'sidebar.default'}
sx={get(theme, variant as ReactText)}
onClick={() => responsive && setCollapsed(true)}
{...rest}
>
<Box variant={'sidebar.headercontainer'}>
{title && (
<Box as="header">
{typeof title === 'string' ? (
<Heading as="h3" variant={'sidebar.heading'}>
{title}
</Heading>
) : (
title
)}
</Box>
)}
</Box>
<Box variant="sidebar.inner">{children}</Box>
</Box>
);
};