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
style(attributes): lay a list of short texts out as chips
Elian Doranc446856e536c
diff --git a/apps/client/src/widgets/react/FormTextBoxList.css b/apps/client/src/widgets/react/FormTextBoxList.cssindex b3c609055e..63d267715e 100644--- a/apps/client/src/widgets/react/FormTextBoxList.css+++ b/apps/client/src/widgets/react/FormTextBoxList.css@@ -1,21 +1,88 @@+/*+ * Laid out as chips rather than as a stack of fields: an option is one short word, so a full-width+ * boxed input per entry spends a whole row of the form on it and lists ten of them down the page.+ * A wrapping run of chips fits several to a line and shows the set as the set it is.+ */.form-textbox-list {+ --textbox-list-remove-size: 18px;+display: flex;- flex-direction: column;+ flex-wrap: wrap;+ align-items: center;gap: 4px;.form-textbox-list-row {display: flex;align-items: center;- gap: 4px;+ gap: 2px;+ /* Tighter at the trailing edge, where the remove button brings padding of its own. */+ padding-inline: 8px 3px;+ border-radius: var(--badge-radius, 12px);+ background-color: var(--hover-item-background-color);++ /* The chip carries the focus ring, its input having given up one of its own — otherwise a+ field's outline would be drawn inside a chip that already looks like a box. */+ &:focus-within {+ outline: 2px solid var(--input-focus-outline-color);+ }> input {- flex: 1;- min-width: 0;+ /* `form-control` is full-width by default; a chip is as wide as the word it holds. */+ width: auto;+ field-sizing: content;+ /* Also the width where `field-sizing` is not supported, the `size` attribute standing+ in for it there — see the component. */+ min-width: 4ch;+ /* The same ceiling the `size` attribute is capped at, so a long option scrolls inside+ its chip either way instead of taking a line to itself. */+ max-width: 30ch;+ padding: 3px 0;+ border: none;+ outline: none;+ font-size: inherit;++ &,+ &:hover,+ &:focus {+ background: transparent;+ }+ }++ > .form-textbox-list-remove {+ --icon-button-size: var(--textbox-list-remove-size);+ --icon-button-icon-ratio: 0.9;++ flex-shrink: 0;+ display: flex;+ align-items: center;+ justify-content: center;+ width: var(--textbox-list-remove-size);+ height: var(--textbox-list-remove-size);+ padding: 0;+ font-size: calc(var(--textbox-list-remove-size) * 0.9);+ line-height: 1;++ /* Blank until aimed at, where the theme's own hover treatment takes back over — the+ chip is the surface it sits on, not a button plate of its own. */+ &:not(:hover):not(:focus-visible) {+ background: transparent;+ }+ }++ /* Nothing to press where the list is read-only: the chips are then only a display of the+ set, and a button offering a removal that is not on the table would say otherwise. */+ > .form-textbox-list-remove:disabled {+ display: none;+ }++ /* With no button at the trailing edge, the chip pads itself as evenly as it started. */+ &:has(> .form-textbox-list-remove:disabled) {+ padding-inline-end: 8px;}}- /* Sized to its words: a full-width bar under the rows would read as a field of the list itself. */+ /* Last in the run rather than under it, so adding reads as extending the set in place. */.form-textbox-list-add {- align-self: flex-start;+ flex-shrink: 0;}}diff --git a/apps/client/src/widgets/react/FormTextBoxList.tsx b/apps/client/src/widgets/react/FormTextBoxList.tsxindex ce393e9a89..038606e461 100644--- a/apps/client/src/widgets/react/FormTextBoxList.tsx+++ b/apps/client/src/widgets/react/FormTextBoxList.tsx@@ -6,6 +6,10 @@ import ActionButton from "./ActionButton.jsx";import Button from "./Button.jsx";import FormTextBox from "./FormTextBox.jsx";+/** Room to start typing an option into an empty chip, and a ceiling so one long one cannot hog a line. */+const MIN_CHIP_CHARS = 6;+const MAX_CHIP_CHARS = 30;+interface FormTextBoxListProps {/*** Seeds the rows. The list keeps its own draft afterwards and does not resync — a host whose@@ -22,9 +26,10 @@ interface FormTextBoxListProps {}/**- * An ordered list of short texts, edited as one box per entry with a remove button and a button- * adding a fresh row. The rows are a draft of the values — a row just added or emptied stays put- * for typing while only the filled rows are reported — so a blank row never reaches the host.+ * An ordered list of short texts, edited as one chip per entry — each holding its own text and a+ * remove button, laid out in a wrapping run alongside a button adding a fresh one. The rows are a+ * draft of the values — a row just added or emptied stays put for typing while only the filled rows+ * are reported — so a blank row never reaches the host.*/export default function FormTextBoxList({ initialValues, disabled, onChange, addButtonText, removeButtonText }: FormTextBoxListProps) {// Ids of their own, so removing a row does not remount — and wipe the drafts of — those after it.@@ -57,11 +62,16 @@ export default function FormTextBoxList({ initialValues, disabled, onChange, add<div key={row.id} className="form-textbox-list-row"><FormTextBoxcurrentValue={row.value}+ // A chip is as wide as its own word. `field-sizing` does this exactly (see+ // the stylesheet); `size` is what browsers without it go by, and is why the+ // width is asked for in characters rather than measured.+ size={Math.min(Math.max(row.value.length + 1, MIN_CHIP_CHARS), MAX_CHIP_CHARS)}disabled={disabled}onChange={(value) => commitRows(rows.map((other) => other.id === row.id ? { ...other, value } : other))}/><ActionButton+ className="form-textbox-list-remove"icon="bx bx-x"text={removeButtonText}disabled={disabled}