Skip to content

Instantly share code, notes, and snippets.

@vshkl
Last active April 5, 2023 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vshkl/40840b032fbe02b3574f2d2a12aa202c to your computer and use it in GitHub Desktop.
Save vshkl/40840b032fbe02b3574f2d2a12aa202c to your computer and use it in GitHub Desktop.
Label component for common typography | react-native, typescript
import React, { useMemo } from 'react'
import { Text } from 'react-native'
type LabelProps = {
children: string | number,
size: 'small' | 'normal' | 'medium' | 'large' | 'xlarge' | 'xxlarge',
weight: 'light' | 'regular' | 'medium' | 'bold',
color: 'primary' | 'secondary' | 'disabled',
}
const Label: React.FC<LabelProps> = ({
children,
size,
weight,
color,
}: LabelProps) => {
const sizeValue: number = useMemo(
() => ({
small: 14,
normal: 16,
medium: 20,
large: 24,
xlarge: 32,
xxlarge: 72,
}[size]),
[size],
)
const weightValue: '300' | '400' | '500' | '700' = useMemo(
() => ({
light: '300' as const,
regular: '400' as const,
medium: '500' as const,
bold: '700' as const,
}[weight]),
[weight],
)
const colorValue: string = useMemo(
() => ({
primary: '#212121',
secondary: '#666666',
disabled: '#00000050',
}[color]),
[color],
)
return (
<Text
style={{
fontSize: sizeValue,
fontWeight: weightValue,
color: colorValue,
}}
>
{children}
</Text>
)
}
export default Label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment