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/text editor/youtube preview: add a better style for error messages
Adorian Doran51e145a1d833
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex 902c579c62..c415c74a2a 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -225,6 +225,11 @@ textarea,color: var(--muted-text-color);}+.form-field-error {+ color: var(--dropdown-item-icon-destructive-color);+ font-size: 0.7em;+}+.form-group.disabled {opacity: 0.5;pointer-events: none;diff --git a/apps/client/src/widgets/dialogs/youtube_video.css b/apps/client/src/widgets/dialogs/youtube_video.cssindex b079e38fe8..2aad7d0597 100644--- a/apps/client/src/widgets/dialogs/youtube_video.css+++ b/apps/client/src/widgets/dialogs/youtube_video.css@@ -18,14 +18,12 @@:is(.modal.youtube-video-dialog, .youtube-video-balloon) {@container (min-width: 501px) {.youtube-video-url {- > .tn-card-option-label {- flex: 0 0 8em;- min-width: max-content;- }+ display: grid;+ grid-template-columns: minmax(8em, max-content) minmax(0, 1fr);+ row-gap: 4px;- > .form-control {- flex: 1 1 auto;- min-width: 0;+ > .form-field-error {+ grid-column: 2;}}}diff --git a/apps/client/src/widgets/dialogs/youtube_video.spec.tsx b/apps/client/src/widgets/dialogs/youtube_video.spec.tsxindex c37a229755..a4efb296e2 100644--- a/apps/client/src/widgets/dialogs/youtube_video.spec.tsx+++ b/apps/client/src/widgets/dialogs/youtube_video.spec.tsx@@ -82,17 +82,20 @@ describe("YouTubeVideoBalloon", () => {if (!form || !field) throw new Error("The balloon has no form.");expect(document.activeElement).toBe(field);expect(insertButton(container)?.disabled).toBe(true);- expect(description(container)).toBeUndefined();+ expect(errorOf(container)).toBeUndefined();await type(field, "https://example.com");expect(insertButton(container)?.disabled).toBe(true);- expect(description(container)).toBe("youtube_video.not_a_video");+ expect(errorOf(container)).toBe("youtube_video.not_a_video");+ expect(container.querySelector(".youtube-video-url .tn-card-option-description"))+ .toBeNull();+ expect(document.activeElement).toBe(field);await submit(form);expect(onInsert).not.toHaveBeenCalled();await type(field, "youtu.be/dQw4w9WgXcQ");expect(insertButton(container)?.disabled).toBe(false);- expect(description(container)).toBeUndefined();+ expect(errorOf(container)).toBeUndefined();await submit(form);expect(onInsert).toHaveBeenCalledExactlyOnceWith("https://youtu.be/dQw4w9WgXcQ");});@@ -189,9 +192,9 @@ function insertButton(container: HTMLElement) {return container.querySelector<HTMLButtonElement>("button[type=submit]");}-/** The text under the label of the URL field, if any. */-function description(container: HTMLElement) {- return container.querySelector(".youtube-video-url .tn-card-option-description")+/** The error shown under the URL field, if any. */+function errorOf(container: HTMLElement) {+ return container.querySelector(".youtube-video-url > input + .form-field-error")?.textContent ?? undefined;}diff --git a/apps/client/src/widgets/dialogs/youtube_video.tsx b/apps/client/src/widgets/dialogs/youtube_video.tsxindex b49db62af6..2373e88cbd 100644--- a/apps/client/src/widgets/dialogs/youtube_video.tsx+++ b/apps/client/src/widgets/dialogs/youtube_video.tsx@@ -139,7 +139,7 @@ function useYouTubeVideoForm() {className="youtube-video-url"name="youtube-video-url"label={t("add_link.url")}- description={text.trim() && !url ? t("youtube_video.not_a_video") : undefined}+ error={text.trim() && !url ? t("youtube_video.not_a_video") : undefined}><FormTextBoxinputRef={inputRef}diff --git a/apps/client/src/widgets/react/Card.spec.tsx b/apps/client/src/widgets/react/Card.spec.tsxindex a72d8b954f..67efcd4d6f 100644--- a/apps/client/src/widgets/react/Card.spec.tsx+++ b/apps/client/src/widgets/react/Card.spec.tsx@@ -1,4 +1,4 @@-import { ComponentChild } from "preact";+import { ComponentChild, render } from "preact";import { act } from "preact/test-utils";import { describe, expect, it, vi } from "vitest";@@ -101,6 +101,29 @@ describe("OptionCardSection", () => {expect(label?.querySelector(".tn-card-option-description")?.textContent).toBe("Wraps long lines.");});+ it("shows an error after the control, outside the label, keeping the control mounted", () => {+ const row = (error?: string) => (+ <OptionCardSection+ name="address" label="Address" description="Where it lives." error={error}+ >+ <input />+ </OptionCardSection>+ );+ const container = renderInto(row());+ const input = container.querySelector("input");+ expect(container.querySelector(".form-field-error")).toBeNull();++ act(() => render(row("Not an address."), container));+ const error = container.querySelector(".tn-card-option > .form-field-error");+ expect(error?.textContent).toBe("Not an address.");+ expect(error?.getAttribute("role")).toBe("alert");+ expect(error?.previousElementSibling).toBe(input);+ expect(container.querySelector("input")).toBe(input);+ expect(container.querySelector("label")?.getAttribute("for")).toBe(input?.id);+ expect(container.querySelector(".tn-card-option-description")?.textContent)+ .toBe("Where it lives.");+ });+it("holds a name made of several things together, rather than stacking its parts", () => {const container = renderInto(<OptionCardSectiondiff --git a/apps/client/src/widgets/react/Card.tsx b/apps/client/src/widgets/react/Card.tsxindex f67b25eb3b..871b983fd1 100644--- a/apps/client/src/widgets/react/Card.tsx+++ b/apps/client/src/widgets/react/Card.tsx@@ -174,6 +174,8 @@ export function CardSection(props: {children: ComponentChildren} & CardSectionPrexport interface OptionCardSectionProps extends CardSectionProps {label: ComponentChildren;description?: ComponentChildren;+ /** A validation message shown after the control, styled by `.form-field-error`. */+ error?: 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.@@ -210,7 +212,7 @@ export interface OptionCardSectionProps extends CardSectionProps {*/export function OptionCardSection(props: OptionCardSectionProps) {const {- label, description, name, stacked, inline, children, className, subSections,+ label, description, error, name, stacked, inline, children, className, subSections,filterExtraKeywords, ...rest} = props;const matched = useFilterMatch(label, description, filterExtraKeywords);@@ -251,6 +253,7 @@ export function OptionCardSection(props: OptionCardSectionProps) {</label>{bound ? cloneElement(children, { id }) : children}+ {error && <small className="form-field-error" role="alert">{error}</small>}</CardSection>;}