Skip to content

Write HTML. Render video. Built for agents.

perf(studio): stop sorting the font list while the font picker is closed (#4769)

Miguel Ángel0f390167df97
diff --git a/packages/studio/src/components/editor/propertyPanelFont.test.tsx b/packages/studio/src/components/editor/propertyPanelFont.test.tsx
index fe4d32a684..32afedc3a3 100644
--- a/packages/studio/src/components/editor/propertyPanelFont.test.tsx
+++ b/packages/studio/src/components/editor/propertyPanelFont.test.tsx
@@ -2,13 +2,32 @@
import React, { act } from "react";
import { createRoot } from "react-dom/client";
-import { afterEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { FontFamilyField } from "./propertyPanelFont";
+import { sortFontOptions } from "./propertyPanelHelpers";
+
+vi.mock("./propertyPanelHelpers", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("./propertyPanelHelpers")>();
+ return { ...actual, sortFontOptions: vi.fn(actual.sortFontOptions) };
+});
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+// The font APIs are stubbed and every family is off the Google lists, so no test reaches the network.
+beforeEach(() => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async (url: string) => ({
+ ok: true,
+ json: async () => ({ fonts: url.includes("google") ? ["Roboto Slab"] : ["Arial"] }),
+ })),
+ );
+});
+
afterEach(() => {
document.body.innerHTML = "";
+ document.head.innerHTML = "";
+ vi.unstubAllGlobals();
});
describe("FontFamilyField flat trigger", () => {
@@ -17,14 +36,38 @@ describe("FontFamilyField flat trigger", () => {
document.body.append(host);
const root = createRoot(host);
act(() => {
- root.render(
- <FontFamilyField flat value="JetBrains Mono" importedFonts={[]} onCommit={vi.fn()} />,
- );
+ root.render(<FontFamilyField flat value="Georgia" importedFonts={[]} onCommit={vi.fn()} />);
});
const trigger = host.querySelector<HTMLButtonElement>('[data-flat-font-trigger="true"]');
expect(trigger).not.toBeNull();
expect(trigger?.className).not.toContain("border-neutral-800");
- expect(host.textContent).toContain("JetBrains Mono");
+ expect(host.textContent).toContain("Georgia");
act(() => root.unmount());
});
});
+
+describe("FontFamilyField font list", () => {
+ it("builds the list only while the dropdown is open", async () => {
+ vi.mocked(sortFontOptions).mockClear();
+ const host = document.createElement("div");
+ document.body.append(host);
+ const root = createRoot(host);
+ const render = (value: string) =>
+ root.render(<FontFamilyField flat value={value} importedFonts={[]} onCommit={vi.fn()} />);
+ try {
+ await act(async () => render("Arial"));
+ // The font lists arrive and the value changes while the dropdown is closed, as during a drag.
+ await act(async () => render("Georgia"));
+ expect(sortFontOptions).not.toHaveBeenCalled();
+
+ const trigger = host.querySelector<HTMLButtonElement>('[data-flat-font-trigger="true"]');
+ await act(async () => trigger?.click());
+ expect(sortFontOptions).toHaveBeenCalledTimes(1);
+ expect(host.textContent).toContain("Roboto Slab");
+ expect(document.head.querySelector('link[href*="fonts.googleapis.com"]')).toBeNull();
+ } finally {
+ act(() => root.unmount());
+ host.remove();
+ }
+ });
+});
diff --git a/packages/studio/src/components/editor/propertyPanelFont.tsx b/packages/studio/src/components/editor/propertyPanelFont.tsx
index c058405a3b..c5b3712883 100644
--- a/packages/studio/src/components/editor/propertyPanelFont.tsx
+++ b/packages/studio/src/components/editor/propertyPanelFont.tsx
@@ -276,6 +276,7 @@ export function FontFamilyField({
);
const options = useMemo(() => {
+ if (!open) return [];
const documentFonts = collectDocumentFontFamilies();
const googleSet = new Set(googleFonts.map((f) => f.toLowerCase()));
const taggedLocal = localFonts.map(
@@ -294,7 +295,7 @@ export function FontFamilyField({
...DEFAULT_FONT_FAMILIES.map((f): FontOption => ({ family: f, source: "System" })),
]),
);
- }, [currentFamily, googleFonts, localFonts, projectFontAssets]);
+ }, [open, currentFamily, googleFonts, localFonts, projectFontAssets]);
const filteredOptions = useMemo(() => {
const matches = options.filter((o) => fontMatchesQuery(o.family, query));