mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-02 02:42:41 +02:00
fix(v2): Fix Collapsible hydration layout shift (#5159)
This commit is contained in:
parent
fa9b0cd9c1
commit
f33cd079c2
1 changed files with 11 additions and 1 deletions
|
@ -5,6 +5,7 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
||||
import React, {
|
||||
useState,
|
||||
useEffect,
|
||||
|
@ -146,9 +147,17 @@ function useCollapseAnimation({
|
|||
}
|
||||
|
||||
type CollapsibleElementType = React.ElementType<
|
||||
Pick<React.HTMLAttributes<unknown>, 'className' | 'onTransitionEnd'>
|
||||
Pick<React.HTMLAttributes<unknown>, 'className' | 'onTransitionEnd' | 'style'>
|
||||
>;
|
||||
|
||||
// Prevent hydration layout shift before anims are handled imperatively with JS
|
||||
function getSSRStyle(collapsed: boolean) {
|
||||
if (ExecutionEnvironment.canUseDOM) {
|
||||
return undefined;
|
||||
}
|
||||
return collapsed ? CollapsedStyles : ExpandedStyles;
|
||||
}
|
||||
|
||||
export function Collapsible({
|
||||
as: As = 'div',
|
||||
collapsed,
|
||||
|
@ -171,6 +180,7 @@ export function Collapsible({
|
|||
<As
|
||||
// @ts-expect-error: see https://twitter.com/sebastienlorber/status/1412784677795110914
|
||||
ref={collapsibleRef}
|
||||
style={getSSRStyle(collapsed)}
|
||||
onTransitionEnd={(e) => {
|
||||
if (e.propertyName !== 'height') {
|
||||
return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue