-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[C-3391] Update native harmony Text component to use harmony theme values #6798
Merged
Kyle-Shanks
merged 1 commit into
main
from
kj-Update-native-harmony-text-to-use-harmony-theme
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export const variantTagMap = { | ||
display: { | ||
xl: 'h1', | ||
l: 'h1', | ||
m: 'h1', | ||
s: 'h1' | ||
}, | ||
heading: { | ||
xl: 'h1', | ||
l: 'h2', | ||
m: 'h3', | ||
s: 'h4' | ||
} | ||
} | ||
|
||
export const variantStylesMap = { | ||
display: { | ||
fontSize: { s: '6xl', m: '7xl', l: '8xl', xl: '9xl' }, | ||
lineHeight: { s: '3xl', m: '4xl', l: '5xl', xl: '6xl' }, | ||
fontWeight: { default: 'bold', strong: 'heavy' } | ||
}, | ||
heading: { | ||
fontSize: { s: 'xl', m: '2xl', l: '3xl', xl: '5xl' }, | ||
lineHeight: { s: 'l', m: 'xl', l: 'xl', xl: '2xl' }, | ||
fontWeight: { default: 'bold', strong: 'heavy' } | ||
}, | ||
title: { | ||
fontSize: { xs: 'xs', s: 's', m: 'm', l: 'l' }, | ||
lineHeight: { xs: 's', s: 's', m: 'm', l: 'l' }, | ||
fontWeight: { weak: 'demiBold', default: 'bold', strong: 'heavy' } | ||
}, | ||
label: { | ||
fontSize: { xs: '2xs', s: 'xs', m: 's', l: 'm', xl: 'xl' }, | ||
lineHeight: { xs: 'xs', s: 'xs', m: 's', l: 's', xl: 'l' }, | ||
fontWeight: { default: 'bold', strong: 'heavy' }, | ||
css: { textTransform: 'uppercase' as const, letterSpacing: 0.5 } | ||
}, | ||
body: { | ||
fontSize: { xs: 'xs', s: 's', m: 'm', l: 'l' }, | ||
lineHeight: { xs: 's', s: 'm', m: 'm', l: 'l' }, | ||
fontWeight: { default: 'medium', strong: 'demiBold' } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { Text } from './Text' | ||
export * from './types' | ||
export * from './constants' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './App' | ||
// // Uncomment to run app in storybook mode | ||
// Uncomment to run app in storybook mode | ||
// export { default as App } from '../../.storybook' | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,25 @@ | ||
import { useMemo } from 'react' | ||
|
||
import type { | ||
TextVariant, | ||
TextSize, | ||
TextColors, | ||
TextStrength | ||
} from '@audius/harmony' | ||
import { variantStylesMap } from '@audius/harmony' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so cool!! |
||
import { css } from '@emotion/native' | ||
import type { TextProps as TextPropsBase, TextStyle } from 'react-native' | ||
import { Text as TextBase } from 'react-native' | ||
|
||
import { makeStyles, useTheme } from '../theme' | ||
import type { HarmonyTheme } from '../theme/theme' | ||
import { useTheme } from '../theme' | ||
|
||
export type TextProps = TextPropsBase & { | ||
variant?: TextVariant | ||
size?: TextSize | ||
strength?: TextStrength | ||
color?: TextColor | ||
color?: TextColors | ||
} | ||
|
||
export type TextStrength = 'weak' | 'default' | 'strong' | ||
|
||
export type TextColor = | ||
| 'heading' | ||
| 'default' | ||
| 'subdued' | ||
| 'disabled' | ||
| 'danger' | ||
| 'warning' | ||
|
||
export type TextSize = 'xl' | 'l' | 'm' | 's' | 'xs' | ||
|
||
export type TextVariant = 'display' | 'heading' | 'title' | 'label' | 'body' | ||
|
||
type VariantConfig = { | ||
variant: TextVariant | ||
fontSize: Record<string, string> | ||
lineHeight: Record<string, string> | ||
fontWeight: Record<string, string> | ||
style?: TextStyle | ||
} | ||
|
||
const generateVariantStyles = (config: VariantConfig, theme: HarmonyTheme) => { | ||
const { variant, fontSize, lineHeight, fontWeight, style } = config | ||
const { typography } = theme | ||
const fontSizeKeys = Object.keys(fontSize) | ||
const fontWeightKeys = Object.keys(fontWeight) | ||
|
||
const variants: Record<string, TextStyle> = {} | ||
for (const fontSizeKey of fontSizeKeys) { | ||
for (const fontWeightKey of fontWeightKeys) { | ||
const variantKey = `${variant}-${fontSizeKey}-${fontWeightKey}` | ||
variants[variantKey] = { | ||
fontSize: typography.size[fontSize[fontSizeKey]], | ||
lineHeight: typography.lineHeight[lineHeight[fontSizeKey]], | ||
fontWeight: typography.weight[fontWeight[fontWeightKey]], | ||
fontFamily: typography.fontByWeight[fontWeight[fontWeightKey]], | ||
...style | ||
} | ||
} | ||
} | ||
return variants | ||
} | ||
|
||
const useStyles = makeStyles((theme) => { | ||
const displayConfig = { | ||
variant: 'display' as const, | ||
fontSize: { s: '6xl', m: '7xl', l: '8xl', xl: '9xl' }, | ||
lineHeight: { s: '3xl', m: '4xl', l: '5xl', xl: '6xl' }, | ||
fontWeight: { default: 'bold', strong: 'heavy' } | ||
} | ||
|
||
const headingConfig = { | ||
variant: 'heading' as const, | ||
fontSize: { s: 'xl', m: '2xl', l: '3xl', xl: '3xl' }, | ||
lineHeight: { s: 'l', m: 'xl', l: 'xl', xl: 'xl' }, | ||
fontWeight: { default: 'bold', strong: 'heavy' } | ||
} | ||
|
||
const titleConfig = { | ||
Kyle-Shanks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
variant: 'title' as const, | ||
fontSize: { xs: 'xs', s: 's', m: 'm', l: 'l' }, | ||
lineHeight: { xs: 's', s: 's', m: 'm', l: 'l' }, | ||
fontWeight: { weak: 'demiBold', default: 'bold', strong: 'heavy' } | ||
} | ||
|
||
const labelConfig = { | ||
variant: 'label' as const, | ||
fontSize: { xs: '2xs', s: 'xs', m: 's', l: 'm', xl: 'xl' }, | ||
lineHeight: { xs: 'xs', s: 'xs', m: 's', l: 's', xl: 'l' }, | ||
fontWeight: { default: 'bold', strong: 'heavy' }, | ||
style: { textTransform: 'uppercase' as const, letterSpacing: 0.5 } | ||
} | ||
|
||
const bodyConfig = { | ||
variant: 'body' as const, | ||
fontSize: { xs: 'xs', s: 's', m: 'm', l: 'l' }, | ||
lineHeight: { xs: 's', s: 'm', m: 'm', l: 'l' }, | ||
fontWeight: { default: 'medium', strong: 'demiBold' } | ||
} | ||
|
||
return [ | ||
displayConfig, | ||
headingConfig, | ||
titleConfig, | ||
labelConfig, | ||
bodyConfig | ||
].reduce( | ||
(styles, config) => ({ | ||
...styles, | ||
...generateVariantStyles(config, theme) | ||
}), | ||
{} | ||
) | ||
}) | ||
|
||
export const Text = (props: TextProps) => { | ||
const { | ||
variant = 'body', | ||
|
@@ -116,21 +29,32 @@ export const Text = (props: TextProps) => { | |
color: colorProp = 'default', | ||
...other | ||
} = props | ||
const styles = useStyles() | ||
const theme = useTheme() | ||
// TODO: make heading a proper gradient | ||
const color = | ||
colorProp === 'heading' | ||
? theme.color.secondary.secondary | ||
: theme.color.textIcon[colorProp] | ||
|
||
const styleKey = `${variant}-${size}-${strength}` | ||
const variantStyles = variantStylesMap[variant] | ||
|
||
const textStyles = useMemo((): TextStyle => { | ||
Kyle-Shanks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const t = theme.typography | ||
return css({ | ||
fontSize: t.size[variantStyles.fontSize[size]], | ||
lineHeight: t.lineHeight[variantStyles.lineHeight[size]], | ||
fontWeight: t.weight[variantStyles.fontWeight[strength]], | ||
fontFamily: t.fontByWeight[variantStyles.fontWeight[strength]], | ||
...('css' in variantStyles ? variantStyles.css : {}), | ||
...(color && { color }) | ||
}) | ||
}, [color, size, strength, theme.typography, variantStyles]) | ||
|
||
const isHeading = variant === 'display' || variant === 'heading' | ||
|
||
return ( | ||
<TextBase | ||
style={[styleProp, styles[styleKey], color && { color }]} | ||
style={[styleProp, textStyles]} | ||
role={isHeading ? 'heading' : undefined} | ||
{...other} | ||
/> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can do this based on the environment variable from the package.json (see example here)
if (process.env.RN_STORYBOOK) { ... }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can also be a separate change