Skip to content

Commit

Permalink
quick fix for CollapsibleBase hydration error
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Dec 23, 2024
1 parent 984f00e commit f0572c7
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import React, {
type SetStateAction,
type ReactNode,
} from 'react';
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import useIsBrowser from '@docusaurus/useIsBrowser';
import useIsomorphicLayoutEffect from '@docusaurus/useIsomorphicLayoutEffect';
import {prefersReducedMotion} from '../../utils/accessibilityUtils';

Expand Down Expand Up @@ -161,8 +161,15 @@ type CollapsibleElementType = React.ElementType<
* Prevent hydration layout shift before animations are handled imperatively
* with JS
*/
function getSSRStyle(collapsed: boolean) {
if (ExecutionEnvironment.canUseDOM) {
function getSSRStyle({
collapsed,
isBrowser,
}: {
collapsed: boolean;
isBrowser: boolean;
}) {
// After hydration, styles are applied
if (isBrowser) {
return undefined;
}
return collapsed ? CollapsedStyles : ExpandedStyles;
Expand Down Expand Up @@ -202,6 +209,7 @@ function CollapsibleBase({
className,
disableSSRStyle,
}: CollapsibleBaseProps) {
const isBrowser = useIsBrowser();
const collapsibleRef = useRef<HTMLElement>(null);

useCollapseAnimation({collapsibleRef, collapsed, animation});
Expand All @@ -211,7 +219,8 @@ function CollapsibleBase({
// @ts-expect-error: the "too complicated type" is produced from
// "CollapsibleElementType" being a huge union
ref={collapsibleRef as RefObject<never>} // Refs are contravariant, which is not expressible in TS
style={disableSSRStyle ? undefined : getSSRStyle(collapsed)}
// Not even sure we need this SSRStyle anymore, try to remove it?
style={disableSSRStyle ? undefined : getSSRStyle({collapsed, isBrowser})}
onTransitionEnd={(e: React.TransitionEvent) => {
if (e.propertyName !== 'height') {
return;
Expand Down

0 comments on commit f0572c7

Please sign in to comment.