Skip to content

Commit 13d7134

Browse files
glsdowntcbegley
andauthored
Add horizontal collapse (#896)
* Add horizontal collapse * Add file to pass linter Co-authored-by: Tom Begley <[email protected]>
1 parent 8233033 commit 13d7134

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

docs/components_page/components/collapse.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ The `Collapse` component can be used to show and hide content in your apps. Simp
99

1010
{{example:components/collapse/simple.py:collapse}}
1111

12+
## Horizontal collapse
13+
14+
To change the animation to appear horizontally instead of vertically, set `dimension='width'` instead. When using this animation, you should use dimensions on the objects inside the `Collapse` as below.
15+
16+
{{example:components/collapse/horizontal.py:collapse}}
17+
1218
## Multiple targets
1319

1420
You can write arbitrarily complex callbacks to control the behaviour of your `Collapse` components. This example has a single button controlling multiple `Collapse` components, as well as multiple buttons sharing control of a single `Collapse` component.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import dash_bootstrap_components as dbc
2+
from dash import Input, Output, State, html
3+
4+
collapse = html.Div(
5+
[
6+
dbc.Button(
7+
"Open collapse",
8+
id="horizontal-collapse-button",
9+
className="mb-3",
10+
color="primary",
11+
n_clicks=0,
12+
),
13+
html.Div(
14+
dbc.Collapse(
15+
dbc.Card(
16+
dbc.CardBody(
17+
"This content appeared horizontally due to the "
18+
"`dimension` attribute"
19+
),
20+
style={"width": "400px"},
21+
),
22+
id="horizontal-collapse",
23+
is_open=False,
24+
dimension="width",
25+
),
26+
style={"minHeight": "100px"},
27+
),
28+
]
29+
)
30+
31+
32+
@app.callback(
33+
Output("horizontal-collapse", "is_open"),
34+
[Input("horizontal-collapse-button", "n_clicks")],
35+
[State("horizontal-collapse", "is_open")],
36+
)
37+
def toggle_collapse(n, is_open):
38+
if n:
39+
return not is_open
40+
return is_open

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exclude =
1010
docs/components_page/components/button_group/radios.py,
1111
docs/components_page/components/carousel/callback.py,
1212
docs/components_page/components/collapse/accordion.py,
13+
docs/components_page/components/collapse/horizontal.py
1314
docs/components_page/components/collapse/multiple.py,
1415
docs/components_page/components/collapse/simple.py,
1516
docs/components_page/components/dropdown/menu_items.py,

src/components/collapse/Collapse.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const Collapse = React.forwardRef((props, ref) => {
3737
);
3838
});
3939

40+
Collapse.defaultProps = {dimension: 'height'};
41+
4042
Collapse.propTypes = {
4143
/**
4244
* The ID of this component, used to identify dash components
@@ -100,7 +102,13 @@ Collapse.propTypes = {
100102
* Holds the name of the component that is loading
101103
*/
102104
component_name: PropTypes.string
103-
})
105+
}),
106+
107+
/**
108+
* The dimension used when collapsing e.g. height will collapse vertically,
109+
* whilst width will collapse horizontally
110+
*/
111+
dimension: PropTypes.oneOf(['height', 'width'])
104112
};
105113

106114
export default Collapse;

src/components/collapse/__tests__/Collapse.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,28 @@ describe('Collapse', () => {
5050
null
5151
);
5252
});
53+
54+
test('default dimension is height', () => {
55+
const {
56+
container: {firstChild: collapse}
57+
} = render(
58+
<Collapse>
59+
<div>Some collapse content</div>
60+
</Collapse>
61+
);
62+
63+
expect(collapse).not.toHaveClass('collapse-horizontal');
64+
});
65+
66+
test('dimension changes the way the collapse is animated', () => {
67+
const {
68+
container: {firstChild: collapse}
69+
} = render(
70+
<Collapse dimension="width">
71+
<div>Some collapse content</div>
72+
</Collapse>
73+
);
74+
75+
expect(collapse).toHaveClass('collapse-horizontal');
76+
});
5377
});

0 commit comments

Comments
 (0)