Skip to content

Commit 5c9a159

Browse files
committed
Handle beforematch with CSS animations
1 parent 00f99b8 commit 5c9a159

File tree

1 file changed

+8
-14
lines changed
  • docs/src/app/(private)/experiments/collapsible

1 file changed

+8
-14
lines changed

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

+8-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Collapsible(props: {
2828
keepMounted = true,
2929
defaultOpen = false,
3030
id,
31-
hiddenUntilFound: hiddenUntilFoundProp = false,
31+
hiddenUntilFound: hiddenUntilFoundProp = true,
3232
} = props;
3333

3434
const [open, setOpen] = React.useState(defaultOpen);
@@ -92,7 +92,7 @@ function Collapsible(props: {
9292
const panelStyles = getComputedStyle(element);
9393
if (
9494
panelStyles.animationName !== 'none' &&
95-
panelStyles.transitionDelay !== '0s'
95+
panelStyles.transitionDuration !== '0s'
9696
) {
9797
warn('CSS transitions and CSS animations both detected');
9898
} else if (
@@ -333,7 +333,7 @@ function Collapsible(props: {
333333

334334
setHeight(panel.scrollHeight);
335335

336-
if (!shouldCancelInitialOpenAnimationRef.current) {
336+
if (!shouldCancelInitialOpenAnimationRef.current && !isBeforeMatchRef.current) {
337337
panel.style.removeProperty('animation-name');
338338
}
339339

@@ -358,12 +358,7 @@ function Collapsible(props: {
358358
useEnhancedEffect(() => {
359359
const panel = panelRef.current;
360360

361-
if (
362-
panel &&
363-
hiddenUntilFoundProp &&
364-
animationTypeRef.current === 'css-transition' &&
365-
isHidden
366-
) {
361+
if (panel && hiddenUntilFoundProp && isHidden) {
367362
/**
368363
* React only supports a boolean for the `hidden` attribute and forces
369364
* legit string values to booleans so we have to force it back in the DOM
@@ -376,7 +371,9 @@ function Collapsible(props: {
376371
* to `'until-found'` as they could have different `display` properties:
377372
* https://github.com/tailwindlabs/tailwindcss/pull/14625
378373
*/
379-
panel.setAttribute('data-starting-style', '');
374+
if (animationTypeRef.current === 'css-transition') {
375+
panel.setAttribute('data-starting-style', '');
376+
}
380377
}
381378
}, [hiddenUntilFoundProp, isHidden]);
382379

@@ -387,10 +384,7 @@ function Collapsible(props: {
387384
return undefined;
388385
}
389386

390-
function handleBeforeMatch(event: Event) {
391-
// TODO: probably remove this because beforematch isn't cancellable anyway
392-
event.preventDefault();
393-
387+
function handleBeforeMatch() {
394388
isBeforeMatchRef.current = true;
395389
setOpen(true);
396390
}

0 commit comments

Comments
 (0)