Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ function applyCollapsedStyle(el: HTMLElement, collapsed: boolean) {
el.style.height = collapsedStyles.height;
}

function parseTransitionTime(time: string) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not fan of this

return time
.split(',')
.map((value) => value.trim())
.reduce((total, value) => {
if (value.endsWith('ms')) {
return total + Number.parseFloat(value);
}
if (value.endsWith('s')) {
return total + Number.parseFloat(value) * 1000;
}
return total;
}, 0);
}

/*
Lex111: Dynamic transition duration is used in Material design, this technique
is good for a large number of items.
Expand All @@ -90,12 +105,19 @@ function useCollapseAnimation({
collapsibleRef,
collapsed,
animation,
onCollapseTransitionEnd,
}: {
collapsibleRef: RefObject<HTMLElement | null>;
collapsed: boolean;
animation?: CollapsibleAnimationConfig;
onCollapseTransitionEnd?: (collapsed: boolean) => void;
}) {
const mounted = useRef(false);
const onCollapseTransitionEndRef = useRef(onCollapseTransitionEnd);

useEffect(() => {
onCollapseTransitionEndRef.current = onCollapseTransitionEnd;
}, [onCollapseTransitionEnd]);
Comment on lines +116 to +120
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not fan of this


useEffect(() => {
const el = collapsibleRef.current!;
Expand All @@ -116,6 +138,18 @@ function useCollapseAnimation({
el.style.height = transitionStyles.height;
}

function finishAnimation() {
applyCollapsedStyle(el, collapsed);
onCollapseTransitionEndRef.current?.(collapsed);
}

function finishAnimationIfTransitionDisabled() {
const {transitionDuration} = getComputedStyle(el);
if (parseTransitionTime(transitionDuration) === 0) {
finishAnimation();
}
}
Comment on lines +141 to +151
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not fan of this


// On mount, we just apply styles, no animated transition
if (!mounted.current) {
applyCollapsedStyle(el, collapsed);
Expand All @@ -134,13 +168,15 @@ function useCollapseAnimation({
requestAnimationFrame(() => {
el.style.height = CollapsedStyles.height;
el.style.overflow = CollapsedStyles.overflow;
finishAnimationIfTransitionDisabled();
});
}
// When expanding
else {
el.style.display = 'block';
requestAnimationFrame(() => {
applyTransitionStyles();
finishAnimationIfTransitionDisabled();
});
}
});
Expand Down Expand Up @@ -185,7 +221,12 @@ function CollapsibleBase({
}: CollapsibleBaseProps) {
const collapsibleRef = useRef<HTMLElement>(null);

useCollapseAnimation({collapsibleRef, collapsed, animation});
useCollapseAnimation({
collapsibleRef,
collapsed,
animation,
onCollapseTransitionEnd,
});

return (
<As
Expand Down