import * as stylex from "@stylexjs/stylex";
import { createContext, useContext } from "react";
import {
Resizable as UiSdkResizable,
ResizableHandle as UiSdkResizableHandle,
ResizablePanel as UiSdkResizablePanel,
} from "@ui-sdk/react/resizable";
import { mergeProps } from "@ui-sdk/react/primitive";
import type { WithStyleOverrides } from "#lib/recipe-props.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticShadows } from '@stylexblocks/ui/semantic-tokens/shadows.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
const splitterResizeTrigger = stylex.create({
base: {
'--splitter-border-color': {
default: semanticColors.border,
':focus': semanticColors.borderEmphasized,
},
'--splitter-thumb-color': {
default: semanticColors.bg,
':focus': colorPalette.subtle,
},
'--splitter-thumb-size': sizes._2,
'--splitter-thumb-inset': 'calc(var(--splitter-thumb-size) * -0.5)',
'--splitter-border-size': '1px',
'--splitter-handle-size': sizes._6,
outline: 'none',
display: 'grid',
placeItems: 'center',
position: 'relative',
},
})
const splitterResizeTriggerSeparator = stylex.create({
base: {
position: 'absolute',
backgroundColor: 'var(--splitter-border-color)',
},
})
const splitterResizeTriggerIndicator = stylex.create({
base: {
position: 'relative',
borderRadius: radii.full,
backgroundColor: 'var(--splitter-thumb-color)',
boxShadow: semanticShadows.xs,
borderWidth: '1px',
zIndex: 1,
},
})
// The generated recipe styles the trigger's colors and vars; the handle's
// orientation-dependent geometry (a horizontal splitter renders a vertical
// divider, and vice versa) lives with the component. UI SDK lays the group
// out as a flex row or column and sizes panels with an inline `flex`.
const splitterStatic = stylex.create({
root: { width: "100%", height: "100%" },
panel: { minWidth: 0, minHeight: 0 },
trigger: { flexShrink: 0, zIndex: 2 },
triggerVertical: { width: "9px", marginBlock: 0, marginInline: "-4px" },
triggerHorizontal: { height: "9px", marginBlock: "-4px", marginInline: 0 },
separatorVertical: {
top: 0,
bottom: 0,
left: "calc(50% - 0.5px)",
width: "var(--splitter-border-size)",
},
separatorHorizontal: {
left: 0,
right: 0,
top: "calc(50% - 0.5px)",
height: "var(--splitter-border-size)",
},
indicator: {
borderStyle: "solid",
borderColor: semanticColors.border,
},
indicatorVertical: {
width: "var(--splitter-thumb-size)",
height: "var(--splitter-handle-size)",
},
indicatorHorizontal: {
height: "var(--splitter-thumb-size)",
width: "var(--splitter-handle-size)",
},
});
interface SplitterState {
orientation: "horizontal" | "vertical";
minSize: number;
}
const SplitterContext = createContext<SplitterState>({
orientation: "horizontal",
minSize: 20,
});
type ResizableProps = React.ComponentProps<typeof UiSdkResizable>;
/**
* Splitter — UI SDK's Resizable wearing the generated recipe.
* `<Splitter.Root>` holds percentage sizes (`size` / `defaultSize` /
* `onSizeChange`) and the arrow-key `step`; `<Splitter.Panel>` takes its
* share (order comes from the DOM, so `index` is optional);
* `<Splitter.ResizeTrigger>` renders the divider. Pointer drag, arrow keys,
* Home/End and double-click collapse are UI SDK's, and the handle carries
* `data-dragging` / `data-hovering`. Composition per part via `render`.
*/
export type SplitterRootProps = WithStyleOverrides<
Omit<ResizableProps, "layout" | "defaultLayout" | "onLayoutChange">
> & {
orientation?: "horizontal" | "vertical";
/** Controlled percentage sizes, one per panel. */
size?: number[];
/** Initial percentage sizes when uncontrolled. */
defaultSize?: number[];
onSizeChange?: (details: { size: number[] }) => void;
/** Smallest percentage a panel may shrink to (default 20). */
minSize?: number;
};
function Root(allProps: SplitterRootProps) {
const {
orientation = "horizontal",
size,
defaultSize,
onSizeChange,
minSize = 20,
palette,
style,
...rest
} = allProps;
return (
<SplitterContext.Provider value={{ orientation, minSize }}>
<UiSdkResizable
{...mergeProps(stylex.props(palette, splitterStatic.root, style), rest)}
orientation={orientation}
layout={size}
defaultLayout={defaultSize ?? (size ? undefined : [50, 50])}
onLayoutChange={(layout) => onSizeChange?.({ size: layout })}
/>
</SplitterContext.Provider>
);
}
Root.displayName = "SplitterRoot";
export type SplitterPanelProps = WithStyleOverrides<
React.ComponentProps<typeof UiSdkResizablePanel>
> & {
/** Zero-based index of this panel. Kept for parity; UI SDK reads the order from the DOM. */
index?: number;
};
function Panel(allProps: SplitterPanelProps) {
const { index: _index, palette, style, ...rest } = allProps;
const splitter = useContext(SplitterContext);
return (
<UiSdkResizablePanel
minSize={splitter.minSize}
{...mergeProps(stylex.props(palette, splitterStatic.panel, style), rest)}
/>
);
}
Panel.displayName = "SplitterPanel";
export type SplitterResizeTriggerProps = WithStyleOverrides<
Omit<React.ComponentProps<typeof UiSdkResizableHandle>, "children">
> & {
/** Extra content inside the handle, after its separator line and grip. */
children?: React.ReactNode;
/** Index of the panel before this trigger. Kept for parity; UI SDK reads the order from the DOM. */
index?: number;
};
function ResizeTrigger(allProps: SplitterResizeTriggerProps) {
const { index: _index, palette, style, children, ...rest } = allProps;
const splitter = useContext(SplitterContext);
// a horizontal splitter lays panels in a row, so its divider is vertical
const vertical = splitter.orientation === "horizontal";
return (
<UiSdkResizableHandle
aria-label="Resize panels"
{...mergeProps(
stylex.props(
palette,
splitterStatic.trigger,
splitterResizeTrigger.base,
vertical ? splitterStatic.triggerVertical : splitterStatic.triggerHorizontal,
style,
),
rest,
)}
>
<div
{...stylex.props(
splitterResizeTriggerSeparator.base,
vertical
? splitterStatic.separatorVertical
: splitterStatic.separatorHorizontal,
)}
/>
<div
{...stylex.props(
splitterResizeTriggerIndicator.base,
splitterStatic.indicator,
vertical
? splitterStatic.indicatorVertical
: splitterStatic.indicatorHorizontal,
)}
/>
{children}
</UiSdkResizableHandle>
);
}
ResizeTrigger.displayName = "SplitterResizeTrigger";
export const Splitter = { Root, Panel, ResizeTrigger };