Skip to Content
BBRICKSv0.7.4
Recipes

Responsive layouts

What the components do under 600, 860 and 1024px, the useViewportTier hook, and tables that become cards on a phone.

BRICKS has three tiers, decided by the window width: desktop above 1024px, tablet from 860 to 1024px, and mobile under 860px. A few components also change under 600px (small phones). Most of it is CSS and needs nothing from you; the shell and anything you lay out yourself can read the tier in JavaScript.

Which tier am I in?

useViewportTier() follows the window: mobile under 860px, tablet from 860 to 1024px, desktop above. useIsMobile() is the same test for phones only. Resize the window (or open this page on a phone) and watch both change.

Code
import { useIsMobile, useViewportTier } from '@ergasti/bricks';

function TierNow() {
  const tier = useViewportTier();
  const mobile = useIsMobile();
  return <p>This frame is <b>{tier}</b> · <code>useIsMobile()</code> is <b>{String(mobile)}</b></p>;
}

What changes where

WidthComponentWhat it does
≤ 1024px (touch tiers)Every control: Button, links, chips, Segmented, Switch, Pager, tabs, the "?" notes, nav itemsGrows its hit area to 44px (the visible size stays), and text fields grow to 44px
≤ 1024pxAppShellThe sidebar becomes an icon rail (860 to 1024px)
< 860pxAppShell, BottomNav, MobileNavSheetA bottom tab bar and a "More" sheet replace the sidebar
< 860pxAppHeaderThe tools row scrolls sideways; menus and the bell open full width under the header
< 860pxDataTable, RowListEach row becomes a card: the primary column (or the first) is its head, every other cell shows its column label
< 860pxToastRegionThe toast spans the width at the bottom
< 860pxLoginLayoutOne column: a short ink band, then the form
≤ 1100pxKpiGrid, StatGridFour across become two
≤ 600pxKpiGrid, StatGridOne per row (phoneColumns={2} keeps two from 360px)
< 600pxHeaderSearchThe pill folds into a 44px search button that opens the box over the header (collapsible={false} keeps the pill)
≤ 600pxListToolbarThe search and the selects take the full width; collapseFiltersBelow folds the selects behind one "Filters" button at a width you choose
≤ 600pxHBarsEach row stacks: the label and value above the bar
≤ 600pxChoiceGroup, Stepper, TierLadder, LeaderboardChoices stack in one column; the stepper names only the current step; the ladder stacks; the leaderboard drops its secondary columns

Steps

  1. Let the shell decide. AppShell reads the tier itself. Pass tier only to force one (a preview, a test):

The shell in each tier

AppShell draws the full sidebar on desktop, an icon rail on tablets, and a bottom tab bar with a "More" sheet on phones. It follows the window by itself; its tier prop forces one, as here, for previews and tests.

Code
import { useState } from 'react';
import { AppHeader, AppShell, Card, Segmented, type NavGroup, type ViewportTier } from '@ergasti/bricks';

const nav: NavGroup[] = [{
  id: 'main',
  items: [
    { id: 'home', label: 'Home', icon: 'home' },
    { id: 'creators', label: 'Creators', icon: 'creators', count: 304 },
    { id: 'coupons', label: 'Coupons', icon: 'coupons' },
    { id: 'payments', label: 'Payments', icon: 'payments' },
    { id: 'analytics', label: 'Analytics', icon: 'analytics' },
    { id: 'settings', label: 'Settings', icon: 'settings' },
  ],
}];

function ShellTiers() {
  const [tier, setTier] = useState<ViewportTier>('tablet');
  const [active, setActive] = useState('creators');
  return (
    <AppShell
      tier={tier} brand={{ name: 'Brand', label: 'Brand', tagline: 'Dashboard' }} nav={nav} activeId={active} onNavigate={(i) => setActive(i.id)}
      header={<AppHeader title="Creators" actions={<Segmented label="Preview tier" size="sm" value={tier} onChange={setTier} options={[['desktop', 'Desktop'], ['tablet', 'Tablet'], ['mobile', 'Phone']]} />} />}
    >
      <Card><Card.Header title={`The ${tier} layout`} sub="The same nav tree feeds the sidebar, the rail, the tab bar and the sheet." /></Card>
    </AppShell>
  );
}
  1. Give every table a primary column. Under 860px the DataTable is a list of cards on its own. Mark the column that names the row (the creator, the product) primary; give non-text headers a cardLabel.
  2. Use cards mode for a grid of your own cards. mode="cards" with renderCard draws your card per row in an auto-fill grid. Switch to it on phones with useViewportTier():

Tables on a phone

Under 860px a DataTable turns every row into a card by itself: the primary column is the card's head and every other cell shows its column label. For a grid of your own cards use mode="cards" with renderCard; here the page switches to it on phones with useViewportTier, and the switch lets you see both.

Code
import { useState } from 'react';
import { Avatar, Card, DataTable, Money, Segmented, TierChip, useViewportTier, type DataColumn } from '@ergasti/bricks';

interface Creator { id: number; name: string; tier: string; orders: number; sales: string }

const rows: Creator[] = [
  { id: 1, name: 'Laila Saeed', tier: 'elite', orders: 214, sales: '58200.00' },
  { id: 2, name: 'Karim Hosny', tier: 'established', orders: 102, sales: '31900.00' },
  { id: 3, name: 'Salma Hosny', tier: 'rising', orders: 44, sales: '15200.00' },
];

const columns: DataColumn<Creator>[] = [
  { key: 'creator', label: 'Creator', primary: true, render: (c) => <span style={{ display: 'flex', gap: 10, alignItems: 'center' }}><Avatar name={c.name} seed={c.id} size="sm" /><b>{c.name}</b></span> },
  { key: 'tier', label: 'Tier', render: (c) => <TierChip tier={c.tier} /> },
  { key: 'orders', label: 'Orders', align: 'end', render: (c) => c.orders },
  { key: 'sales', label: 'Sales', align: 'end', figure: 'sales', render: (c) => <Money value={c.sales} compact /> },
];

function PhoneCards() {
  const tier = useViewportTier();
  const [mode, setMode] = useState<'auto' | 'table' | 'cards'>('auto');
  const cards = mode === 'cards' || (mode === 'auto' && tier === 'mobile');
  return (
    <div style={{ display: 'grid', gap: 12 }}>
      <Segmented label="Layout" size="sm" value={mode} onChange={setMode} options={[['auto', 'Auto'], ['table', 'Table'], ['cards', 'Cards']]} />
      <DataTable
        label="Creators" rows={rows} rowKey={(c) => c.id} columns={columns} onRowClick={() => {}}
        mode={cards ? 'cards' : 'table'}
        renderCard={(c) => (
          <Card>
            <Card.Header title={c.name} sub={`${c.orders} orders`} actions={<TierChip tier={c.tier} />} />
            <Money value={c.sales} />
          </Card>
        )}
      />
    </div>
  );
}
  1. Read the tier for your own layout. useViewportTier() returns 'mobile' | 'tablet' | 'desktop' and follows resizes; it reads the width on the first render, so a phone never flashes the desktop layout. useIsMobile() is the phone-only test. Prefer CSS for anything that is only a different arrangement; use the hook when the content itself changes (a different component, fewer columns).
  2. Lay out KPI rows with the grid. KpiGrid does the 4 / 2 / 1 steps; phoneColumns={2} keeps short numbers two across on a phone:

KPI rows

KpiGrid puts four cards across, two under 1100px and one under 600px; phoneColumns={2} keeps two across on phones from 360px.

Code
import { KpiCard, KpiGrid, Money } from '@ergasti/bricks';

function Kpis() {
  return (
    <KpiGrid phoneColumns={2}>
      <KpiCard tone="lime" icon="creators" label="Creators" value="304" sub="4 of 400 seats used" progress={76} />
      <KpiCard tone="sky" icon="coupons" label="Coupons used" value="1,284" sub="Last 30 days" />
      <KpiCard tone="butter" icon="payments" label="Paid" value={<Money value="275000.00" compact />} sub="All time" />
      <KpiCard tone="rasp" icon="analytics" label="Sales" value={<Money value="412300.00" compact />} sub="Last 30 days" />
    </KpiGrid>
  );
}

Related: useViewportTier, useIsMobile, Build an app frame, Accessibility.

On this page