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
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion packages/font/src/google/get-google-fonts-url.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { sortFontsVariantValues } from './sort-fonts-variant-values'

/**
* Generate the Google Fonts URL given the requested weight(s), style(s) and additional variable axes
*/
Expand Down Expand Up @@ -53,7 +55,7 @@ export function getGoogleFontsUrl(
if (variants.length > 0) {
url = `${url}:${variants[0].map(([key]) => key).join(',')}@${variants
.map((variant) => variant.map(([, val]) => val).join(','))
.sort()
.sort(sortFontsVariantValues)
.join(';')}`
}

Expand Down
35 changes: 35 additions & 0 deletions packages/font/src/google/sort-fonts-variant-values.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { sortFontsVariantValues } from './sort-fonts-variant-values'

describe('sortFontsVariantValues', () => {
it('should correctly compare and return result for plain integer values', () => {
// Testing plain integer values
expect(sortFontsVariantValues('100', '200')).toBe(-100)
expect(sortFontsVariantValues('200', '100')).toBe(100)
expect(sortFontsVariantValues('50', '150')).toBe(-100)
expect(sortFontsVariantValues('150', '50')).toBe(100)
})

it('should correctly compare and return result for comma-separated values', () => {
// Testing "ital,wght" format
expect(sortFontsVariantValues('1,100', '0,200')).toBe(1)
expect(sortFontsVariantValues('0,200', '1,100')).toBe(-1)
expect(sortFontsVariantValues('1,100', '1,200')).toBe(-100)
expect(sortFontsVariantValues('1,200', '1,100')).toBe(100)
expect(sortFontsVariantValues('0,100', '0,200')).toBe(-100)
expect(sortFontsVariantValues('0,200', '0,100')).toBe(100)
})

it('should sort an array of plain integer values correctly', () => {
const unsortedArray = ['100', '1000', '300', '200', '500']
const sortedArray = unsortedArray.slice().sort(sortFontsVariantValues)

expect(sortedArray).toEqual(['100', '200', '300', '500', '1000'])
})

it('should sort an array of values with comma-separated values correctly', () => {
const unsortedArray = ['1,100', '1,200', '0,100', '0,200']
const sortedArray = unsortedArray.slice().sort(sortFontsVariantValues)

expect(sortedArray).toEqual(['0,100', '0,200', '1,100', '1,200'])
})
})
25 changes: 25 additions & 0 deletions packages/font/src/google/sort-fonts-variant-values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Callback function for sorting font variant values.
* Used as a parameter in `Array.prototype.sort` function to ensure correct sorting.
*/

export function sortFontsVariantValues(valA: string, valB: string) {
// 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)
}
Loading