Skip to content

Commit 32d05e7

Browse files
committed
docs: add collapse example
This example shows how to use CSS & JS together to get a nice collapse transition and animate content height.
1 parent 5defdad commit 32d05e7

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

dev/src/pages/kitchen-sink.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,68 @@ const SwitchJS: Component = () => {
106106
);
107107
};
108108

109+
const Collapse: Component = () => {
110+
const [show, toggleShow] = createSignal(true);
111+
112+
const COLLAPSED_PROPERTIES = {
113+
height: 0,
114+
marginTop: 0,
115+
marginBottom: 0,
116+
paddingTop: 0,
117+
paddingBottom: 0,
118+
borderTopWidth: 0,
119+
borderBottomWidth: 0
120+
};
121+
122+
function getHeight(el: Element): string {
123+
const rect = el.getBoundingClientRect();
124+
return `${rect.height}px`;
125+
}
126+
127+
function onEnter(el: Element, done: VoidFunction) {
128+
const a = el.animate(
129+
[
130+
COLLAPSED_PROPERTIES,
131+
{
132+
height: getHeight(el)
133+
}
134+
],
135+
{ duration: 500, easing: "ease" }
136+
);
137+
138+
a.finished.then(done);
139+
}
140+
141+
function onExit(el: Element, done: VoidFunction) {
142+
const a = el.animate(
143+
[
144+
{
145+
height: getHeight(el)
146+
},
147+
COLLAPSED_PROPERTIES
148+
],
149+
{ duration: 500, easing: "ease" }
150+
);
151+
152+
a.finished.then(done);
153+
}
154+
155+
return (
156+
<>
157+
<button onClick={() => toggleShow(!show())}>{show() ? "Hide" : "Show"}</button>
158+
<br />
159+
<Transition mode="outin" name="collapse" onEnter={onEnter} onExit={onExit}>
160+
<Show when={show()}>
161+
<p>
162+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris facilisis enim libero,
163+
at lacinia diam fermentum id. Pellentesque habitant morbi tristique senectus et netus.
164+
</p>
165+
</Show>
166+
</Transition>
167+
</>
168+
);
169+
};
170+
109171
const Example = () => {
110172
const [show, toggleShow] = createSignal(true);
111173

@@ -164,6 +226,11 @@ const Example = () => {
164226
<SwitchJS />
165227
<br />
166228

229+
<b>Collapse OutIn CSS & JS</b>
230+
<br />
231+
<Collapse />
232+
<br />
233+
167234
<b>Group</b>
168235
<br />
169236
<Group />

dev/src/styles.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ button {
8484
}
8585
}
8686

87+
.collapse-exit-active,
88+
.collapse-enter-active {
89+
overflow: hidden;
90+
}
91+
8792
.group-item {
8893
transition: all 0.5s;
8994
}

0 commit comments

Comments
 (0)