Skip to content

Build your personal knowledge base with Trilium Notes

ui/cards: add support for help buttons

Adorian Doranb67c85473f3d
diff --git a/apps/client/src/widgets/react/Card.css b/apps/client/src/widgets/react/Card.css
index 1e4075a7af..963460facc 100644
--- a/apps/client/src/widgets/react/Card.css
+++ b/apps/client/src/widgets/react/Card.css
@@ -23,6 +23,8 @@
}
.tn-card-heading {
+ display: flex;
+ align-items: center;
margin-bottom: 10px;
font-size: .75rem;
font-weight: 600;
@@ -30,6 +32,60 @@
text-transform: uppercase;
}
+/* Kept at the far end of the heading line, where the options page's own sections keep theirs, and
+ sized against the heading rather than against a full-height toolbar. */
+.tn-card-heading-actions {
+ --icon-button-size: 28px;
+ display: flex;
+ gap: 4px;
+ margin-inline-start: auto;
+}
+
+.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.tsx
index d16e1f83e4..0ad9fd3337 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 Frame
export interface CardFrameProps extends HTMLAttributes<HTMLDivElement> {
@@ -29,11 +31,26 @@ export function CardFrame({className, highlightOnHover, children, ...rest}: Card
export interface CardProps {
className?: string;
heading?: string;
+ /** Sentence introducing the card, shown between the heading and the first section. */
+ description?: ComponentChildren;
+ /**
+ * Controls for the card as a whole, kept at the far end of its heading — a help mark, or a
+ * button that adds to what the card holds.
+ *
+ * Taken as children rather than named one by one, so that the card needs no import of its own
+ * for them: `Card` is in the login and setup bundles, which have no business pulling in the app
+ * a help mark would reach for.
+ */
+ actions?: 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.heading || props.actions) && <h5 class="tn-card-heading">
+ {props.heading}
+ {props.actions && <span className="tn-card-heading-actions">{props.actions}</span>}
+ </h5>}
+ {props.description && <p className="tn-card-description">{props.description}</p>}
<div className="tn-card-body">
{props.children}
</div>
@@ -82,4 +99,39 @@ 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(props: CardOptionProps) {
+ const {label, description, name, children, className, ...rest} = props;
+ 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