Skip to content

Commit

Permalink
Use tokens as prescribed
Browse files Browse the repository at this point in the history
  • Loading branch information
ajenkins-mparticle committed Jun 5, 2024
1 parent 45cb7a5 commit ef602a3
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions src/components/general/Typography/Typography.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Typography as AntTypography, type TypographyProps as AntTypographyProps } from 'antd'
import {
Typography as AntTypography,
type TypographyProps as AntTypographyProps,
theme,
ConfigProvider as AntConfigProvider,
} from 'antd'
import { ConfigProvider } from 'src/components'
import { type ReactNode } from 'react'
import { type TextProps as AntTextProps } from 'antd/es/typography/Text'
import { type TitleProps as AntTitleProps } from 'antd/es/typography/Title'
import { type LinkProps as AntLinkProps } from 'antd/es/typography/Link'
import { type ParagraphProps as AntParagraphProps } from 'antd/es/typography/Paragraph'
import type { FontMapToken } from 'antd/es/theme/interface'

const { useToken } = theme

export interface ITypographyProps extends AntTypographyProps {
children: ReactNode
Expand All @@ -21,25 +29,38 @@ export interface ITextProps extends AntTextProps {
size?: TypographySize
}

const getFontSizeVariable = (size: TypographySize): string => `--font-size${size === 'base' ? '' : `-${size}`}`
const getFontSizeToken = (size: TypographySize): keyof FontMapToken => {
if (size === 'base') return 'fontSize'
if (size === 'sm') return 'fontSizeSM'
if (size === 'lg') return 'fontSizeLG'
return 'fontSizeXL'
}

const getLineHeightVariable = (size: TypographySize): string => {
if (size === 'sm') return `--line-height-sm`
if (size === 'lg' || size === 'xl') return '--line-height-lg'
return `--line-height`
const getLineHeightToken = (size: TypographySize): keyof FontMapToken => {
if (size === 'base') return 'lineHeight'
if (size === 'sm') return 'lineHeightSM'
return 'lineHeightLG'
}

const Text = ({ size = 'base', ...props }: ITextProps) => {
const { token } = useToken()

const fontSize = getFontSizeToken(size)
const lineHeight = getLineHeightToken(size)

return (
<ConfigProvider>
<AntTypography.Text
style={{
fontSize: `var(${getFontSizeVariable(size)})`,
lineHeight: `var(${getLineHeightVariable(size)})`,
}}
{...props}>
{props.children}
</AntTypography.Text>
<AntConfigProvider
theme={{
components: {
Typography: {
fontSize: token[fontSize],
lineHeight: token[lineHeight],
},
},
}}>
<AntTypography.Text {...props}>{props.children}</AntTypography.Text>
</AntConfigProvider>
</ConfigProvider>
)
}
Expand Down

0 comments on commit ef602a3

Please sign in to comment.