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
client/ui: add filtering support for Card and OptionCardSection
Adorian Doran275182bfd22a
diff --git a/apps/client/src/widgets/react/Card.css b/apps/client/src/widgets/react/Card.cssindex e26d7def88..ef08004b0b 100644--- a/apps/client/src/widgets/react/Card.css+++ b/apps/client/src/widgets/react/Card.css@@ -172,4 +172,17 @@ body.mobile .tn-card-option:has(> .btn, > .slider) {background-color: color-mix(in srgb, var(--card-background-color) calc(100% / (var(--tn-card-section-nesting-level) + 1)) , transparent);}}+}++/* A card the filter did not ask for by name is left with the settings that matched: whatever else it+ holds belongs to the card as a whole, and the card goes once no setting is left in it. Hidden+ rather than dropped, so that what is filtered out keeps its state and comes back untouched. */+.tn-card-filter-unmatched {+ > .tn-card-body > :not(.tn-card-option, .tn-card-section-nested) {+ display: none;+ }++ &:not(:has(> .tn-card-body > .tn-card-option)) {+ display: none;+ }}\ No newline at end of filediff --git a/apps/client/src/widgets/react/Card.spec.tsx b/apps/client/src/widgets/react/Card.spec.tsxindex 1b4611bcc3..dfd2032b86 100644--- a/apps/client/src/widgets/react/Card.spec.tsx+++ b/apps/client/src/widgets/react/Card.spec.tsx@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from "vitest";import { renderInto } from "../../test/render";import { Card, CardFrame, CardSection, OptionCardSection } from "./Card";+import { FilterProvider } from "./filter";describe("Card", () => {it("carries a heading, a sentence and controls of its own, and drops the heading line with nothing on it", () => {@@ -142,3 +143,81 @@ describe("a segment that leads somewhere", () => {expect(own.querySelector("a")?.hasAttribute("data-no-contained-navigation")).toBe(true);});});++describe("under a filter", () => {+ const settings = (+ <>+ <OptionCardSection className="wrapping" label="Word wrapping" description="Wraps long lines." />+ <OptionCardSection className="theme" label="Theme" />+ </>+ );++ it("shows a setting only while it matches, by its label or by its sentence", () => {+ const unfiltered = renderInto(settings);+ expect(unfiltered.querySelectorAll(".tn-card-option")).toHaveLength(2);++ const byLabel = renderInto(<FilterProvider query="theme">{settings}</FilterProvider>);+ expect(byLabel.querySelector(".wrapping")).toBeNull();+ expect(byLabel.querySelector(".theme")).not.toBeNull();++ const bySentence = renderInto(<FilterProvider query="long lines">{settings}</FilterProvider>);+ expect(bySentence.querySelector(".wrapping")).not.toBeNull();+ expect(bySentence.querySelector(".theme")).toBeNull();+ });++ it("hands a card that matched by name everything it holds", () => {+ const container = renderInto(+ <FilterProvider query="appearance">+ <Card heading="Appearance" description="How the app is drawn.">+ {settings}+ <CardSection className="picture">an illustrated choice</CardSection>+ </Card>+ </FilterProvider>+ );++ expect(container.querySelector(".tn-card")?.className).not.toContain("tn-card-filter-unmatched");+ expect(container.querySelectorAll(".tn-card-option")).toHaveLength(2);+ expect(container.querySelector(".picture")).not.toBeNull();+ });++ it("marks a card that did not match, so CSS can take it down to the settings that did", () => {+ const container = renderInto(+ <FilterProvider query="theme">+ <Card heading="Appearance">+ {settings}+ <CardSection className="picture">an illustrated choice</CardSection>+ </Card>+ </FilterProvider>+ );++ expect(container.querySelector(".tn-card")?.className).toContain("tn-card-filter-unmatched");+ expect(container.querySelector(".wrapping")).toBeNull();+ expect(container.querySelector(".theme")).not.toBeNull();+ // Still rendered: hiding it is the CSS rule's job, which happy-dom does not apply.+ expect(container.querySelector(".picture")).not.toBeNull();+ });++ it("leaves a card unmarked while nothing is being filtered", () => {+ const container = renderInto(<Card heading="Appearance">{settings}</Card>);++ 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(+ <FilterProvider query={query}>+ <Card heading="Appearance">+ <OptionCardSection label="Custom fonts" subSections={subSections} 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.tsxindex 095fab9796..c5cb9f67b1 100644--- a/apps/client/src/widgets/react/Card.tsx+++ b/apps/client/src/widgets/react/Card.tsx@@ -4,6 +4,7 @@ import { JSX, HTMLAttributes } from "preact";import { useContext } from "preact/hooks";import clsx from "clsx";+import { FilterPassthrough, useFilterMatch, useIsFiltering } from "./filter";import { useUniqueName } from "./hooks";// #region Card Frame@@ -44,15 +45,28 @@ export interface CardProps {actions?: ComponentChildren;}+/**+ * A titled group of sections.+ *+ * Under a `FilterProvider`, a card matching by its heading or description shows everything it holds,+ * since asking for the group is asking for its contents. One that does not match keeps only the+ * sections that matched on their own, and disappears when none did (both through CSS, so nothing is+ * torn down and rebuilt as the query changes).+ */export function Card(props: {children: ComponentChildren} & CardProps) {- return <div className={clsx("tn-card", props.className)}>+ const matched = useFilterMatch(props.heading, props.description);+ const filtering = useIsFiltering();++ return <div className={clsx("tn-card", props.className, {+ "tn-card-filter-unmatched": filtering && !matched+ })}>{(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}+ <FilterPassthrough when={matched}>{props.children}</FilterPassthrough></div></div>;}@@ -149,15 +163,24 @@ export interface OptionCardSectionProps extends CardSectionProps {/*** 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.+ *+ * 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.*/export function OptionCardSection(props: OptionCardSectionProps) {- const {label, description, name, stacked, children, className, ...rest} = props;+ const {label, description, name, stacked, children, className, subSections, ...rest} = props;+ const matched = useFilterMatch(label, description);const id = useUniqueName(name);const bound = !!name && isValidElement(children);+ if (!matched) return null;+return <CardSection className={clsx("tn-card-option", className, {"tn-card-option-stacked": stacked- })} {...rest}>+ })}+ subSections={subSections && <FilterPassthrough>{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 andwould otherwise stack whatever the name is made of too — a badge marking which