Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 33e2dfd

Browse files
author
Alexandru Buliga
authored
feat(text): color prop (#597)
* feat(text): color prop * addressed comments * changelog * amended changelog * made text color override other props that change color
1 parent 359a083 commit 33e2dfd

File tree

13 files changed

+109
-29
lines changed

13 files changed

+109
-29
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717

1818
## [Unreleased]
1919

20+
### Features
21+
- Add `color` prop to `Text` component @Bugaa92 ([#597](https://github.com/stardust-ui/react/pull/597))
22+
2023
<!--------------------------------[ v0.15.0 ]------------------------------- -->
2124
## [v0.15.0](https://github.com/stardust-ui/react/tree/v0.15.0) (2018-12-17)
2225
[Compare changes](https://github.com/stardust-ui/react/compare/v0.14.0...v0.15.0)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react'
2+
import _ from 'lodash'
3+
import { Text, ProviderConsumer } from '@stardust-ui/react'
4+
5+
const TextExampleColor = () => (
6+
<ProviderConsumer
7+
render={({ siteVariables: { emphasisColors, naturalColors } }) =>
8+
_.keys({ ...emphasisColors, ...naturalColors }).map(color => (
9+
<>
10+
<Text key={color} color={color} content={_.startCase(color)} />
11+
<br />
12+
</>
13+
))
14+
}
15+
/>
16+
)
17+
18+
export default TextExampleColor
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
import _ from 'lodash'
3+
import { Text, ProviderConsumer } from '@stardust-ui/react'
4+
5+
const TextExampleColor = () => (
6+
<ProviderConsumer
7+
render={({ siteVariables: { emphasisColors, naturalColors } }) =>
8+
_.keys({ ...emphasisColors, ...naturalColors }).map(color => (
9+
<>
10+
<Text key={color} color={color}>
11+
{_.startCase(color)}
12+
</Text>
13+
<br />
14+
</>
15+
))
16+
}
17+
/>
18+
)
19+
20+
export default TextExampleColor

docs/src/examples/components/Text/Variations/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import ExampleSection from 'docs/src/components/ComponentDoc/ExampleSection'
44

55
const Variations = () => (
66
<ExampleSection title="Variations">
7+
<ComponentExample
8+
title="Color"
9+
description="A Text component can have different colors."
10+
examplePath="components/Text/Variations/TextExampleColor"
11+
/>
712
<ComponentExample
813
title="@ mention"
914
description="A Text component for @ mentions."

src/components/Text/Text.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ import {
99
ContentComponentProps,
1010
ChildrenComponentProps,
1111
commonPropTypes,
12+
ColorComponentProps,
1213
} from '../../lib'
1314

1415
import { Extendable } from '../../../types/utils'
1516

16-
export interface TextProps extends UIComponentProps, ContentComponentProps, ChildrenComponentProps {
17+
export interface TextProps
18+
extends UIComponentProps,
19+
ContentComponentProps,
20+
ChildrenComponentProps,
21+
ColorComponentProps {
1722
/** At mentions can be formatted to draw users' attention. Mentions for "me" can be formatted to appear differently. */
1823
atMention?: boolean | 'me'
1924

@@ -63,7 +68,7 @@ class Text extends UIComponent<Extendable<TextProps>, any> {
6368
static displayName = 'Text'
6469

6570
static propTypes = {
66-
...commonPropTypes.createCommon(),
71+
...commonPropTypes.createCommon({ color: true }),
6772
atMention: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['me'])]),
6873
disabled: PropTypes.bool,
6974
error: PropTypes.bool,

src/lib/colorUtils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as _ from 'lodash'
2+
import { SiteVariablesInput, ColorVariants, ColorValues } from '../themes/types'
3+
4+
export const mapColorsToScheme = <T>(
5+
siteVars: SiteVariablesInput,
6+
mapper: keyof ColorVariants | ((color: ColorVariants) => T),
7+
): ColorValues<T> =>
8+
_.mapValues(
9+
{ ...siteVars.emphasisColors, ...siteVars.naturalColors },
10+
typeof mapper === 'number' ? String(mapper) : (mapper as any),
11+
) as ColorValues<T>

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as commonPropTypes from './commonPropTypes'
33

44
export { default as AutoControlledComponent } from './AutoControlledComponent'
55
export { default as childrenExist } from './childrenExist'
6+
export { mapColorsToScheme } from './colorUtils'
67
export { default as UIComponent } from './UIComponent'
78
export { EventStack } from './eventStack'
89
export { felaRenderer, felaRtlRenderer } from './felaRenderer'

src/themes/teams/colors.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ export const naturalColors: NaturalColors = {
8888
800: '#F9D844',
8989
900: '#F8D22A',
9090
},
91-
9291
darkOrange: {
9392
50: '#F9ECEA',
9493
100: '#ECBCB3',

src/themes/teams/components/Divider/dividerStyles.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,32 @@ import * as _ from 'lodash'
22

33
import { childrenExist } from '../../../../lib'
44
import { pxToRem } from '../../utils'
5-
import { ComponentSlotStylesInput, ICSSInJSStyle, ICSSPseudoElementStyle } from '../../../types'
5+
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
66
import { DividerPropsWithDefaults } from '../../../../components/Divider/Divider'
7+
import { DividerVariables } from './dividerVariables'
78

8-
const dividerBorderStyle = (size, color): ICSSInJSStyle => ({
9-
height: `${size + 1}px`,
10-
background: color,
11-
})
12-
13-
const beforeAndAfter = (color, size, type, variables): ICSSPseudoElementStyle => ({
9+
const beforeAndAfter = (
10+
color: string,
11+
size: number,
12+
variables: DividerVariables,
13+
): ICSSInJSStyle => ({
1414
content: '""',
1515
flex: 1,
16-
...dividerBorderStyle(size, variables.dividerColor),
17-
...(color && {
18-
...dividerBorderStyle(size, _.get(variables.colors, color)),
19-
}),
16+
height: `${size + 1}px`,
17+
background: _.get(variables.colors, color, variables.dividerColor),
2018
})
2119

22-
const dividerStyles: ComponentSlotStylesInput<DividerPropsWithDefaults, any> = {
20+
const dividerStyles: ComponentSlotStylesInput<DividerPropsWithDefaults, DividerVariables> = {
2321
root: ({ props, variables }): ICSSInJSStyle => {
24-
const { children, color, fitted, size, type, important, content } = props
22+
const { children, color, fitted, size, important, content } = props
2523
return {
26-
color: variables.textColor,
24+
color: _.get(variables.colors, color, variables.textColor),
2725
display: 'flex',
2826
alignItems: 'center',
2927
...(!fitted && {
3028
paddingTop: variables.dividerPadding,
3129
paddingBottom: variables.dividerPadding,
3230
}),
33-
...(color && {
34-
color: _.get(variables.colors, color),
35-
}),
3631
...(important && {
3732
fontWeight: variables.importantFontWeight,
3833
}),
@@ -42,17 +37,17 @@ const dividerStyles: ComponentSlotStylesInput<DividerPropsWithDefaults, any> = {
4237
fontSize: pxToRem(12 + size),
4338
lineHeight: variables.textLineHeight,
4439
'::before': {
45-
...beforeAndAfter(color, size, type, variables),
40+
...beforeAndAfter(color, size, variables),
4641
marginRight: pxToRem(20),
4742
},
4843
'::after': {
49-
...beforeAndAfter(color, size, type, variables),
44+
...beforeAndAfter(color, size, variables),
5045
marginLeft: pxToRem(20),
5146
},
5247
}
5348
: {
5449
'::before': {
55-
...beforeAndAfter(color, size, type, variables),
50+
...beforeAndAfter(color, size, variables),
5651
},
5752
}),
5853
}

src/themes/teams/components/Divider/dividerVariables.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
import * as _ from 'lodash'
2-
import { pxToRem } from '../../utils'
2+
import { FontWeightProperty } from 'csstype'
33

4-
import { EmphasisColors, NaturalColors } from '../../../types'
4+
import { pxToRem } from '../../utils'
5+
import { ColorValues } from '../../../types'
6+
import { mapColorsToScheme } from '../../../../lib'
57

68
export interface DividerVariables {
7-
colors: Record<keyof (EmphasisColors & NaturalColors), string>
9+
colors: ColorValues<string>
810
dividerColor: string
911
textColor: string
1012
textFontSize: string
1113
textLineHeight: string
12-
importantFontWeight: string
14+
importantFontWeight: FontWeightProperty
1315
dividerPadding: string
1416
}
1517

1618
export default (siteVars: any): DividerVariables => {
17-
const colorVariant = '500'
19+
const colorVariant = 500
1820

1921
return {
20-
colors: _.mapValues({ ...siteVars.emphasisColors, ...siteVars.naturalColors }, colorVariant),
22+
colors: mapColorsToScheme(siteVars, colorVariant),
2123
dividerColor: siteVars.gray09,
2224
textColor: siteVars.gray03,
2325
textFontSize: siteVars.fontSizeSmall,

0 commit comments

Comments
 (0)