Skip to Content
BBRICKSv0.7.4
Recipes

Details drawer with one loading state

Open a drawer from a row at once, draw the v3 head from the row, and load only the body.

Pressing a row should open the drawer immediately. The row already knows the name, the avatar and the handle, so the drawer draws its head from those straight away, and only the body under the head waits for the request. That is one loading state, in one place.

The drawer is the v3 composition the blocks' own panels use (MyCreatorPanel, PayoutBatchDetail): a Drawer, a DrawerHead tile, KeyValue for the soft tiles, a kit-blk-h3 heading per section and RowItems for lists.

The whole recipe

Pressing a row opens the drawer at once with what the row already knows (the seed: name, avatar, handle), drawn as the v3 head tile, and only the body under it waits for the request. Closing mid-request aborts it. Open one, close it before it loads, open another.

Code
import { useState } from 'react';
import { Avatar, Button, ButtonLink, CreatorCell, DataTable, DialogSkeleton, Drawer, DrawerHead, KeyValue, Money, RowItems, TierChip, count, useDrawerResource, useLanguage, type DataColumn } from '@ergasti/bricks';
import { creators, type Creator } from './sample-data';

interface Detail { orders: number; sales: string; joined: string; products: { id: number; name: string; sales: string }[] }

const columns: DataColumn<Creator>[] = [
  { key: 'creator', label: 'Creator', render: (c) => <CreatorCell id={c.id} name={c.name} identity={c.identity} /> },
  { key: 'tier', label: 'Tier', render: (c) => <TierChip tier={c.tier} /> },
  { key: 'sales', label: 'Sales', align: 'end', render: (c) => <Money value={c.sales.toFixed(2)} compact /> },
];

const fetchDetail = (id: number, signal: AbortSignal) => new Promise<Detail>((resolve, reject) => {
  const row = creators.find((c) => c.id === id);
  const timer = window.setTimeout(() => resolve({
    orders: row?.orders ?? 0,
    sales: (row?.sales ?? 0).toFixed(2),
    joined: '15 Jul 2026',
    products: [
      { id: 1, name: 'Glow serum 30 ml', sales: '4800.00' },
      { id: 2, name: 'Night cream', sales: '2150.50' },
    ],
  }), 700);
  signal.addEventListener('abort', () => { window.clearTimeout(timer); reject(new DOMException('Aborted', 'AbortError')); });
});

function Screen() {
  const language = useLanguage();
  const [open, setOpen] = useState<Creator | null>(null);
  const drawer = useDrawerResource<Detail, Creator>(open ? `creator-${open.id}` : null, open, {
    fetcher: (url, signal) => fetchDetail(Number(url.replace('creator-', '')), signal),
  });
  const seed = drawer.seed;
  return (
    <>
      <DataTable label="Creators" columns={columns} rows={creators.slice(0, 6)} rowKey={(c) => c.id} onRowClick={setOpen} />
      <Drawer
        open={drawer.open}
        onClose={() => setOpen(null)}
        title={seed?.name ?? ''}
        hideTitle
        footer={seed && <ButtonLink href={`/creators/${seed.id}`} size="sm">Open full profile</ButtonLink>}
      >
        {seed && (
          <div className="kit-blk kit-blk-panel">
            <DrawerHead
              tone="lime"
              title={seed.name}
              sub={seed.identity.handle ? `@${seed.identity.handle}` : undefined}
              lead={<Avatar name={seed.name} seed={seed.id} src={seed.identity.avatarUrl} size="lg" />}
            />
            {drawer.loading && <DialogSkeleton />}
            {drawer.error && (
              <>
                <p role="alert">We could not load this creator.</p>
                <Button size="sm" onClick={drawer.retry}>Try again</Button>
              </>
            )}
            {drawer.data && (
              <>
                <KeyValue items={[
                  { label: 'Tier', value: <TierChip tier={seed.tier} /> },
                  { label: 'Orders (90 days)', value: count(drawer.data.orders, language) },
                  { label: 'Sales', figure: 'creator_sales', value: <Money value={drawer.data.sales} /> },
                  { label: 'Joined', value: drawer.data.joined },
                ]} />
                <RowItems
                  heading="Top products"
                  label="Top products"
                  items={drawer.data.products.map((p) => ({ id: p.id, lead: p.name, value: <Money value={p.sales} /> }))}
                />
              </>
            )}
          </div>
        )}
      </Drawer>
    </>
  );
}

How it fits together

  1. Keep the opened row in state (const [open, setOpen] = useState<Creator | null>(null)). onRowClick={setOpen} on the table opens it.
  2. useDrawerResource(key, seed, { fetcher }) takes a key (the request's identity; null while closed), the seed (the row you opened) and an optional fetcher. It returns open, seed, data, loading, error and retry. With no fetcher the GET goes through the DataProvider's client and its error carries the server's message.
  3. Drawer hideTitle keeps title as the dialog's accessible name but draws no heading row: the head tile shows the name instead. Put the footer buttons in footer.
  4. Draw the head from seed: DrawerHead with a tone, the title, a sub line and a lead (an Avatar). It is the first child, so it is there from the click on and never flickers.
  5. Draw the body from data, under the head. While loading, show one DialogSkeleton there. Do not pass loading to the Drawer in this layout: it swaps all the children for the skeleton, the head included. (Drawer loading fits a drawer whose name is in its own title row, with title, lead and hint.)
  6. Wrap the head and body in <div className="kit-blk kit-blk-panel">, as the panels do. It stacks them with the mock's spacing: 18px under the head, and the section headings' own margins instead of the drawer's 18px gap.
  7. Closing aborts. Setting the open row to null aborts the request in flight; a late answer for a closed drawer is ignored. Open another row and only its answer is used.
  8. Errors: drawer.error is set when the request fails; show a message and a retry button (drawer.retry).

The pieces of a v3 drawer body

PieceUse it for
DrawerHeadThe tinted head tile: tone, title, sub, lead.
KeyValueSoft tiles of label and value, 1 to 3 columns. A money row takes figure for its "?"; a row's control holds a Switch named by the row.
<h3 className="kit-blk-h3">A section heading (14px / 700, the display font).
RowItemsA list of people or products with a figure at the end; onSelect makes a row a button, heading draws the section heading for you.
StepTimelineWhere an order or a payout is.
RankListA ranked list: medals for 1 to 3, the value, and with max a bar against the leader.
DetailsBarsScore bars (variant="stacked" for the PR mock's score rows).

Notes

  • Opening a modal from the drawer (a proof, a form) is supported: Escape closes only the top layer.
  • useOpenedRow(rows, idOf) keeps an opened row fresh: it reads the opened row from the current page, so a reload or an action updates it, and the last snapshot stands in while the row is filtered or paged away.
  • Numbers go through count() and money through <Money>, as everywhere else. ButtonLink passes its href through safeHref.

Existing DetailsDrawer code

DetailsDrawer is deprecated since 0.5.0: its bordered tiles and 800 headings are not the v3 look. It keeps working until 1.0. The Migration guide (part 4, "DetailsDrawer → the composed v3 drawer body") maps each of its props to the composed body. DetailsDrawer gets no new props; its score bars are DetailsBars, a component of its own that is not deprecated.

Related: useDrawerResource, Drawer, useOpenedRow.

On this page