fix: auto-select 2 font weights when picking a new font-family

This commit is contained in:
Amruth Pillai
2026-02-08 18:14:16 +01:00
parent 2f7aabcfe9
commit 2b8fa9c7e8
7 changed files with 144 additions and 97 deletions
+5
View File
@@ -53,6 +53,11 @@
text-underline-offset: 0.15rem;
}
hr {
margin: 1rem 0;
border-color: var(--page-text-color);
}
strong {
font-weight: var(--page-body-font-weight-bold);
}
+20 -3
View File
@@ -37,11 +37,28 @@ function buildWebFontMap() {
const webFontMap: Map<string, WebFont> = buildWebFontMap();
export function getNextWeight(fontFamily: string): Weight | null {
export function getNextWeights(fontFamily: string): Weight[] | null {
const fontData = webFontMap.get(fontFamily);
if (!fontData || !Array.isArray(fontData.weights) || fontData.weights.length === 0) return null;
if (fontData.weights.includes("400")) return "400";
return fontData.weights[0] as Weight;
const uniqueWeights = Array.from(new Set(fontData.weights)) as Weight[];
// Try to pick 400 and 600 if available
const weights: Weight[] = [];
if (uniqueWeights.includes("400")) weights.push("400");
if (uniqueWeights.includes("600")) weights.push("600");
// If we didn't find both, fill in with first/last, ensuring uniqueness
while (weights.length < 2 && uniqueWeights.length > 0) {
// candidateIndex: 0 (first), 1 (last)
const lastIndex = uniqueWeights.length - 1;
const candidate = weights.length === 0 ? uniqueWeights[0] : uniqueWeights[lastIndex];
if (!weights.includes(candidate)) weights.push(candidate);
else break;
}
return weights.length > 0 ? weights : null;
}
type FontFamilyComboboxProps = Omit<ComboboxProps, "options">;