|Kit

Splitter

Resizable panes. Two or more panels share a box, and the user drags the line between them, or uses the keyboard, to give one more room.

Splitter puts panels side by side, or stacked, and a handle between every two of them. Dragging a handle gives one panel more room and its neighbour less. Use it where people need to choose the balance themselves: a project subject next to the code, or a list next to its detail view in a staff tool.

import { Splitter } from '@42-central/ui-react/splitter';

<div className="h-96">
  <Splitter>
    <Splitter.Panel id="subject" defaultSize={40} min={25} max={60}>
      <Subject />
    </Splitter.Panel>
    <Splitter.Panel id="code" min={30}>
      <Code />
    </Splitter.Panel>
  </Splitter>
</div>

get_next_line

Write a function that returns a line read from a file descriptor.

Repeated calls should let you read the text file one line at a time.

Your function should return the line that was read. If there is nothing else to read or an error occurred, it should return NULL.

Make sure it behaves well both when reading a file and when reading from the standard input.

The returned line should include the terminating \n, except at the end of the file if it does not end with one.

char	*get_next_line(int fd)
{
	static char	*stash;
	char			*line;

	if (fd < 0 || BUFFER_SIZE <= 0)
		return (NULL);
	stash = read_until_newline(fd, stash);
	if (!stash)
		return (NULL);
	line = extract_line(stash);
	stash = keep_rest(stash);
	return (line);
}

Code

<Splitter>
  <Splitter.Panel id="subject" defaultSize={40} min={25} max={60}>
    <Subject />
  </Splitter.Panel>
  <Splitter.Panel id="code" min={30}>
    <Code />
  </Splitter.Panel>
</Splitter>

The splitter fills its container (height and width are 100%), so give the container a height, or size the splitter with className.

Playground

Splitter

Loading preview

Props

Code

<Splitter
  orientation="horizontal"
  step={1}
  collapsible={false}
  disabled={false}
/>

Full prop browser

Splitter

Orientation

orientation="vertical" stacks the panels, and the handles turn horizontal. Everything else works the same way: up and down instead of left and right.

char	*get_next_line(int fd)
{
	static char	*stash;
	char			*line;

	if (fd < 0 || BUFFER_SIZE <= 0)
		return (NULL);
	stash = read_until_newline(fd, stash);
	if (!stash)
		return (NULL);
	line = extract_line(stash);
	stash = keep_rest(stash);
	return (line);
}
$ ./gnl tests/42.txt
line 1: Hello, 42!
line 2: (null)

Code

<Splitter orientation="vertical">
  <Splitter.Panel id="code" defaultSize={65} min={30}>
    <Code />
  </Splitter.Panel>
  <Splitter.Panel id="output" min={15}>
    <Output />
  </Splitter.Panel>
</Splitter>

Sizes and limits

Each panel sets its own sizes:

  • defaultSize is where it starts. Panels without one share what's left equally.
  • min and max are as far as a drag or a key press can take it.

A number is a percentage of the splitter. A string can also be a CSS length ("240px", "15rem"), measured against the splitter's own size. Any number of panels works. A handle moves only its two neighbours, and when one of them hits its limit the next panel along gives way.

Files

12rem by default, 8rem to 20rem.

Editor

Takes the rest, never under 30%.

Preview

25% by default, 15% to 40%.

Code

<Splitter>
  <Splitter.Panel id="files" defaultSize="12rem" min="8rem" max="20rem">…</Splitter.Panel>
  <Splitter.Panel id="editor" min={30}>…</Splitter.Panel>
  <Splitter.Panel id="preview" defaultSize={25} min={15} max={40}>…</Splitter.Panel>
</Splitter>

Splitter.Panels must be direct children of Splitter: the root reads their sizes from their props, so a panel wrapped in your own component isn't seen. Give each panel an id when panels come and go, or when something refers to one (Splitter.Toggle, useSplitterContext). Without one, a panel is panel-0, panel-1, and so on.

Collapsible panels

A collapsible panel snaps to its collapsedSize (0 by default) when it's dragged past half of its min, and back out when it's dragged open again. A collapsible panel needs a min: it's the size it reopens at when it has no earlier size to go back to.

There are three ways to collapse and expand it:

  • Drag the handle past half of min.
  • Press Enter on the handle after it. That toggles the panel before the handle.
  • Splitter.Toggle, a button you put anywhere inside the splitter, usually in a panel's header. It's in the tab order, has aria-expanded and aria-controls, and takes its name from translations. Give it children to replace the chevron.

A panel collapsed to nothing is made inert, so its content leaves the tab order and the accessibility tree. With a collapsedSize above 0 (a rail of icons), it stays interactive.

Students

  • norminet

    libft

  • mbeilles

    get_next_line

  • jdoe

    ft_printf

  • asmith

    push_swap

  • kbrown

    minishell

  • lgarcia

    so_long

  • pmartin

    philosophers

  • rlee

    cub3d

  • tnguyen

    webserv

  • zali

    ft_transcendence

mbeilles

Waiting for correction

get_next_line, second attempt. Two evaluations booked for tomorrow.

Code

<Splitter>
  <Splitter.Panel id="list" defaultSize={35} min={25} max={50} collapsible>
    <header className="sticky top-0 …">
      Students
      <Splitter.Toggle panel="list" />
    </header>
    <StudentList />
  </Splitter.Panel>
  <Splitter.Panel id="detail">
    <Detail />
  </Splitter.Panel>
</Splitter>

// Inside the detail: a way back once the list is gone.
const splitter = useSplitterContext();
{splitter.collapsed('list') && <Splitter.Toggle panel="list" />}

onCollapse and onExpand tell you which panel changed, with its id.

Panels scroll their own content

Each panel is its own scroll container (overflow: auto), and it can shrink below its content (min-width: 0, min-height: 0), so a long subject or a wide table scrolls inside its pane instead of pushing the handle away. Put any content in a panel.

Because the panel is the scroller, position: sticky works inside it: the list's header above stays at the top of its panel while the students scroll under it. A sticky element inside a nested scroller of your own sticks to that one instead.

If you lay a panel's content out with grid or flex, the same rule applies one level down: a grid or flex item that should scroll needs min-h-0 (or min-w-0) and its own overflow, or it grows to fit its content and the panel scrolls instead.

Controlled sizes

size and onResize hold the sizes in your state: an array of percentages, in panel order. onResize fires on every change, while dragging too. Without size, the splitter keeps its own, starting at each panel's defaultSize.

onResizeEnd fires once per gesture (the end of a drag, a key press, a toggle or a reset) with the final sizes.

Remembering the sizes

There's no storage prop. Keep the sizes in useLocalStorage instead, and they come back on the next visit, and in every tab:

get_next_line

Write a function that returns a line read from a file descriptor.

Repeated calls should let you read the text file one line at a time.

Your function should return the line that was read. If there is nothing else to read or an error occurred, it should return NULL.

Make sure it behaves well both when reading a file and when reading from the standard input.

The returned line should include the terminating \n, except at the end of the file if it does not end with one.

char	*get_next_line(int fd)
{
	static char	*stash;
	char			*line;

	if (fd < 0 || BUFFER_SIZE <= 0)
		return (NULL);
	stash = read_until_newline(fd, stash);
	if (!stash)
		return (NULL);
	line = extract_line(stash);
	stash = keep_rest(stash);
	return (line);
}

50% / 50%

Code

import { useLocalStorage } from '@42-central/ui-react';

const [size, setSize] = useLocalStorage({ key: 'subject-split', defaultValue: [50, 50] });

<Splitter size={size} onResize={setSize}>
  <Splitter.Panel id="subject" defaultSize={50} min={20}>…</Splitter.Panel>
  <Splitter.Panel id="code" min={20}>…</Splitter.Panel>
</Splitter>

useLocalStorage renders defaultValue on the server and on the first client render, then switches to the stored value, so a visitor with saved sizes sees the handle move once after the page loads. To write to storage less often, keep the live sizes in useState and save them in onResizeEnd.

Reset

A double-click on a handle puts every panel back to its defaultSize, and reports it through onResize and onResizeEnd. useSplitterContext().reset() does the same from your own button. A disabled handle doesn't reset.

Nested splitters

A panel can hold another splitter, in the other orientation. Each splitter has its own handles and its own sizes, and useSplitterContext() reads the nearest one.

get_next_line

Write a function that returns a line read from a file descriptor.

Repeated calls should let you read the text file one line at a time.

Your function should return the line that was read. If there is nothing else to read or an error occurred, it should return NULL.

Make sure it behaves well both when reading a file and when reading from the standard input.

The returned line should include the terminating \n, except at the end of the file if it does not end with one.

char	*get_next_line(int fd)
{
	static char	*stash;
	char			*line;

	if (fd < 0 || BUFFER_SIZE <= 0)
		return (NULL);
	stash = read_until_newline(fd, stash);
	if (!stash)
		return (NULL);
	line = extract_line(stash);
	stash = keep_rest(stash);
	return (line);
}

Terminal

$ make && ./gnl

Code

<Splitter>
  <Splitter.Panel id="subject" defaultSize={35} min={20}>…</Splitter.Panel>
  <Splitter.Panel id="work" min={30}>
    <Splitter orientation="vertical">
      <Splitter.Panel id="code" defaultSize={70} min={30}>…</Splitter.Panel>
      <Splitter.Panel id="terminal" min={15} collapsible>…</Splitter.Panel>
    </Splitter>
  </Splitter.Panel>
</Splitter>

Configuring a handle

The splitter adds a handle between every two panels by itself. Place a Splitter.Handle yourself only to configure one: lock it with disabled, or give it a class. A handle you place replaces the one the splitter would have added there.

<Splitter>
  <Splitter.Panel id="nav" defaultSize="16rem">…</Splitter.Panel>
  <Splitter.Handle disabled />
  <Splitter.Panel id="main">…</Splitter.Panel>
</Splitter>

A disabled handle keeps its line, loses its grip, leaves the tab order, and ignores the pointer, the keyboard and double-clicks.

Right-to-left

The direction comes from the locale, as for the kit's other interactive parts. Under an RTL locale the first panel sits on the right, a drag follows the pointer, and ← moves the handle left, which grows the first panel.

القائمة

اللوحة الأولى، على اليمين.

التفاصيل

اسحب الخط: يتبع المؤشر.

Code

<IntlProvider locale="ar-EG">
  <div dir="rtl" lang="ar">
    <Splitter translations={{ handle: 'تغيير الحجم' }}>
      <Splitter.Panel id="list" defaultSize={35} min={20}>…</Splitter.Panel>
      <Splitter.Panel id="detail" min={30}>…</Splitter.Panel>
    </Splitter>
  </div>
</IntlProvider>

Splitter stamps the locale's dir on its root. <html dir="rtl"> alone leaves it left to right: see Text direction.

Anatomy

<Splitter testId="workspace">
  <Splitter.Panel id="list" testId="workspace-list">
    <Splitter.Toggle panel="list" testId="workspace-list-toggle" />
  </Splitter.Panel>
  <Splitter.Handle testId="workspace-handle" /> {/* optional: one is added for you */}
  <Splitter.Panel id="detail" testId="workspace-detail" />
</Splitter>
  • Splitter (also Splitter.Root): the flex container. It reads its panels, adds the handles and runs the machine. testId lands on it.
  • Splitter.Panel: one pane, a scroll container. testId lands on it. It takes asChild, to render as your own element (a <section>, an <aside>).
  • Splitter.Handle: the separator between two panels, a <button role="separator"> with the grip inside. testId lands on the button. Added automatically; place one only to configure it. No asChild: it renders its own grip.
  • Splitter.Toggle: a collapse button for one panel. testId lands on the button. It takes asChild.

Every part also carries Ark's data-scope="splitter" and data-part. A panel has data-collapsed while it's collapsed, and a handle has data-dragging during a drag and data-focus while focused. See Testing.

API

Splitter

Prop

Type

Every other <div> attribute is passed to the root. No asChild: the root adds handles between its children.

Splitter.Panel

Prop

Type

Splitter.Handle

Prop

Type

Splitter.Toggle

Prop

Type

useSplitterContext

useSplitterContext() returns the nearest splitter's sizes and the calls that change them. Call it inside a panel.

Prop

Type

Accessibility

  • A separator per handle. Each handle is a focusable role="separator" with aria-orientation, aria-valuenow (the size of the panel before it), aria-valuemin, aria-valuemax and aria-controls naming its two panels. It takes its name from translations.handle.

  • Keyboard.

    KeyAction
    TabFocuses the next handle (or toggle).
    ← →Moves a vertical line by step, horizontal splitters. Mirrored under RTL.
    ↑ ↓Moves a horizontal line by step, vertical splitters.
    Shift + arrowMoves by 10.
    Home / EndShrinks / grows the panel before the handle as far as its limits allow.
    EnterCollapses or expands the panel before the handle, when it's collapsible.
    F6 / Shift + F6Moves focus to the next / previous handle.
  • A collapse button. Splitter.Toggle is a real <button> with aria-expanded and aria-controls, for anyone who'd rather not drag to the edge.

  • Pointer. The line is 1px, but its hit area is 13px wide, so it's easy to grab. The grip darkens on hover and turns brand-coloured while dragging. A double-click resets the sizes.

  • Visible focus. The kit's focus ring goes around the whole line when it's focused from the keyboard, not when it's clicked.

  • Collapsed panels leave the tab order. A panel collapsed to 0 is inert.

  • Forced colors. The line and the grip switch to system colors in Windows High Contrast.

  • Right-to-left. The direction comes from the locale. See Right-to-left.

Internationalization

translations holds the three names the splitter announces. All are optional; a partial override keeps the other English defaults.

KeyDefault
handle"Resize"Each handle's accessible name.
collapse"Collapse panel"Splitter.Toggle's name while its panel is expanded.
expand"Expand panel"Splitter.Toggle's name while its panel is collapsed.
<Splitter translations={{ handle: t('split.resize'), collapse: t('split.collapse'), expand: t('split.expand') }}>
  …
</Splitter>

Changelog

v0.30.0
  • New Splitter: resizable, collapsible panes with keyboard support, RTL drag and useSplitterContext

    #486

On this page