Skip to content

Commit

Permalink
fix(core): added fill support for font face config
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKilbane authored and natew committed Nov 9, 2022
1 parent 3b5ef1c commit 537d63a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
59 changes: 37 additions & 22 deletions packages/core/src/createFont.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
import { GenericFont } from './types'
import { FontWeightSteps, GenericFont } from './types'

const fontWeights: FontWeightSteps[] = [
'100',
'200',
'300',
'400',
'500',
'600',
'700',
'800',
'900',
]

const processSection = <T>(
section: string | Record<string, T>,
keys: string[]
): string | Record<string, T> => {
if (typeof section === 'string') return section

let fillValue = section[Object.keys(section)[0]]

return Object.fromEntries(
keys.map((key) => {
const value = section[key] ?? fillValue
fillValue = value
return [key, value]
})
)
}

export const createFont = <A extends GenericFont>(font: A): A => {
// fills in any missing values based on size keys being standard
const sizeKeys = Object.keys(font.size)

// fill in intermediate values
for (const key in font) {
if (key === 'size' || key === 'family' || key === 'face') {
continue
}
const section = font[key] as Object
const keys = Object.keys(section)

// fill with first filled in value to start
let fillVal = section[keys[0]]

for (const sKey of sizeKeys) {
if (!(sKey in section)) {
section[sKey] = fillVal
} else {
// if exists, set as the fillVal
fillVal = section[sKey]
}
}
}
Object.fromEntries(
Object.entries(font).map(([key, section]) => {
const fillKeys = key === 'face' ? fontWeights : sizeKeys

return [key, processSection(section, fillKeys)]
})
)

return Object.freeze(font)
}
2 changes: 1 addition & 1 deletion packages/core/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export type GenericFont<Key extends number | string = number | string> = {
transform?: Partial<{ [key in Key]: TextStyle['textTransform'] | Variable }>
color?: Partial<{ [key in Key]: string | Variable }>
// for native use only, lets you map to alternative fonts
face?: { [key in FontWeightSteps]: { normal?: string; italic?: string } }
face?: Partial<{ [key in FontWeightSteps]: { normal?: string; italic?: string } }>
}

// media
Expand Down

1 comment on commit 537d63a

@vercel
Copy link

@vercel vercel bot commented on 537d63a Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

site – ./

site-tamagui.vercel.app
site-beta-beige.vercel.app
site-git-master-tamagui.vercel.app

Please sign in to comment.