@blackbelt-technology/pi-dashboard-client-utils 0.5.3 → 0.6.0
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.
- package/package.json +33 -5
- package/src/AGENTS.md +25 -0
- package/src/Confirm.tsx +58 -0
- package/src/Dialog.tsx +194 -0
- package/src/EmptyState.tsx +87 -0
- package/src/SearchableSelectDialog.tsx +6 -14
- package/src/Skeleton.tsx +52 -0
- package/src/StatusPill.tsx +2 -2
- package/src/__tests__/Confirm.test.tsx +105 -0
- package/src/__tests__/Dialog.test.tsx +215 -0
- package/src/__tests__/EmptyState.test.tsx +44 -0
- package/src/__tests__/Skeleton.test.tsx +51 -0
- package/src/__tests__/escape-stack.test.ts +166 -0
- package/src/__tests__/statusPresentation.test.ts +34 -0
- package/src/__tests__/useFocusTrap.test.tsx +68 -0
- package/src/__tests__/useZoomPan.test.ts +66 -2
- package/src/escape-stack.ts +126 -0
- package/src/extension-ui/AGENTS.md +10 -0
- package/src/focusRing.ts +11 -0
- package/src/minimal-chat/AGENTS.md +9 -0
- package/src/minimal-chat/MinimalChatView.tsx +10 -3
- package/src/minimal-chat/__tests__/MinimalChatView.test.tsx +5 -2
- package/src/minimal-chat/types.ts +12 -0
- package/src/statusPresentation.ts +48 -0
- package/src/useFocusTrap.ts +82 -0
- package/src/useMediaQuery.ts +4 -2
- package/src/useZoomPan.ts +26 -4
- package/src/ConfirmDialog.tsx +0 -37
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { useEffect, useId, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shared escape-dismiss stack.
|
|
5
|
+
*
|
|
6
|
+
* A **single module-stable** `keydown` listener maintains a LIFO stack of
|
|
7
|
+
* registered dismissible layers (dialogs, image lightbox, file-preview overlay).
|
|
8
|
+
* On `Escape` it invokes the `onEscape` of **only the topmost** registered layer,
|
|
9
|
+
* so stacked full-screen surfaces peel one-per-press instead of all closing at
|
|
10
|
+
* once. Topmost-only dismissal is enforced by the stack itself — no cross-listener
|
|
11
|
+
* arbitration is required.
|
|
12
|
+
*
|
|
13
|
+
* ## Listener phase — `document`, bubble
|
|
14
|
+
*
|
|
15
|
+
* The listener attaches on `document` in the **bubble** phase (not capture) so a
|
|
16
|
+
* focused input / typeahead's own Escape handler runs FIRST and can opt out:
|
|
17
|
+
* - `e.defaultPrevented` → the stack skips dismissal (Escape-to-clear on a
|
|
18
|
+
* focused field does not also dismiss the surrounding layer).
|
|
19
|
+
* On consume the handler calls `preventDefault()` + `stopImmediatePropagation()`.
|
|
20
|
+
* This reliably suppresses `window`-level listeners (bubble reaches `window`
|
|
21
|
+
* after `document`) and any `document`-bubble listener registered *after* this
|
|
22
|
+
* one. It does NOT suppress a `document` listener registered *before* it (the DOM
|
|
23
|
+
* fires same-target listeners in registration order) — a best-effort bound on
|
|
24
|
+
* interference with unmigrated peers, not a correctness dependency.
|
|
25
|
+
*
|
|
26
|
+
* ## Attach-once / never-detach
|
|
27
|
+
*
|
|
28
|
+
* The listener is attached lazily on the first registration and then **stays
|
|
29
|
+
* attached** (it early-returns when the stack is empty). Detaching on empty would
|
|
30
|
+
* let another `document` listener slip ahead in registration order during an idle
|
|
31
|
+
* window and permanently sit in front of the stack.
|
|
32
|
+
*
|
|
33
|
+
* ## React-synthetic interaction (documented constraint)
|
|
34
|
+
*
|
|
35
|
+
* A child that calls React's synthetic `e.stopPropagation()` on Escape halts the
|
|
36
|
+
* native event at the React root before it reaches `document`, so this shared
|
|
37
|
+
* listener never runs and the layer will not dismiss on that keypress. This is
|
|
38
|
+
* the CORRECT behavior for an **open** combobox/typeahead (Escape closes the
|
|
39
|
+
* child, not the layer — it opts out by design). Migration rule: a stack-eligible
|
|
40
|
+
* surface MUST NOT contain a child that *unconditionally* `stopPropagation`s
|
|
41
|
+
* Escape.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
interface EscapeEntry {
|
|
45
|
+
id: string;
|
|
46
|
+
onEscape: () => void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const stack: EscapeEntry[] = [];
|
|
50
|
+
let attached = false;
|
|
51
|
+
|
|
52
|
+
function handleKeydown(e: KeyboardEvent): void {
|
|
53
|
+
if (e.key !== "Escape") return;
|
|
54
|
+
// Held Escape must peel one layer per press, not auto-repeat through the stack.
|
|
55
|
+
if (e.repeat) return;
|
|
56
|
+
// A focused input already handled it (Escape-to-clear) → do not also dismiss.
|
|
57
|
+
if (e.defaultPrevented) return;
|
|
58
|
+
if (stack.length === 0) return;
|
|
59
|
+
const top = stack[stack.length - 1];
|
|
60
|
+
e.preventDefault();
|
|
61
|
+
e.stopImmediatePropagation();
|
|
62
|
+
top.onEscape();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function ensureAttached(): void {
|
|
66
|
+
if (attached) return;
|
|
67
|
+
if (typeof document === "undefined") return;
|
|
68
|
+
document.addEventListener("keydown", handleKeydown);
|
|
69
|
+
attached = true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Register (or refresh) a dismissible layer by stable id. Idempotent per id. */
|
|
73
|
+
export function registerEscapeLayer(id: string, onEscape: () => void): void {
|
|
74
|
+
ensureAttached();
|
|
75
|
+
const existing = stack.findIndex((entry) => entry.id === id);
|
|
76
|
+
if (existing !== -1) {
|
|
77
|
+
stack[existing].onEscape = onEscape;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
stack.push({ id, onEscape });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Unregister a layer by its own identity (order-independent — never `pop()`). */
|
|
84
|
+
export function unregisterEscapeLayer(id: string): void {
|
|
85
|
+
const idx = stack.findIndex((entry) => entry.id === id);
|
|
86
|
+
if (idx !== -1) stack.splice(idx, 1);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Register `onEscape` with the shared stack while `active` is true.
|
|
91
|
+
*
|
|
92
|
+
* - Stable `useId` id survives a StrictMode mount→unmount→mount, so the remount's
|
|
93
|
+
* cleanup removes the right entry and the duplicate-guard keeps a single live
|
|
94
|
+
* entry.
|
|
95
|
+
* - `onEscape` is ref-backed: the latest callback is used on Escape without
|
|
96
|
+
* re-registering (no stack churn when the callback identity changes).
|
|
97
|
+
* - Removal is by id (order-independent), so interleaved non-LIFO lifecycles are
|
|
98
|
+
* safe.
|
|
99
|
+
*/
|
|
100
|
+
export function useEscapeDismiss(active: boolean, onEscape: () => void): void {
|
|
101
|
+
const id = useId();
|
|
102
|
+
const onEscapeRef = useRef(onEscape);
|
|
103
|
+
onEscapeRef.current = onEscape;
|
|
104
|
+
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
if (!active) return;
|
|
107
|
+
registerEscapeLayer(id, () => onEscapeRef.current());
|
|
108
|
+
return () => unregisterEscapeLayer(id);
|
|
109
|
+
}, [active, id]);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Test-only reset: empties the stack and detaches the listener so each test
|
|
114
|
+
* starts from a clean module state. Dev/test-gated — a no-op under
|
|
115
|
+
* `NODE_ENV=production` so production code cannot wipe global dismissal.
|
|
116
|
+
*/
|
|
117
|
+
export function __resetEscapeStack(): void {
|
|
118
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
stack.length = 0;
|
|
122
|
+
if (attached && typeof document !== "undefined") {
|
|
123
|
+
document.removeEventListener("keydown", handleKeydown);
|
|
124
|
+
}
|
|
125
|
+
attached = false;
|
|
126
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# DOX — packages/client-utils/src/extension-ui
|
|
2
|
+
|
|
3
|
+
Files in this directory. One row per source file.
|
|
4
|
+
|
|
5
|
+
| File | Purpose |
|
|
6
|
+
|------|---------|
|
|
7
|
+
| `AgentMetricSlot.tsx` | Phase-2 `agent-metric` slot. Exports `AgentMetricSlot({ session, agentId })` — renders `kind: "agent-metric"` decorators matching `payload.agentId`, inside `FlowAgentCard`. Ignores unknown agentId. Uses `decoratorsOfKind`. |
|
|
8
|
+
| `BreadcrumbSlot.tsx` | Phase-2 `breadcrumb` slot. Exports `BreadcrumbSlot({ session })` — renders most-recently-cached `kind: "breadcrumb"` descriptor as step indicator at top of `FlowDashboard`. Last-write-wins on collision. Step status active/done/error/pending. Uses `decoratorsOfKind`. |
|
|
9
|
+
| `decorator-utils.ts` | Exports `decoratorsOfKind(decorators, kind)` — type-narrowing helper filtering `DashboardSession.uiDecorators` record by `DecoratorKind`. Shared by all Phase-2 decorator slot components (`AgentMetricSlot`, `BreadcrumbSlot`, `GateSlot`). |
|
|
10
|
+
| `GateSlot.tsx` | Phase-2 `gate` slot. Exports `GateSlot({ session, flowId })` + `aggregateGateState` pure helper. Most-restrictive-wins: any `available: false` for `flowId` blocks; reasons concatenated. Renders amber banner inside `FlowLaunchDialog`; `useGateState` disables Run button. |
|
package/src/focusRing.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `focusRing` — the className string for the shared `.focus-ring` utility
|
|
3
|
+
* defined in `packages/client/src/index.css`. Scoped to `:focus-visible`
|
|
4
|
+
* (keyboard focus only, never mouse-click), ≥2px effective thickness, ≥3:1
|
|
5
|
+
* contrast against adjacent colors.
|
|
6
|
+
*
|
|
7
|
+
* Replaces the ad-hoc `focus:outline-none` + 1px `focus:border-*` pattern.
|
|
8
|
+
* Rule: WCAG 2.2 §2.4.7 / §2.4.11.
|
|
9
|
+
* See change: extend-client-utils-state-feedback-primitives.
|
|
10
|
+
*/
|
|
11
|
+
export const focusRing = "focus-ring";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# DOX — packages/client-utils/src/minimal-chat
|
|
2
|
+
|
|
3
|
+
Files in this directory. One row per source file.
|
|
4
|
+
|
|
5
|
+
| File | Purpose |
|
|
6
|
+
|------|---------|
|
|
7
|
+
| `index.ts` | Barrel for `./minimal-chat` subpath export. Re-exports `MinimalChatView`, `statusVisualsFor`, `extractInputPreview` from `MinimalChatView.js`; re-exports types `MinimalChatEntry`, `MinimalChatMeta`, `MinimalChatMode`, `MinimalChatStatus`, `MinimalChatViewProps` from `types.js`. Import path: `@blackbelt-technology/pi-dashboard-client-utils/minimal-chat`. |
|
|
8
|
+
| `MinimalChatView.tsx` | Shared subagent/agent timeline renderer. Exports `MinimalChatView` (props `MinimalChatViewProps`), `statusVisualsFor(status)` → `{ iconPath, colorClass }`, `extractInputPreview(toolName, input)` → preview string. Modes: `inline` (body `h-[60vh]`), `popout` (`h-full`), `row` (single-line, no body). Resolves `MarkdownContent`, `formatTokens`, `formatDuration`, `toolCallStep`, `thinkingBlock` via `useUiPrimitive`/`useUiPrimitiveOrNull`; falls back to inline renderers when primitive absent. Entry kinds: `tool`, `text`, `thinking`, `error`. Single source of truth replacing duplicated timeline UI in `SubagentDetailView` and `FlowAgentDetail`. Header renders `meta.cost` (pre-formatted string) between tokens and duration when present. See change: surface-flow-agent-cost. |
|
|
9
|
+
| `types.ts` | Type contracts for `MinimalChatView`. Exports `MinimalChatMode` (`"inline" | "popout" | "row"`), `MinimalChatStatus` (`"pending" | "running" | "complete" | "error" | "blocked"`), `MinimalChatEntry` (discriminated union `tool | text | thinking | error`), `MinimalChatMeta` (`modelName?`, `tokens?`, `cost?` pre-formatted USD string, `durationMs?`; see change surface-flow-agent-cost), `MinimalChatViewProps`. Producer adapters map plugin-specific state into these structural types at shim boundary. |
|
|
@@ -96,10 +96,12 @@ function ToolCallEntry({
|
|
|
96
96
|
entry,
|
|
97
97
|
index,
|
|
98
98
|
sessionId,
|
|
99
|
+
hideStatusIcon,
|
|
99
100
|
}: {
|
|
100
101
|
entry: Extract<MinimalChatEntry, { kind: "tool" }>;
|
|
101
102
|
index: number;
|
|
102
103
|
sessionId?: string;
|
|
104
|
+
hideStatusIcon?: boolean;
|
|
103
105
|
}) {
|
|
104
106
|
// Prefer the shell-registered `toolCallStep` primitive for parity with the
|
|
105
107
|
// main chat view (per-tool renderers, collapsible output, status icon).
|
|
@@ -128,6 +130,7 @@ function ToolCallEntry({
|
|
|
128
130
|
status={status}
|
|
129
131
|
result={result}
|
|
130
132
|
sessionId={sessionId}
|
|
133
|
+
hideStatusIcon={hideStatusIcon}
|
|
131
134
|
/>
|
|
132
135
|
);
|
|
133
136
|
}
|
|
@@ -238,6 +241,7 @@ export function MinimalChatView({
|
|
|
238
241
|
status,
|
|
239
242
|
entries,
|
|
240
243
|
meta,
|
|
244
|
+
hideToolStatusIcon,
|
|
241
245
|
mode = "inline",
|
|
242
246
|
onBack,
|
|
243
247
|
emptyMessage,
|
|
@@ -273,11 +277,12 @@ export function MinimalChatView({
|
|
|
273
277
|
const showMeta =
|
|
274
278
|
!!meta &&
|
|
275
279
|
(meta.modelName !== undefined ||
|
|
280
|
+
meta.cost !== undefined ||
|
|
276
281
|
meta.durationMs !== undefined ||
|
|
277
282
|
(meta.tokens !== undefined && (meta.tokens.input !== undefined || meta.tokens.output !== undefined)));
|
|
278
283
|
|
|
279
284
|
const hasTokensOrDuration =
|
|
280
|
-
!!meta && (meta.tokens !== undefined || meta.durationMs !== undefined);
|
|
285
|
+
!!meta && (meta.tokens !== undefined || meta.durationMs !== undefined || meta.cost !== undefined);
|
|
281
286
|
|
|
282
287
|
// Header (inline + popout share)
|
|
283
288
|
const header = (
|
|
@@ -318,9 +323,11 @@ export function MinimalChatView({
|
|
|
318
323
|
{meta?.tokens && (
|
|
319
324
|
<>
|
|
320
325
|
↑{formatTokens(meta.tokens.input ?? 0)} ↓{formatTokens(meta.tokens.output ?? 0)}
|
|
321
|
-
{meta.durationMs !== undefined && " · "}
|
|
326
|
+
{(meta.cost !== undefined || meta.durationMs !== undefined) && " · "}
|
|
322
327
|
</>
|
|
323
328
|
)}
|
|
329
|
+
{meta?.cost !== undefined && meta.cost}
|
|
330
|
+
{meta?.cost !== undefined && meta.durationMs !== undefined && " · "}
|
|
324
331
|
{meta?.durationMs !== undefined && formatDuration(meta.durationMs)}
|
|
325
332
|
</span>
|
|
326
333
|
)}
|
|
@@ -356,7 +363,7 @@ export function MinimalChatView({
|
|
|
356
363
|
entries.map((entry, i) => {
|
|
357
364
|
switch (entry.kind) {
|
|
358
365
|
case "tool":
|
|
359
|
-
return <ToolCallEntry key={i} entry={entry} index={i} sessionId={sessionId} />;
|
|
366
|
+
return <ToolCallEntry key={i} entry={entry} index={i} sessionId={sessionId} hideStatusIcon={hideToolStatusIcon} />;
|
|
360
367
|
case "text":
|
|
361
368
|
return <TextEntry key={i} text={entry.text} />;
|
|
362
369
|
case "thinking":
|
|
@@ -95,9 +95,12 @@ describe("MinimalChatView", () => {
|
|
|
95
95
|
<MinimalChatView title="a" status="running" entries={[]} mode="inline" />,
|
|
96
96
|
);
|
|
97
97
|
const root = container.firstElementChild as HTMLElement;
|
|
98
|
-
// Inline mode uses a STABLE
|
|
99
|
-
//
|
|
98
|
+
// Inline mode now uses a STABLE `h-[60vh]` (not `max-h-[60vh]`) so the
|
|
99
|
+
// container doesn't bounce as content streams in. The body's
|
|
100
|
+
// `flex-1 min-h-0 overflow-y-auto` provides the scroll surface.
|
|
101
|
+
// See change: fix-flows-plugin-polish (stable inline height).
|
|
100
102
|
expect(root.className).toContain("h-[60vh]");
|
|
103
|
+
expect(root.className).not.toContain("max-h-[60vh]");
|
|
101
104
|
}
|
|
102
105
|
cleanup();
|
|
103
106
|
{
|
|
@@ -52,6 +52,9 @@ export type MinimalChatEntry =
|
|
|
52
52
|
export interface MinimalChatMeta {
|
|
53
53
|
modelName?: string;
|
|
54
54
|
tokens?: { input?: number; output?: number };
|
|
55
|
+
/** Pre-formatted USD cost string (e.g. "$0.0142"). Rendered verbatim between
|
|
56
|
+
* tokens and duration; the producer formats + suppresses zero/absent. */
|
|
57
|
+
cost?: string;
|
|
55
58
|
durationMs?: number;
|
|
56
59
|
}
|
|
57
60
|
|
|
@@ -87,4 +90,13 @@ export interface MinimalChatViewProps {
|
|
|
87
90
|
* See change: fix-flows-plugin-polish (chat-view parity).
|
|
88
91
|
*/
|
|
89
92
|
sessionId?: string;
|
|
93
|
+
/**
|
|
94
|
+
* When true, tool-call rows omit the leading status glyph (the per-entry
|
|
95
|
+
* status/`ask_user` icon). Used by flow agent detail views to reduce noise;
|
|
96
|
+
* the main chat leaves it `false` (icon shown). Forwarded to the
|
|
97
|
+
* `toolCallStep` primitive as `hideStatusIcon`.
|
|
98
|
+
*
|
|
99
|
+
* See change: improve-flow-ui.
|
|
100
|
+
*/
|
|
101
|
+
hideToolStatusIcon?: boolean;
|
|
90
102
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* statusPresentation — shared status vocabulary that expresses each state via a
|
|
3
|
+
* semantic `--status-*` token AND a mandatory non-hue channel (a glyph), so
|
|
4
|
+
* status is never conveyed by color alone.
|
|
5
|
+
*
|
|
6
|
+
* Covered surfaces (composer `ArtifactChip`, OpenSpec board state pill) consume
|
|
7
|
+
* this helper instead of re-rolling their own color-only maps. The glyph keeps
|
|
8
|
+
* "done" distinguishable from "todo" in grayscale; `statusAriaLabel` names the
|
|
9
|
+
* item + state for screen readers.
|
|
10
|
+
*
|
|
11
|
+
* Rule: WCAG 2.2 §1.4.1; DRY; H4.
|
|
12
|
+
* See change: extend-client-utils-state-feedback-primitives.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export type StatusKind = "done" | "current" | "todo" | "error";
|
|
16
|
+
|
|
17
|
+
export interface StatusPresentation {
|
|
18
|
+
/** Non-hue channel — a shape/glyph distinct per state, color-independent. */
|
|
19
|
+
glyph: string;
|
|
20
|
+
/** Semantic color token reference (CSS var). */
|
|
21
|
+
tokenVar: string;
|
|
22
|
+
/** Human-readable state label, e.g. "done". */
|
|
23
|
+
label: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const STATUS_MAP: Record<StatusKind, StatusPresentation> = {
|
|
27
|
+
done: { glyph: "✓", tokenVar: "var(--status-idle)", label: "done" },
|
|
28
|
+
current: { glyph: "▸", tokenVar: "var(--status-working)", label: "current" },
|
|
29
|
+
todo: { glyph: "○", tokenVar: "var(--text-muted)", label: "todo" },
|
|
30
|
+
error: { glyph: "✕", tokenVar: "var(--status-error)", label: "error" },
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export function statusPresentation(kind: StatusKind): StatusPresentation {
|
|
34
|
+
return STATUS_MAP[kind];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Accessible name combining the item name and its state, e.g. "Proposal, done".
|
|
39
|
+
* Pass `label` to localize the status word; defaults to the English label so
|
|
40
|
+
* non-i18n callers stay correct.
|
|
41
|
+
*/
|
|
42
|
+
export function statusAriaLabel(
|
|
43
|
+
name: string,
|
|
44
|
+
kind: StatusKind,
|
|
45
|
+
label: string = STATUS_MAP[kind].label,
|
|
46
|
+
): string {
|
|
47
|
+
return `${name}, ${label}`;
|
|
48
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useEffect, type RefObject } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Focus management for modal dialogs.
|
|
5
|
+
*
|
|
6
|
+
* On `open` transition to true: stores the previously-focused element,
|
|
7
|
+
* moves focus to the first focusable child inside `ref` (or the container
|
|
8
|
+
* itself if none), and traps `Tab` / `Shift+Tab` within the container.
|
|
9
|
+
* On close (or unmount): restores focus to the previously-focused element
|
|
10
|
+
* if it is still in the document.
|
|
11
|
+
*
|
|
12
|
+
* See change: unify-dialog-system (design.md D4).
|
|
13
|
+
*/
|
|
14
|
+
const FOCUSABLE_SELECTOR = [
|
|
15
|
+
"a[href]",
|
|
16
|
+
"button:not([disabled])",
|
|
17
|
+
"textarea:not([disabled])",
|
|
18
|
+
"input:not([disabled])",
|
|
19
|
+
"select:not([disabled])",
|
|
20
|
+
'[tabindex]:not([tabindex="-1"])',
|
|
21
|
+
].join(",");
|
|
22
|
+
|
|
23
|
+
function getFocusable(container: HTMLElement): HTMLElement[] {
|
|
24
|
+
return Array.from(
|
|
25
|
+
container.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR),
|
|
26
|
+
).filter((el) => !el.hasAttribute("hidden") && el.getAttribute("aria-hidden") !== "true");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function useFocusTrap(
|
|
30
|
+
ref: RefObject<HTMLElement | null>,
|
|
31
|
+
open: boolean,
|
|
32
|
+
): void {
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (!open) return;
|
|
35
|
+
const container = ref.current;
|
|
36
|
+
if (!container) return;
|
|
37
|
+
|
|
38
|
+
const previouslyFocused = document.activeElement as HTMLElement | null;
|
|
39
|
+
|
|
40
|
+
const focusable = getFocusable(container);
|
|
41
|
+
if (focusable.length > 0) {
|
|
42
|
+
focusable[0].focus();
|
|
43
|
+
} else {
|
|
44
|
+
container.focus();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function onKeyDown(e: KeyboardEvent) {
|
|
48
|
+
if (e.key !== "Tab") return;
|
|
49
|
+
const node = ref.current;
|
|
50
|
+
if (!node) return;
|
|
51
|
+
const items = getFocusable(node);
|
|
52
|
+
if (items.length === 0) {
|
|
53
|
+
e.preventDefault();
|
|
54
|
+
node.focus();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const first = items[0];
|
|
58
|
+
const last = items[items.length - 1];
|
|
59
|
+
const active = document.activeElement;
|
|
60
|
+
if (e.shiftKey) {
|
|
61
|
+
if (active === first || !node.contains(active)) {
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
last.focus();
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
if (active === last || !node.contains(active)) {
|
|
67
|
+
e.preventDefault();
|
|
68
|
+
first.focus();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
document.addEventListener("keydown", onKeyDown, true);
|
|
74
|
+
|
|
75
|
+
return () => {
|
|
76
|
+
document.removeEventListener("keydown", onKeyDown, true);
|
|
77
|
+
if (previouslyFocused && document.contains(previouslyFocused)) {
|
|
78
|
+
previouslyFocused.focus();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}, [ref, open]);
|
|
82
|
+
}
|
package/src/useMediaQuery.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Reactive hook that tracks a CSS media query.
|
|
@@ -6,11 +6,13 @@ import { useState, useEffect } from "react";
|
|
|
6
6
|
*/
|
|
7
7
|
export function useMediaQuery(query: string): boolean {
|
|
8
8
|
const [matches, setMatches] = useState(() => {
|
|
9
|
-
if (typeof window === "undefined"
|
|
9
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function")
|
|
10
|
+
return false;
|
|
10
11
|
return window.matchMedia(query).matches;
|
|
11
12
|
});
|
|
12
13
|
|
|
13
14
|
useEffect(() => {
|
|
15
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function") return;
|
|
14
16
|
const mql = window.matchMedia(query);
|
|
15
17
|
setMatches(mql.matches);
|
|
16
18
|
|
package/src/useZoomPan.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback, useRef, useState } from "react";
|
|
2
2
|
|
|
3
3
|
export interface ZoomPanState {
|
|
4
4
|
scale: number;
|
|
@@ -16,6 +16,10 @@ const DEFAULT_MIN = 0.5;
|
|
|
16
16
|
const DEFAULT_MAX = 4;
|
|
17
17
|
const DEFAULT_STEP = 1.2;
|
|
18
18
|
const WHEEL_SENSITIVITY = 0.002;
|
|
19
|
+
// Pointer must move more than this (CSS px) before a press becomes a pan.
|
|
20
|
+
// Below it, the press stays a click so child onClick handlers fire (capturing
|
|
21
|
+
// on pointerdown would retarget the pointerup → click to the container).
|
|
22
|
+
const DRAG_THRESHOLD = 4;
|
|
19
23
|
|
|
20
24
|
export function useZoomPan(options?: UseZoomPanOptions) {
|
|
21
25
|
const minScale = options?.minScale ?? DEFAULT_MIN;
|
|
@@ -26,6 +30,9 @@ export function useZoomPan(options?: UseZoomPanOptions) {
|
|
|
26
30
|
|
|
27
31
|
// Refs for drag tracking (avoid re-renders during drag)
|
|
28
32
|
const dragging = useRef(false);
|
|
33
|
+
// Press recorded on pointerdown but not yet promoted to a drag (capture
|
|
34
|
+
// deferred until movement exceeds DRAG_THRESHOLD). Null when idle/dragging.
|
|
35
|
+
const pendingStart = useRef<{ x: number; y: number } | null>(null);
|
|
29
36
|
const lastPointer = useRef({ x: 0, y: 0 });
|
|
30
37
|
// Pinch tracking
|
|
31
38
|
const pinchDist = useRef<number | null>(null);
|
|
@@ -72,13 +79,27 @@ export function useZoomPan(options?: UseZoomPanOptions) {
|
|
|
72
79
|
const onPointerDown = useCallback((e: React.PointerEvent | PointerEvent) => {
|
|
73
80
|
// Only primary button
|
|
74
81
|
if (e.button !== 0) return;
|
|
75
|
-
|
|
82
|
+
// Record the press; do NOT capture yet. Capture is deferred to the move
|
|
83
|
+
// handler so a click-without-drag reaches the element under the pointer.
|
|
84
|
+
pendingStart.current = { x: e.clientX, y: e.clientY };
|
|
76
85
|
lastPointer.current = { x: e.clientX, y: e.clientY };
|
|
77
|
-
|
|
86
|
+
dragging.current = false;
|
|
78
87
|
}, []);
|
|
79
88
|
|
|
80
89
|
const onPointerMove = useCallback((e: React.PointerEvent | PointerEvent) => {
|
|
81
|
-
if (!dragging.current)
|
|
90
|
+
if (!dragging.current) {
|
|
91
|
+
const start = pendingStart.current;
|
|
92
|
+
if (!start) return;
|
|
93
|
+
const ddx = e.clientX - start.x;
|
|
94
|
+
const ddy = e.clientY - start.y;
|
|
95
|
+
// Below threshold → still a potential click; don't pan, don't capture.
|
|
96
|
+
if (ddx * ddx + ddy * ddy <= DRAG_THRESHOLD * DRAG_THRESHOLD) return;
|
|
97
|
+
// Threshold crossed → promote to a drag and capture once. lastPointer is
|
|
98
|
+
// still the pointerdown point, so the first translate applies the full
|
|
99
|
+
// accumulated delta (no jump, no lost motion).
|
|
100
|
+
dragging.current = true;
|
|
101
|
+
(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
|
|
102
|
+
}
|
|
82
103
|
const dx = e.clientX - lastPointer.current.x;
|
|
83
104
|
const dy = e.clientY - lastPointer.current.y;
|
|
84
105
|
lastPointer.current = { x: e.clientX, y: e.clientY };
|
|
@@ -91,6 +112,7 @@ export function useZoomPan(options?: UseZoomPanOptions) {
|
|
|
91
112
|
|
|
92
113
|
const onPointerUp = useCallback(() => {
|
|
93
114
|
dragging.current = false;
|
|
115
|
+
pendingStart.current = null;
|
|
94
116
|
}, []);
|
|
95
117
|
|
|
96
118
|
// ── Touch (pinch) handler ──────────────────────────────────────────
|
package/src/ConfirmDialog.tsx
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { Icon } from "@mdi/react";
|
|
3
|
-
import { mdiCheck } from "@mdi/js";
|
|
4
|
-
|
|
5
|
-
interface Props {
|
|
6
|
-
message: string;
|
|
7
|
-
confirmLabel?: string;
|
|
8
|
-
onConfirm: () => void;
|
|
9
|
-
onCancel: () => void;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function ConfirmDialog({ message, confirmLabel = "Confirm", onConfirm, onCancel }: Props) {
|
|
13
|
-
return (
|
|
14
|
-
<div className="fixed inset-0 z-[60] flex items-center justify-center" data-testid="confirm-dialog">
|
|
15
|
-
<div className="absolute inset-0 bg-[var(--bg-overlay)]" onClick={onCancel} />
|
|
16
|
-
<div className="relative bg-[var(--bg-secondary)] border border-[var(--border-secondary)] rounded-lg p-4 max-w-sm mx-4 space-y-3">
|
|
17
|
-
<p className="text-sm text-[var(--text-secondary)]">{message}</p>
|
|
18
|
-
<div className="flex justify-end gap-2">
|
|
19
|
-
<button
|
|
20
|
-
onClick={onCancel}
|
|
21
|
-
className="text-xs px-3 py-1.5 rounded border border-[var(--border-secondary)] text-[var(--text-secondary)] hover:text-[var(--text-primary)]"
|
|
22
|
-
data-testid="confirm-cancel"
|
|
23
|
-
>
|
|
24
|
-
Cancel
|
|
25
|
-
</button>
|
|
26
|
-
<button
|
|
27
|
-
onClick={onConfirm}
|
|
28
|
-
className="text-xs px-3 py-1.5 rounded bg-red-600 text-[var(--text-primary)] hover:bg-red-500"
|
|
29
|
-
data-testid="confirm-ok"
|
|
30
|
-
>
|
|
31
|
-
<Icon path={mdiCheck} size={0.45} className="inline mr-0.5" />{confirmLabel}
|
|
32
|
-
</button>
|
|
33
|
-
</div>
|
|
34
|
-
</div>
|
|
35
|
-
</div>
|
|
36
|
-
);
|
|
37
|
-
}
|