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

Commit 1d8e168

Browse files
Alexandru Buligamanajdov
Alexandru Buliga
authored and
manajdov
committed
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 df0b11a commit 1d8e168

File tree

13 files changed

+109
-29
lines changed

13 files changed

+109
-29
lines changed

CHANGELOG.md

+3
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)
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
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

+5
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

+7-2
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

+11
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

+1
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

-1
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

+15-20
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

+8-6
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,

src/themes/teams/components/Text/textStyles.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as _ from 'lodash'
2+
13
import { ComponentStyleFunctionParam, ICSSInJSStyle } from '../../../types'
24
import { truncateStyle } from '../../../../styles/customCSS'
35
import { TextVariables } from './textVariables'
@@ -7,6 +9,7 @@ export default {
79
root: ({
810
props: {
911
atMention,
12+
color,
1013
disabled,
1114
error,
1215
size,
@@ -43,6 +46,7 @@ export default {
4346
fontWeight: v.importantWeight,
4447
color: v.importantColor,
4548
}),
49+
...(color && { color: _.get(v.colors, color) }),
4650

4751
...(weight === 'light' && {
4852
fontWeight: v.fontWeightLight,

src/themes/teams/components/Text/textVariables.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import { ColorValues } from '../../../types'
2+
import { mapColorsToScheme } from '../../../../lib'
3+
14
export interface TextVariables {
5+
colors: ColorValues<string>
26
atMentionMeColor: string
37
atMentionMeFontWeight: number
48
atMentionOtherColor: string
@@ -29,7 +33,10 @@ export interface TextVariables {
2933
}
3034

3135
export default (siteVariables): TextVariables => {
36+
const colorVariant = 500
37+
3238
return {
39+
colors: mapColorsToScheme(siteVariables, colorVariant),
3340
atMentionOtherColor: siteVariables.brand06,
3441
atMentionMeColor: siteVariables.orange04,
3542
atMentionMeFontWeight: siteVariables.fontWeightBold,

src/themes/types.ts

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ type EmphasisColorsStrict = Partial<{
7171

7272
export type EmphasisColors = Extendable<EmphasisColorsStrict, ColorVariants>
7373

74+
/**
75+
* A type for extracting the color names.
76+
*/
77+
type ColorNames = keyof (EmphasisColorsStrict & NaturalColorsStrict)
78+
79+
/**
80+
* A type for an extendable set of ColorNames properties of type T
81+
*/
82+
export type ColorValues<T> = Extendable<Partial<Record<ColorNames, T>>, T>
83+
7484
/**
7585
* A type for a base colors.
7686
*/

0 commit comments

Comments
 (0)