# Dynamic UI Hooks and Context

The hooks exported by Dynamic UI, grouped by purpose, and the context provider they depend on. This page does not copy signatures: get the exact parameters and return value with `widgets-get-component-props` and the hook name (`section` is `hooks`). `widgets-list-components` lists them all.

---

## DContextProvider

The root provider of Dynamic UI. In the canonical template it wraps each mounted instance in `src/app/main.tsx`:

```tsx
<DContextProvider availablePortals={{}}>
  <App />
</DContextProvider>
```

| Prop | Use |
|------|-----|
| `availablePortals` | The registry of portals (modals, offcanvas) — see `modyo://docs/widgets/components-overlay` |
| `language`, `currency` | Language and currency format used by the components |
| `icon`, `iconRegistry`, `iconMap` | Icon configuration |
| `portalName` | The id of the DOM node portals render into |

The template sets `language` and `currency` at runtime from the site's Liquid variables: `App` calls `setContext(CONTEXT_CONFIG)` from `useDContext`.

`breakpoints` has no effect: breakpoints are read from the `--bs-breakpoint-*` custom properties of the site's CSS.

---

## Hooks by purpose

| Purpose | Hooks |
|---------|-------|
| Context | `useDContext` — reads the context and exposes `setContext` |
| Portals | `useDPortalContext` — `openPortal(name, payload)`, `closePortal()`, `stack` |
| Confirmations | `useConfirmModal` — a ready-made confirmation dialog (needs a `DConfirmModalContainer`) |
| Toasts | `useDToast` — `toast(data, options)` (needs a `DToastContainer`) |
| Error boundaries | `useErrorBoundary` — `showBoundary(error)` and `resetBoundary()` from inside a `DErrorBoundary` (re-exported from react-error-boundary) |
| Tabs | `useTabContext` — exposes `isSelected`, inside `DTabs` or `DTabs.Provider` |
| Breakpoints | `useMediaBreakpointUpXs`, `useMediaBreakpointUpSm`, `useMediaBreakpointUpMd`, `useMediaBreakpointUpLg`, `useMediaBreakpointUpXl`, `useMediaBreakpointUpXxl`, `useMediaQuery(query)` — return a boolean; each takes an optional `useListener` argument |
| Currency | `useFormatCurrency(...values)` — formats amounts with the context currency; `useInputCurrency` — the state behind `DInputCurrency` |
| Selection | `useItemSelection` — `selectedItems`, `isSelectedItem`, `toggleSelectedItem`, `setSelectedItems`, `resetSelectedItems` |
| OTP | `useOtp` — the logic of `DOtp` (code, validation, submit, resend countdown) without its markup |
| Countdown | `useCountdown(seconds)` — `secondsLeft` and `restartCountdown` |
| Screenshots | `useScreenshot`, `useScreenshotDownload`, `useScreenshotWebShare` — capture an element as an image, download it or share it |
| Utilities | `useStackState`, `useProvidedRefOrCreate`, `useDisableBodyScrollEffect`, `useDisableInputWheel` |

---

## Examples from the canonical template

```tsx
const { setContext } = useDContext();

useEffect(() => {
  setContext(CONTEXT_CONFIG);
}, [setContext]);
```

```tsx
const { openPortal } = useDPortalContext<EntityPortals>();
```

```tsx
const { closePortal, stack } = useDPortalContext<EntityPortals>();
```

---

## Not Dynamic UI hooks

`useTranslation` comes from react-i18next, `useQuery`/`useMutation` from TanStack Query, and the feature hooks (`useEntities`, …) from the widget itself (`modyo://docs/widgets/patterns-hooks`).
