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
ui/cards: add support for a description
Adorian Doranb1dc18095637
diff --git a/apps/client/src/widgets/react/Card.css b/apps/client/src/widgets/react/Card.cssindex 1e4075a7af..563c681333 100644--- a/apps/client/src/widgets/react/Card.css+++ b/apps/client/src/widgets/react/Card.css@@ -30,6 +30,51 @@text-transform: uppercase;}+.tn-card-heading:has(+ .tn-card-description) {+ margin-bottom: 4px;+}++.tn-card-description {+ margin-bottom: 10px;+ color: var(--muted-text-color);+ font-size: .9em;+}++.tn-card-option {+ display: flex;+ align-items: center;+ gap: 8px;++ /* The controls keep their natural width. Left to shrink, a button's nowrap label spills across+ its own padding and ends up reading as though it had none; the label below gives instead. */+ > * {+ flex-shrink: 0;+ }++ .tn-card-option-label {+ display: flex;+ flex: 1 1 auto;+ flex-direction: column;+ gap: 2px;+ margin-bottom: 0;+ min-width: 0;++ /* Tied to a control, so that the text operates it too. */+ &[for] {+ cursor: pointer;+ }+ }++ .tn-card-option-description {+ font-size: .85em;+ opacity: .7;+ }++ .btn {+ white-space: nowrap;+ }+}+.tn-card-body {display: flex;flex-direction: column;diff --git a/apps/client/src/widgets/react/Card.tsx b/apps/client/src/widgets/react/Card.tsxindex d16e1f83e4..d0b52217a7 100644--- a/apps/client/src/widgets/react/Card.tsx+++ b/apps/client/src/widgets/react/Card.tsx@@ -1,9 +1,11 @@import "./Card.css";-import { ComponentChildren, createContext } from "preact";+import { cloneElement, ComponentChildren, createContext, isValidElement } from "preact";import { JSX, HTMLAttributes } from "preact";import { useContext } from "preact/hooks";import clsx from "clsx";+import { useUniqueName } from "./hooks";+// #region Card Frameexport interface CardFrameProps extends HTMLAttributes<HTMLDivElement> {@@ -29,11 +31,14 @@ export function CardFrame({className, highlightOnHover, children, ...rest}: Cardexport interface CardProps {className?: string;heading?: string;+ /** Sentence introducing the card, shown between the heading and the first section. */+ description?: ComponentChildren;}export function Card(props: {children: ComponentChildren} & CardProps) {return <div className={clsx("tn-card", props.className)}>{props.heading && <h5 class="tn-card-heading">{props.heading}</h5>}+ {props.description && <p className="tn-card-description">{props.description}</p>}<div className="tn-card-body">{props.children}</div>@@ -82,4 +87,38 @@ export function CardSection(props: {children: ComponentChildren} & CardSectionPr</>;}+// #endregion++// #region Card Option++export interface CardOptionProps extends CardSectionProps {+ label: ComponentChildren;+ description?: ComponentChildren;+ /**+ * Binds the label to the control, so that clicking the text operates it. Only a single element+ * child can be bound, which covers the usual case of one toggle or one input per option.+ */+ name?: string;+ /** The controls the option is operated with, placed on the trailing edge. */+ children?: ComponentChildren;+}++/**+ * A card section built as one setting: what it is on the leading edge, what changes it on the+ * trailing one, with the sentence explaining it below the label.+ */+export function CardOption({label, description, name, children, className, ...rest}: CardOptionProps) {+ const id = useUniqueName(name);+ const bound = !!name && isValidElement(children);++ return <CardSection className={clsx("tn-card-option", className)} {...rest}>+ <label className="tn-card-option-label" for={bound ? id : undefined}>+ {label}+ {description && <small className="tn-card-option-description">{description}</small>}+ </label>++ {bound ? cloneElement(children, { id }) : children}+ </CardSection>;+}+// #endregion\ No newline at end of file