This repository was archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathExpansion.js
109 lines (95 loc) · 2.42 KB
/
Expansion.js
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
import React from "react";
import styled, { css } from "styled-components";
import PropTypes from "prop-types";
import { Collapse } from "react-collapse";
import { PALETTE, CSS_TRANSITION_SPEED } from "constants/styles";
import { BasicButton } from "basics/Buttons";
export const expansionStyles = css`
.ReactCollapse--collapse {
transition: height ${CSS_TRANSITION_SPEED.default};
}
`;
const ExpansionHeaderEl = styled(BasicButton)`
cursor: pointer;
display: flex;
justify-content: space-between;
`;
const ExpansionEl = styled.div.attrs((props) => ({
"aria-expanded": props.isExpanded,
}))`
display: flex;
flex-direction: column;
border-radius: 4px;
border: none;
${ExpansionHeaderEl} {
padding: 1rem 0;
}
${({ hasBorder }) =>
hasBorder &&
css`
${ExpansionHeaderEl} {
border: solid 1px ${PALETTE.white60};
padding: 1rem;
}
`}
`;
const ExpansionIconEl = styled.div`
display: block;
width: auto;
align-items: center;
justify-content: space-between;
`;
export const ExpandedSection = Collapse;
ExpandedSection.propTypes = {
/**
* If true, expands the section, otherwise collapse it. Setting this prop enables control over the panel.
*/
isExpanded: PropTypes.bool,
/**
* The content of the expansion component.
*/
children: PropTypes.node.isRequired,
};
export const Expansion = React.forwardRef(function Expansion(
{
title,
expandedModeTitle,
hasBorder = false,
expandIcon,
collapseIcon,
children,
isDefaultExpanded = false,
...props
},
ref,
) {
const [isExpanded, setExpanded] = React.useState(isDefaultExpanded);
const onHandleClick = () => {
setExpanded(!isExpanded);
};
return (
<ExpansionEl
{...props}
hasBorder={hasBorder}
isExpanded={isExpanded}
ref={ref}
>
<ExpansionHeaderEl onClick={onHandleClick}>
{isExpanded ? expandedModeTitle : title}
<ExpansionIconEl>
{isExpanded ? collapseIcon : expandIcon}
</ExpansionIconEl>
</ExpansionHeaderEl>
<Collapse isOpened={isExpanded}>{children}</Collapse>
</ExpansionEl>
);
});
Expansion.propTypes = {
title: PropTypes.node.isRequired,
expandedModeTitle: PropTypes.node.isRequired,
hasBorder: PropTypes.bool,
isDefaultExpanded: PropTypes.bool,
expandIcon: PropTypes.node.isRequired,
collapseIcon: PropTypes.node.isRequired,
children: PropTypes.node.isRequired,
};