Skip to content

Build your personal knowledge base with Trilium Notes

client/options: add support for nested card sections

Adorian Doran08390f4573e2
diff --git a/apps/client/src/widgets/react/Card.css b/apps/client/src/widgets/react/Card.css
index ef08004b0b..fbe19fa3b5 100644
--- a/apps/client/src/widgets/react/Card.css
+++ b/apps/client/src/widgets/react/Card.css
@@ -182,7 +182,15 @@ body.mobile .tn-card-option:has(> .btn, > .slider) {
display: none;
}
- &:not(:has(> .tn-card-body > .tn-card-option)) {
+ &:not(:has(> .tn-card-body > .tn-card-option:not(.tn-card-option-filter-unmatched))) {
display: none;
}
+}
+
+/* A setting kept only for the details under it stands on whether one of them is still there. They
+ follow it as siblings, and a detail that did not match leaves nothing behind, so the one left
+ next to it is the one that matched. Details that never take part in a filter (a plain section
+ rather than a setting) are not what it stands on, or it would stay for every search. */
+.tn-card-option-filter-unmatched:not(:has(+ .tn-card-option.tn-card-section-nested)) {
+ display: none;
}
\ No newline at end of file
diff --git a/apps/client/src/widgets/react/Card.spec.tsx b/apps/client/src/widgets/react/Card.spec.tsx
index dfd2032b86..96b9551d02 100644
--- a/apps/client/src/widgets/react/Card.spec.tsx
+++ b/apps/client/src/widgets/react/Card.spec.tsx
@@ -203,21 +203,44 @@ describe("under a filter", () => {
expect(container.querySelector(".tn-card")?.className).not.toContain("tn-card-filter-unmatched");
});
- it("keeps a setting's sub-sections with it, instead of filtering them on their own", () => {
- const subSections = [ <OptionCardSection key="size" className="size" label="Font size" /> ];
- const card = (query: string) => renderInto(
+ it("brings a setting's sub-sections along when the setting itself matched", () => {
+ const container = fontsCard("custom fonts");
+
+ expect(container.querySelector(".size")).not.toBeNull();
+ // The filter sits between the section and what it nests, so the indent must survive it.
+ expect(container.querySelector(".size")?.className).toContain("tn-card-section-nested");
+ expect(container.querySelector(".tn-card-option-filter-unmatched")).toBeNull();
+ });
+
+ it("keeps a setting whose sub-section matched, so the detail is read under what it belongs to", () => {
+ const container = fontsCard("font size");
+
+ expect(container.querySelector(".size")).not.toBeNull();
+ // Kept for the sub-section's sake rather than its own, which is what the mark says: CSS
+ // takes it down again once nothing is left beside it, and happy-dom applies no CSS.
+ expect(container.querySelector(".fonts")?.className).toContain("tn-card-option-filter-unmatched");
+ });
+
+ it("leaves a setting nothing to stand on once none of its sub-sections matched", () => {
+ const container = fontsCard("nothing at all");
+
+ expect(container.querySelector(".size")).toBeNull();
+ expect(container.querySelector(".fonts")?.className).toContain("tn-card-option-filter-unmatched");
+ });
+
+ /** A setting with a single detail under it, the pairing the filter has to tell apart. */
+ function fontsCard(query: string) {
+ return renderInto(
<FilterProvider query={query}>
<Card heading="Appearance">
- <OptionCardSection label="Custom fonts" subSections={subSections} subSectionsVisible />
+ <OptionCardSection
+ className="fonts"
+ label="Custom fonts"
+ subSections={[ <OptionCardSection key="size" className="size" label="Font size" /> ]}
+ subSectionsVisible
+ />
</Card>
</FilterProvider>
);
-
- const withParent = card("custom fonts");
- expect(withParent.querySelector(".size")).not.toBeNull();
- // The filter sits between the section and what it nests, so the indent must survive it.
- expect(withParent.querySelector(".size")?.className).toContain("tn-card-section-nested");
-
- expect(card("font size").querySelector(".size")).toBeNull();
- });
+ }
});
diff --git a/apps/client/src/widgets/react/Card.tsx b/apps/client/src/widgets/react/Card.tsx
index c5cb9f67b1..bbe02784eb 100644
--- a/apps/client/src/widgets/react/Card.tsx
+++ b/apps/client/src/widgets/react/Card.tsx
@@ -165,8 +165,10 @@ export interface OptionCardSectionProps extends CardSectionProps {
* trailing one, with the sentence explaining it below the label.
*
* Under a `FilterProvider`, the setting shows itself only while its label or description matches
- * what is being looked for, and its sub-sections come with it: they are details of this setting
- * rather than settings of their own, so they are neither shown without it nor matched separately.
+ * what is being looked for. A setting that matched brings its sub-sections with it, since they are
+ * details of it rather than settings of their own; one that did not stays only for as long as a
+ * sub-section of its own matched, so that the detail found is read under the setting it belongs to.
+ * That last part is settled in CSS, from whether any sub-section is still there.
*/
export function OptionCardSection(props: OptionCardSectionProps) {
const {label, description, name, stacked, children, className, subSections, ...rest} = props;
@@ -174,12 +176,13 @@ export function OptionCardSection(props: OptionCardSectionProps) {
const id = useUniqueName(name);
const bound = !!name && isValidElement(children);
- if (!matched) return null;
+ if (!matched && !subSections) return null;
return <CardSection className={clsx("tn-card-option", className, {
- "tn-card-option-stacked": stacked
+ "tn-card-option-stacked": stacked,
+ "tn-card-option-filter-unmatched": !matched
})}
- subSections={subSections && <FilterPassthrough>{subSections}</FilterPassthrough>}
+ subSections={subSections && <FilterPassthrough when={matched}>{subSections}</FilterPassthrough>}
{...rest}>
<label className="tn-card-option-label" for={bound ? id : undefined}>
{/* Held together as one thing, because the label stacks the sentence under the name and
diff --git a/apps/client/src/widgets/type_widgets/options/search_page.css b/apps/client/src/widgets/type_widgets/options/search_page.css
index 606f4976de..e460bdc14f 100644
--- a/apps/client/src/widgets/type_widgets/options/search_page.css
+++ b/apps/client/src/widgets/type_widgets/options/search_page.css
@@ -38,10 +38,11 @@
*
* Something is left when a card matched by name, or when a single setting inside one did — the same
* test decides whether a block is worth keeping, whether a page is worth naming, and whether the
- * search came back with anything at all.
+ * search came back with anything at all. A setting kept only for the details under it does not
+ * count, since it is shown for their sake and goes with them (see `Card.css`).
*/
-.options-search-page > :not(.tn-card, .options-search-page-title):not(:has(.tn-card:not(.tn-card-filter-unmatched), .tn-card-option)),
-.options-search-page:not(:has(.tn-card:not(.tn-card-filter-unmatched), .tn-card-option)),
-.options-search-results:has(.tn-card:not(.tn-card-filter-unmatched), .tn-card-option) .options-search-empty {
+.options-search-page > :not(.tn-card, .options-search-page-title):not(:has(.tn-card:not(.tn-card-filter-unmatched), .tn-card-option:not(.tn-card-option-filter-unmatched))),
+.options-search-page:not(:has(.tn-card:not(.tn-card-filter-unmatched), .tn-card-option:not(.tn-card-option-filter-unmatched))),
+.options-search-results:has(.tn-card:not(.tn-card-filter-unmatched), .tn-card-option:not(.tn-card-option-filter-unmatched)) .options-search-empty {
display: none;
}