Skip to content

Commit d33ec17

Browse files
committed
WIP 2
1 parent 1734079 commit d33ec17

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

docs/src/app/(private)/experiments/collapsible/animation.module.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
.Root {
5353
--width: 320px;
54-
--duration: 1000ms;
54+
--duration: 1500ms;
5555

5656
width: var(--width);
5757

docs/src/app/(private)/experiments/collapsible/animation.tsx

+35-19
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ function Collapsible(props: {
2626

2727
// keyframe animations doesn't need data-starting/ending-style
2828

29-
const [height, setHeight] = React.useState<number | undefined>(undefined);
29+
function initializeHeight() {
30+
return open ? undefined : 0;
31+
}
32+
const [height, setHeight] = React.useState<number | undefined>(initializeHeight);
3033

34+
const latestAnimationNameRef = React.useRef<string>('none');
3135
// const shouldCancelInitialOpenTransitionRef = React.useRef(open);
3236

3337
const isHidden = React.useMemo(() => {
@@ -38,27 +42,24 @@ function Collapsible(props: {
3842
return !open && !mounted;
3943
}, [keepMounted, open, mounted]);
4044

41-
const animationTypeRef = React.useRef<AnimationType>(null);
4245
const panelRef: React.RefObject<HTMLElement | null> = React.useRef(null);
4346

4447
const handlePanelRef = useEventCallback((element: HTMLElement) => {
4548
if (!element) {
4649
return;
4750
}
4851

49-
element.style.animationDuration = '0s';
52+
// element.style.animationDuration = '0s';
53+
const panelStyles = getComputedStyle(element);
54+
latestAnimationNameRef.current = panelStyles.animationName;
5055

51-
// if (height === undefined) {
52-
// // set` display: block !important` here to force layout
53-
// element.style.setProperty('display', 'block', 'important');
54-
// // measure the height
55-
// setHeight(element.scrollHeight);
56-
// element.style.removeProperty('display');
57-
58-
// if (shouldCancelInitialOpenTransitionRef.current) {
59-
// element.style.animationDuration = '0s';
60-
// }
61-
// }
56+
if (height === undefined) {
57+
// set` display: block !important` here to force layout
58+
element.style.setProperty('display', 'block', 'important');
59+
// measure the height
60+
setHeight(element.scrollHeight);
61+
element.style.removeProperty('display');
62+
}
6263

6364
// requestAnimationFrame(() => {
6465
// shouldCancelInitialOpenTransitionRef.current = false;
@@ -86,18 +87,34 @@ function Collapsible(props: {
8687

8788
if (nextOpen) {
8889
/* opening */
90+
91+
panel.style.removeProperty('display');
92+
93+
/* opening */
94+
panel.style.height = '0px';
95+
96+
requestAnimationFrame(() => {
97+
panel.style.removeProperty('height');
98+
setHeight(panel.scrollHeight);
99+
});
89100
} else {
90101
/* closing */
102+
panel.style.height = `${panel.scrollHeight}px`;
103+
// panel.style.removeProperty('display');
104+
requestAnimationFrame(() => {
105+
panel.style.removeProperty('height');
106+
setHeight(0);
107+
});
91108
}
92109
});
93110

94111
return (
95112
<div
96113
className={classes.Root}
97114
style={{
98-
// animationDuration: '0s',
99115
// @ts-ignore
100-
'--collapsible-panel-height': height !== undefined ? `${height}px` : 'auto',
116+
'--collapsible-panel-height':
117+
height !== undefined ? `${height}px` : undefined,
101118
}}
102119
>
103120
<button
@@ -115,10 +132,9 @@ function Collapsible(props: {
115132
// @ts-ignore
116133
ref={mergedRef}
117134
className={classes.Panel}
118-
// {...{ [open ? 'data-open' : 'data-closed']: '' }}
119-
data-open=""
135+
{...{ [open ? 'data-open' : 'data-closed']: '' }}
120136
// {...styleHooks}
121-
hidden={isHidden}
137+
hidden={!open}
122138
id={id}
123139
>
124140
<div className={classes.Content}>

docs/src/styles.css

+4
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@
222222
[data-demo]:not([data-demo='tailwind']) * {
223223
all: revert-layer;
224224
}
225+
226+
[hidden]:where(:not([hidden='until-found'])) {
227+
display: revert-layer !important;
228+
}
225229
}
226230

227231
@layer all {

0 commit comments

Comments
 (0)