Skip to content
Closed
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: 5 additions & 1 deletion examples/StyledExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export function StyledExample() {
<p>Use a custom divider component for different styles.</p>
</div>
<div className="example-content">
<SplitPane direction="horizontal" divider={CustomDivider}>
<SplitPane
direction="horizontal"
divider={CustomDivider}
dividerSize="8px"
>
<Pane defaultSize="33%">
<div className="pane styled-pane purple">
<h3>Panel 1</h3>
Expand Down
1 change: 1 addition & 0 deletions examples/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ body {
/* Styled example */
.styled-pane {
color: #fff;
border-radius: 40px;
}

.styled-pane h3 {
Expand Down
22 changes: 20 additions & 2 deletions src/components/SplitPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function SplitPane(props: SplitPaneProps) {
className,
style,
divider: CustomDivider,
dividerSize,
dividerStyle,
dividerClassName,
children,
Expand Down Expand Up @@ -117,13 +118,26 @@ export function SplitPane(props: SplitPaneProps) {
return { minSizes: mins, maxSizes: maxs };
}, [containerSize, paneCount, paneConfigs]);

// Caluclate space for dividers
const calculateDividersTotalSize = useCallback(
(containerSz: number) => {
return (
(paneCount - 1) * convertToPixels(dividerSize ?? '0px', containerSz)
);
},
[paneCount, dividerSize]
);

// Calculate initial sizes from pane configs
const calculateInitialSizes = useCallback(
(containerSz: number): number[] => {
if (containerSz === 0) {
return new Array(paneCount).fill(0);
}

// Subtract space for dividers
containerSz -= calculateDividersTotalSize(containerSz);

// First pass: calculate sizes for panes with explicit sizes
const sizes: (number | null)[] = paneConfigs.map((config) => {
const paneSize = config.size ?? config.defaultSize;
Expand All @@ -145,7 +159,7 @@ export function SplitPane(props: SplitPaneProps) {
// Second pass: fill in auto-sized panes
return sizes.map((size) => (size === null ? autoSize : size));
},
[paneCount, paneConfigs]
[paneCount, paneConfigs, dividerSize]
);

const [paneSizes, setPaneSizes] = useState<number[]>(() =>
Expand Down Expand Up @@ -207,7 +221,11 @@ export function SplitPane(props: SplitPaneProps) {
return calculateInitialSizes(newContainerSize);
}
// For uncontrolled panes, distribute proportionally
return distributeSizes(currentSizes, newContainerSize);
return distributeSizes(
currentSizes,
newContainerSize,
calculateDividersTotalSize(newContainerSize)
);
}

// First measurement - use initial sizes
Expand Down
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export interface SplitPaneProps {
/** Custom divider component */
divider?: React.ComponentType<DividerProps>;

/** Custom divider size */
dividerSize?: Size;

/** Custom divider styles */
dividerStyle?: CSSProperties;

Expand Down
6 changes: 5 additions & 1 deletion src/utils/calculations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ export function snapToPoint(
*/
export function distributeSizes(
currentSizes: number[],
newContainerSize: number
newContainerSize: number,
dividersTotalSize: number = 0
): number[] {
// Subtract space for dividers
newContainerSize -= dividersTotalSize;

const totalCurrentSize = currentSizes.reduce((sum, size) => sum + size, 0);

if (totalCurrentSize === 0) {
Expand Down