-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
Collapse.tsx
162 lines (153 loc) · 4.11 KB
/
Collapse.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { css, cx } from '@emotion/css';
import React, { FunctionComponent, useState } from 'react';
import { GrafanaTheme2 } from '@grafana/data';
import { useStyles2 } from '../../themes/ThemeContext';
import { clearButtonStyles } from '../Button';
import { Icon } from '../Icon/Icon';
const getStyles = (theme: GrafanaTheme2) => ({
collapse: css`
label: collapse;
margin-bottom: ${theme.spacing(1)};
background-color: ${theme.colors.background.primary};
border: 1px solid ${theme.colors.border.medium};
position: relative;
border-radius: 3px;
width: 100%;
display: flex;
flex-direction: column;
flex: 1 1 0;
`,
collapseBody: css`
label: collapse__body;
padding: ${theme.spacing(theme.components.panel.padding)};
padding-top: 0;
flex: 1;
overflow: hidden;
display: flex;
flex-direction: column;
`,
bodyContentWrapper: css`
label: bodyContentWrapper;
flex: 1;
overflow: hidden;
`,
loader: css`
label: collapse__loader;
height: 2px;
position: relative;
overflow: hidden;
background: none;
margin: ${theme.spacing(0.5)};
`,
loaderActive: css`
label: collapse__loader_active;
&:after {
content: ' ';
display: block;
width: 25%;
top: 0;
top: -50%;
height: 250%;
position: absolute;
animation: loader 2s cubic-bezier(0.17, 0.67, 0.83, 0.67) 500ms;
animation-iteration-count: 100;
left: -25%;
background: ${theme.colors.primary.main};
}
@keyframes loader {
from {
left: -25%;
opacity: 0.1;
}
to {
left: 100%;
opacity: 1;
}
}
`,
header: css`
label: collapse__header;
padding: ${theme.spacing(1, 2, 1, 2)};
display: flex;
transition: all 0.1s linear;
`,
headerCollapsed: css`
label: collapse__header--collapsed;
padding: ${theme.spacing(1, 2, 1, 2)};
`,
headerLabel: css`
label: collapse__header-label;
font-weight: ${theme.typography.fontWeightMedium};
margin-right: ${theme.spacing(1)};
font-size: ${theme.typography.size.md};
`,
icon: css`
label: collapse__icon;
margin: ${theme.spacing(0.25, 1, 0, -1)};
`,
});
export interface Props {
/** Expand or collapse te content */
isOpen?: boolean;
/** Element or text for the Collapse header */
label: React.ReactNode;
/** Indicates loading state of the content */
loading?: boolean;
/** Toggle collapsed header icon */
collapsible?: boolean;
/** Callback for the toggle functionality */
onToggle?: (isOpen: boolean) => void;
/** Additional class name for the root element */
className?: string;
}
export const ControlledCollapse: FunctionComponent<Props> = ({ isOpen, onToggle, ...otherProps }) => {
const [open, setOpen] = useState(isOpen);
return (
<Collapse
isOpen={open}
collapsible
{...otherProps}
onToggle={() => {
setOpen(!open);
if (onToggle) {
onToggle(!open);
}
}}
/>
);
};
export const Collapse = ({
isOpen,
label,
loading,
collapsible,
onToggle,
className,
children,
}: React.PropsWithChildren<Props>) => {
const buttonStyles = useStyles2(clearButtonStyles);
const style = useStyles2(getStyles);
const onClickToggle = () => {
if (onToggle) {
onToggle(!isOpen);
}
};
const panelClass = cx([style.collapse, className]);
const loaderClass = loading ? cx([style.loader, style.loaderActive]) : cx([style.loader]);
const headerClass = collapsible ? cx([style.header]) : cx([style.headerCollapsed]);
return (
<div className={panelClass}>
<button type="button" className={cx(buttonStyles, headerClass)} onClick={onClickToggle}>
{collapsible && <Icon className={style.icon} name={isOpen ? 'angle-down' : 'angle-right'} />}
<div className={cx([style.headerLabel])}>{label}</div>
</button>
{isOpen && (
<div className={cx([style.collapseBody])}>
<div className={loaderClass} />
<div className={style.bodyContentWrapper}>{children}</div>
</div>
)}
</div>
);
};
Collapse.displayName = 'Collapse';