Skip to content

Build your personal knowledge base with Trilium Notes

fix(table): show a colour cell as a swatch rather than as a fill

Elian Doranbdfee9c4ed16
diff --git a/apps/client/src/widgets/collections/table/columns.css b/apps/client/src/widgets/collections/table/columns.css
index a5dcaeda00..c218a7fdea 100644
--- a/apps/client/src/widgets/collections/table/columns.css
+++ b/apps/client/src/widgets/collections/table/columns.css
@@ -94,14 +94,21 @@
}
/*
- * A colour cell is read as the colour filling it, which it gives up while it is being edited to look
- * like any other cell in hand: the swatch is what says the colour then, and a cell still flooded with
- * it would leave the pair standing on the very value they are there to change — the field lost in it,
- * and a colour picked showing the old one all around.
+ * A colour cell says its colour with a swatch of its own, as its editor does, rather than by being
+ * filled with it: a row keeps the striping, hover and selection that a flood of colour covers over,
+ * and a cell reads the same whether it is being looked at or edited.
*
- * Insisted upon because the fill is written onto the cell itself by the formatter, which the rule
- * dressing an editing cell is otherwise powerless against.
+ * Sized to the line rather than to the editor's field, a swatch as tall as that one would make every
+ * row in the table taller — rows being of one height, colours in them or not.
*/
-.tabulator-cell.tabulator-editing:has(> .table-color-editor) {
- background-color: var(--cell-editing-background-color) !important;
+.table-color-swatch {
+ display: inline-block;
+ width: 1.25em;
+ height: 1.25em;
+ vertical-align: middle;
+ border-radius: 25%;
+
+ /* Drawn within, so that a colour close to the row's own still reads as a swatch rather than as a
+ gap — and so that a value naming no colour at all is an empty swatch rather than nothing. */
+ box-shadow: inset 0 0 0 1px var(--main-border-color);
}
diff --git a/apps/client/src/widgets/collections/table/columns.spec.ts b/apps/client/src/widgets/collections/table/columns.spec.ts
index 505534e74e..6a61965e67 100644
--- a/apps/client/src/widgets/collections/table/columns.spec.ts
+++ b/apps/client/src/widgets/collections/table/columns.spec.ts
@@ -200,6 +200,34 @@ describe("buildColumnDefinitions — colour columns", () => {
expect(success).toHaveBeenCalledWith("");
});
+ it("shows a colour as a swatch, naming it in the tooltip, and an unset one as nothing", () => {
+ const [ column ] = buildColumnDefinitions({
+ info: [ { name: "tint", type: "color" } ],
+ movableRows: false,
+ existingColumnData: undefined,
+ rowNumberHint: 1
+ }).filter((candidate) => candidate.field === "labels.tint");
+
+ const formatter = column?.formatter;
+ if (typeof formatter !== "function") throw new Error("expected a formatter of its own");
+ const format = (value: unknown) =>
+ formatter({ getValue: () => value } as CellComponent, {}, () => {}) as HTMLElement;
+
+ // Filling the cell instead would paint over the row's own striping, hover and selection.
+ const swatch = format("#ff2e88");
+ expect(swatch.className).toBe("table-color-swatch");
+ expect(swatch.style.backgroundColor).toBe("#ff2e88");
+ expect(swatch.title).toBe("#ff2e88");
+
+ // A cell holds whatever the label does: text naming no colour keeps the swatch, which the
+ // browser leaves unpainted, and says what is stored where it can be read.
+ expect(format("nonsense").title).toBe("nonsense");
+
+ for (const empty of [ "", undefined, null ]) {
+ expect(format(empty).className).toBe("");
+ }
+ });
+
it("leaves a cell alone until a colour is picked, an opened picker being no decision", async () => {
// A colour input has no empty value, so it shows black over an unset cell; committing that on
// the way out would fill in a colour nobody chose.
diff --git a/apps/client/src/widgets/collections/table/columns.tsx b/apps/client/src/widgets/collections/table/columns.tsx
index 00624b589b..b2f41914fb 100644
--- a/apps/client/src/widgets/collections/table/columns.tsx
+++ b/apps/client/src/widgets/collections/table/columns.tsx
@@ -92,7 +92,7 @@ const labelTypeMappings: Record<ColumnType, Partial<ColumnDefinition>> = {
},
color: {
editor: wrapEditor(ColorEditor),
- formatter: "color"
+ formatter: wrapFormatter(ColorFormatter)
},
relation: {
editor: wrapEditor(RelationEditor),
@@ -479,6 +479,27 @@ function ColorEditor({ cell, success }: EditorOpts) {
);
}
+/**
+ * A colour cell as the swatch its editor shows, so that reading and editing a colour look alike, and
+ * so that the row keeps its own striping, hover and selection — all of which a cell flooded with the
+ * colour, as Tabulator's own colour formatter fills it, hides.
+ *
+ * The value itself is carried in the tooltip rather than set beside the swatch: a column of colours is
+ * read by eye, and the text behind one is wanted only now and then — including where it names no
+ * colour at all, a cell holding whatever the label does, which the swatch alone could not say.
+ */
+function ColorFormatter({ cell }: FormatterOpts) {
+ const value = cell.getValue();
+ const color = typeof value === "string" ? value : "";
+ // A formatter hands back an element, so an unset cell is an empty one rather than nothing at all.
+ if (!color) return <span />;
+
+ // The colour is the one thing about the swatch that cannot be told beforehand; its shape and size
+ // are its class's. A value naming no colour is dropped by the browser, leaving the outline behind
+ // rather than showing it as a colour it is not.
+ return <span className="table-color-swatch" title={color} style={{ backgroundColor: color }} />;
+}
+
function RelationEditor({ cell, success }: EditorOpts) {
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => inputRef.current?.focus());