Skip to content

Build your personal knowledge base with Trilium Notes

refactor(quick_search): draw the search box in the component

Elian Doran24ac00b9acc2
diff --git a/apps/client/src/stylesheets/theme-next/shell.css b/apps/client/src/stylesheets/theme-next/shell.css
index 1500ddd478..b0565557c4 100644
--- a/apps/client/src/stylesheets/theme-next/shell.css
+++ b/apps/client/src/stylesheets/theme-next/shell.css
@@ -558,102 +558,6 @@ div.bookmark-folder-widget .note-link .tn-icon {
* QUICK SEARCH BOX
*/
-div.quick-search {
- --padding-top: 8px;
- --padding-inline-start: 8px;
- --padding-inline-end: 8px;
- --padding-bottom: 8px;
-
- position: relative;
- flex-direction: row-reverse;
- align-items: center;
- height: unset;
- contain: unset;
- padding: var(--padding-top) var(--padding-inline-end) var(--padding-bottom) var(--padding-inline-start);
-}
-
-div.quick-search,
-div.quick-search:hover,
-div.quick-search:focus-within {
- /* Prevent changes to background and outline when the state of the input group changes */
- background: transparent;
- outline: none;
-}
-
-div.quick-search::before {
- /* The background rectangle of the search box */
- position: absolute;
- content: "";
- top: var(--padding-top);
- inset-inline-start: var(--padding-inline-start);
- bottom: var(--padding-bottom);
- inset-inline-end: var(--padding-inline-end);
- z-index: 0;
- border: 2px solid transparent;
- border-radius: 6px;
- background: var(--quick-search-background);
- transition: background-color 200ms ease-in;
-}
-
-div.quick-search:hover:before {
- /* Hovered search box background rectangle */
- background: var(--quick-search-hover-background);
- transition: background-color 75ms ease-out;
-}
-
-div.quick-search:focus-within:before {
- /* Focused search box background rectangle */
- border-color: var(--quick-search-focus-border);
- background: var(--quick-search-focus-background);
- transition: background-color 100ms ease-out;
-}
-
-div.quick-search input {
- padding-inline-start: 15px !important;
- box-shadow: unset !important;
- background: transparent !important;
-}
-
-div.quick-search input::placeholder {
- color: var(--quick-search-color);
-}
-
-div.quick-search:focus-within input {
- color: var(--quick-search-focus-color) !important;
-}
-
-div.quick-search .search-button {
- display: flex;
- align-items: center;
- justify-content: center;
- width: 25px;
- height: 25px;
- margin-inline-end: 8px;
- border-radius: 50%;
- padding: 0;
- color: var(--quick-search-color) !important;
- transition: background-color 200ms ease-in !important;
-}
-
-div.quick-search .search-button:active {
- transform: scale(0.85);
-}
-
-div.quick-search:focus-within:has(input:not(:placeholder-shown)) .search-button {
- /* Matches when the input has a value and the focus is inside the search box */
- background: var(--left-pane-item-action-button-background) !important;
- color: var(--left-pane-item-action-button-color) !important;
- transition: background-color 500ms ease-out !important;
-}
-
-html body .quick-search:focus-within .search-button:hover,
-div.quick-search .search-button.show {
- /* Hover state */
- background: var(--left-pane-item-action-button-hover-background) !important;
- color: var(--left-pane-item-action-button-color) !important;
- transition: background-color 100ms ease-out !important;
-}
-
/* The results popup is portaled to `<body>`, outside `div.quick-search`. `:root` outranks the
popup's own rules in quick_search.css. */
:root .dropdown-menu.quick-search-menu {
diff --git a/apps/client/src/widgets/quick_search.css b/apps/client/src/widgets/quick_search.css
index 1631ec124a..6d58b9e991 100644
--- a/apps/client/src/widgets/quick_search.css
+++ b/apps/client/src/widgets/quick_search.css
@@ -1,11 +1,52 @@
+/* Colors come from the theme's `--quick-search-*` variables; the fallbacks cover the themes that
+ define none of them. */
.quick-search {
+ /* The room between the widget's edge and the box drawn inside it. */
+ --quick-search-inset: 8px;
+ --quick-search-button-active-background:
+ var(--left-pane-item-action-button-background, var(--hover-item-background-color));
+ --quick-search-button-hover-background:
+ var(--left-pane-item-action-button-hover-background, var(--hover-item-background-color));
+ --quick-search-button-active-color:
+ var(--left-pane-item-action-button-color, var(--main-text-color));
+
+ position: relative;
display: flex;
- padding: 10px 10px 10px 0px;
- height: 50px;
+ flex-direction: row-reverse;
+ align-items: center;
+ padding: var(--quick-search-inset);
- button, .search-string {
- border: 0;
- font-size: 100% !important;
+ /* `.component`, which `BasicWidget` adds, sets `contain: size` and would collapse the box. */
+ &.component {
+ contain: unset;
+ }
+
+ /* The box. Drawn inside the padding, which stays part of the widget. */
+ &::before {
+ content: "";
+ position: absolute;
+ inset: var(--quick-search-inset);
+ z-index: 0;
+ border: 2px solid transparent;
+ border-radius: 6px;
+ background: var(--quick-search-background, var(--accented-background-color));
+ transition: background-color 200ms ease-in;
+ }
+
+ &:hover::before {
+ background: var(--quick-search-hover-background, var(--accented-background-color));
+ transition: background-color 75ms ease-out;
+ }
+
+ &:focus-within::before {
+ border-color: var(--quick-search-focus-border, var(--main-border-color));
+ background: var(--quick-search-focus-background, var(--main-background-color));
+ transition: background-color 100ms ease-out;
+ }
+
+ /* Over the box. */
+ & > * {
+ position: relative;
}
.search-string {
@@ -13,6 +54,18 @@
flex: 1 1 auto;
align-items: center;
min-width: 0;
+ padding-inline-start: 15px;
+ border: 0;
+ font-size: 100% !important;
+
+ /* The box behind the field takes the place of the field's own fill and focus outline. */
+ &,
+ &:hover,
+ &:focus-within {
+ background: transparent;
+ outline: none;
+ box-shadow: none;
+ }
.cm-editor {
width: 100%;
@@ -22,6 +75,48 @@
.cm-line {
white-space: pre;
}
+
+ .cm-placeholder {
+ color: var(--quick-search-color, var(--muted-text-color));
+ }
+ }
+
+ &:focus-within .search-string {
+ color: var(--quick-search-focus-color, var(--main-text-color));
+ }
+
+ /* `!important` outranks Bootstrap's `.btn` state rules (`:hover`, `:active`, `.show`). */
+ .search-button {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ width: 25px;
+ height: 25px;
+ margin-inline-end: 8px;
+ padding: 0;
+ border: 0;
+ border-radius: 50%;
+ background: transparent;
+ font-size: 100% !important;
+ color: var(--quick-search-color, var(--muted-text-color)) !important;
+ transition: background-color 200ms ease-in !important;
+
+ &:active {
+ transform: scale(0.85);
+ }
+ }
+
+ &.has-query:focus-within .search-button {
+ background: var(--quick-search-button-active-background) !important;
+ color: var(--quick-search-button-active-color) !important;
+ transition: background-color 500ms ease-out !important;
+ }
+
+ &:focus-within .search-button:hover,
+ .search-button.show {
+ background: var(--quick-search-button-hover-background) !important;
+ color: var(--quick-search-button-active-color) !important;
+ transition: background-color 100ms ease-out !important;
}
}
@@ -118,7 +213,8 @@
/* Uses the same highlight variables as SearchResultsList.css and ListOrGridView.css. */
:is(.search-result-title, .search-result-content, .search-result-attributes) b {
background: var(--note-list-view-search-result-highlight-background, transparent);
- color: var(--note-list-view-search-result-highlight-color, var(--admonition-warning-accent-color));
+ color:
+ var(--note-list-view-search-result-highlight-color, var(--admonition-warning-accent-color));
font-weight: 600;
text-decoration: underline;
diff --git a/apps/client/src/widgets/quick_search.spec.tsx b/apps/client/src/widgets/quick_search.spec.tsx
index ca9138be1b..2fd6e5e4a7 100644
--- a/apps/client/src/widgets/quick_search.spec.tsx
+++ b/apps/client/src/widgets/quick_search.spec.tsx
@@ -22,17 +22,24 @@ describe("QuickSearch", () => {
vi.restoreAllMocks();
});
- it("renders the search field and focuses it on the quickSearch shortcut", async () => {
+ it("renders the search field, focuses it on the quickSearch shortcut and marks a typed query", async () => {
const host = new Component();
const { container, editor } = await mount(host);
+ const root = container.querySelector(".quick-search");
- expect(container.querySelector(".quick-search .search-string .cm-editor")).not.toBeNull();
+ expect(root?.querySelector(".search-string .cm-editor")).not.toBeNull();
expect(editor.hasFocus).toBe(false);
await act(async () => {
await host.handleEvent("quickSearch", {});
});
expect(editor.hasFocus).toBe(true);
+
+ expect(root?.classList.contains("has-query")).toBe(false);
+ typeQuery(editor, "hello");
+ expect(root?.classList.contains("has-query")).toBe(true);
+ typeQuery(editor, "");
+ expect(root?.classList.contains("has-query")).toBe(false);
});
it("searches on Enter and lists the first results with their highlighted snippets", async () => {
diff --git a/apps/client/src/widgets/quick_search.tsx b/apps/client/src/widgets/quick_search.tsx
index 761366a6a7..0ad16b6163 100644
--- a/apps/client/src/widgets/quick_search.tsx
+++ b/apps/client/src/widgets/quick_search.tsx
@@ -2,6 +2,7 @@ import "./quick_search.css";
import type { FieldEditor } from "@triliumnext/codemirror/src/field_editor";
import type { QuickSearchResponse, SearchResultDetails } from "@triliumnext/commons";
+import clsx from "clsx";
import { useRef, useState } from "preact/hooks";
import { t } from "../services/i18n";
@@ -53,7 +54,7 @@ export default function QuickSearch() {
}
return (
- <div className="quick-search">
+ <div className={clsx("quick-search", searchString && "has-query")}>
<DropdownPanel
className="quick-search-toggle"
buttonClassName="search-button"