@djangocfg/ui-core 2.1.380 → 2.1.382

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,249 @@
1
+ # Boundary
2
+
3
+ React error boundary primitive with four visual variants, a `useBoundary()` hook for async errors, and safe-by-default behaviour (no infinite loops, normalized errors, accessible fallbacks).
4
+
5
+ Designed as a drop-in replacement for `react-error-boundary` with batteries included: built-in fallback variants, dev logging, optional context-based escape hatch.
6
+
7
+ ---
8
+
9
+ ## When to use
10
+
11
+ | Situation | Component |
12
+ |---|---|
13
+ | Wrap a non-critical widget (chat launcher, embed, ad) | `<Boundary variant="silent">` |
14
+ | Wrap a row / inline block (table cell, list item) | `<Boundary variant="inline">` |
15
+ | Wrap a feature panel (analytics card, settings group) | `<Boundary>` (card, default) |
16
+ | Wrap an entire screen / root layout | `<Boundary variant="fullscreen">` |
17
+ | Page/layout level with backend reporting | `<MonitorBoundary>` from `@djangocfg/layouts` |
18
+
19
+ **Granularity matters.** One global boundary at the app root hides bugs and ruins UX (whole page = error screen). Prefer many small per-feature boundaries.
20
+
21
+ ---
22
+
23
+ ## Basic usage
24
+
25
+ ```tsx
26
+ import { Boundary } from '@djangocfg/ui-core';
27
+
28
+ <Boundary variant="card" name="dashboard-stats">
29
+ <StatsPanel />
30
+ </Boundary>
31
+ ```
32
+
33
+ The `name` prop is used in dev console logs — set it to something findable.
34
+
35
+ ---
36
+
37
+ ## Variants
38
+
39
+ ```tsx
40
+ <Boundary variant="silent">…</Boundary> // renders null on error
41
+ <Boundary variant="inline">…</Boundary> // compact one-line alert + Retry
42
+ <Boundary variant="card">…</Boundary> // bordered card with Retry (default)
43
+ <Boundary variant="fullscreen">…</Boundary> // centered fullscreen + Refresh page
44
+ ```
45
+
46
+ All visible variants get `role="alert"` and `aria-live="assertive"` so screen readers announce them.
47
+
48
+ ---
49
+
50
+ ## Custom fallback
51
+
52
+ Two forms — pick by use case:
53
+
54
+ ```tsx
55
+ // Render-prop: simple inline cases
56
+ <Boundary fallback={({ error, reset }) => <MyError onRetry={reset} />}>…</Boundary>
57
+
58
+ // Component: better for memoization / testing
59
+ function MyFallback({ error, errorInfo, reset }: BoundaryRenderProps) {
60
+ return <ErrorCard message={error.message} onRetry={reset} />;
61
+ }
62
+ <Boundary FallbackComponent={MyFallback}>…</Boundary>
63
+ ```
64
+
65
+ `fallback` (function form) takes precedence over `FallbackComponent`. Both receive `{ error, errorInfo, reset }`.
66
+
67
+ ---
68
+
69
+ ## Reset patterns
70
+
71
+ ### 1. Auto-reset on route change
72
+
73
+ ```tsx
74
+ const pathname = usePathname();
75
+ <Boundary resetKeys={[pathname]}>{children}</Boundary>
76
+ ```
77
+
78
+ When any value in `resetKeys` changes (shallow `Object.is` per index), the boundary clears its error state.
79
+
80
+ **⚠ Anti-pattern:** `resetKeys={[Math.random()]}` or `resetKeys={[{}, []]}` causes an infinite reset loop because the array contents change on every render.
81
+
82
+ ### 2. React Query / refetch on recovery
83
+
84
+ ```tsx
85
+ <Boundary
86
+ onReset={({ reason }) => {
87
+ // reason === 'imperative' (Retry button) or 'keys' (resetKeys changed)
88
+ queryClient.invalidateQueries({ queryKey: ['dashboard'] });
89
+ }}
90
+ >
91
+ <Dashboard />
92
+ </Boundary>
93
+ ```
94
+
95
+ `onReset` is called in a microtask **after** state is cleared — your refetch sees a fresh component.
96
+
97
+ ### 3. Full remount (rare)
98
+
99
+ If you need to fully discard component state (not just the error), use React's `key` prop instead:
100
+
101
+ ```tsx
102
+ <Boundary key={feature.id} resetKeys={[feature.id]}>…</Boundary>
103
+ ```
104
+
105
+ ---
106
+
107
+ ## Async / event-handler errors
108
+
109
+ **React error boundaries do not catch async errors.** That's a React limitation, not a bug.
110
+
111
+ Use `useBoundary()` to push them into the nearest boundary:
112
+
113
+ ```tsx
114
+ import { useBoundary } from '@djangocfg/ui-core';
115
+
116
+ function LoadButton() {
117
+ const { showBoundary, resetBoundary } = useBoundary();
118
+
119
+ return (
120
+ <Button onClick={async () => {
121
+ try {
122
+ await api.loadData();
123
+ } catch (err) {
124
+ showBoundary(err); // bubbles up to the closest <Boundary>
125
+ }
126
+ }}>
127
+ Load
128
+ </Button>
129
+ );
130
+ }
131
+ ```
132
+
133
+ `useBoundary()` works inside any component rendered under a `<Boundary>`. Throws if used outside one.
134
+
135
+ ---
136
+
137
+ ## Logging
138
+
139
+ By default, caught errors are logged with `console.error` in development and silenced in production. Wire to your telemetry by passing `logger` or `onError`:
140
+
141
+ ```tsx
142
+ <Boundary
143
+ onError={(error, info) => {
144
+ Sentry.captureException(error, { extra: { componentStack: info.componentStack } });
145
+ }}
146
+ >
147
+
148
+ </Boundary>
149
+ ```
150
+
151
+ Or use `MonitorBoundary` from `@djangocfg/layouts` for automatic reporting to `@djangocfg/monitor`.
152
+
153
+ ---
154
+
155
+ ## Safety guarantees
156
+
157
+ - **Fallback can throw safely.** If the user's fallback render itself errors, the boundary degrades to a minimal static alert instead of looping forever.
158
+ - **`onError` / `onReset` can throw safely.** Caught and logged via `logger`. Won't crash the boundary.
159
+ - **Non-`Error` throws are normalized.** `throw 'oops'`, `throw { code: 500 }` and bare objects become real `Error` instances with stack traces (where possible).
160
+ - **No duplicate `onError` calls.** Internal tracking prevents firing twice for the same error instance.
161
+ - **Reset clears state before `onReset`.** Errors thrown inside `onReset` don't re-trigger the just-cleared boundary.
162
+
163
+ ---
164
+
165
+ ## Pairing with `<Suspense>`
166
+
167
+ Put `<Boundary>` **outside** `<Suspense>` — otherwise errors thrown by a suspended component bubble past it.
168
+
169
+ ```tsx
170
+ <Boundary variant="card">
171
+ <Suspense fallback={<Spinner />}>
172
+ <DataPanel />
173
+ </Suspense>
174
+ </Boundary>
175
+ ```
176
+
177
+ ---
178
+
179
+ ## Edge cases & gotchas
180
+
181
+ | Issue | Cause | Solution |
182
+ |---|---|---|
183
+ | Boundary doesn't catch error | Error thrown in async code / event handler | Use `useBoundary().showBoundary(err)` |
184
+ | Boundary doesn't catch error (SSR) | `componentDidCatch` doesn't run on server | Errors are caught on client hydration. For SSR-only errors, use Next.js `error.tsx` |
185
+ | Infinite reset loop | `resetKeys` contains unstable values | Don't put new objects/arrays/random in `resetKeys` |
186
+ | Fallback renders briefly with stale data | React 18 concurrent rendering / `useDeferredValue` | Expected — the boundary unmounts children on error |
187
+ | `error.stack` is missing | Code threw a non-`Error` value | Boundary normalizes, but stack is reconstructed from current line. Prefer `throw new Error(...)` |
188
+ | Component stack only in dev | React strips it in prod by default | Use `onError`'s `info.componentStack` server-side via monitor |
189
+
190
+ ---
191
+
192
+ ## API
193
+
194
+ ```ts
195
+ interface BoundaryProps {
196
+ children: ReactNode;
197
+ variant?: 'silent' | 'inline' | 'card' | 'fullscreen'; // default 'card'
198
+ fallback?: ReactNode | ((props: BoundaryRenderProps) => ReactNode);
199
+ FallbackComponent?: ComponentType<BoundaryRenderProps>;
200
+ resetKeys?: ReadonlyArray<unknown>;
201
+ onError?: (error: Error, info: ErrorInfo) => void;
202
+ onReset?: (details: BoundaryResetDetails) => void;
203
+ name?: string; // dev log tag
204
+ className?: string; // fallback wrapper className
205
+ logger?: BoundaryLogger; // override default console.error in dev
206
+ }
207
+
208
+ interface BoundaryRenderProps {
209
+ error: Error;
210
+ errorInfo: ErrorInfo | null;
211
+ reset: () => void;
212
+ }
213
+
214
+ interface BoundaryResetDetails {
215
+ reason: 'imperative' | 'keys';
216
+ prevResetKeys?: ReadonlyArray<unknown>;
217
+ nextResetKeys?: ReadonlyArray<unknown>;
218
+ }
219
+
220
+ function useBoundary(): {
221
+ showBoundary: (error: unknown) => void;
222
+ resetBoundary: () => void;
223
+ };
224
+ ```
225
+
226
+ ---
227
+
228
+ ## Comparison with `react-error-boundary`
229
+
230
+ | Feature | `react-error-boundary` | `@djangocfg/ui-core` `Boundary` |
231
+ |---|---|---|
232
+ | `fallback` / `fallbackRender` / `FallbackComponent` | ✅ separate props | ✅ unified: `fallback` (node or fn) + `FallbackComponent` |
233
+ | `resetKeys` | ✅ | ✅ (shallow `Object.is` per index) |
234
+ | `onError` | ✅ | ✅ |
235
+ | `onReset` | ✅ | ✅ (with `reason` + prev/next keys) |
236
+ | `useErrorBoundary()` / `useBoundary()` | ✅ | ✅ |
237
+ | Built-in visual variants | ❌ | ✅ silent / inline / card / fullscreen |
238
+ | Safe fallback rendering | ❌ (can loop) | ✅ |
239
+ | `onError` / `onReset` thrown errors caught | ❌ | ✅ |
240
+ | Non-Error normalization | ❌ | ✅ |
241
+ | Accessible defaults (`role`, `aria-live`) | ❌ | ✅ |
242
+ | Backend telemetry integration | manual | `MonitorBoundary` wrapper |
243
+
244
+ ---
245
+
246
+ ## Related
247
+
248
+ - **`MonitorBoundary`** (`@djangocfg/layouts`) — wraps `Boundary` and reports to `@djangocfg/monitor` automatically.
249
+ - **Next.js `error.tsx`** — handles route-level errors. Use `Boundary` *inside* pages for finer granularity.
@@ -0,0 +1,191 @@
1
+ import { defineStory, useSelect } from '@djangocfg/playground';
2
+ import { useState } from 'react';
3
+
4
+ import { Button } from '../forms/button';
5
+ import { Boundary, useBoundary } from '.';
6
+ import type { BoundaryRenderProps, BoundaryVariant } from '.';
7
+
8
+ export default defineStory({
9
+ title: 'Core/Boundary',
10
+ component: Boundary,
11
+ description:
12
+ 'React error boundary with multiple visual variants. Wrap untrusted subtrees (widgets, third-party iframes, dynamic renderers) so a single render error does not crash the whole page. Includes `useBoundary()` hook for async / event-handler errors.',
13
+ });
14
+
15
+ function BoomButton({ label = 'Throw render error' }: { label?: string }) {
16
+ const [boom, setBoom] = useState(false);
17
+ if (boom) throw new Error('Demo crash from BoomButton');
18
+ return (
19
+ <Button variant="outline" size="sm" onClick={() => setBoom(true)}>
20
+ {label}
21
+ </Button>
22
+ );
23
+ }
24
+
25
+ export const Interactive = () => {
26
+ const [variant] = useSelect('variant', {
27
+ options: ['silent', 'inline', 'card', 'fullscreen'] as const,
28
+ defaultValue: 'card',
29
+ label: 'Variant',
30
+ description: 'Fallback visual style',
31
+ });
32
+
33
+ return (
34
+ <div className="max-w-lg space-y-3">
35
+ <p className="text-sm text-muted-foreground">
36
+ Click the button to throw — the surrounding page stays alive, only this block swaps to the fallback.
37
+ </p>
38
+ <Boundary variant={variant as BoundaryVariant} name="story">
39
+ <div className="rounded-md border p-4">
40
+ <BoomButton />
41
+ </div>
42
+ </Boundary>
43
+ </div>
44
+ );
45
+ };
46
+
47
+ export const Silent = () => (
48
+ <div className="max-w-lg space-y-2">
49
+ <p className="text-sm text-muted-foreground">
50
+ variant=&quot;silent&quot; renders nothing on error. Use for non-critical widgets (chat launcher, embeds).
51
+ </p>
52
+ <Boundary variant="silent" name="silent-demo">
53
+ <BoomButton label="Crash silently" />
54
+ </Boundary>
55
+ <p className="text-xs text-muted-foreground">↑ button disappears after click. Page is fine.</p>
56
+ </div>
57
+ );
58
+
59
+ export const Inline = () => (
60
+ <div className="max-w-lg">
61
+ <Boundary variant="inline" name="inline-demo">
62
+ <BoomButton />
63
+ </Boundary>
64
+ </div>
65
+ );
66
+
67
+ export const Card = () => (
68
+ <div className="max-w-lg">
69
+ <Boundary variant="card" name="card-demo">
70
+ <BoomButton />
71
+ </Boundary>
72
+ </div>
73
+ );
74
+
75
+ export const CustomFallback = () => (
76
+ <div className="max-w-lg">
77
+ <Boundary
78
+ fallback={({ error, reset }) => (
79
+ <div className="rounded-md border-2 border-dashed border-amber-500 bg-amber-500/5 p-4">
80
+ <p className="text-sm font-semibold text-amber-700">Custom fallback</p>
81
+ <p className="mt-1 text-xs text-amber-700/80">{error.message}</p>
82
+ <Button size="sm" variant="outline" className="mt-2" onClick={reset}>
83
+ Reset boundary
84
+ </Button>
85
+ </div>
86
+ )}
87
+ >
88
+ <BoomButton />
89
+ </Boundary>
90
+ </div>
91
+ );
92
+
93
+ function MyFallback({ error, reset }: BoundaryRenderProps) {
94
+ return (
95
+ <div className="rounded-md border border-blue-500/40 bg-blue-500/5 p-4">
96
+ <p className="text-sm font-semibold text-blue-700">FallbackComponent prop</p>
97
+ <p className="mt-1 text-xs text-blue-700/80">{error.message}</p>
98
+ <Button size="sm" variant="outline" className="mt-2" onClick={reset}>
99
+ Reset
100
+ </Button>
101
+ </div>
102
+ );
103
+ }
104
+
105
+ export const FallbackComponentProp = () => (
106
+ <div className="max-w-lg space-y-2">
107
+ <p className="text-sm text-muted-foreground">
108
+ Pass a component type instead of a render function — better memoization, easier to test.
109
+ </p>
110
+ <Boundary FallbackComponent={MyFallback}>
111
+ <BoomButton />
112
+ </Boundary>
113
+ </div>
114
+ );
115
+
116
+ export const ResetKeys = () => {
117
+ const [key, setKey] = useState(0);
118
+ return (
119
+ <div className="max-w-lg space-y-3">
120
+ <p className="text-sm text-muted-foreground">
121
+ Pass <code className="text-xs">resetKeys</code> — when any value in the array changes, the boundary auto-resets.
122
+ Good for clearing errors on route change (e.g. <code className="text-xs">[pathname]</code>).
123
+ </p>
124
+ <Button size="sm" onClick={() => setKey((k) => k + 1)}>
125
+ Bump resetKey ({key})
126
+ </Button>
127
+ <Boundary variant="card" resetKeys={[key]} name="resetkeys-demo">
128
+ <BoomButton />
129
+ </Boundary>
130
+ </div>
131
+ );
132
+ };
133
+
134
+ export const OnResetCallback = () => {
135
+ const [resets, setResets] = useState<string[]>([]);
136
+ return (
137
+ <div className="max-w-lg space-y-3">
138
+ <p className="text-sm text-muted-foreground">
139
+ <code className="text-xs">onReset</code> fires when the boundary recovers — wire it to React Query invalidation, refetch, etc.
140
+ </p>
141
+ <Boundary
142
+ variant="card"
143
+ onReset={(details) =>
144
+ setResets((prev) => [...prev, `reset @ ${new Date().toISOString().slice(11, 19)} (${details.reason})`])
145
+ }
146
+ >
147
+ <BoomButton />
148
+ </Boundary>
149
+ <div className="text-xs text-muted-foreground">
150
+ Resets:
151
+ <ul className="mt-1 space-y-0.5">
152
+ {resets.map((r, i) => (
153
+ <li key={i}>· {r}</li>
154
+ ))}
155
+ </ul>
156
+ </div>
157
+ </div>
158
+ );
159
+ };
160
+
161
+ function AsyncCrasher() {
162
+ const { showBoundary } = useBoundary();
163
+ return (
164
+ <Button
165
+ variant="outline"
166
+ size="sm"
167
+ onClick={async () => {
168
+ // Regular React boundaries DON'T catch this. useBoundary() does.
169
+ await new Promise((r) => setTimeout(r, 200));
170
+ try {
171
+ throw new Error('Async fetch failed after 200ms');
172
+ } catch (err) {
173
+ showBoundary(err);
174
+ }
175
+ }}
176
+ >
177
+ Throw async error
178
+ </Button>
179
+ );
180
+ }
181
+
182
+ export const AsyncErrorsViaHook = () => (
183
+ <div className="max-w-lg space-y-2">
184
+ <p className="text-sm text-muted-foreground">
185
+ React error boundaries only catch <em>render</em> errors. Use <code className="text-xs">useBoundary().showBoundary(err)</code> to push async / event-handler errors into the nearest boundary.
186
+ </p>
187
+ <Boundary variant="card" name="async-demo">
188
+ <AsyncCrasher />
189
+ </Boundary>
190
+ </div>
191
+ );
@@ -0,0 +1,9 @@
1
+ export { Boundary, useBoundary } from './Boundary';
2
+ export type {
3
+ BoundaryProps,
4
+ BoundaryVariant,
5
+ BoundaryRenderProps,
6
+ BoundaryResetReason,
7
+ BoundaryResetDetails,
8
+ BoundaryLogger,
9
+ } from './Boundary';
@@ -162,6 +162,19 @@ export { Preloader, PreloaderSkeleton } from './feedback/preloader';
162
162
  export type { PreloaderProps, PreloaderSkeletonProps } from './feedback/preloader';
163
163
  export { Toaster } from './feedback/sonner';
164
164
 
165
+ // ─────────────────────────────────────────────────────────────────────────────
166
+ // Boundary
167
+ // ─────────────────────────────────────────────────────────────────────────────
168
+ export { Boundary, useBoundary } from './boundary';
169
+ export type {
170
+ BoundaryProps,
171
+ BoundaryVariant,
172
+ BoundaryRenderProps,
173
+ BoundaryResetReason,
174
+ BoundaryResetDetails,
175
+ BoundaryLogger,
176
+ } from './boundary';
177
+
165
178
  // ─────────────────────────────────────────────────────────────────────────────
166
179
  // Specialized
167
180
  // ─────────────────────────────────────────────────────────────────────────────
@@ -38,6 +38,23 @@ export interface ComboboxProps {
38
38
  disabled?: boolean
39
39
  renderOption?: (option: ComboboxOption) => React.ReactNode
40
40
  renderValue?: (option: ComboboxOption | undefined) => React.ReactNode
41
+ /**
42
+ * Replace the default `<Button variant="outline" w-full>` trigger
43
+ * entirely. Use when the default trigger is too wide / too heavy for
44
+ * the host (e.g. a 28×28 flag icon in a chat header). The element
45
+ * receives Radix `PopoverTrigger asChild` props (refs + a11y) so
46
+ * pass any native element here — `button`, `div role="button"`, etc.
47
+ */
48
+ renderTrigger?: (
49
+ selected: ComboboxOption | undefined,
50
+ open: boolean,
51
+ ) => React.ReactElement
52
+ /** Forwarded to `PopoverContent.className` — width, padding, etc. */
53
+ contentClassName?: string
54
+ /** Forwarded to `PopoverContent.style` — host can bump z-index when
55
+ * the combobox is rendered above another overlay (chat dock,
56
+ * modal, …) whose stacking context outranks ui-core's default. */
57
+ contentStyle?: React.CSSProperties
41
58
  /** Custom filter function. If provided, replaces default filtering logic. */
42
59
  filterFunction?: (option: ComboboxOption, search: string) => boolean
43
60
  /**
@@ -80,6 +97,9 @@ export function Combobox({
80
97
  disabled = false,
81
98
  renderOption,
82
99
  renderValue,
100
+ renderTrigger,
101
+ contentClassName,
102
+ contentStyle,
83
103
  filterFunction,
84
104
  storageKey,
85
105
  storageType,
@@ -242,26 +262,34 @@ export function Combobox({
242
262
  }}
243
263
  >
244
264
  <PopoverTrigger asChild>
245
- <Button
246
- variant="outline"
247
- role="combobox"
248
- aria-expanded={open}
249
- className={cn(
250
- "w-full justify-between",
251
- !value && "text-muted-foreground",
252
- className
253
- )}
254
- disabled={disabled}
255
- >
256
- {renderValue && selectedOption
257
- ? renderValue(selectedOption)
258
- : selectedOption
259
- ? renderSelectedBadge(selectedOption)
260
- : resolvedPlaceholder}
261
- <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
262
- </Button>
265
+ {renderTrigger ? (
266
+ renderTrigger(selectedOption, open)
267
+ ) : (
268
+ <Button
269
+ variant="outline"
270
+ role="combobox"
271
+ aria-expanded={open}
272
+ className={cn(
273
+ "w-full justify-between",
274
+ !value && "text-muted-foreground",
275
+ className
276
+ )}
277
+ disabled={disabled}
278
+ >
279
+ {renderValue && selectedOption
280
+ ? renderValue(selectedOption)
281
+ : selectedOption
282
+ ? renderSelectedBadge(selectedOption)
283
+ : resolvedPlaceholder}
284
+ <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
285
+ </Button>
286
+ )}
263
287
  </PopoverTrigger>
264
- <PopoverContent className="w-[var(--radix-popover-trigger-width)] p-0" align="start">
288
+ <PopoverContent
289
+ className={cn("w-[var(--radix-popover-trigger-width)] p-0", contentClassName)}
290
+ style={contentStyle}
291
+ align="start"
292
+ >
265
293
  <Command shouldFilter={false} className="flex flex-col">
266
294
  <CommandInput
267
295
  placeholder={resolvedSearchPlaceholder}