Skip to content

Commit

Permalink
fix: allow setting id on split-view component (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwalley committed Jan 5, 2024
1 parent e429598 commit d23cf34
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/allotment.tsx
Expand Up @@ -77,13 +77,13 @@ export const Pane = forwardRef<HTMLDivElement, PaneProps>(
className={classNames(
"split-view-view",
styles.splitViewView,
className
className,
)}
>
{children}
</div>
);
}
},
);

Pane.displayName = "Allotment.Pane";
Expand All @@ -97,6 +97,8 @@ export type AllotmentProps = {
children: React.ReactNode;
/** Initial size of each element */
defaultSizes?: number[];
/** The id to set on the SplitView component. */
id?: string;
/** Resize each view proportionally when resizing container */
proportionalLayout?: boolean;
/** Whether to render a separator between panes */
Expand Down Expand Up @@ -128,6 +130,7 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
{
children,
className,
id,
maxSize = Infinity,
minSize = 30,
proportionalLayout = true,
Expand All @@ -142,7 +145,7 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
onDragStart,
onDragEnd,
},
ref
ref,
) => {
const containerRef = useRef<HTMLDivElement>(null!);
const previousKeys = useRef<string[]>([]);
Expand All @@ -156,13 +159,13 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(

if (process.env.NODE_ENV !== "production" && sizes) {
console.warn(
`Prop sizes is deprecated. Please use defaultSizes instead.`
`Prop sizes is deprecated. Please use defaultSizes instead.`,
);
}

const childrenArray = useMemo(
() => React.Children.toArray(children).filter(React.isValidElement),
[children]
[children],
);

const resizeToPreferredSize = useCallback((index: number): boolean => {
Expand Down Expand Up @@ -204,13 +207,13 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
initializeSizes = false;

console.warn(
`Expected ${defaultSizes.length} children based on defaultSizes but found ${splitViewViewRef.current.size}`
`Expected ${defaultSizes.length} children based on defaultSizes but found ${splitViewViewRef.current.size}`,
);
}

if (initializeSizes && defaultSizes) {
previousKeys.current = childrenArray.map(
(child) => child.key as string
(child) => child.key as string,
);
}

Expand All @@ -223,7 +226,7 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
size: defaultSizes.reduce((a, b) => a + b, 0),
views: defaultSizes.map((size, index) => {
const props = splitViewPropsRef.current.get(
previousKeys.current[index]
previousKeys.current[index],
);

const view = new PaneView(layoutService.current, {
Expand Down Expand Up @@ -254,7 +257,7 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
options,
onChange,
onDragStart,
onDragEnd
onDragEnd,
);

splitViewRef.current.on("sashDragStart", () => {
Expand All @@ -275,7 +278,7 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
if (props.visible !== splitViewRef.current.isViewVisible(index)) {
onVisibleChange(
index,
splitViewRef.current.isViewVisible(index)
splitViewRef.current.isViewVisible(index),
);
}
}
Expand Down Expand Up @@ -345,19 +348,19 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
splitViewViewRef.current.get(enterKey)!,
view,
Sizing.Distribute,
keys.findIndex((key) => key === enterKey)
keys.findIndex((key) => key === enterKey),
);

panes.splice(
keys.findIndex((key) => key === enterKey),
0,
enterKey
enterKey,
);

views.current.splice(
keys.findIndex((key) => key === enterKey),
0,
view
view,
);
}

Expand All @@ -370,7 +373,7 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
splitViewRef.current?.moveView(
splitViewViewRef.current.get(key) as HTMLElement,
index,
i
i,
);

const tempKey = panes[index];
Expand Down Expand Up @@ -503,13 +506,14 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
styles.splitView,
vertical ? styles.vertical : styles.horizontal,
{ [styles.separatorBorder]: separator },
className
className,
)}
id={id}
>
<div
className={classNames(
"split-view-container",
styles.splitViewContainer
styles.splitViewContainer,
)}
>
{React.Children.toArray(children).map((child) => {
Expand Down Expand Up @@ -559,7 +563,7 @@ const Allotment = forwardRef<AllotmentHandle, AllotmentProps>(
</div>
</div>
);
}
},
);

Allotment.displayName = "Allotment";
Expand All @@ -576,7 +580,7 @@ export function setSashSize(sashSize: number) {
document.documentElement.style.setProperty("--sash-size", size + "px");
document.documentElement.style.setProperty(
"--sash-hover-size",
hoverSize + "px"
hoverSize + "px",
);

setGlobalSashSize(size);
Expand Down

0 comments on commit d23cf34

Please sign in to comment.