Skip to content

Build your personal knowledge base with Trilium Notes

refactor(client): one ProgressBar for every progress bar in setup

Elian Doran83e85b7774bd
diff --git a/apps/client/src/setup.css b/apps/client/src/setup.css
index 0ac6bf2e18..c211712485 100644
--- a/apps/client/src/setup.css
+++ b/apps/client/src/setup.css
@@ -484,21 +484,8 @@ body.setup .bx-spin {
align-items: center;
gap: 0.5rem;
- progress {
- width: 100%;
- height: 1rem;
- border-radius: 0.5rem;
- overflow: hidden;
- appearance: none;
-
- &::-webkit-progress-bar {
- background-color: var(--main-border-color);
- }
-
- &::-webkit-progress-value {
- background-color: var(--main-text-color);
- transition: width 0.2s ease-out;
- }
+ .tn-progress-bar {
+ flex: 1;
}
span {
diff --git a/apps/client/src/setup.tsx b/apps/client/src/setup.tsx
index 50edeff939..9116177c6c 100644
--- a/apps/client/src/setup.tsx
+++ b/apps/client/src/setup.tsx
@@ -26,6 +26,7 @@ import FormGroup from "./widgets/react/FormGroup";
import { FormListItem } from "./widgets/react/FormList";
import FormTextBox from "./widgets/react/FormTextBox";
import Icon from "./widgets/react/Icon";
+import ProgressBar from "./widgets/react/ProgressBar";
import SetupPage from "./widgets/react/SetupPage";
import SlidePages from "./widgets/react/SlidePages";
@@ -448,7 +449,7 @@ export function SyncInProgress({ device, setState }: { device: "server" | "deskt
{s.label}
{s.key === "syncing" && (
<div class="sync-progress">
- <progress value={syncingDone ? 1 : pulled} max={syncingDone ? 1 : (stats.totalPullCount ?? 1)} />
+ <ProgressBar value={syncingDone ? 1 : pulled} max={syncingDone ? 1 : (stats.totalPullCount ?? 1)} />
<span>{progress}%</span>
</div>
)}
diff --git a/apps/client/src/setup_existing.css b/apps/client/src/setup_existing.css
index 7b9d14a569..23ee2bd6a2 100644
--- a/apps/client/src/setup_existing.css
+++ b/apps/client/src/setup_existing.css
@@ -42,7 +42,7 @@
/* Wide enough to read as a measure of the whole write, never wider than the page. */
width: min(100%, 27.5em);
- progress {
+ .tn-progress-bar {
flex: 1;
}
diff --git a/apps/client/src/setup_existing.spec.tsx b/apps/client/src/setup_existing.spec.tsx
index c39cfb52c3..4e9eace6ad 100644
--- a/apps/client/src/setup_existing.spec.tsx
+++ b/apps/client/src/setup_existing.spec.tsx
@@ -321,7 +321,8 @@ describe("backing it up first", () => {
statusAnswers({ state: "running", fraction: 0.42 });
await vi.advanceTimersByTimeAsync(1000);
- expect(container.querySelector("progress")?.value).toBeCloseTo(0.42);
+ expect(Number(container.querySelector("[role=progressbar]")?.getAttribute("aria-valuenow")))
+ .toBeCloseTo(0.42);
expect(container.textContent).toContain("42%");
// ...and steps aside once there is one, rather than the two competing.
expect(container.querySelector(".spinner-border")).toBeNull();
diff --git a/apps/client/src/setup_existing.tsx b/apps/client/src/setup_existing.tsx
index 1abeb7e575..d88f943ed4 100644
--- a/apps/client/src/setup_existing.tsx
+++ b/apps/client/src/setup_existing.tsx
@@ -20,6 +20,7 @@ import Button from "./widgets/react/Button";
import { Card, CardSection, OptionCardSection } from "./widgets/react/Card";
import FormRadioGroup from "./widgets/react/FormRadioGroup";
import Icon from "./widgets/react/Icon";
+import ProgressBar from "./widgets/react/ProgressBar";
import SetupPage from "./widgets/react/SetupPage";
import SlidePages from "./widgets/react/SlidePages";
@@ -303,7 +304,7 @@ export function ExistingDataBackingUp() {
{fraction !== null && (
<div class="existing-data-progress-bar">
- <progress value={fraction} max={1} />
+ <ProgressBar value={fraction} max={1} />
<span>{Math.floor(fraction * 100)}%</span>
</div>
)}
diff --git a/apps/client/src/setup_restore.css b/apps/client/src/setup_restore.css
index 13e210f755..2bca8e515b 100644
--- a/apps/client/src/setup_restore.css
+++ b/apps/client/src/setup_restore.css
@@ -90,8 +90,7 @@
gap: 0.5em;
}
- progress {
- width: 100%;
+ .tn-progress-bar {
margin-top: 0.75em;
}
@@ -140,7 +139,7 @@
never wider than the page it is centred on. */
width: min(100%, 27.5em);
- progress {
+ .tn-progress-bar {
flex: 1;
}
diff --git a/apps/client/src/setup_restore.spec.tsx b/apps/client/src/setup_restore.spec.tsx
index 257cc78ac9..cedde789c6 100644
--- a/apps/client/src/setup_restore.spec.tsx
+++ b/apps/client/src/setup_restore.spec.tsx
@@ -458,7 +458,7 @@ describe("uploading a backup", () => {
.toContain("setup.restore-upload-reconnecting");
expect(container.querySelector(".restore-upload-speed")).toBeFalsy();
// Still on the upload rather than thrown back to the picker: waiting is not failing.
- expect(container.querySelector("progress")).toBeTruthy();
+ expect(container.querySelector("[role=progressbar]")).toBeTruthy();
report({ sentBytes: 2048, totalBytes: 4096, fraction: 0.5, bytesPerSecond: 512, reconnecting: false });
await flushEffects();
@@ -588,8 +588,8 @@ describe("following the restore", () => {
restore = { stage: "staging", fraction: 0.42 };
await nextPoll();
- const bar = container.querySelector<HTMLProgressElement>(".restore-current-step progress");
- expect(bar?.value).toBe(0.42);
+ const bar = container.querySelector(".restore-current-step [role=progressbar]");
+ expect(bar?.getAttribute("aria-valuenow")).toBe("0.42");
expect(container.querySelector(".restore-stage-progress")?.textContent).toContain("42%");
});
@@ -598,14 +598,14 @@ describe("following the restore", () => {
restore = { stage: "staging", fraction: 0.9 };
await nextPoll();
- expect(container.querySelector(".restore-current-step progress")).toBeTruthy();
+ expect(container.querySelector(".restore-current-step [role=progressbar]")).toBeTruthy();
// Checking a database reads it from end to end with nothing to report along the way; the
// previous step's bar must not be left sitting under it.
restore = { stage: "validating" };
await nextPoll();
- expect(container.querySelector(".restore-current-step progress")).toBeFalsy();
+ expect(container.querySelector(".restore-current-step [role=progressbar]")).toBeFalsy();
});
it("finishes setup once the restore is done", async () => {
diff --git a/apps/client/src/setup_restore.tsx b/apps/client/src/setup_restore.tsx
index aa78f7e47f..704521f51b 100644
--- a/apps/client/src/setup_restore.tsx
+++ b/apps/client/src/setup_restore.tsx
@@ -15,6 +15,7 @@ import DatabaseFileBadges from "./widgets/react/DatabaseFileBadges";
import FormGroup from "./widgets/react/FormGroup";
import FormTextBox from "./widgets/react/FormTextBox";
import Icon from "./widgets/react/Icon";
+import ProgressBar from "./widgets/react/ProgressBar";
import SetupPage from "./widgets/react/SetupPage";
import SlidePages from "./widgets/react/SlidePages";
@@ -466,7 +467,7 @@ function UploadProgress({ upload }: { upload: UploadState }) {
<Icon icon="bx bx-loader-circle bx-spin" /> {t("setup.restore-uploading")}
</div>
- <progress value={upload.sentBytes} max={upload.totalBytes} />
+ <ProgressBar value={upload.sentBytes} max={upload.totalBytes} />
<div class="restore-progress-detail">
<span>
@@ -611,7 +612,7 @@ function RestoreProgress({ reported, onRestored, onFailed }: {
rather than an empty bar that never moves. */}
{shownFraction !== null && (
<div class="restore-stage-progress">
- <progress value={shownFraction} max={1} />
+ <ProgressBar value={shownFraction} max={1} />
<span>{Math.floor(shownFraction * 100)}%</span>
</div>
)}
diff --git a/apps/client/src/widgets/react/ProgressBar.css b/apps/client/src/widgets/react/ProgressBar.css
new file mode 100644
index 0000000000..12899f70a4
--- /dev/null
+++ b/apps/client/src/widgets/react/ProgressBar.css
@@ -0,0 +1,15 @@
+.tn-progress-bar {
+ height: 6px;
+ border-radius: 3px;
+ overflow: hidden;
+ background-color: var(--main-border-color);
+
+ &::after {
+ content: "";
+ display: block;
+ width: var(--tn-progress);
+ height: 100%;
+ background-color: var(--main-text-color);
+ transition: width 200ms ease-out;
+ }
+}
diff --git a/apps/client/src/widgets/react/ProgressBar.spec.tsx b/apps/client/src/widgets/react/ProgressBar.spec.tsx
new file mode 100644
index 0000000000..52a1c58ebf
--- /dev/null
+++ b/apps/client/src/widgets/react/ProgressBar.spec.tsx
@@ -0,0 +1,23 @@
+import { describe, expect, it } from "vitest";
+
+import { renderInto } from "../../test/render";
+import ProgressBar from "./ProgressBar";
+
+describe("ProgressBar", () => {
+ it("fills as far as the value goes, within the bar, and says so to assistive technology", () => {
+ const fill = (value: number, max: number) => {
+ const bar = renderInto(<ProgressBar value={value} max={max} />)
+ .querySelector<HTMLElement>("[role=progressbar]");
+ expect(bar).not.toBeNull();
+ return bar?.style.getPropertyValue("--tn-progress");
+ };
+
+ expect(fill(25, 200)).toBe("12.5%");
+ expect(fill(300, 200)).toBe("100%");
+ expect(fill(5, 0)).toBe("0%");
+
+ const bar = renderInto(<ProgressBar value={25} max={200} />).querySelector("[role=progressbar]");
+ expect(bar?.getAttribute("aria-valuenow")).toBe("25");
+ expect(bar?.getAttribute("aria-valuemax")).toBe("200");
+ });
+});
diff --git a/apps/client/src/widgets/react/ProgressBar.tsx b/apps/client/src/widgets/react/ProgressBar.tsx
new file mode 100644
index 0000000000..e5afd90d81
--- /dev/null
+++ b/apps/client/src/widgets/react/ProgressBar.tsx
@@ -0,0 +1,28 @@
+import "./ProgressBar.css";
+
+import clsx from "clsx";
+
+interface ProgressBarProps {
+ value: number;
+ max: number;
+ className?: string;
+}
+
+/**
+ * A thin bar showing how far a task has come. Use it instead of a `<progress>`, which each browser
+ * draws in a style of its own.
+ */
+export default function ProgressBar({ value, max, className }: ProgressBarProps) {
+ const fraction = max > 0 ? Math.min(1, Math.max(0, value / max)) : 0;
+
+ return (
+ <div
+ className={clsx("tn-progress-bar", className)}
+ role="progressbar"
+ aria-valuemin={0}
+ aria-valuemax={max}
+ aria-valuenow={value}
+ style={{ "--tn-progress": `${fraction * 100}%` }}
+ />
+ );
+}