import { type ComponentPropsWithoutRef, isValidElement, type ReactNode, } from "react" import { cn } from "../../libs/utils" export type TwoColsLayoutProps = ComponentPropsWithoutRef<"div"> export const TwoColsLayout = ({ className, children, ...props }: TwoColsLayoutProps) => { const [left, right] = splitChildren(children) return (
{left}
{right}
) } const isIterable = (obj: unknown): obj is Iterable => obj != null && typeof obj === "object" && Symbol.iterator in obj && typeof obj[Symbol.iterator] === "function" const splitChildren = (children: ReactNode): [ReactNode, ReactNode] => { if (!isIterable(children)) { return [children, null] } const childrenArray = Array.from(children) const splitter = childrenArray.findIndex( (child) => isValidElement(child) && child.type === "hr", ) if (splitter === -1) { return [children, null] } const left = childrenArray.slice(0, splitter) const right = childrenArray.slice(splitter + 1) return [left, right.length > 0 ? right : null] }