Skip to content

Build your personal knowledge base with Trilium Notes

client/ui/filtering: add support for companion card sections

Adorian Doran0c45caa3b241
diff --git a/apps/client/src/widgets/react/Card.css b/apps/client/src/widgets/react/Card.css
index 5993b45096..b4f5aa0e5f 100644
--- a/apps/client/src/widgets/react/Card.css
+++ b/apps/client/src/widgets/react/Card.css
@@ -174,23 +174,20 @@ body.mobile .tn-card-option:has(> .btn, > .slider) {
}
}
-/* 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:not(.tn-card-option-filter-unmatched))) {
+/* A card the filter did not ask for by name is left with what matched and what accompanies it:
+ whatever else it holds belongs to the card as a whole. Details nested under a setting come with
+ it, whether or not they take part in a filter themselves. (An emptied card goes by the rule in
+ filter.css.) Hidden rather than dropped, so that what is filtered out keeps its state. */
+.tn-card.tn-filter-scope > .tn-card-body {
+ > :not(.tn-filter-match, .tn-filter-companion, .tn-card-section-nested) {
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)) {
+ next to it is the one that matched. It has to be a detail: the next setting along, matched on its
+ own, says nothing about this one. */
+.tn-card-option.tn-filter-companion:not(:has(+ .tn-card-section-nested.tn-filter-match)) {
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 d998cd0ee4..c8390dc4f7 100644
--- a/apps/client/src/widgets/react/Card.spec.tsx
+++ b/apps/client/src/widgets/react/Card.spec.tsx
@@ -181,11 +181,27 @@ describe("under a filter", () => {
</FilterProvider>
);
- expect(classesOf(container, ".tn-card")).not.toContain("tn-card-filter-unmatched");
+ expect(classesOf(container, ".tn-card")).toContain("tn-filter-match");
expect(container.querySelectorAll(".tn-card-option")).toHaveLength(2);
expect(container.querySelector(".picture")).not.toBeNull();
});
+ it("keeps a section that accompanies the settings instead of being one", () => {
+ const card = (query: string) => renderInto(
+ <FilterProvider query={query}>
+ <Card heading="Appearance">
+ {settings}
+ <CardSection className="preview" filterRole="companion">a preview</CardSection>
+ </Card>
+ </FilterProvider>
+ );
+
+ // Stands beside the setting that matched, where a plain section would have been taken down.
+ expect(classesOf(card("theme"), ".preview")).toContain("tn-filter-companion");
+ // And is never a result of its own, so it cannot keep an emptied card alive.
+ expect(card("nothing at all").querySelector(".tn-filter-match")).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">
@@ -196,7 +212,7 @@ describe("under a filter", () => {
</FilterProvider>
);
- expect(classesOf(container, ".tn-card")).toContain("tn-card-filter-unmatched");
+ expect(classesOf(container, ".tn-card")).toContain("tn-filter-scope");
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.
@@ -206,7 +222,7 @@ describe("under a filter", () => {
it("leaves a card unmarked while nothing is being filtered", () => {
const container = renderInto(<Card heading="Appearance">{settings}</Card>);
- expect(classesOf(container, ".tn-card")).not.toContain("tn-card-filter-unmatched");
+ expect(classesOf(container, ".tn-card")).not.toContain("tn-filter-scope");
});
it("brings a setting's sub-sections along when the setting itself matched", () => {
@@ -215,7 +231,7 @@ describe("under a filter", () => {
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();
+ expect(container.querySelector(".tn-filter-companion")).toBeNull();
});
it("keeps a setting whose sub-section matched, so the detail is read under it", () => {
@@ -224,14 +240,14 @@ describe("under a filter", () => {
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(classesOf(container, ".fonts")).toContain("tn-card-option-filter-unmatched");
+ expect(classesOf(container, ".fonts")).toContain("tn-filter-companion");
});
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(classesOf(container, ".fonts")).toContain("tn-card-option-filter-unmatched");
+ expect(classesOf(container, ".fonts")).toContain("tn-filter-companion");
});
/** A setting with a single detail under it, the pairing the filter has to tell apart. */
diff --git a/apps/client/src/widgets/react/Card.tsx b/apps/client/src/widgets/react/Card.tsx
index 3778564431..8f024b44a8 100644
--- a/apps/client/src/widgets/react/Card.tsx
+++ b/apps/client/src/widgets/react/Card.tsx
@@ -4,7 +4,7 @@ import { JSX, HTMLAttributes } from "preact";
import { useContext } from "preact/hooks";
import clsx from "clsx";
-import { FilterPassthrough, useFilterMatch, useIsFiltering } from "./filter";
+import { type FilterRole, FilterPassthrough, filterRoleClass, useFilterMatch, useIsFiltering } from "./filter";
import { useUniqueName } from "./hooks";
// #region Card Frame
@@ -57,9 +57,8 @@ export function Card(props: {children: ComponentChildren} & CardProps) {
const matched = useFilterMatch(props.heading, props.description);
const filtering = useIsFiltering();
- return <div className={clsx("tn-card", props.className, {
- "tn-card-filter-unmatched": filtering && !matched
- })}>
+ return <div className={clsx("tn-card", props.className,
+ filterRoleClass(filtering && (matched ? "match" : "scope")))}>
{(props.heading || props.actions) && <h5 class="tn-card-heading">
{props.heading}
{props.actions && <span className="tn-card-heading-actions">{props.actions}</span>}
@@ -96,6 +95,12 @@ export interface CardSectionProps {
* the click before {@link CardSectionProps.onAction} ever saw it. Ignored without an `href`.
*/
noContainedNavigation?: boolean;
+ /**
+ * What this section is worth to a filter over the card, for a section that is not a setting.
+ * `companion` is the usual one: a preview of what the settings around it do, shown for as long
+ * as any of them is (see {@link FilterRole}).
+ */
+ filterRole?: FilterRole;
}
interface CardSectionContextType {
@@ -108,7 +113,7 @@ export function CardSection(props: {children: ComponentChildren} & CardSectionPr
const parentContext = useContext(CardSectionContext);
const nestingLevel = (parentContext && parentContext.nestingLevel + 1) ?? 0;
- const className = clsx("tn-card-section", props.className, {
+ const className = clsx("tn-card-section", props.className, filterRoleClass(props.filterRole), {
"tn-card-section-nested": nestingLevel > 0,
"tn-card-highlight-on-hover": props.highlightOnHover || props.onAction || props.href,
"tn-no-padding": props.noPadding
@@ -173,15 +178,17 @@ export interface OptionCardSectionProps extends CardSectionProps {
export function OptionCardSection(props: OptionCardSectionProps) {
const {label, description, name, stacked, children, className, subSections, ...rest} = props;
const matched = useFilterMatch(label, description);
+ const filtering = useIsFiltering();
const id = useUniqueName(name);
const bound = !!name && isValidElement(children);
if (!matched && !subSections) return null;
return <CardSection className={clsx("tn-card-option", className, {
- "tn-card-option-stacked": stacked,
- "tn-card-option-filter-unmatched": !matched
+ "tn-card-option-stacked": stacked
})}
+ // Kept for its details rather than for itself, which is what a companion is.
+ filterRole={filtering ? (matched ? "match" : "companion") : undefined}
subSections={subSections && (
<FilterPassthrough when={matched}>{subSections}</FilterPassthrough>
)}
diff --git a/apps/client/src/widgets/react/filter.css b/apps/client/src/widgets/react/filter.css
new file mode 100644
index 0000000000..37297303f5
--- /dev/null
+++ b/apps/client/src/widgets/react/filter.css
@@ -0,0 +1,11 @@
+/*
+ * What the parts of a filtered subtree are marked as, so that a component can say what its content
+ * is worth to a filter without owning the rules for it. See `FilterRole` in filter.tsx.
+ *
+ * A group holding nothing that matched is worth nothing, so it goes. Everything else follows from
+ * the marks alone, which is why only one rule is needed here: what a group considers its own chrome
+ * is the group's business, and stays with the component that knows its shape.
+ */
+.tn-filter-scope:not(:has(.tn-filter-match)) {
+ display: none;
+}
diff --git a/apps/client/src/widgets/react/filter.spec.tsx b/apps/client/src/widgets/react/filter.spec.tsx
index 933dbb2ea8..06878a6aed 100644
--- a/apps/client/src/widgets/react/filter.spec.tsx
+++ b/apps/client/src/widgets/react/filter.spec.tsx
@@ -5,8 +5,8 @@ import { describe, expect, it } from "vitest";
import { renderInto } from "../../test/render";
import {
- FilterPassthrough, FilterProvider, filterText, filterTokens, matchesFilter, useFilterMatch,
- useFilterState, useIsFiltering
+ FilterPassthrough, FilterProvider, filterRoleClass, filterText, filterTokens, matchesFilter,
+ useFilterMatch, useFilterState, useIsFiltering
} from "./filter";
describe("filterText", () => {
@@ -67,6 +67,17 @@ describe("matchesFilter", () => {
});
});
+describe("filterRoleClass", () => {
+ it("names the part content plays, and says nothing where it plays none", () => {
+ expect(filterRoleClass("match")).toBe("tn-filter-match");
+ expect(filterRoleClass("scope")).toBe("tn-filter-scope");
+ expect(filterRoleClass("companion")).toBe("tn-filter-companion");
+
+ expect(filterRoleClass(undefined)).toBeUndefined();
+ expect(filterRoleClass(false)).toBeUndefined();
+ });
+});
+
describe("useFilterMatch", () => {
it("renders everything when there is no filter, and only matches when there is one", () => {
const outside = renderInto(<Settings />);
diff --git a/apps/client/src/widgets/react/filter.tsx b/apps/client/src/widgets/react/filter.tsx
index e82f9951c7..71a661142e 100644
--- a/apps/client/src/widgets/react/filter.tsx
+++ b/apps/client/src/widgets/react/filter.tsx
@@ -1,3 +1,5 @@
+import "./filter.css";
+
import { type ComponentChildren, createContext, isValidElement } from "preact";
import { useContext, useMemo } from "preact/hooks";
@@ -116,6 +118,25 @@ export function useIsFiltering(): boolean {
return useContext(FilterContext) !== null;
}
+/**
+ * What a piece of content is worth to the filter around it, marked on the element for CSS to act on
+ * (see filter.css). A component states the part its content plays and is spared the rules:
+ *
+ * - `match`: content that matched, and so is a result in its own right.
+ * - `scope`: a group of content, which goes as soon as it holds no match.
+ * - `companion`: content that is no result of its own but belongs beside those that are, such as a
+ * preview of what the settings around it do. It is shown for as long as its group is, and never
+ * keeps that group alive by itself.
+ *
+ * The marks do nothing outside a filter, so a component may leave them on.
+ */
+export type FilterRole = "match" | "scope" | "companion";
+
+/** The class standing for a {@link FilterRole}, or nothing for content playing no part. */
+export function filterRoleClass(role: FilterRole | undefined | false) {
+ return role ? `tn-filter-${role}` : undefined;
+}
+
/**
* Splits a query into the terms to match by, lowercased and stripped of accents so that typing
* "Modele" finds "Modèle". Returns an empty array for a query holding no terms.