@ordinatio/ui 1.9.0 → 1.11.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/CHANGELOG.md +18 -0
- package/README.md +34 -0
- package/dist/board-dnd.d.mts +119 -0
- package/dist/board-dnd.mjs +219 -0
- package/dist/board-types-MZA9M0cR.d.mts +200 -0
- package/dist/index.d.mts +160 -1
- package/dist/index.mjs +644 -0
- package/package.json +18 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
Entries start at 1.9.0 (older history is in git).
|
|
4
4
|
|
|
5
|
+
## 1.11.0
|
|
6
|
+
|
|
7
|
+
Board refinements found in the first real browser passes. Every new prop is optional and an app that passes none behaves as in 1.10.0, except `stretchColumns` (columns now share the tallest column's height by default; pass `false` for the old look).
|
|
8
|
+
|
|
9
|
+
- `AddCardBox` / `BoardColumn` / `PipelineBoard`: `closeOnAdd` (the form closes after a successful add and focus returns to the "+ Add a card" button). `AddCardBox` now sends an EMPTY currency when the amount is empty, even if a currency is preselected (a title-only add no longer trips the "currency without amount" rule).
|
|
10
|
+
- `BoardColumnData.canAdd` (false: no add control, for example an archived stage that still holds deals). `BoardCard.draggable` (false: the drag layer never lifts the card). `BoardCard.ownerFormer` with `texts.ownerFormer` (an owner who left is named in the owner mark's accessible name). A "+n" mark when a card has more than two tags (`texts.moreTags(n)` for your own words).
|
|
11
|
+
- Lost step: it stays open after a refusal with the sentence and the typed reason (the card does not move until the move succeeds); optional `texts.lostTitleFor(stageName)`, `texts.lostHelp`, and `texts.lostIncoming(cardTitle)` (a marker drawn in the target Lost column while the step is open); `LostStep` takes `stageName`; `DealCard` takes `keepControls` and `waiting`; `BoardColumn` takes `incoming`.
|
|
12
|
+
- Moves to stages the board does not draw (more stages than columns): a stage listed by `legalTargets` can be moved to even when it has no column; `MoveTarget.kind` makes an undrawn Lost stage ask for a reason.
|
|
13
|
+
- Drag layer: `boardDnd({ refusal })` gives a reason-specific note for a refused drop (falls back to `texts.cannotDrop`); new pure `refusalNote` and type `DropRefusal`.
|
|
14
|
+
- `PipelineBoard.stretchColumns` (default true).
|
|
15
|
+
|
|
16
|
+
## 1.10.0
|
|
17
|
+
|
|
18
|
+
- New pipeline board components: `PipelineBoard`, `BoardColumn`, `DealCard`, `AddCardBox`, `MoveMenu`, `LostStep`, `ReopenStep`, the pure `nearestColumnIndex`, and the types (`BoardCard`, `BoardColumnData`, `BoardTexts`, `BoardClassNames`, `MoveRequest`, `DropRule`, `BoardDnd`, and more). A Trello-style row of columns with cards. Contract: the app injects ALL words (`texts`, no English defaults) and, optionally, `classNames` (replace the default look; the sideways-scroll structure is always kept inline). The board holds no rules: `legalTargets`, `reopenFor`, `lost` (`validateReason`, `humanize`, suggestions) and every callback come from the app. A move into a lost column asks for a reason first; a move shows "moving" in the new column and snaps back with the sentence on a refusal; a closed card can be reopened (stage picker or plain).
|
|
19
|
+
- New entry `@ordinatio/ui/board-dnd` (`boardDnd`, `resolveDrop`, `columnShift`, `buildAnnouncements`): drag and drop as an optional layer over the same move callback. `@dnd-kit/core` 6.3.1 and `@dnd-kit/utilities` 3.2.2 are OPTIONAL exact peer dependencies: apps that never import `board-dnd` do not need them.
|
|
20
|
+
- Drag guards: no drag starts inside the card's own controls; keyboard drag only from the focused card; the board refuses Lost columns when no `lost` step is given; focus returns to the moved card.
|
|
21
|
+
- Additive: no existing export changes.
|
|
22
|
+
|
|
5
23
|
## 1.9.0
|
|
6
24
|
|
|
7
25
|
- New `Pager` component (and the `PagerProps`, `PagerClassNames`, `PagerLabels`, `PagerLinkProps` types): the "Showing X to Y of Z" line with Previous and Next links. Contract: the caller passes a `page` already inside [1, last page] and builds `hrefs: { previous, next }` from that same number; Pager decides which links show and never renders a link for a page out of range. If `page` was not in range (NaN, 0, negative, fractional, past the end) Pager shows the text for the nearest real page and NO links. No prop is a function except `LinkComponent`, which across a server-to-client boundary must be a client component or client reference (`next/link` works). Also `classNames`, `labels` (strings only; `showing` is a `{start}`/`{end}`/`{total}` template, for example an en dash form `'Showing {start}–{end} of {total}'`) and `hideWhenSinglePage` (default true: renders nothing when everything fits on one page; false shows the "Showing" line whenever the list is not empty). Tolerates NaN, Infinity, negative and fractional numbers.
|
package/README.md
CHANGED
|
@@ -290,6 +290,40 @@ A box for something that is readable only right now (an API key, a signing secre
|
|
|
290
290
|
- `labels`: `warning, copy, done, copied, failed, secret`. `classNames`: `root, title, warning, secret, actions, copy, done, note`.
|
|
291
291
|
- `useCopyToClipboard({ copiedNote, failedNote, resetAfterMs })` returns `{ copy, note, status, reset }`. `copy(text)` resolves true or false and never throws (a refused or missing clipboard gives the failed note). The last of overlapping copies decides the note; `resetAfterMs` fades it; nothing is set after unmount.
|
|
292
292
|
|
|
293
|
+
### PipelineBoard
|
|
294
|
+
|
|
295
|
+
A Trello-style board: columns of cards, a "+ Add a card" box at the bottom of each OPEN column, sideways scroll with snap on a phone. The board is rule-free and word-free: the app passes everything.
|
|
296
|
+
|
|
297
|
+
```tsx
|
|
298
|
+
import { PipelineBoard } from '@ordinatio/ui';
|
|
299
|
+
import { boardDnd } from '@ordinatio/ui/board-dnd'; // optional; needs @dnd-kit/core 6.3.1 + @dnd-kit/utilities 3.2.2
|
|
300
|
+
|
|
301
|
+
<PipelineBoard
|
|
302
|
+
columns={columns} // BoardColumnData[]: id, name, kind 'open'|'won'|'lost', count, totals[], cards[]
|
|
303
|
+
texts={texts} // every word the board shows (BoardTexts); no English defaults
|
|
304
|
+
legalTargets={(card) => targets} // the app's rule: which stages a card may move to
|
|
305
|
+
onMove={async ({ dealId, toStageId, expectedVersion, reason }) => ({ error })} // return { error } to refuse
|
|
306
|
+
lost={{ suggestions, used, validateReason, humanize }} // asks for a reason on a move into a lost column
|
|
307
|
+
reopenFor={(card) => ({ needsStage, stages })} onReopen={...}
|
|
308
|
+
onAddCard={async (stageId, { title, amount, currency }) => ({ error })}
|
|
309
|
+
classNames={{ card: '...' }} // replaces the default look of a part; structure is kept
|
|
310
|
+
dnd={boardDnd({ canDrop, texts: dragTexts })} // optional drag and drop over the same onMove
|
|
311
|
+
/>
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
**Things the app must know:**
|
|
315
|
+
|
|
316
|
+
- `onMove` success: the card stays in its new column only until the app re-reads. The app MUST re-read (or update) `columns` after a successful `onMove`, `onReopen` or `onAddCard`; until then the card data (version, badge, totals) is stale.
|
|
317
|
+
- `card.overdue` must be `false` for closed (won or lost) deals; the board shows whatever it is given.
|
|
318
|
+
- `lost` is `{ suggestions: {code, label?}[], used: {code, label?}[], validateReason(text) => code | null, humanize(code) => string }`. Without `lost`, Lost columns are not offered in the menu and a drop on one is refused (the board will not send a lost move with no reason).
|
|
319
|
+
- Drag texts, `BoardDndTexts`: `{ instructions, roleDescription, pickedUp(title), over(title, column), dropped(title, column), cancelled(title), sameColumnNote, cannotDrop }`.
|
|
320
|
+
- `canDrop(card, fromStageId, toStageId, columns) => boolean` (the `DropRule`). A refused drop is announced with `cannotDrop`, not "dropped"; the visible note clears on the next pointer or key action.
|
|
321
|
+
- Drag guards: a drag never starts inside an input, textarea, select, button, link, `[contenteditable]` or `[data-no-drag]`; a keyboard drag starts with Space only when focus is on the card itself (Space inside the card's controls works normally).
|
|
322
|
+
- After a move settles, focus goes to the moved card; after "add", focus returns to the title field.
|
|
323
|
+
- NOT proven in jsdom (needs the browser pass): arrow-key moves between columns, real drops, touch long-press, focus return after a drop, auto-scroll at the row edge.
|
|
324
|
+
|
|
325
|
+
Injection contract: pure rules (reason validation, which move is legal) belong in the engine package and are passed in as `validateReason`, `humanize`, `legalTargets` and `canDrop`; this package never imports them.
|
|
326
|
+
|
|
293
327
|
## Hooks
|
|
294
328
|
|
|
295
329
|
### Form and screen hooks (1.7.0, 1.8.0)
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { c as BoardCard, b as BoardColumnData, D as DropRule, e as BoardDnd } from './board-types-MZA9M0cR.mjs';
|
|
3
|
+
|
|
4
|
+
/** How a drag starts. A mouse needs a small move; a finger needs a long press (so scrolling the row still works) with a small allowed wobble. The keyboard uses Space to pick up and drop and Escape to cancel. */
|
|
5
|
+
declare const BOARD_DND_SENSORS: {
|
|
6
|
+
readonly mouse: {
|
|
7
|
+
readonly distance: 6;
|
|
8
|
+
};
|
|
9
|
+
readonly touch: {
|
|
10
|
+
readonly delay: 250;
|
|
11
|
+
readonly tolerance: 8;
|
|
12
|
+
};
|
|
13
|
+
readonly keyboard: {
|
|
14
|
+
readonly start: readonly ["Space"];
|
|
15
|
+
readonly end: readonly ["Space"];
|
|
16
|
+
readonly cancel: readonly ["Escape"];
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
type DropOutcome = {
|
|
20
|
+
kind: 'move';
|
|
21
|
+
cardId: string;
|
|
22
|
+
toStageId: string;
|
|
23
|
+
}
|
|
24
|
+
/** Dropped on the column it came from: a plain note, nothing is sent (cards stay newest first). */
|
|
25
|
+
| {
|
|
26
|
+
kind: 'same-column';
|
|
27
|
+
}
|
|
28
|
+
/** The app's rule refuses that column. */
|
|
29
|
+
| {
|
|
30
|
+
kind: 'illegal';
|
|
31
|
+
}
|
|
32
|
+
/** Dropped on nothing, or on something that is not a column of this board. */
|
|
33
|
+
| {
|
|
34
|
+
kind: 'none';
|
|
35
|
+
};
|
|
36
|
+
declare function findCard(columns: ReadonlyArray<BoardColumnData>, cardId: string): {
|
|
37
|
+
card: BoardCard;
|
|
38
|
+
column: BoardColumnData;
|
|
39
|
+
} | null;
|
|
40
|
+
/** What a drop of `activeId` on `overId` means. Never throws. */
|
|
41
|
+
declare function resolveDrop(activeId: string, overId: string | null, columns: ReadonlyArray<BoardColumnData>, canDrop: DropRule): DropOutcome;
|
|
42
|
+
/** The app's own sentence for a refused drop, or null to fall back to the general one. */
|
|
43
|
+
type DropRefusal = (card: BoardCard, fromStageId: string, toStageId: string, columns: ReadonlyArray<BoardColumnData>) => string | null;
|
|
44
|
+
/** The note for a drop the app's rule refused: the app's specific sentence when it gives a non-empty one, else the general `cannotDrop` text. A throwing `refusal` falls back too. */
|
|
45
|
+
declare function refusalNote(texts: {
|
|
46
|
+
cannotDrop: string;
|
|
47
|
+
}, refusal: DropRefusal | undefined, columns: ReadonlyArray<BoardColumnData>, activeId: string, overId: string | null): string;
|
|
48
|
+
interface ColumnRect {
|
|
49
|
+
id: string;
|
|
50
|
+
left: number;
|
|
51
|
+
width: number;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* How far to shift a dragged card sideways for an arrow key so it lands in the next column: the distance between the left edges of the current column and its neighbour, or null when there is no neighbour
|
|
55
|
+
* (or the key is not a sideways arrow). `currentLeft` is the left edge of the dragged card's box.
|
|
56
|
+
*/
|
|
57
|
+
declare function columnShift(key: string, currentLeft: number, columns: ReadonlyArray<ColumnRect>): number | null;
|
|
58
|
+
interface BoardDndTexts {
|
|
59
|
+
/** Read to a screen-reader user when focus lands on a card that can be dragged. */
|
|
60
|
+
instructions: string;
|
|
61
|
+
/** Spoken as the role of a draggable card, for example "draggable card". */
|
|
62
|
+
roleDescription: string;
|
|
63
|
+
pickedUp: (cardTitle: string) => string;
|
|
64
|
+
over: (cardTitle: string, columnName: string) => string;
|
|
65
|
+
dropped: (cardTitle: string, columnName: string) => string;
|
|
66
|
+
cancelled: (cardTitle: string) => string;
|
|
67
|
+
/** The note shown (and spoken) when a card is dropped on its own column. */
|
|
68
|
+
sameColumnNote: string;
|
|
69
|
+
/** The note shown when the app's rule refuses the column. */
|
|
70
|
+
cannotDrop: string;
|
|
71
|
+
}
|
|
72
|
+
interface AnnouncementLookup {
|
|
73
|
+
cardTitle: (cardId: string) => string | null;
|
|
74
|
+
columnName: (columnId: string | null) => string | null;
|
|
75
|
+
/** Whether a drop of this card on this column would send a move. Required, so a caller cannot forget it. */
|
|
76
|
+
canLand: (cardId: string, columnId: string) => boolean;
|
|
77
|
+
}
|
|
78
|
+
/** The spoken steps of a drag, in the app's words. A step about a card or column that is not on the board says nothing. */
|
|
79
|
+
declare function buildAnnouncements(texts: BoardDndTexts, lookup: AnnouncementLookup): {
|
|
80
|
+
onDragStart: ({ active }: {
|
|
81
|
+
active: {
|
|
82
|
+
id: unknown;
|
|
83
|
+
};
|
|
84
|
+
}) => string | undefined;
|
|
85
|
+
onDragOver: ({ active, over }: {
|
|
86
|
+
active: {
|
|
87
|
+
id: unknown;
|
|
88
|
+
};
|
|
89
|
+
over: {
|
|
90
|
+
id: unknown;
|
|
91
|
+
} | null;
|
|
92
|
+
}) => string | undefined;
|
|
93
|
+
onDragEnd: ({ active, over }: {
|
|
94
|
+
active: {
|
|
95
|
+
id: unknown;
|
|
96
|
+
};
|
|
97
|
+
over: {
|
|
98
|
+
id: unknown;
|
|
99
|
+
} | null;
|
|
100
|
+
}) => string | undefined;
|
|
101
|
+
onDragCancel: ({ active }: {
|
|
102
|
+
active: {
|
|
103
|
+
id: unknown;
|
|
104
|
+
};
|
|
105
|
+
}) => string | undefined;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
interface BoardDndConfig {
|
|
109
|
+
canDrop: DropRule;
|
|
110
|
+
/** Optional. The sentence for a refused drop, for example "A closed deal must be reopened first."; null or missing falls back to `texts.cannotDrop`. */
|
|
111
|
+
refusal?: DropRefusal;
|
|
112
|
+
texts: BoardDndTexts;
|
|
113
|
+
/** What follows the pointer while dragging. Default: the card's title in a plain box. */
|
|
114
|
+
renderPreview?: (card: BoardCard) => ReactNode;
|
|
115
|
+
}
|
|
116
|
+
/** The descriptor for `<PipelineBoard dnd={...} />`. Call it once per render with the app's drop rule and texts. */
|
|
117
|
+
declare function boardDnd(config: BoardDndConfig): BoardDnd;
|
|
118
|
+
|
|
119
|
+
export { type AnnouncementLookup, BOARD_DND_SENSORS, type BoardDndConfig, type BoardDndTexts, type ColumnRect, type DropOutcome, type DropRefusal, boardDnd, buildAnnouncements, columnShift, findCard, refusalNote, resolveDrop };
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// src/board-dnd/board-dnd.tsx
|
|
4
|
+
import {
|
|
5
|
+
DndContext,
|
|
6
|
+
DragOverlay,
|
|
7
|
+
KeyboardSensor,
|
|
8
|
+
MouseSensor,
|
|
9
|
+
TouchSensor,
|
|
10
|
+
useDraggable,
|
|
11
|
+
useDroppable,
|
|
12
|
+
useSensor,
|
|
13
|
+
useSensors
|
|
14
|
+
} from "@dnd-kit/core";
|
|
15
|
+
import { createContext, useContext, useEffect, useMemo, useState } from "react";
|
|
16
|
+
|
|
17
|
+
// src/board-dnd/drop.ts
|
|
18
|
+
var BOARD_DND_SENSORS = {
|
|
19
|
+
mouse: { distance: 6 },
|
|
20
|
+
touch: { delay: 250, tolerance: 8 },
|
|
21
|
+
keyboard: { start: ["Space"], end: ["Space"], cancel: ["Escape"] }
|
|
22
|
+
};
|
|
23
|
+
var NO_DRAG_SELECTOR = "input, textarea, select, button, a, [contenteditable], [data-no-drag]";
|
|
24
|
+
function isInsideControl(target) {
|
|
25
|
+
const el = target;
|
|
26
|
+
return !!el && typeof el.closest === "function" && el.closest(NO_DRAG_SELECTOR) !== null;
|
|
27
|
+
}
|
|
28
|
+
function keyStartsDrag(target, currentTarget) {
|
|
29
|
+
return target !== null && target !== void 0 && target === currentTarget;
|
|
30
|
+
}
|
|
31
|
+
function findCard(columns, cardId) {
|
|
32
|
+
for (const column of columns) {
|
|
33
|
+
for (const card of column.cards) if (card.id === cardId) return { card, column };
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function resolveDrop(activeId, overId, columns, canDrop) {
|
|
38
|
+
if (overId === null) return { kind: "none" };
|
|
39
|
+
const found = findCard(columns, activeId);
|
|
40
|
+
const target = columns.find((c) => c.id === overId);
|
|
41
|
+
if (!found || !target) return { kind: "none" };
|
|
42
|
+
if (found.column.id === target.id) return { kind: "same-column" };
|
|
43
|
+
if (!canDrop(found.card, found.column.id, target.id, columns)) return { kind: "illegal" };
|
|
44
|
+
return { kind: "move", cardId: activeId, toStageId: target.id };
|
|
45
|
+
}
|
|
46
|
+
function refusalNote(texts, refusal, columns, activeId, overId) {
|
|
47
|
+
const found = findCard(columns, activeId);
|
|
48
|
+
if (!refusal || !found || overId === null) return texts.cannotDrop;
|
|
49
|
+
try {
|
|
50
|
+
const sentence = refusal(found.card, found.column.id, overId, columns);
|
|
51
|
+
return typeof sentence === "string" && sentence.trim() !== "" ? sentence : texts.cannotDrop;
|
|
52
|
+
} catch {
|
|
53
|
+
return texts.cannotDrop;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function columnShift(key, currentLeft, columns) {
|
|
57
|
+
if (key !== "ArrowRight" && key !== "ArrowLeft") return null;
|
|
58
|
+
const sorted = [...columns].sort((a, b) => a.left - b.left);
|
|
59
|
+
if (sorted.length < 2) return null;
|
|
60
|
+
let current = 0;
|
|
61
|
+
for (let i = 0; i < sorted.length; i++) {
|
|
62
|
+
const column = sorted[i];
|
|
63
|
+
if (column && currentLeft >= column.left - 1) current = i;
|
|
64
|
+
}
|
|
65
|
+
const next = sorted[current + (key === "ArrowRight" ? 1 : -1)];
|
|
66
|
+
const here = sorted[current];
|
|
67
|
+
if (!next || !here) return null;
|
|
68
|
+
return next.left - here.left;
|
|
69
|
+
}
|
|
70
|
+
function buildAnnouncements(texts, lookup) {
|
|
71
|
+
const idOf = (value) => String(value);
|
|
72
|
+
return {
|
|
73
|
+
onDragStart: ({ active }) => {
|
|
74
|
+
const title = lookup.cardTitle(idOf(active.id));
|
|
75
|
+
return title === null ? void 0 : texts.pickedUp(title);
|
|
76
|
+
},
|
|
77
|
+
onDragOver: ({ active, over }) => {
|
|
78
|
+
const title = lookup.cardTitle(idOf(active.id));
|
|
79
|
+
const column = over ? lookup.columnName(idOf(over.id)) : null;
|
|
80
|
+
return title === null || column === null ? void 0 : texts.over(title, column);
|
|
81
|
+
},
|
|
82
|
+
onDragEnd: ({ active, over }) => {
|
|
83
|
+
const title = lookup.cardTitle(idOf(active.id));
|
|
84
|
+
const column = over ? lookup.columnName(idOf(over.id)) : null;
|
|
85
|
+
if (title === null) return void 0;
|
|
86
|
+
if (column !== null && over && !lookup.canLand(idOf(active.id), idOf(over.id))) return texts.cannotDrop;
|
|
87
|
+
return column === null ? texts.cancelled(title) : texts.dropped(title, column);
|
|
88
|
+
},
|
|
89
|
+
onDragCancel: ({ active }) => {
|
|
90
|
+
const title = lookup.cardTitle(idOf(active.id));
|
|
91
|
+
return title === null ? void 0 : texts.cancelled(title);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/board-dnd/board-dnd.tsx
|
|
97
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
98
|
+
var GuardedMouseSensor = class extends MouseSensor {
|
|
99
|
+
static activators = [{
|
|
100
|
+
eventName: "onMouseDown",
|
|
101
|
+
handler: ({ nativeEvent }, { onActivation }) => {
|
|
102
|
+
if (nativeEvent.button === 2 || isInsideControl(nativeEvent.target)) return false;
|
|
103
|
+
onActivation?.({ event: nativeEvent });
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
}];
|
|
107
|
+
};
|
|
108
|
+
var GuardedTouchSensor = class extends TouchSensor {
|
|
109
|
+
static activators = [{
|
|
110
|
+
eventName: "onTouchStart",
|
|
111
|
+
handler: ({ nativeEvent }, { onActivation }) => {
|
|
112
|
+
if (nativeEvent.touches.length > 1 || isInsideControl(nativeEvent.target)) return false;
|
|
113
|
+
onActivation?.({ event: nativeEvent });
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
}];
|
|
117
|
+
};
|
|
118
|
+
var GuardedKeyboardSensor = class extends KeyboardSensor {
|
|
119
|
+
static activators = [{
|
|
120
|
+
eventName: "onKeyDown",
|
|
121
|
+
handler: (event, { keyboardCodes, onActivation }) => {
|
|
122
|
+
const start = keyboardCodes?.start ?? ["Space", "Enter"];
|
|
123
|
+
if (!start.includes(event.nativeEvent.code) || !keyStartsDrag(event.target, event.currentTarget)) return false;
|
|
124
|
+
event.preventDefault();
|
|
125
|
+
onActivation?.({ event: event.nativeEvent });
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
}];
|
|
129
|
+
};
|
|
130
|
+
var ConfigContext = createContext(null);
|
|
131
|
+
var columnJump = (event, { currentCoordinates, context }) => {
|
|
132
|
+
const rects = Array.from(context.droppableRects.entries()).map(([id, rect]) => ({ id: String(id), left: rect.left, width: rect.width }));
|
|
133
|
+
const shift = columnShift(event.code === "ArrowRight" || event.code === "ArrowLeft" ? event.code : "", context.collisionRect?.left ?? currentCoordinates.x, rects);
|
|
134
|
+
if (shift === null) return void 0;
|
|
135
|
+
return { x: currentCoordinates.x + shift, y: currentCoordinates.y };
|
|
136
|
+
};
|
|
137
|
+
function usePrefersReducedMotion() {
|
|
138
|
+
const [reduced, setReduced] = useState(false);
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function") return;
|
|
141
|
+
const query = window.matchMedia("(prefers-reduced-motion: reduce)");
|
|
142
|
+
setReduced(query.matches);
|
|
143
|
+
const onChange = (event) => setReduced(event.matches);
|
|
144
|
+
query.addEventListener?.("change", onChange);
|
|
145
|
+
return () => query.removeEventListener?.("change", onChange);
|
|
146
|
+
}, []);
|
|
147
|
+
return reduced;
|
|
148
|
+
}
|
|
149
|
+
function BoardDndProvider({ columns, onDrop, allow, config, children }) {
|
|
150
|
+
const appRule = config.canDrop;
|
|
151
|
+
const cfg = useMemo(() => ({ ...config, canDrop: (card, from, to, cols) => (allow ? allow(card.id, to) : true) && appRule(card, from, to, cols) }), [config, allow, appRule]);
|
|
152
|
+
const sensors = useSensors(
|
|
153
|
+
useSensor(GuardedMouseSensor, { activationConstraint: BOARD_DND_SENSORS.mouse }),
|
|
154
|
+
useSensor(GuardedTouchSensor, { activationConstraint: BOARD_DND_SENSORS.touch }),
|
|
155
|
+
useSensor(GuardedKeyboardSensor, { coordinateGetter: columnJump, keyboardCodes: { start: [...BOARD_DND_SENSORS.keyboard.start], end: [...BOARD_DND_SENSORS.keyboard.end], cancel: [...BOARD_DND_SENSORS.keyboard.cancel] } })
|
|
156
|
+
);
|
|
157
|
+
const [activeId, setActiveId] = useState(null);
|
|
158
|
+
const [note, setNote] = useState(null);
|
|
159
|
+
const reduced = usePrefersReducedMotion();
|
|
160
|
+
const announcements = useMemo(
|
|
161
|
+
() => buildAnnouncements(cfg.texts, {
|
|
162
|
+
cardTitle: (id) => findCard(columns, id)?.card.title ?? null,
|
|
163
|
+
columnName: (id) => id === null ? null : columns.find((c) => c.id === id)?.name ?? null,
|
|
164
|
+
canLand: (cardId, columnId) => resolveDrop(cardId, columnId, columns, cfg.canDrop).kind === "move"
|
|
165
|
+
}),
|
|
166
|
+
[cfg.texts, cfg.canDrop, columns]
|
|
167
|
+
);
|
|
168
|
+
const active = activeId ? findCard(columns, activeId) : null;
|
|
169
|
+
const onDragStart = (event) => {
|
|
170
|
+
setNote(null);
|
|
171
|
+
setActiveId(String(event.active.id));
|
|
172
|
+
};
|
|
173
|
+
const onDragEnd = (event) => {
|
|
174
|
+
setActiveId(null);
|
|
175
|
+
const outcome = resolveDrop(String(event.active.id), event.over ? String(event.over.id) : null, columns, cfg.canDrop);
|
|
176
|
+
if (outcome.kind === "move") onDrop(outcome.cardId, outcome.toStageId);
|
|
177
|
+
else if (outcome.kind === "same-column") setNote(cfg.texts.sameColumnNote);
|
|
178
|
+
else if (outcome.kind === "illegal") setNote(refusalNote(cfg.texts, cfg.refusal, columns, String(event.active.id), event.over ? String(event.over.id) : null));
|
|
179
|
+
};
|
|
180
|
+
return /* @__PURE__ */ jsx(ConfigContext.Provider, { value: cfg, children: /* @__PURE__ */ jsxs(
|
|
181
|
+
DndContext,
|
|
182
|
+
{
|
|
183
|
+
sensors,
|
|
184
|
+
accessibility: { announcements, screenReaderInstructions: { draggable: cfg.texts.instructions } },
|
|
185
|
+
autoScroll: { threshold: { x: 0.15, y: 0 } },
|
|
186
|
+
onDragStart,
|
|
187
|
+
onDragEnd,
|
|
188
|
+
onDragCancel: () => setActiveId(null),
|
|
189
|
+
children: [
|
|
190
|
+
/* @__PURE__ */ jsx("div", { style: { display: "contents" }, onPointerDownCapture: () => note && setNote(null), onKeyDownCapture: () => note && setNote(null), children }),
|
|
191
|
+
/* @__PURE__ */ jsx(DragOverlay, { dropAnimation: reduced ? null : void 0, children: active ? cfg.renderPreview ? cfg.renderPreview(active.card) : /* @__PURE__ */ jsx("div", { style: { padding: "12px", borderRadius: "8px", background: "#ffffff", boxShadow: "0 4px 12px rgba(0,0,0,0.3)", fontSize: "16px" }, children: active.card.title }) : null }),
|
|
192
|
+
note ? /* @__PURE__ */ jsx("p", { role: "status", style: { margin: "8px 0 0", fontSize: "16px" }, children: note }) : null
|
|
193
|
+
]
|
|
194
|
+
}
|
|
195
|
+
) });
|
|
196
|
+
}
|
|
197
|
+
function DragColumn({ column, children }) {
|
|
198
|
+
const { setNodeRef, isOver } = useDroppable({ id: column.id });
|
|
199
|
+
const shell = { ref: setNodeRef };
|
|
200
|
+
return /* @__PURE__ */ jsx(Fragment, { children: children(isOver ? { ...shell, "data-drop-over": "true" } : shell, isOver) });
|
|
201
|
+
}
|
|
202
|
+
function DragCard({ card, children }) {
|
|
203
|
+
const cfg = useContext(ConfigContext);
|
|
204
|
+
const { attributes, listeners, setNodeRef, isDragging } = useDraggable({ id: card.id, attributes: { role: "listitem", roleDescription: cfg?.texts.roleDescription ?? "" } });
|
|
205
|
+
const shell = { ref: setNodeRef, ...attributes, ...listeners ?? {} };
|
|
206
|
+
return /* @__PURE__ */ jsx(Fragment, { children: children(shell, isDragging) });
|
|
207
|
+
}
|
|
208
|
+
function boardDnd(config) {
|
|
209
|
+
return { Provider: BoardDndProvider, Column: DragColumn, Card: DragCard, config };
|
|
210
|
+
}
|
|
211
|
+
export {
|
|
212
|
+
BOARD_DND_SENSORS,
|
|
213
|
+
boardDnd,
|
|
214
|
+
buildAnnouncements,
|
|
215
|
+
columnShift,
|
|
216
|
+
findCard,
|
|
217
|
+
refusalNote,
|
|
218
|
+
resolveDrop
|
|
219
|
+
};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { ComponentType, ReactNode, HTMLAttributes, Ref } from 'react';
|
|
2
|
+
|
|
3
|
+
/** One card on the board. Display text is built by the caller (money, dates and names are formatted outside this package). */
|
|
4
|
+
interface BoardCard {
|
|
5
|
+
id: string;
|
|
6
|
+
title: string;
|
|
7
|
+
/** The version the card was read at: it goes back out with a move so a stale move is refused. */
|
|
8
|
+
version: number;
|
|
9
|
+
stageId: string;
|
|
10
|
+
/** For example "$1,200.00". Null or missing: no amount line. */
|
|
11
|
+
amountLine?: string | null;
|
|
12
|
+
/** For example "Close 2026-12-31". */
|
|
13
|
+
closeDateLine?: string | null;
|
|
14
|
+
overdue?: boolean;
|
|
15
|
+
ownerInitials?: string | null;
|
|
16
|
+
/** At most two are drawn. */
|
|
17
|
+
tags?: ReadonlyArray<{
|
|
18
|
+
id: string;
|
|
19
|
+
label: string;
|
|
20
|
+
}>;
|
|
21
|
+
/** A closed card shows its badge. */
|
|
22
|
+
badge?: 'won' | 'lost' | null;
|
|
23
|
+
/** False: a closed or otherwise fixed card is not lifted by the drag layer (the menu is unaffected). Missing means draggable. */
|
|
24
|
+
draggable?: boolean;
|
|
25
|
+
/** The owner has left the team: the owner mark is named with `texts.ownerFormer` when that text is given. */
|
|
26
|
+
ownerFormer?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** open: new cards can be added here. won and lost: a stage that ends a deal. */
|
|
29
|
+
type BoardColumnKind = 'open' | 'won' | 'lost';
|
|
30
|
+
interface BoardColumnData {
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
kind: BoardColumnKind;
|
|
34
|
+
/** The number the header shows (may be larger than the cards drawn). */
|
|
35
|
+
count: number;
|
|
36
|
+
/** One ready-made line per currency, for example "$4,000.00". Never summed across currencies. */
|
|
37
|
+
totals: ReadonlyArray<string>;
|
|
38
|
+
cards: ReadonlyArray<BoardCard>;
|
|
39
|
+
/** More cards exist in this stage than are drawn. */
|
|
40
|
+
hasMore?: boolean;
|
|
41
|
+
/** False: no "+ Add a card" here even in an open column (for example an archived stage that still holds deals). Missing means true. */
|
|
42
|
+
canAdd?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** What a move sends. `reason` is present only for a move into a lost stage. */
|
|
45
|
+
interface MoveRequest {
|
|
46
|
+
dealId: string;
|
|
47
|
+
toStageId: string;
|
|
48
|
+
expectedVersion: number;
|
|
49
|
+
reason?: string;
|
|
50
|
+
}
|
|
51
|
+
/** What a callback returns: nothing or an object without `error` is success; `{ error }` is a refusal a person can read. */
|
|
52
|
+
type BoardResult = {
|
|
53
|
+
error?: string;
|
|
54
|
+
[extra: string]: unknown;
|
|
55
|
+
} | void;
|
|
56
|
+
interface AddCardInput {
|
|
57
|
+
title: string;
|
|
58
|
+
/** The amount exactly as typed; empty means no amount. */
|
|
59
|
+
amount: string;
|
|
60
|
+
/** Empty when no currency applies. */
|
|
61
|
+
currency: string;
|
|
62
|
+
}
|
|
63
|
+
type MoveHandler = (request: MoveRequest) => Promise<BoardResult>;
|
|
64
|
+
type AddCardHandler = (stageId: string, input: AddCardInput) => Promise<BoardResult>;
|
|
65
|
+
/** Whether a card may be dropped on a stage. Pure; the caller supplies the app's rule (for example the one in @ordinatio/pipelines/board). */
|
|
66
|
+
type DropRule = (card: BoardCard, fromStageId: string, toStageId: string, columns: ReadonlyArray<BoardColumnData>) => boolean;
|
|
67
|
+
interface MoveTarget {
|
|
68
|
+
stageId: string;
|
|
69
|
+
label: string;
|
|
70
|
+
/** Needed only for a stage the board does not draw (more stages than columns): a Lost stage still asks for a reason. */
|
|
71
|
+
kind?: BoardColumnKind;
|
|
72
|
+
}
|
|
73
|
+
/** Every text arrives from outside (no English default lives in this package), so an app in any language shows only its own words. */
|
|
74
|
+
interface BoardTexts {
|
|
75
|
+
/** Accessible name of the scrolling row. */
|
|
76
|
+
boardLabel: string;
|
|
77
|
+
/** For example (2, 6) gives "Stage 2 of 6". */
|
|
78
|
+
stagePosition: (position: number, total: number) => string;
|
|
79
|
+
emptyBoard: string;
|
|
80
|
+
emptyColumn: string;
|
|
81
|
+
/** For example (4) gives "4 cards". */
|
|
82
|
+
columnCount: (count: number) => string;
|
|
83
|
+
showMore: string;
|
|
84
|
+
moving: string;
|
|
85
|
+
overdue: string;
|
|
86
|
+
won: string;
|
|
87
|
+
lost: string;
|
|
88
|
+
/** Spoken before the initials, for example "Owner". */
|
|
89
|
+
owner: string;
|
|
90
|
+
addCard: string;
|
|
91
|
+
addTitleLabel: string;
|
|
92
|
+
addAmountLabel: string;
|
|
93
|
+
addCurrencyLabel: string;
|
|
94
|
+
addSubmit: string;
|
|
95
|
+
addCancel: string;
|
|
96
|
+
/** Shown when the title is empty. */
|
|
97
|
+
addTitleRequired: string;
|
|
98
|
+
addPending: string;
|
|
99
|
+
moveLabel: string;
|
|
100
|
+
moveChoose: string;
|
|
101
|
+
moveSubmit: string;
|
|
102
|
+
movePending: string;
|
|
103
|
+
lostTitle: string;
|
|
104
|
+
lostSuggested: string;
|
|
105
|
+
lostUsed: string;
|
|
106
|
+
lostOwnLabel: string;
|
|
107
|
+
lostConfirm: string;
|
|
108
|
+
lostCancel: string;
|
|
109
|
+
/** Shown when no reason, or a reason of invalid shape, is chosen. */
|
|
110
|
+
lostRequired: string;
|
|
111
|
+
/** The button on a closed card that opens the reopen step. */
|
|
112
|
+
reopen: string;
|
|
113
|
+
reopenTitle: string;
|
|
114
|
+
reopenStageLabel: string;
|
|
115
|
+
reopenConfirm: string;
|
|
116
|
+
reopenCancel: string;
|
|
117
|
+
/** Shown when an action throws instead of returning a refusal. */
|
|
118
|
+
unexpectedError: string;
|
|
119
|
+
/** Optional. The Lost step title with the target stage named, for example (name) gives "Move to Lost?". Replaces `lostTitle` when given. */
|
|
120
|
+
lostTitleFor?: (stageName: string) => string;
|
|
121
|
+
/** Optional. One plain line under the Lost step title, for example "The card moves when you press Mark lost." */
|
|
122
|
+
lostHelp?: string;
|
|
123
|
+
/** Optional. The marker drawn in the target Lost column while its step is open; receives the card title. */
|
|
124
|
+
lostIncoming?: (cardTitle: string) => string;
|
|
125
|
+
/** Optional. The mark for hidden tags; receives how many are hidden. Without it the mark is "+n". */
|
|
126
|
+
moreTags?: (hidden: number) => string;
|
|
127
|
+
/** Optional. Spoken after the owner mark of a card whose owner left, for example "former member". */
|
|
128
|
+
ownerFormer?: string;
|
|
129
|
+
}
|
|
130
|
+
/** The parts of the board that can take a class name. Any part left out keeps a plain default look. */
|
|
131
|
+
interface BoardClassNames {
|
|
132
|
+
root?: string;
|
|
133
|
+
row?: string;
|
|
134
|
+
position?: string;
|
|
135
|
+
column?: string;
|
|
136
|
+
columnDropOver?: string;
|
|
137
|
+
columnHeader?: string;
|
|
138
|
+
columnName?: string;
|
|
139
|
+
columnCount?: string;
|
|
140
|
+
columnTotals?: string;
|
|
141
|
+
cardList?: string;
|
|
142
|
+
empty?: string;
|
|
143
|
+
card?: string;
|
|
144
|
+
cardPending?: string;
|
|
145
|
+
cardDragging?: string;
|
|
146
|
+
cardTitle?: string;
|
|
147
|
+
cardMeta?: string;
|
|
148
|
+
overdue?: string;
|
|
149
|
+
ownerMark?: string;
|
|
150
|
+
tagChip?: string;
|
|
151
|
+
badgeWon?: string;
|
|
152
|
+
badgeLost?: string;
|
|
153
|
+
cardError?: string;
|
|
154
|
+
showMore?: string;
|
|
155
|
+
addButton?: string;
|
|
156
|
+
addForm?: string;
|
|
157
|
+
input?: string;
|
|
158
|
+
select?: string;
|
|
159
|
+
primaryButton?: string;
|
|
160
|
+
secondaryButton?: string;
|
|
161
|
+
error?: string;
|
|
162
|
+
panel?: string;
|
|
163
|
+
panelTitle?: string;
|
|
164
|
+
choice?: string;
|
|
165
|
+
choiceActive?: string;
|
|
166
|
+
note?: string;
|
|
167
|
+
}
|
|
168
|
+
/** Wraps a card title, for example in the app's router link. Receives the title text as `children`. */
|
|
169
|
+
type RenderCardTitle = (card: BoardCard, children: ReactNode) => ReactNode;
|
|
170
|
+
/** What the drag layer (`@ordinatio/ui/board-dnd`) hands to the plain board. The plain board needs none of it: with no `dnd` it is complete (a menu moves cards). */
|
|
171
|
+
type CardShellProps = HTMLAttributes<HTMLLIElement> & {
|
|
172
|
+
ref?: Ref<HTMLLIElement>;
|
|
173
|
+
};
|
|
174
|
+
type ColumnShellProps = HTMLAttributes<HTMLElement> & {
|
|
175
|
+
ref?: Ref<HTMLElement>;
|
|
176
|
+
};
|
|
177
|
+
interface BoardDnd {
|
|
178
|
+
/** Wraps the whole board (the drag context, the drag preview and the plain-words announcements). */
|
|
179
|
+
Provider: ComponentType<{
|
|
180
|
+
columns: ReadonlyArray<BoardColumnData>;
|
|
181
|
+
onDrop: (cardId: string, toStageId: string) => void; /** The board's own veto (for example a Lost column with no lost-reason step): checked before the app's `canDrop`. */
|
|
182
|
+
allow?: (cardId: string, toStageId: string) => boolean;
|
|
183
|
+
config: unknown;
|
|
184
|
+
children: ReactNode;
|
|
185
|
+
}>;
|
|
186
|
+
/** Makes a column a drop target; calls its child with the attributes to spread on the column and whether a card is over it. */
|
|
187
|
+
Column: ComponentType<{
|
|
188
|
+
column: BoardColumnData;
|
|
189
|
+
children: (props: ColumnShellProps, over: boolean) => ReactNode;
|
|
190
|
+
}>;
|
|
191
|
+
/** Makes a card draggable; calls its child with the attributes to spread on the card and whether it is being dragged. */
|
|
192
|
+
Card: ComponentType<{
|
|
193
|
+
card: BoardCard;
|
|
194
|
+
children: (props: CardShellProps, dragging: boolean) => ReactNode;
|
|
195
|
+
}>;
|
|
196
|
+
/** Passed back to `Provider` as-is (the app's drop rule and the drag texts). */
|
|
197
|
+
config: unknown;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export type { AddCardHandler as A, BoardTexts as B, CardShellProps as C, DropRule as D, MoveHandler as M, RenderCardTitle as R, BoardClassNames as a, BoardColumnData as b, BoardCard as c, MoveTarget as d, BoardDnd as e, AddCardInput as f, BoardResult as g, BoardColumnKind as h, ColumnShellProps as i, MoveRequest as j };
|