Skip to Content
dashfoo

Adaptive layouts

Stack and lock the layout at a breakpoint with the responsive prop or useResponsiveLayout, without remounting or losing state.

A dashfoo layout is already fluid: panels are weight-based percentages, so the whole tree scales with its container at any size. A breakpoint needs a structural change: a three-pane terminal should collapse into a single scrollable column on a phone, where dragging a tab onto a 22%-wide dock band or grabbing a splitter is too cramped.

The responsive prop handles that case. Below a width it renders the layout as one stacked column and locks every structural interaction (tab and tabset drag, split resize, floating, maximize), leaving tap-to-switch. VS Code makes the same call: docking is a desktop interaction, so mobile gets a read-only, navigable view rather than a cramped one.

The responsive prop

import type { Dashfoo, TabNode } from "@dashfoo/core";
import { model, row, tab, tabset } from "@dashfoo/core";
import { DashfooLayout } from "@dashfoo/react";
import type { ReactNode } from "react";
import { useMemo } from "react";

const dashboard = (): Dashfoo =>
  model(
    row([
      tabset([tab("chart", "Chart"), tab("trades", "Trades")], { id: "left", weight: 2 }),
      tabset([tab("book", "Order Book")], { id: "right", weight: 1 }),
    ]),
  );

const renderPanel = (node: TabNode): ReactNode => <div>{node.name}</div>;

const Dashboard = (): ReactNode => {
  const defaultModel = useMemo(() => dashboard(), []);
  return (
    <DashfooLayout
      defaultModel={defaultModel}
      factory={renderPanel}
      responsive={{ maxWidth: 720 }}
    />
  );
};

export { Dashboard };

responsive is { maxWidth: number; orientation?: Orientation }. At or below maxWidth the layout is compact: stacked and locked. orientation controls the stack direction ("column" by default). The width is measured on the layout's own container with a ResizeObserver, not the viewport. A docking layout often lives inside a sidebar or split, so it reacts to its own width.

State is preserved across the breakpoint

The stacked view is a derived projection of your model, not a replacement: the projection does not replace the canonical model. Widening past maxWidth restores its desktop arrangement, and the row hierarchy stays mounted. Tab selection, closing and renaming still update that canonical model. Undo history, persistence, tab selection, and mounted panel content survive the breakpoint cross. Tap-to-select still works because the projection preserves tabset and tab ids, so a tap routes to the same tabset in the canonical model.

The projection keeps nested row identities, so mounted panel state survives an ordinary breakpoint change. Entering compact mode from a maximized view, moving a tab between tabsets, or supplying a different tree can remount content, so keep durable application state outside panel components.

Existing floats stay overlays in compact mode. Their displayed bounds fit the viewport without changing the saved geometry, and the float controls still minimize or dock them back.

On touch devices, the theme grows tab hit targets to the 44px minimum under @media (pointer: coarse), so tap-to-switch stays comfortable once drag is off.

Hand-built layouts

The responsive prop runs on useResponsiveLayout, exported from @dashfoo/react. If you compose the Layout.* primitives yourself instead of using DashfooLayout, call the hook with the store's model and the same options. It measures the container behind rootRef and returns the compact view plus the lock that goes with it, so a hand-built layout compacts exactly like the built-in one.

import type { Dashfoo, TabNode } from "@dashfoo/core";
import { Layout, useDashfooStore, useResponsiveLayout } from "@dashfoo/react";
import type { ReactNode } from "react";

const renderPanel = (node: TabNode): ReactNode => <div>{node.name}</div>;

const HandBuilt = ({ defaultModel }: { defaultModel: Dashfoo }): ReactNode => {
  const store = useDashfooStore({ defaultModel });
  const responsive = useResponsiveLayout(store.model, { maxWidth: 720 });
  const view = responsive.model;

  return (
    <Layout.Root
      {...responsive.locks}
      dispatch={store.dispatch}
      model={view}
      renderers={{ tab: renderPanel }}
      rootRef={responsive.rootRef}
    >
      <Layout.DragLayer>
        <Layout.Rows node={view.layout} />
      </Layout.DragLayer>
    </Layout.Root>
  );
};

export { HandBuilt };

useResponsiveLayout(model, options) takes the same { maxWidth: number; orientation?: Orientation } as the prop, or undefined to never compact. It returns:

FieldTypeDescription
modelDashfooThe compact projection at or below maxWidth, otherwise the model you passed, unchanged.
isCompactbooleanWhether the container is at or below maxWidth.
locksPick<LayoutOptions, "maximizable" | "restructurable">{} when wide, { maximizable: false, restructurable: false } when compact. See below.
rootRef(element: HTMLElement | null) => voidPut it on Layout.Root so the hook measures the layout's own container.

Spread locks onto Layout.Root after any capability props you set, so compact mode overrides them. restructurable: false switches off tab and tabset drag, split resizing and floating. maximizable: false hides maximize: the projection clears the maximized tabset, so maximizing would show nothing. Wide, locks is empty and your own props apply. Layout.Rows handles maximize itself: while a tabset in view.layout is maximized it renders only that tabset, so the example needs no maximize branch.

The width starts at infinity, so the server render and the first client render agree on the wide layout before the container is measured. The projection is view-only: store.dispatch still targets the canonical model, which is why tab selection, closing and renaming keep working in compact mode and widening restores the arrangement. Splitters stay put while the stacked view re-measures: the resize layer only commits a split after a user drag, and it is locked in compact mode anyway.

The same hook works on a controlled layout. Swap the store line in HandBuilt for a model you own, and update it from onModelChange; without that every edit, tab selection included, is dropped:

const [model, setModel] = useState(defaultModel);
const store = useDashfooStore({ model, onModelChange: setModel });

See also: The layout model · Persisting layouts.