Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/components/SplitPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useResizer } from '../hooks/useResizer';
import { useKeyboardResize } from '../hooks/useKeyboardResize';
import { convertToPixels, distributeSizes } from '../utils/calculations';
import { cn } from '../utils/classNames';
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect';

const DEFAULT_CLASSNAME = 'split-pane';
const MIN_PANES = 2;
Expand Down Expand Up @@ -158,8 +159,9 @@ export function SplitPane(props: SplitPaneProps) {
);

// Sync paneSizes with controlled size props when they change
// This handles the case where parent state is reset (e.g., clicking a "Reset" button)
useEffect(() => {
// This handles the case where parent state is reset (e.g., clicking a "Reset" button).
// Runs in a layout effect so the new width is committed in the same paint as possible content changes.
useIsomorphicLayoutEffect(() => {
if (containerSize === 0) return;

// Check if any pane has a controlled size prop
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useResizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
snapToPoint,
applyStep,
} from '../utils/calculations';
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect';

/**
* Options for the useResizer hook.
Expand Down Expand Up @@ -85,8 +86,10 @@ export function useResizer(options: UseResizerOptions): UseResizerResult {
onResizeEndRef.current = onResizeEnd;

// Sync sizes from props when not dragging (React 19 compatible)
// Layout effect so an external size change (e.g. a controlled pane collapsing)
// updates the rendered width in lockstep with consumer content.
const sizesRef = useRef(sizes);
useEffect(() => {
useIsomorphicLayoutEffect(() => {
if (
!isDragging &&
JSON.stringify(sizes) !== JSON.stringify(sizesRef.current)
Expand Down
7 changes: 7 additions & 0 deletions src/utils/useIsomorphicLayoutEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useEffect, useLayoutEffect } from 'react';

/**
* `useLayoutEffect` that degrades to `useEffect` during server rendering.
*/
export const useIsomorphicLayoutEffect =
typeof window === 'undefined' ? useEffect : useLayoutEffect;