Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weight values above 900 not working with Google Fonts #54339

Merged
merged 5 commits into from
Aug 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/font/src/google/get-google-fonts-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,36 @@ export function getGoogleFontsUrl(
'+'
)}`

function sortVariantValues(valA: string, valB: string) {
indraantoor marked this conversation as resolved.
Show resolved Hide resolved
// If both values contain commas, it indicates they are in "ital,wght" format
if (valA.includes(',') && valB.includes(',')) {
// Split the values into prefix and suffix
const [aPrefix, aSuffix] = valA.split(',')
const [bPrefix, bSuffix] = valB.split(',')

// Compare the prefixes (ital values)
if (aPrefix === bPrefix) {
// If prefixes are equal, then compare the suffixes (wght values)
return parseInt(aSuffix) - parseInt(bSuffix)
} else {
// If prefixes are different, then compare the prefixes directly
return parseInt(aPrefix) - parseInt(bPrefix)
}
}

// If values are not in "ital,wght" format, then directly compare them as integers
return parseInt(valA) - parseInt(valB)
}

if (variants.length > 0) {
url = `${url}:${variants[0].map(([key]) => key).join(',')}@${variants
.map((variant) => variant.map(([, val]) => val).join(','))
.sort()
.sort(sortVariantValues)
.join(';')}`
}

url = `${url}&display=${display}`

return url
}

Loading