Skip to content

Build your personal knowledge base with Trilium Notes

client/ui/sortable card: allow configuring whether the reorder grip is on the start/end edge of the item

Adorian Dorane6bc7e934eb2
diff --git a/apps/client/src/widgets/react/SortableCard.css b/apps/client/src/widgets/react/SortableCard.css
index 1a84135e7d..bc78796faf 100644
--- a/apps/client/src/widgets/react/SortableCard.css
+++ b/apps/client/src/widgets/react/SortableCard.css
@@ -85,6 +85,12 @@
margin-inline-end: calc(var(--card-padding-inline) / -2);
padding: 0.7em calc(var(--card-padding-inline) / 2);
}
+
+ /* Standing first, it reaches for the leading edge instead. */
+ .tn-sortable-grip:first-child {
+ margin-inline-start: calc(var(--card-padding-inline) / -2);
+ margin-inline-end: 0;
+ }
}
/* Under the pointer rather than in the flow: it follows the finger, and the transition the other
diff --git a/apps/client/src/widgets/react/SortableCard.spec.tsx b/apps/client/src/widgets/react/SortableCard.spec.tsx
index e2a4971aea..f910e79b0a 100644
--- a/apps/client/src/widgets/react/SortableCard.spec.tsx
+++ b/apps/client/src/widgets/react/SortableCard.spec.tsx
@@ -553,6 +553,21 @@ describe("SortableCard", () => {
expect(first.querySelector(".tn-sortable-grip")?.getAttribute("aria-hidden")).toBe("true");
});
+ /** The other edge, for a card whose entries carry something of their own on the trailing one. */
+ it("carries it on the leading edge where the caller asks for it there", () => {
+ draw({ gripPlacement: "start" });
+ const [ first ] = segments();
+
+ expect(first.firstElementChild?.className).toContain("tn-sortable-grip");
+ expect(first.lastElementChild?.className).toContain("tn-sortable-content");
+
+ // And carries a segment from there just the same.
+ grab(first);
+ moveTo(SEGMENT_HEIGHT + SEGMENT_GAP + 1);
+ drop();
+ expect(changed.at(-1)?.map((item) => item.key)).toEqual([ "b", "a", "c" ]);
+ });
+
/**
* The order is the caller's: what it hands back is what the card draws, so a caller that keeps
* the order elsewhere, or refuses a move, is drawn as it decides rather than as the card left
diff --git a/apps/client/src/widgets/react/SortableCard.tsx b/apps/client/src/widgets/react/SortableCard.tsx
index ca2568ee4e..529ba8e94f 100644
--- a/apps/client/src/widgets/react/SortableCard.tsx
+++ b/apps/client/src/widgets/react/SortableCard.tsx
@@ -64,6 +64,12 @@ export interface SortableCardProps<T extends SortableItem> extends CardProps {
* The first of them is what Enter beside an entry makes, that being the one a card leads with.
*/
itemCreationButtons?: ItemCreationButton<T>[];
+ /**
+ * Which edge of a segment the grip stands on. The trailing one by default, where it is out of
+ * the way of what the segment reads; the leading one suits a card whose entries carry controls
+ * of their own on that side.
+ */
+ gripPlacement?: "start" | "end";
/** The entry the reader is on, for a card whose selection the caller keeps. */
selectedKey?: string;
onSelect?: (key: string) => void;
@@ -81,7 +87,8 @@ export interface SortableCardProps<T extends SortableItem> extends CardProps {
* list of preferences, of columns, or of anything else named by a key.
*/
export function SortableCard<T extends SortableItem>({
- items, onChange, renderItem, itemCreationButtons, selectedKey, onSelect, className, ...card
+ items, onChange, renderItem, itemCreationButtons, gripPlacement = "end", selectedKey, onSelect,
+ className, ...card
}: SortableCardProps<T>) {
if ((itemCreationButtons?.length ?? 0) > MAX_CREATION_BUTTONS) {
throw new Error("Up to three item creation buttons are supported");
@@ -358,6 +365,21 @@ export function SortableCard<T extends SortableItem>({
focusEntry(to);
}, [ focusEntry, shown ]);
+ /** What a segment is carried by, drawn on whichever edge the caller asked for. */
+ const grip = (key: string) => (
+ <span
+ className="tn-sortable-grip"
+ title={t("sortable_card.reorder")}
+ aria-hidden="true"
+ onPointerDown={(event) => beginDrag(event, key)}
+ >
+ <svg viewBox="0 0 16 16">
+ <line x1="3" y1="6" x2="13" y2="6" />
+ <line x1="3" y1="10" x2="13" y2="10" />
+ </svg>
+ </span>
+ );
+
return (
<Card className={clsx("tn-sortable-card", className)} {...card}>
<div
@@ -415,6 +437,8 @@ export function SortableCard<T extends SortableItem>({
onKeyDown={(event) => onSegmentKeyDown(event, index)}
onAnimationEnd={() => setAddedKey(undefined)}
>
+ {gripPlacement === "start" && grip(item.key)}
+
<span className="tn-sortable-content">
{renderItem
? renderItem(item, index)
@@ -424,17 +448,7 @@ export function SortableCard<T extends SortableItem>({
</>}
</span>
- <span
- className="tn-sortable-grip"
- title={t("sortable_card.reorder")}
- aria-hidden="true"
- onPointerDown={(event) => beginDrag(event, item.key)}
- >
- <svg viewBox="0 0 16 16">
- <line x1="3" y1="6" x2="13" y2="6" />
- <line x1="3" y1="10" x2="13" y2="10" />
- </svg>
- </span>
+ {gripPlacement === "end" && grip(item.key)}
</section>
))}
diff --git a/packages/commons/src/lib/script_api_preact.ts b/packages/commons/src/lib/script_api_preact.ts
index c3aef26312..b818f3761e 100644
--- a/packages/commons/src/lib/script_api_preact.ts
+++ b/packages/commons/src/lib/script_api_preact.ts
@@ -421,6 +421,8 @@ interface SortableCardProps<T extends SortableItem> {
renderItem?: (item: T, index: number) => ComponentChildren;
/** Up to three, drawn as a row of buttons at the foot of the card. */
itemCreationButtons?: ItemCreationButton<T>[];
+ /** Which edge of a segment the grip stands on. The trailing one by default. */
+ gripPlacement?: "start" | "end";
selectedKey?: string;
onSelect?: (key: string) => void;
heading?: string;