feat(web): group imported theme variants

This commit is contained in:
t3-code[bot] committed 2026-08-08 00:25:57 +00:00
1 parent 7bcd756fc5
commit bd9e3f953d
5 files changed
+208 -39

No files matched your search

@@ -1,4 +1,6 @@
import {
ChevronLeftIcon,
ChevronRightIcon,
CopyIcon,
DownloadIcon,
PenLineIcon,
@@ -75,6 +77,7 @@ function ThemeLibraryCard({
onDuplicate,
onDownload,
onRemove,
variantNavigation,
}: {
theme: ThemeCardDefinition;
isActive: boolean;
@@ -85,6 +88,13 @@ function ThemeLibraryCard({
onDuplicate?: () => void;
onDownload?: () => void;
onRemove?: () => void;
variantNavigation?: {
collectionLabel: string;
index: number;
count: number;
onPrevious: () => void;
onNext: () => void;
};
}) {
// A one-appearance theme can only take its own side of the mix, so the card
// tooltip promises exactly what clicking it does.
@@ -111,6 +121,41 @@ function ThemeLibraryCard({
onSelectMode={onUseMode}
previews={theme.previews}
/>
{variantNavigation ? (
<div
className="flex items-center gap-1 border-t border-border/50 px-2 py-1"
aria-label={variantNavigation.collectionLabel}
>
<Button
aria-label={`Previous ${variantNavigation.collectionLabel} variant`}
size="icon-xs"
variant="ghost"
onClick={(event) => {
event.stopPropagation();
variantNavigation.onPrevious();
}}
>
<ChevronLeftIcon />
</Button>
<span
className="min-w-0 flex-1 truncate text-center text-xs text-muted-foreground"
aria-live="polite"
>
{theme.label} · {variantNavigation.index + 1} of {variantNavigation.count}
</span>
<Button
aria-label={`Next ${variantNavigation.collectionLabel} variant`}
size="icon-xs"
variant="ghost"
onClick={(event) => {
event.stopPropagation();
variantNavigation.onNext();
}}
>
<ChevronRightIcon />
</Button>
</div>
) : null}
<div className="flex items-center gap-2 px-3 pb-3 pt-2">
<div className="min-w-0 flex-1">
<div className="flex items-center gap-1.5">
@@ -124,7 +169,7 @@ function ThemeLibraryCard({
onUse();
}}
>
{theme.label}
{variantNavigation?.collectionLabel ?? theme.label}
</button>
</div>
</div>
@@ -224,6 +269,68 @@ function ThemeLibraryCard({
);
}
function CustomThemeCollectionCard({
themes,
activeModesFor,
onUse,
onUseMode,
onDuplicate,
onEdit,
onDownload,
onRemove,
}: {
themes: ReadonlyArray<ThemeDefinition>;
activeModesFor: (themeId: string) => ReadonlyArray<ThemeMode>;
onUse: (theme: ThemeDefinition) => void;
onUseMode: (theme: ThemeDefinition, mode: ThemeMode) => void;
onDuplicate: (theme: ThemeDefinition) => void;
onEdit: (theme: ThemeDefinition) => void;
onDownload: (theme: ThemeDefinition) => void;
onRemove: (theme: ThemeDefinition) => void;
}) {
const [variantIndex, setVariantIndex] = useState(() => {
const activeIndex = themes.findIndex((theme) => activeModesFor(theme.id).length > 0);
return activeIndex < 0 ? 0 : activeIndex;
});
const safeIndex = Math.min(variantIndex, themes.length - 1);
const theme = themes[safeIndex];
useEffect(() => {
if (variantIndex !== safeIndex) setVariantIndex(safeIndex);
}, [safeIndex, variantIndex]);
if (!theme) return null;
const collectionLabel = theme.collection?.label ?? theme.label;
const move = (offset: number) => {
setVariantIndex((current) => (current + offset + themes.length) % themes.length);
};
return (
<ThemeLibraryCard
activeModes={activeModesFor(theme.id)}
isActive={false}
onDownload={() => onDownload(theme)}
onDuplicate={() => onDuplicate(theme)}
onEdit={() => onEdit(theme)}
onRemove={() => onRemove(theme)}
onUse={() => onUse(theme)}
onUseMode={(mode) => onUseMode(theme, mode)}
theme={getThemeCardDefinition(theme)}
{...(themes.length > 1
? {
variantNavigation: {
collectionLabel,
index: safeIndex,
count: themes.length,
onPrevious: () => move(-1),
onNext: () => move(1),
},
}
: {})}
/>
);
}
export function ThemeLibrary({
theme,
setTheme,
@@ -460,6 +567,18 @@ export function ThemeLibrary({
</div>
);
const customThemeCollections = [
...customThemes
.reduce((groups, customTheme) => {
const groupId = customTheme.collection?.id ?? `theme:${customTheme.id}`;
const group = groups.get(groupId);
if (group) group.push(customTheme);
else groups.set(groupId, [customTheme]);
return groups;
}, new Map<string, ThemeDefinition[]>())
.entries(),
];
const renderPairGrid = () => (
// One shared provider so every tooltip in the grid hands off instantly to
// the next hovered trigger instead of stacking on top of it. The card
@@ -501,43 +620,39 @@ export function ThemeLibrary({
/>
);
})}
{customThemes.map((customTheme) => {
const card = getThemeCardDefinition(customTheme);
return (
<ThemeLibraryCard
activeModes={pickedModesFor(customTheme.id)}
isActive={false}
key={customTheme.id}
onDuplicate={() =>
openThemeEditor({
editingThemeId: null,
seedThemeId: customTheme.id,
seedName: `${customTheme.label} copy`,
initialAppearance,
})
}
onEdit={() =>
openThemeEditor({
editingThemeId: customTheme.id,
seedThemeId: null,
seedName: null,
initialAppearance,
})
}
onDownload={() =>
downloadThemeFile(`${customTheme.id}.json`, serializeThemeFile(customTheme))
}
onRemove={() => handleRemoveTheme(customTheme)}
onUse={() => {
const modes = getThemeModes(customTheme);
if (modes.length === 1) assignHalf(modes[0]!, customTheme.id);
else persistTheme(customTheme.id);
}}
onUseMode={handlePairPick(customTheme.id)}
theme={card}
/>
);
})}
{customThemeCollections.map(([collectionId, themes]) => (
<CustomThemeCollectionCard
activeModesFor={pickedModesFor}
key={collectionId}
onDownload={(customTheme) =>
downloadThemeFile(`${customTheme.id}.json`, serializeThemeFile(customTheme))
}
onDuplicate={(customTheme) =>
openThemeEditor({
editingThemeId: null,
seedThemeId: customTheme.id,
seedName: `${customTheme.label} copy`,
initialAppearance,
})
}
onEdit={(customTheme) =>
openThemeEditor({
editingThemeId: customTheme.id,
seedThemeId: null,
seedName: null,
initialAppearance,
})
}
onRemove={handleRemoveTheme}
onUse={(customTheme) => {
const modes = getThemeModes(customTheme);
if (modes.length === 1) assignHalf(modes[0]!, customTheme.id);
else persistTheme(customTheme.id);
}}
onUseMode={(customTheme, mode) => handlePairPick(customTheme.id)(mode)}
themes={themes}
/>
))}
</div>
</TooltipProvider>
);
+1
View File
@@ -260,6 +260,7 @@ describe("Open VSX themes", () => {
expect(themes).toHaveLength(2);
expect(new Set(themes.map((theme) => theme.id)).size).toBe(2);
expect(themes.every((theme) => theme.collection?.id === "open-vsx:demo.theme")).toBe(true);
const paired = themes.find(
(theme) =>
getThemeColorsForMode(theme, "light") !== null &&
+7 -1
View File
@@ -587,6 +587,7 @@ export async function importOpenVsxThemeExtension(
signal?.throwIfAborted();
inspectZip(zip);
} catch (cause) {
if (signal?.aborted) signal.throwIfAborted();
if (cause instanceof Error && cause.message.startsWith("That extension package")) throw cause;
throw new Error("That Open VSX extension package could not be opened.", { cause });
}
@@ -649,5 +650,10 @@ export async function importOpenVsxThemeExtension(
throw new Error(failures[0] ?? "That extension has no compatible color themes.");
}
const paired = pairVsCodeThemes(resolveThemeLabelCollisions(parsed));
return resolveThemeLabelCollisions(paired.map((theme) => ({ theme })));
const themes = resolveThemeLabelCollisions(paired.map((theme) => ({ theme })));
const collection = {
id: `open-vsx:${extension.id.toLowerCase()}`,
label: extension.name.slice(0, 48),
};
return themes.map((theme) => ({ ...theme, collection }));
}
+36
View File
@@ -417,6 +417,42 @@ describe("theme files", () => {
vi.unstubAllGlobals();
});
it("preserves valid imported-theme collections and drops malformed metadata", () => {
vi.stubGlobal("window", {
localStorage: {
getItem: (key: string) =>
key === CUSTOM_THEMES_STORAGE_KEY
? JSON.stringify([
{
id: "github-dark",
label: "GitHub Dark",
appearance: "dark",
colors: { canvas: "#0d1117" },
collection: { id: "open-vsx:github.github-vscode-theme", label: "GitHub Theme" },
},
{
id: "github-light",
label: "GitHub Light",
appearance: "light",
colors: { canvas: "#ffffff" },
collection: { id: "bad collection id", label: "GitHub Theme" },
},
])
: null,
},
});
invalidateCustomThemes();
expect(getCustomThemes()).toMatchObject([
{ collection: { id: "open-vsx:github.github-vscode-theme", label: "GitHub Theme" } },
{ id: "github-light" },
]);
expect(getCustomThemes()[1]).not.toHaveProperty("collection");
vi.unstubAllGlobals();
invalidateCustomThemes();
});
it("updates a personal theme without changing its id", () => {
const stored = new Map<string, string>();
vi.stubGlobal("window", {
+11
View File
@@ -90,12 +90,15 @@ export type ThemeColorOverrides = Readonly<Partial<Record<ThemeColorRole, string
export type ThemeVariants = Readonly<Partial<Record<ThemeAppearance, ThemeColors>>>;
export type ThemeVariantOverrides = Readonly<Partial<Record<ThemeAppearance, ThemeColorOverrides>>>;
export type ThemePreferenceMode = ThemeAppearance | "system";
export type ThemeCollection = Readonly<{ id: string; label: string }>;
export type ThemeDefinition = Readonly<{
id: string;
label: string;
appearance: ThemeAppearance;
colors: ThemeColors;
variants?: ThemeVariants;
/** Groups related imported variants into one library card. */
collection?: ThemeCollection;
/** Allows fixed Dev/Nightly artwork to render over this theme's sidebar. */
sidebarArtwork?: boolean;
/** True when the palette was generated by the guided editor from its
@@ -216,6 +219,13 @@ function parseStoredTheme(value: unknown): ThemeDefinition | null {
if (!colors) return null;
const variants = parseStoredThemeVariants(value.variants, value.appearance);
if (value.variants !== undefined && variants === null) return null;
const collection =
isRecord(value.collection) &&
typeof value.collection.id === "string" &&
/^[a-z0-9][a-z0-9.:-]{0,127}$/i.test(value.collection.id) &&
isThemeLabel(value.collection.label)
? { id: value.collection.id, label: value.collection.label.trim() }
: undefined;
return {
id: value.id,
@@ -223,6 +233,7 @@ function parseStoredTheme(value: unknown): ThemeDefinition | null {
appearance: value.appearance,
colors,
...(variants ? { variants } : {}),
...(collection ? { collection } : {}),
...(value.sidebarArtwork === true ? { sidebarArtwork: true } : {}),
...(value.managed === true ? { managed: true } : {}),
};