Skip to Content
BBRICKSv0.7.4
DataHooks

useDrawerResource

One drawer's data, the BUG-2 pattern for every drawer: the drawer opens on the click with the row as seed and stays the same mounted dialog while url loads; only its body swaps skeleton → content. A new url aborts the previous request; a null url closes. fetcher defaults to a GET through the DataProvider's HttpClient (the same client the data hooks use; a same-origin client with no provider), and a failure carries the server's message.

import { useDrawerResource } from '@ergasti/bricks';

Examples

One loading state for a drawer

You open a drawer with the row you already have (the seed), so the title and avatar show at once and only the body loads: loading is true until fetcher answers. With no fetcher the GET goes through the DataProvider's client.

Code
import { useState } from 'react';
import { Avatar, Button, Drawer, KeyValue, TierChip, useDrawerResource } from '@ergasti/bricks';
import { creators, type Creator } from './sample-data';

function Details() {
  const [open, setOpen] = useState<Creator | null>(null);
  const drawer = useDrawerResource<{ orders: number }, Creator>(open ? `creator-${open.id}` : null, open, {
    fetcher: (_url, signal) => new Promise((resolve, reject) => {
      const timer = window.setTimeout(() => resolve({ orders: open?.orders ?? 0 }), 700);
      signal.addEventListener('abort', () => { window.clearTimeout(timer); reject(new DOMException('Aborted', 'AbortError')); });
    }),
  });
  return (
    <>
      <Button variant="ink" onClick={() => setOpen(creators[2])}>Open Salma Hosny</Button>
      <Drawer
        open={drawer.open} loading={drawer.loading} onClose={() => setOpen(null)} title={drawer.seed?.name ?? ''}
        lead={drawer.seed && <Avatar name={drawer.seed.name} seed={drawer.seed.id} size="lg" />}
        hint={drawer.seed && <TierChip tier={drawer.seed.tier} />}
      >
        {drawer.data && <KeyValue items={[{ label: 'Orders in the last 90 days', value: drawer.data.orders }]} />}
      </Drawer>
    </>
  );
}

Signature

function useDrawerResource<T, S>(url: string | null, seed?: S | null, options3?: { fetcher?: (url: string, signal: AbortSignal) => Promise<T>; }): DrawerResource<T, S>

Parameters

options3: { fetcher?: (url: string, signal: AbortSignal) => Promise<T>; }

Prop

Type

Returns

DrawerResource<T, S>

useHttpClient · Button · Drawer · Avatar · TierChip · KeyValue · useAction · useDataLanguage · useDebouncedCallback · useHashState

Source

src/hooks/useDrawerResource.ts (line 37) on GitHub.

On this page