import type { CustomTheme } from '../custom-theme'; import type { MergeTheme } from '../merge-theme'; import type { StyleContextValue } from '../style-context'; import type { NumberValueWithPercentage } from '../theme'; import type { ThemeContextValue } from '../theme-context'; export type EmValue< V extends NumberValueWithPercentage = NumberValueWithPercentage > = V | keyof MergeTheme['spacing']; export type EmReturnType = V extends `${number}%` ? string : number; export function em( value: V, context: StyleContextValue, theme: ThemeContextValue ): EmReturnType { if (typeof value === 'string') { if (value.endsWith('em')) { return (Number.parseFloat(value) * theme.em) as EmReturnType; } else if (value.endsWith('%')) { return value as EmReturnType; } else { return em(theme.spacing[value] as V, context, theme); } } else { return value as EmReturnType; } }