forked from MetaMask/metamask-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/4073 ps text component (MetaMask#4265)
* Begin creating BaseText component * Create useStyles hook to abstract applying theme and vars to stylesheet * Update design-tokens library to introduce typography * Update design tokens library to fix typography font weight * Create BaseText component. Provide types and styles. * Remove old files * Update BaseText with README, story, and test. * Refine BaseText test * Add BaseText to stories * Clean up BaseText. Mock useAppThemeFromContext * Update BaseText storybook * Temporarily fallback to mock theme to patch storybook theme context * Fix tests * Update comments * Update base style * Fix docs * Add default color to text component * Fix unit test
- Loading branch information
Showing
21 changed files
with
231 additions
and
18 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
app/component-library/components/BaseText/BaseText.stories.tsx
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,12 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react-native'; | ||
import BaseText, { BaseTextVariant } from './'; | ||
|
||
storiesOf('Component Library / BaseText', module) | ||
.addDecorator((getStory) => getStory()) | ||
.add('Small Display MD', () => ( | ||
<BaseText variant={BaseTextVariant.sDisplayMD}>{`I'm Text!`}</BaseText> | ||
)) | ||
.add('Small Body MD', () => ( | ||
<BaseText variant={BaseTextVariant.sBodyMD}>{`I'm Text!`}</BaseText> | ||
)); |
28 changes: 28 additions & 0 deletions
28
app/component-library/components/BaseText/BaseText.styles.ts
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,28 @@ | ||
import { StyleSheet, TextStyle } from 'react-native'; | ||
import { BaseTextStyleSheet, BaseTextStyleSheetVars } from './BaseText.types'; | ||
import { Theme } from '../../../util/theme/models'; | ||
|
||
/** | ||
* Style sheet function for BaseText component. | ||
* | ||
* @param params Style sheet params. | ||
* @param params.theme App theme from ThemeContext. | ||
* @param params.vars Inputs that the style sheet depends on. | ||
* @returns StyleSheet object. | ||
*/ | ||
const styleSheet = (params: { | ||
theme: Theme; | ||
vars: BaseTextStyleSheetVars; | ||
}): BaseTextStyleSheet => { | ||
const { theme, vars } = params; | ||
const { variant, style } = vars; | ||
return StyleSheet.create({ | ||
base: Object.assign( | ||
{ color: theme.colors.text.default }, | ||
theme.typography[variant], | ||
style, | ||
) as TextStyle, | ||
}); | ||
}; | ||
|
||
export default styleSheet; |
12 changes: 12 additions & 0 deletions
12
app/component-library/components/BaseText/BaseText.test.tsx
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,12 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import BaseText, { BaseTextVariant } from './'; | ||
|
||
describe('BaseText', () => { | ||
it('should render correctly', () => { | ||
const wrapper = shallow( | ||
<BaseText variant={BaseTextVariant.lBodyMD}>{`I'm Text!`}</BaseText>, | ||
); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
}); |
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,22 @@ | ||
/* eslint-disable react/prop-types */ | ||
import React from 'react'; | ||
import { Text } from 'react-native'; | ||
import { useStyles } from '../../hooks'; | ||
import styleSheet from './BaseText.styles'; | ||
import { BaseTextProps } from './BaseText.types'; | ||
|
||
const BaseText: React.FC<BaseTextProps> = ({ | ||
variant, | ||
style, | ||
children, | ||
...props | ||
}) => { | ||
const styles = useStyles(styleSheet, { variant, style }); | ||
return ( | ||
<Text {...props} style={styles.base}> | ||
{children} | ||
</Text> | ||
); | ||
}; | ||
|
||
export default BaseText; |
57 changes: 57 additions & 0 deletions
57
app/component-library/components/BaseText/BaseText.types.ts
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,57 @@ | ||
import { StyleProp, TextProps, TextStyle } from 'react-native'; | ||
|
||
/** | ||
* BaseText component variants. | ||
*/ | ||
export enum BaseTextVariant { | ||
sDisplayMD = 'sDisplayMD', | ||
sHeadingLG = 'sHeadingLG', | ||
sHeadingMD = 'sHeadingMD', | ||
sHeadingSMRegular = 'sHeadingSMRegular', | ||
sHeadingSM = 'sHeadingSM', | ||
sBodyMD = 'sBodyMD', | ||
sBodyMDBold = 'sBodyMDBold', | ||
sBodySM = 'sBodySM', | ||
sBodySMBold = 'sBodySMBold', | ||
sBodyXS = 'sBodyXS', | ||
lDisplayMD = 'lDisplayMD', | ||
lHeadingLG = 'lHeadingLG', | ||
lHeadingMD = 'lHeadingMD', | ||
lHeadingSMRegular = 'lHeadingSMRegular', | ||
lHeadingSM = 'lHeadingSM', | ||
lBodyMD = 'lBodyMD', | ||
lBodyMDBold = 'lBodyMDBold', | ||
lBodySM = 'lBodySM', | ||
lBodySMBold = 'lBodySMBold', | ||
lBodyXS = 'lBodyXS', | ||
} | ||
|
||
/** | ||
* BaseText component props. | ||
*/ | ||
export interface BaseTextProps extends TextProps { | ||
/** | ||
* Enum to select between Typography variants. | ||
*/ | ||
variant: BaseTextVariant; | ||
/** | ||
* Escape hatch for applying extra styles. Only use if absolutely necessary. | ||
*/ | ||
style?: StyleProp<TextStyle>; | ||
/** | ||
* Children component of a Text component. | ||
*/ | ||
children?: React.ReactNode; | ||
} | ||
|
||
/** | ||
* BaseText component style sheet. | ||
*/ | ||
export interface BaseTextStyleSheet { | ||
base: TextStyle; | ||
} | ||
|
||
/** | ||
* Style sheet input parameters. | ||
*/ | ||
export type BaseTextStyleSheetVars = Pick<BaseTextProps, 'variant' | 'style'>; |
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,15 @@ | ||
# BaseText | ||
|
||
BaseText is a text component that standardizes on typography provided through theme via @metamask/design-tokens library. | ||
|
||
## Props | ||
|
||
This component extends `TextProps` from React Native's [Text Component](https://reactnative.dev/docs/text). | ||
|
||
### `variant` | ||
|
||
Enum to select between Typography variants. | ||
|
||
| <span style="color:gray;font-size:14px">TYPE</span> | <span style="color:gray;font-size:14px">REQUIRED</span> | | ||
| :-------------------------------------------------- | :------------------------------------------------------ | | ||
| [BaseTextVariant](./BaseText.types.ts#L6) | Yes | |
18 changes: 18 additions & 0 deletions
18
app/component-library/components/BaseText/__snapshots__/BaseText.test.tsx.snap
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,18 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`BaseText should render correctly 1`] = ` | ||
<Text | ||
style={ | ||
Object { | ||
"color": "#24272A", | ||
"fontFamily": "Euclid Circular B", | ||
"fontSize": 16, | ||
"fontWeight": "400", | ||
"letterSpacing": 0, | ||
"lineHeight": 24, | ||
} | ||
} | ||
> | ||
I'm Text! | ||
</Text> | ||
`; |
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,2 @@ | ||
export { default } from './BaseText'; | ||
export { BaseTextVariant } from './BaseText.types'; |
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,2 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
export { useStyles } from './useStyles'; |
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,23 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import { useMemo } from 'react'; | ||
import { useAppThemeFromContext } from '../../util/theme'; | ||
import { Theme } from '../../util/theme/models'; | ||
|
||
/** | ||
* Hook that handles both passing style sheet variables into style sheet and memoization. | ||
* | ||
* @param styleSheet Return value of useStyles hook. | ||
* @param vars Variables of styleSheet function. | ||
* @returns StyleSheet object. | ||
*/ | ||
export const useStyles = <R, V>( | ||
styleSheet: (params: { theme: Theme; vars: V }) => R, | ||
vars: V, | ||
): R => { | ||
const theme = useAppThemeFromContext(); | ||
const styles = useMemo( | ||
() => styleSheet({ theme, vars }), | ||
[styleSheet, theme, vars], | ||
); | ||
return styles; | ||
}; |
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
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
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
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
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
Oops, something went wrong.