Docking Layout Manager for React
FlexLayout is a layout manager for React that arranges components in multiple tabsets. Tabs can be resized, moved, and organized into complex layouts.
FlexLayout's only dependency is React.
Features:
enableTabStrip, enableDock, enableDrop, etc.) — see Min SizesFlexLayout is available on npm. Install it using:
npm install flexlayout-reactThe package is ESM-only and must be consumed via import (it cannot be loaded with require()). Import FlexLayout and its model in your modules:
import { Layout, Model, Actions, DockLocation } from 'flexlayout-react';Include a theme. Choose from alpha_light, alpha_dark, alpha_rounded, light, dark, underline, gray, rounded, or combined (see the demo for examples):
import 'flexlayout-react/style/alpha_light.css';Learn how to change the theme dynamically in code
The `` component renders the tabsets and splitters. It takes the following props:
| Prop | Description |
|---|---|
model |
The layout model |
factory |
A factory function for creating React components |
Additional optional props
The model is a tree of Node objects that define the structure of the layout.
The factory is a function that takes a Node object and returns a React component to be hosted within a tab.
Models can be created using the Model.fromJson(jsonObject) static method and saved using the model.toJson() method.
…const model = Model.fromJson(json);
function App() {
const factory = (node) => {
const component = node.getComponent();
if (component === "placeholder") {
return
;
}
}
return (
);
}The above code renders two tabsets horizontally, each containing a single tab that hosts a div component (returned from the factory). Tabs can be moved and resized by dragging and dropping. Additional tabs can be added to the layout by sending actions to the model.
Try it now using CodeSandbox — see Basic — examples/basic/Basic.tsx.
Note: The `` component must be hosted in a container element (with CSS position: absolute or relative). The layout will fill the containing element.
A simple TypeScript starter project can be found here:
https://github.com/nealus/flexlayout-vite-example
The model JSON contains four top-level elements:
global - (optional) Global options.layout - The main row/tabset/tabs layout hierarchy.borders - (optional) Up to four borders ("top", "bottom", "left", "right").subLayouts - (optional) Where sub layouts for popout windows, floating panels and tabs are defined.The layout element is built using three types of nodes:
row - Rows contain a list of tabsets and child rows. The top-level row renders horizontally by default (unless the global attribute rootOrientationVertical is set). Child rows render in the opposite orientation to their parent row.tabset - Tabsets contain a list of tabs and the index of the selected tab.tab - Tabs specify the component to host (loaded via the factory) and the tab's display text.The layout structure is defined with rows within rows that contain tabsets that themselves contain tabs.
Within the demo app, you can view the layout structure by checking the 'Structure' box. Rows are shown in blue, and tabsets in orange.
The optional borders element is made up of border nodes:
border - Borders contain a list of tabs and the index of the selected tab. They can only be used within the borders top-level element.The JSON model tree structure is defined as TypeScript interfaces; see JSON Model.
Each node type has a defined set of required and optional attributes.
Weights on rows and tabsets specify their relative size within the parent row. The absolute values do not matter, only their proportions (e.g., two tabsets with weights 30 and 70 would render the same as if they had weights 3 and 7).
NOTE: The easiest way to create your initial layout JSON is to use the demo app. Modify an existing layout by dragging, dropping, and adding nodes, then press the 'print' button to print the JSON to the browser's developer console. Use the Model Explorer panel to view and modify the attributes of the layout and its nodes, and use the render dropdown in the demo to blank out the panels (other than the Model Explorer) so you can focus on the layout.
By changing global or node attributes, you can modify the layout's appearance and functionality. For example, setting tabSetEnableTabStrip: false in the global options would change the layout into a multi-splitter (without tabs or drag-and-drop):
global: {tabSetEnableTabStrip:false},Node attributes inherit their default value from the corresponding global attribute, so you can set a value for all nodes of a type in one place and override it per-node. The global attribute is the node attribute prefixed with the node type: tab for tab attributes (e.g. tabEnableClose), tabSet for tabset attributes (e.g. tabSetEnableDrag), and border for border attributes (e.g. borderSize).
For example, to make all tabs non-closeable but allow the tab with id "persistent" to be closed anyway:
{
"global": { "tabEnableClose": false },
"layout": {
"type": "row",
"children": [
{
"type": "tabset",
"children": [
{ "type": "tab", "name": "One", "component": "placeholder" },
{ "type": "tab", "name": "Two", "component": "placeholder" },
{ "type": "tab", "id": "persistent", "name": "Persistent", "component": "placeholder", "enableClose": true }
]
}
]
}
}The "Persistent" tab's enableClose: true overrides the inherited global default of false; the other tabs keep the global value.
The combined.css theme includes all other themes and supports dynamic theme switching.
When using combined.css, add a className (in the form flexlayout__theme_[theme-name]) to the div containing the `` to select the desired theme.
For example:
Change the theme in code by changing the className on the containing div.
For example:
containerRef.current!.className = "flexlayout__theme_alpha_dark"See Theme — examples/theme/Theme.tsx.
All themeable values (--fl-color-*, --fl-font-*, --fl-splitter-size, --fl-tab-button-radius, etc.) are CSS custom properties defined in style/_themes.scss. Each one is defined as var(--flexlayout-, ) — so the theme default applies unless the matching global --flexlayout- variable is defined, in which case that value is used everywhere (float windows, sublayouts, and popout windows included).
To restyle the whole layout without rebuilding the scss, set the global variable on a common ancestor (e.g. :root or the div wrapping the ``):
:root {
--flexlayout-color-1: #90a4ae; /* base color, feeds derived colors */
--flexlayout-color-tabset-background: #ffffff; /* or override a specific value */
--flexlayout-splitter-size: 10px;
}Override a --flexlayout- variable and every theme uses it; leave it unset and each theme keeps its own default (so theme switching is unaffected). The single-theme stylesheets (light.css, dark.css, etc.) work the same way — a global override reaches every layout on the page, including float windows.
You can use the `` prop onRenderTab to customize tab rendering:
Update the renderValues parameter as needed:
renderValues.leading: The area shown in red.
renderValues.content: The area shown in green.
renderValues.buttons: The area shown in yellow.
For example:
onRenderTab = (node: TabNode, renderValues: ITabRenderValues) => {
// renderValues.leading = ;
// renderValues.content += " *";
renderValues.buttons.push();
}See Tab Rendering — examples/tab-rendering/TabRendering.tsx.
You can use the `` prop onRenderTabSet to customize tabset rendering:
Update the renderValues parameter as needed:
renderValues.leading: The area shown in blue.
renderValues.stickyButtons: The area shown in red.
renderValues.buttons: The area shown in green.
For example:
onRenderTabSet = (node: (TabSetNode | BorderNode), renderValues: ITabSetRenderValues) => {
renderValues.stickyButtons.push(
{No open issues yet, or sync has not completed.