| 1 |
import { StyleAttr } from "./styling-helpers"; |
| 2 |
|
| 3 |
/** |
| 4 |
* The typography options a block can offer, the same set the core blocks do. |
| 5 |
* Plain CSS values, so they can be dropped straight into a style attribute. |
| 6 |
*/ |
| 7 |
export interface TypographyValue { |
| 8 |
fontFamily: string; |
| 9 |
fontSize: string; |
| 10 |
fontStyle: string; |
| 11 |
fontWeight: string; |
| 12 |
lineHeight: string; |
| 13 |
letterSpacing: string; |
| 14 |
textDecoration: string; |
| 15 |
textTransform: string; |
| 16 |
} |
| 17 |
|
| 18 |
export const defaultTypography: TypographyValue = { |
| 19 |
fontFamily: "", |
| 20 |
fontSize: "", |
| 21 |
fontStyle: "", |
| 22 |
fontWeight: "", |
| 23 |
lineHeight: "", |
| 24 |
letterSpacing: "", |
| 25 |
textDecoration: "", |
| 26 |
textTransform: "", |
| 27 |
}; |
| 28 |
|
| 29 |
/** Fills in the missing properties of a stored (partial) typography value. */ |
| 30 |
export function getTypography( |
| 31 |
typography?: Partial<TypographyValue> |
| 32 |
): TypographyValue { |
| 33 |
return { ...defaultTypography, ...(typography || {}) }; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Typography as inline styles. Empty values are left out so the theme keeps |
| 38 |
* deciding until the user picks something. |
| 39 |
*/ |
| 40 |
export function getTypographyCss( |
| 41 |
typography?: Partial<TypographyValue> |
| 42 |
): StyleAttr { |
| 43 |
const { |
| 44 |
fontFamily, |
| 45 |
fontSize, |
| 46 |
fontStyle, |
| 47 |
fontWeight, |
| 48 |
lineHeight, |
| 49 |
letterSpacing, |
| 50 |
textDecoration, |
| 51 |
textTransform, |
| 52 |
} = getTypography(typography); |
| 53 |
|
| 54 |
return { |
| 55 |
fontFamily: fontFamily || undefined, |
| 56 |
fontSize: fontSize || undefined, |
| 57 |
fontStyle: fontStyle || undefined, |
| 58 |
fontWeight: fontWeight || undefined, |
| 59 |
lineHeight: lineHeight || undefined, |
| 60 |
letterSpacing: letterSpacing || undefined, |
| 61 |
textDecoration: textDecoration || undefined, |
| 62 |
textTransform: (textTransform as StyleAttr["textTransform"]) || undefined, |
| 63 |
}; |
| 64 |
} |
| 65 |
|