mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-10 06:42:31 +02:00
fix(v2): improve work of useCollapsible hook with multiple clicks (#5146)
This commit is contained in:
parent
0fdb6836ad
commit
45a87a1479
1 changed files with 35 additions and 18 deletions
|
@ -119,23 +119,26 @@ function useCollapseAnimation({
|
||||||
el.style.willChange = 'height';
|
el.style.willChange = 'height';
|
||||||
|
|
||||||
function startAnimation(): () => void {
|
function startAnimation(): () => void {
|
||||||
|
const animationFrame = requestAnimationFrame(() => {
|
||||||
// When collapsing
|
// When collapsing
|
||||||
if (collapsed) {
|
if (collapsed) {
|
||||||
applyTransitionStyles();
|
applyTransitionStyles();
|
||||||
const animationFrame = requestAnimationFrame(() => {
|
|
||||||
|
requestAnimationFrame(() => {
|
||||||
el.style.height = CollapsedStyles.height;
|
el.style.height = CollapsedStyles.height;
|
||||||
el.style.overflow = CollapsedStyles.overflow;
|
el.style.overflow = CollapsedStyles.overflow;
|
||||||
});
|
});
|
||||||
return () => cancelAnimationFrame(animationFrame);
|
|
||||||
}
|
}
|
||||||
// When expanding
|
// When expanding
|
||||||
else {
|
else {
|
||||||
el.style.display = 'block';
|
el.style.display = 'block';
|
||||||
const animationFrame = requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
applyTransitionStyles();
|
applyTransitionStyles();
|
||||||
});
|
});
|
||||||
return () => cancelAnimationFrame(animationFrame);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => cancelAnimationFrame(animationFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
return startAnimation();
|
return startAnimation();
|
||||||
|
@ -169,8 +172,22 @@ export function Collapsible({
|
||||||
// @ts-expect-error: see https://twitter.com/sebastienlorber/status/1412784677795110914
|
// @ts-expect-error: see https://twitter.com/sebastienlorber/status/1412784677795110914
|
||||||
ref={collapsibleRef}
|
ref={collapsibleRef}
|
||||||
onTransitionEnd={(e) => {
|
onTransitionEnd={(e) => {
|
||||||
if (e.propertyName === 'height') {
|
if (e.propertyName !== 'height') {
|
||||||
applyCollapsedStyle(collapsibleRef.current!, collapsed);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const el = collapsibleRef.current!;
|
||||||
|
const currentCollapsibleElementHeight = el.style.height;
|
||||||
|
|
||||||
|
if (
|
||||||
|
!collapsed &&
|
||||||
|
parseInt(currentCollapsibleElementHeight, 10) === el.scrollHeight
|
||||||
|
) {
|
||||||
|
applyCollapsedStyle(el, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentCollapsibleElementHeight === CollapsedStyles.height) {
|
||||||
|
applyCollapsedStyle(el, true);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
className={className}>
|
className={className}>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue