{"slug":"splitter","title":"Splitter","description":"Using the splitter machine in your project.","contentType":"component","framework":"react","content":"A splitter allow create dynamic layouts split into vertically or horizontally\narranged panes. Panes are separated by the splitter bars that allow dragging to\nresize or expand/collapse them.\n\n## Resources\n\n\n[Latest version: v1.31.0](https://www.npmjs.com/package/@zag-js/splitter)\n[Logic Visualizer](https://zag-visualizer.vercel.app/splitter)\n[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/splitter)\n\n\n\n**Features**\n\n- Built with flexbox for flexible layout and SSR\n- Support both dynamic horizontal and vertical panels\n- Support multiple panels and splitters\n- Support for collapsible panels\n- Support for panel constraints like min and max sizes\n- Programmatic control of panel sizes\n- Implements the\n  [Window Splitter pattern](https://www.w3.org/WAI/ARIA/apg/patterns/windowsplitter/)\n  for accessibility and keyboard controls\n\n## Installation\n\nTo use the splitter machine in your project, run the following command in your\ncommand line:\n\n```bash\nnpm install @zag-js/splitter @zag-js/react\n# or\nyarn add @zag-js/splitter @zag-js/react\n```\n\n## Anatomy\n\nTo set up the slider correctly, you'll need to understand its anatomy and how we\nname its parts.\n\n> Each part includes a `data-part` attribute to help identify them in the DOM.\n\n\n\n## Usage\n\nFirst, import the splitter package into your project\n\n```jsx\nimport * as splitter from \"@zag-js/splitter\"\n```\n\nThe splitter package exports two key functions:\n\n- `machine` — The state machine logic for the splitter widget.\n- `connect` — The function that translates the machine's state to JSX attributes\n  and event handlers.\n\n> You'll also need to provide a unique `id` to the `useMachine` hook. This is\n> used to ensure that every part has a unique identifier.\n\nNext, import the required hooks and functions for your framework and use the\nsplitter machine in your project 🔥\n\n```jsx\nimport * as splitter from \"@zag-js/splitter\"\nimport { useMachine, normalizeProps } from \"@zag-js/react\"\nimport { useId } from \"react\"\n\nexport function Splitter() {\n  const service = useMachine(splitter.machine, {\n    id: useId(),\n    defaultSize: [80, 20],\n    panels: [\n      { id: \"a\", minSize: 10 },\n      { id: \"b\", minSize: 10 },\n    ],\n  })\n\n  const api = slider.connect(service, normalizeProps)\n\n  return (\n    <div {...api.getRootProps()}>\n      <div {...api.getPanelProps({ id: \"a\" })}>\n        <p>A</p>\n      </div>\n      <div {...api.getResizeTriggerProps({ id: \"a:b\" })} />\n      <div {...api.getPanelProps({ id: \"b\" })}>\n        <p>B</p>\n      </div>\n    </div>\n  )\n}\n```\n\n### Setting the initial size\n\nTo set the initial size of the splitter panels, use the `defaultSize` property.\nEnsure the `defaultSize` totals to `100`.\n\n> Note: The splitter only supports setting percentage values.\n\n```jsx {3}\nconst service = useMachine(splitter.machine, {\n  // ...\n  defaultSize: [40, 60],\n})\n```\n\n### Listening for resize events\n\nWhen the resize trigger is dragged, the `onResize`, `onResizeStart` and\n`onResizeEnd` callback is invoked.\n\n```jsx {3-10}\nconst service = useMachine(splitter.machine, {\n  // ...\n  onResize(detail) {\n    console.log(\"resize\", detail)\n  },\n  onResizeStart(detail) {\n    console.log(\"change start\", detail)\n  },\n  onResizeEnd(detail) {\n    console.log(\"change end\", detail)\n  },\n})\n```\n\n### Changing the orientation\n\nBy default, the splitter is assumed to be horizontal. To change the orientation\nto vertical, set the `orientation` property in the machine's context to\n`vertical`.\n\n```jsx {3}\nconst service = useMachine(splitter.machine, {\n  // ...\n  orientation: \"vertical\",\n})\n```\n\n## Specifying constraints\n\nUse the `panels` property to specify constraints like `minSize` and `maxSize`\nfor the splitter panels.\n\n```jsx {3-6}\nconst service = useMachine(splitter.machine, {\n  // ...\n  panels: [\n    { id: \"a\", minSize: 100, maxSize: 300 },\n    { id: \"b\", minSize: 100, maxSize: 300 },\n  ],\n})\n```\n\n### Setting the collapsed size\n\nSet the `collapsedSize` and `collapsible` of a panel to specify the collapsed\nsize of the panel.\n\n> For best results, ensure you also set the `minSize` of the panel\n\n```jsx {4}\nconst service = useMachine(splitter.machine, {\n  // ...\n  panels: [\n    { id: \"a\", collapsible: true, collapsedSize: 5, minSize: 10, maxSize: 20 },\n    { id: \"b\", minSize: 50 },\n  ],\n})\n```\n\nThis allows the user to drag the splitter to collapse the panel to the\n`collapsedSize`.\n\n### Listening for collapse events\n\nWhen the splitter panel is collapsed, the `onCollapse` callback is invoked.\nAlternatively, the `onExpand` callback is invoked when the panel is expanded.\n\n```jsx {3-8}\nconst service = useMachine(splitter.machine, {\n  // ...\n  onCollapse(detail) {\n    console.log(\"collapse\", detail)\n  },\n  onExpand(detail) {\n    console.log(\"expand\", detail)\n  },\n})\n```\n\n## Styling guide\n\nEarlier, we mentioned that each accordion part has a `data-part` attribute added\nto them to select and style them in the DOM.\n\n### Resize trigger\n\nWhen an splitter item is horizontal or vertical, a `data-state` attribute is set\non the item and content elements.\n\n```css\n[data-scope=\"splitter\"][data-part=\"resize-trigger\"] {\n  /* styles for the item */\n}\n\n[data-scope=\"splitter\"][data-part=\"resize-trigger\"][data-orientation=\"horizontal\"] {\n  /* styles for the item is horizontal state */\n}\n\n[data-scope=\"splitter\"][data-part=\"resize-trigger\"][data-orientation=\"vertical\"] {\n  /* styles for the item is horizontal state */\n}\n\n[data-scope=\"splitter\"][data-part=\"resize-trigger\"][data-focus] {\n  /* styles for the item is focus state */\n}\n\n[data-scope=\"splitter\"][data-part=\"resize-trigger\"]:active {\n  /* styles for the item is active state */\n}\n\n[data-scope=\"splitter\"][data-part=\"resize-trigger\"][data-disabled] {\n  /* styles for the item is disabled state */\n}\n```\n\n## Methods and Properties\n\nThe splitter's `api` exposes the following methods and properties:\n\n### Machine Context\n\nThe splitter machine exposes the following context properties:\n\n**`orientation`**\nType: `\"horizontal\" | \"vertical\"`\nDescription: The orientation of the splitter. Can be `horizontal` or `vertical`\n\n**`size`**\nType: `number[]`\nDescription: The controlled size data of the panels\n\n**`defaultSize`**\nType: `number[]`\nDescription: The initial size of the panels when rendered.\nUse when you don't need to control the size of the panels.\n\n**`panels`**\nType: `PanelData[]`\nDescription: The size constraints of the panels.\n\n**`onResize`**\nType: `(details: ResizeDetails) => void`\nDescription: Function called when the splitter is resized.\n\n**`onResizeStart`**\nType: `() => void`\nDescription: Function called when the splitter resize starts.\n\n**`onResizeEnd`**\nType: `(details: ResizeEndDetails) => void`\nDescription: Function called when the splitter resize ends.\n\n**`ids`**\nType: `Partial<{ root: string; resizeTrigger: (id: string) => string; label: (id: string) => string; panel: (id: string | number) => string; }>`\nDescription: The ids of the elements in the splitter. Useful for composition.\n\n**`keyboardResizeBy`**\nType: `number`\nDescription: The number of pixels to resize the panel by when the keyboard is used.\n\n**`nonce`**\nType: `string`\nDescription: The nonce for the injected splitter cursor stylesheet.\n\n**`onCollapse`**\nType: `(details: ExpandCollapseDetails) => void`\nDescription: Function called when a panel is collapsed.\n\n**`onExpand`**\nType: `(details: ExpandCollapseDetails) => void`\nDescription: Function called when a panel is expanded.\n\n**`dir`**\nType: `\"ltr\" | \"rtl\"`\nDescription: The document's text/writing direction.\n\n**`id`**\nType: `string`\nDescription: The unique identifier of the machine.\n\n**`getRootNode`**\nType: `() => ShadowRoot | Node | Document`\nDescription: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.\n\n### Machine API\n\nThe splitter `api` exposes the following methods:\n\n**`dragging`**\nType: `boolean`\nDescription: Whether the splitter is currently being resized.\n\n**`orientation`**\nType: `\"horizontal\" | \"vertical\"`\nDescription: The orientation of the splitter.\n\n**`getSizes`**\nType: `() => number[]`\nDescription: Returns the current sizes of the panels.\n\n**`setSizes`**\nType: `(size: number[]) => void`\nDescription: Sets the sizes of the panels.\n\n**`getItems`**\nType: `() => SplitterItem[]`\nDescription: Returns the items of the splitter.\n\n**`getPanels`**\nType: `() => PanelData[]`\nDescription: Returns the panels of the splitter.\n\n**`getPanelById`**\nType: `(id: string) => PanelData`\nDescription: Returns the panel with the specified id.\n\n**`getPanelSize`**\nType: `(id: string) => number`\nDescription: Returns the size of the specified panel.\n\n**`isPanelCollapsed`**\nType: `(id: string) => boolean`\nDescription: Returns whether the specified panel is collapsed.\n\n**`isPanelExpanded`**\nType: `(id: string) => boolean`\nDescription: Returns whether the specified panel is expanded.\n\n**`collapsePanel`**\nType: `(id: string) => void`\nDescription: Collapses the specified panel.\n\n**`expandPanel`**\nType: `(id: string, minSize?: number) => void`\nDescription: Expands the specified panel.\n\n**`resizePanel`**\nType: `(id: string, unsafePanelSize: number) => void`\nDescription: Resizes the specified panel.\n\n**`getLayout`**\nType: `() => string`\nDescription: Returns the layout of the splitter.\n\n**`resetSizes`**\nType: `VoidFunction`\nDescription: Resets the splitter to its initial state.\n\n**`getResizeTriggerState`**\nType: `(props: ResizeTriggerProps) => ResizeTriggerState`\nDescription: Returns the state of the resize trigger.\n\n### Data Attributes\n\n**`Root`**\n\n**`data-scope`**: splitter\n**`data-part`**: root\n**`data-orientation`**: The orientation of the splitter\n**`data-dragging`**: Present when in the dragging state\n\n**`Panel`**\n\n**`data-scope`**: splitter\n**`data-part`**: panel\n**`data-orientation`**: The orientation of the panel\n**`data-dragging`**: Present when in the dragging state\n**`data-id`**: \n**`data-index`**: The index of the item\n\n**`ResizeTrigger`**\n\n**`data-scope`**: splitter\n**`data-part`**: resize-trigger\n**`data-id`**: \n**`data-orientation`**: The orientation of the resizetrigger\n**`data-focus`**: Present when focused\n**`data-dragging`**: Present when in the dragging state\n**`data-disabled`**: Present when disabled","package":"@zag-js/splitter","editUrl":"https://github.com/chakra-ui/zag/edit/main/website/data/components/splitter.mdx"}