Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(table): flood a read-only colour chip with the colour itself
Elian Doran346c5458a5dd
diff --git a/apps/client/src/widgets/attribute_widgets/label_value_display.css b/apps/client/src/widgets/attribute_widgets/label_value_display.cssindex 0bc3ce2a0a..9c256c853b 100644--- a/apps/client/src/widgets/attribute_widgets/label_value_display.css+++ b/apps/client/src/widgets/attribute_widgets/label_value_display.css@@ -23,6 +23,19 @@opacity: 0.55;}+/*+ * A read-only chip that is the colour it stands for. Holding nothing — no text, no button — it has+ * nothing to hold it open, so it is sized to show a stretch of the colour at the height the swatch+ * takes. Ringed within like the swatch, so that a colour close to the field beneath still reads as a+ * chip — and so that a value naming no colour, whose ground the browser refuses, is an empty ring+ * rather than a colour it is not.+ */+.label-color-chip {+ min-width: 2.5em;+ min-height: 1.25em;+ box-shadow: inset 0 0 0 1px var(--main-border-color);+}+/** A colour as its own square. Sized to the line rather than to a field's own height: a swatch as tall* as that would make every row of a table taller, rows being of one height whether there are coloursdiff --git a/apps/client/src/widgets/attribute_widgets/label_value_display.tsx b/apps/client/src/widgets/attribute_widgets/label_value_display.tsxindex eea9107255..25eacf5193 100644--- a/apps/client/src/widgets/attribute_widgets/label_value_display.tsx+++ b/apps/client/src/widgets/attribute_widgets/label_value_display.tsx@@ -78,9 +78,10 @@ function renderFlag(value: string) {}/**- * A colour as the square it is read as, wherever one is shown: a field of its own, or a chip in a- * field holding several. Shown as a swatch rather than by flooding what holds it with the colour, so- * that a table row keeps its own striping, hover and selection.+ * A colour as the square it is read as where the colour cannot be the whole ground: a single-valued+ * cell, whose surface is the row's — flooding it would hide striping, hover and selection — and a+ * chip being edited, which also holds the button removing it, a mark no user-picked ground can be+ * trusted to keep visible. A chip only being read holds neither, and wears the colour itself.** 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@@ -103,7 +104,15 @@ export function LabelValueChips({ values, labelType, className }: {return (<span className={clsx("label-value-chips", className)}>{values.map((value) => (- <span key={value} className="tn-chip"><span>{renderLabelValue(value, labelType)}</span></span>+ labelType === "color" ? (+ // Read-only, the chip holds nothing — no text, no button — so the colour can be+ // its whole ground, the stored text kept to the tooltip. Set as the property so+ // that text naming no colour is dropped by the browser, leaving the chip's own+ // ground rather than showing a colour it is not.+ <span key={value} className="tn-chip label-color-chip" title={value} style={{ backgroundColor: value }} />+ ) : (+ <span key={value} className="tn-chip"><span>{renderLabelValue(value, labelType)}</span></span>+ )))}</span>);diff --git a/apps/client/src/widgets/attribute_widgets/multi_value_input.tsx b/apps/client/src/widgets/attribute_widgets/multi_value_input.tsxindex d3bfc5de79..ae8a62647f 100644--- a/apps/client/src/widgets/attribute_widgets/multi_value_input.tsx+++ b/apps/client/src/widgets/attribute_widgets/multi_value_input.tsx@@ -1,7 +1,7 @@import { type LabelType } from "@triliumnext/commons";import { t } from "../../services/i18n";-import { ColorSwatch, renderLabelValue } from "./label_value_display";+import { renderLabelValue } from "./label_value_display";import { SelectValuesInput } from "./select_input";import ValuesInput from "./values_input";@@ -60,9 +60,6 @@ export default function MultiValueInput({ labelType, values, onCommit, options,labelType={labelType}values={values}placeholder={t("promoted_attributes.values_placeholder")}- // A colour reads as its swatch even while being edited, `#3d5a80` naming nothing to the- // eye. The rest show what is stored, which is what is being edited.- renderValue={labelType === "color" ? renderColorValue : undefined}inputId={inputId}tabIndex={tabIndex}disabled={disabled}@@ -77,7 +74,3 @@ export const BOOLEAN_OPTIONS = [ "true", "false" ];function renderBooleanValue(value: string) {return renderLabelValue(value, "boolean");}--function renderColorValue(value: string) {- return <ColorSwatch color={value} />;-}diff --git a/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx b/apps/client/src/widgets/attribute_widgets/values_input.spec.tsxindex 335a9bf0e7..b3766f1fd0 100644--- a/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx+++ b/apps/client/src/widgets/attribute_widgets/values_input.spec.tsx@@ -121,15 +121,12 @@ describe("ValuesInput", () => {it("takes a colour when the pick is settled, not through the drag, and shows it as given", async () => {const onCommit = vi.fn();- const input = await mount({- labelType: "color",- values: [ "#ff0000" ],- onCommit,- renderValue: (value) => <i data-color={value} />- });+ const input = await mount({ labelType: "color", values: [ "#ff0000" ], onCommit });- // Stored as text a picker cannot show, so the chip is whatever the host makes of it.- expect(container.querySelector(".tn-chip i")?.getAttribute("data-color")).toBe("#ff0000");+ // Stored as text a picker cannot show, so the chip holds the value's own swatch — a swatch+ // rather than the chip's whole ground, this chip also holding the button removing it.+ expect(container.querySelector(".tn-chip .label-color-swatch")?.getAttribute("title"))+ .toBe("#ff0000");expect(input?.type).toBe("color");// Nothing may be written into the picker while the pick is being made: the browser takes itsdiff --git a/apps/client/src/widgets/attribute_widgets/values_input.tsx b/apps/client/src/widgets/attribute_widgets/values_input.tsxindex b9575e42bc..183b70d35e 100644--- a/apps/client/src/widgets/attribute_widgets/values_input.tsx+++ b/apps/client/src/widgets/attribute_widgets/values_input.tsx@@ -9,6 +9,7 @@ import { t } from "../../services/i18n";import ActionButton from "../react/ActionButton";import Chip from "../react/Chip";import FormTextBox from "../react/FormTextBox";+import { ColorSwatch } from "./label_value_display";import { DEFAULT_COLOR, LABEL_MAPPINGS } from "./label_value_input";interface ValuesInputProps {@@ -18,11 +19,6 @@ interface ValuesInputProps {values: readonly string[];/** Receives the values as they now stand, whenever one is taken or dropped. */onCommit(values: string[]): void;- /**- * Shows a value as something other than the text it is stored as — a colour as its swatch. Left- * out, a chip reads as what is stored, which for most types is what is being edited anyway.- */- renderValue?(value: string): ComponentChildren;/** What removing a chip means, in the host's own words. Left out, a chip holds "a value". */removeButtonText?: string;/** What taking the box's content means, in the host's own words. Left out, it adds "a value". */@@ -44,7 +40,7 @@ interface ValuesInputProps {* rather than quietly dropped. A value already held is not taken a second time: the chips are a set,* and two alike could not be told apart.*/-export default function ValuesInput({ labelType, values, onCommit, renderValue, removeButtonText, addButtonText, inputId, tabIndex, placeholder, disabled }: ValuesInputProps) {+export default function ValuesInput({ labelType, values, onCommit, removeButtonText, addButtonText, inputId, tabIndex, placeholder, disabled }: ValuesInputProps) {const [ draft, setDraft ] = useState("");const inputRef = useRef<HTMLInputElement>(null);const isColor = labelType === "color";@@ -123,7 +119,11 @@ export default function ValuesInput({ labelType, values, onCommit, renderValue,disabled={disabled}onRemove={() => drop(value)}>- <span>{renderValue ? renderValue(value) : value}</span>+ {/* A colour reads as its swatch even while being edited, `#3d5a80` naming+ nothing to the eye. A swatch rather than the chip's own ground, as the+ read-only chips wear it: this chip also holds the button removing it,+ which no ground a user may pick can be trusted to keep visible. */}+ <span>{isColor ? <ColorSwatch color={value} /> : value}</span></Chip>))}{/* The box and the button that asks for what it holds are one thing on the page, so theydiff --git a/apps/client/src/widgets/collections/table/columns.spec.ts b/apps/client/src/widgets/collections/table/columns.spec.tsindex dd1326b7a7..7d0c4548a3 100644--- a/apps/client/src/widgets/collections/table/columns.spec.ts+++ b/apps/client/src/widgets/collections/table/columns.spec.ts@@ -390,8 +390,9 @@ describe("buildColumnDefinitions — multi-valued columns", () => {expect(flags.querySelector(".label-flag-set")?.className).toContain("bx-check");expect(flags.querySelector(".label-flag-unset")?.className).toContain("bx-x");- // And a colour as the same swatch a single one is shown by.- expect(format("color", [ "#ff2e88" ]).querySelector(".label-color-swatch")?.getAttribute("title"))+ // And a colour floods the chip itself — read-only, the chip holds nothing the colour could+ // hide — with the stored text kept to the tooltip.+ expect(format("color", [ "#ff2e88" ]).querySelector(".label-color-chip")?.getAttribute("title")).toBe("#ff2e88");});