Skip to content

Commit

Permalink
Fix regression that broke server rendering (#242)
Browse files Browse the repository at this point in the history
Panels should fall back to `defaultSize` on initial render.

Fixes #240
  • Loading branch information
bvaughn authored Dec 24, 2023
1 parent 2175180 commit f689c59
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/react-resizable-panels/src/Panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export function PanelWithForwardedRef({
]
);

const style = getPanelStyle(panelDataRef.current);
const style = getPanelStyle(panelDataRef.current, defaultSize);

return createElement(Type, {
...rest,
Expand Down
3 changes: 2 additions & 1 deletion packages/react-resizable-panels/src/PanelGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,13 @@ function PanelGroupWithForwardedRef({

// This API should never read from committedValuesRef
const getPanelStyle = useCallback(
(panelData: PanelData) => {
(panelData: PanelData, defaultSize: number | undefined) => {
const { panelDataArray } = eagerValuesRef.current;

const panelIndex = findPanelDataIndex(panelDataArray, panelData);

return computePanelFlexBoxStyle({
defaultSize,
dragState,
layout,
panelData: panelDataArray,
Expand Down
5 changes: 4 additions & 1 deletion packages/react-resizable-panels/src/PanelGroupContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const PanelGroupContext = createContext<{
dragState: DragState | null;
expandPanel: (panelData: PanelData) => void;
getPanelSize: (panelData: PanelData) => number;
getPanelStyle: (panelData: PanelData) => CSSProperties;
getPanelStyle: (
panelData: PanelData,
defaultSize: number | undefined
) => CSSProperties;
groupId: string;
isPanelCollapsed: (panelData: PanelData) => boolean;
isPanelExpanded: (panelData: PanelData) => boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { CSSProperties } from "../vendor/react";

// the % of the group's overall space this panel should occupy.
export function computePanelFlexBoxStyle({
defaultSize,
dragState,
layout,
panelData,
panelIndex,
precision = 3,
}: {
defaultSize: number | undefined;
layout: number[];
dragState: DragState | null;
panelData: PanelData[];
Expand All @@ -21,10 +23,12 @@ export function computePanelFlexBoxStyle({
const size = layout[panelIndex];

let flexGrow;
if (panelData.length === 1) {
flexGrow = "1";
} else if (size == null) {
if (size == null) {
// Initial render (before panels have registered themselves)
// In order to support server rendering, fall back to default size if provided
flexGrow = defaultSize ?? "1";
} else if (panelData.length === 1) {
// Special case: Single panel group should always fill full width/height
flexGrow = "1";
} else {
flexGrow = size.toPrecision(precision);
Expand Down

1 comment on commit f689c59

@vercel
Copy link

@vercel vercel bot commented on f689c59 Dec 24, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.