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

Commit 87ae826

Browse files
committed
feat(text): color prop
1 parent 359a083 commit 87ae826

File tree

11 files changed

+101
-27
lines changed

11 files changed

+101
-27
lines changed
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: string | number | 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/components/Divider/dividerStyles.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,28 @@ 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 = (color, size, variables: DividerVariables): ICSSInJSStyle => ({
1410
content: '""',
1511
flex: 1,
16-
...dividerBorderStyle(size, variables.dividerColor),
17-
...(color && {
18-
...dividerBorderStyle(size, _.get(variables.colors, color)),
19-
}),
12+
height: `${size + 1}px`,
13+
background: color ? _.get(variables.colors, color) : variables.dividerColor,
2014
})
2115

22-
const dividerStyles: ComponentSlotStylesInput<DividerPropsWithDefaults, any> = {
16+
const dividerStyles: ComponentSlotStylesInput<DividerPropsWithDefaults, DividerVariables> = {
2317
root: ({ props, variables }): ICSSInJSStyle => {
24-
const { children, color, fitted, size, type, important, content } = props
18+
const { children, color, fitted, size, important, content } = props
2519
return {
26-
color: variables.textColor,
20+
color: color ? _.get(variables.colors, color) : variables.textColor,
2721
display: 'flex',
2822
alignItems: 'center',
2923
...(!fitted && {
3024
paddingTop: variables.dividerPadding,
3125
paddingBottom: variables.dividerPadding,
3226
}),
33-
...(color && {
34-
color: _.get(variables.colors, color),
35-
}),
3627
...(important && {
3728
fontWeight: variables.importantFontWeight,
3829
}),
@@ -42,17 +33,17 @@ const dividerStyles: ComponentSlotStylesInput<DividerPropsWithDefaults, any> = {
4233
fontSize: pxToRem(12 + size),
4334
lineHeight: variables.textLineHeight,
4435
'::before': {
45-
...beforeAndAfter(color, size, type, variables),
36+
...beforeAndAfter(color, size, variables),
4637
marginRight: pxToRem(20),
4738
},
4839
'::after': {
49-
...beforeAndAfter(color, size, type, variables),
40+
...beforeAndAfter(color, size, variables),
5041
marginLeft: pxToRem(20),
5142
},
5243
}
5344
: {
5445
'::before': {
55-
...beforeAndAfter(color, size, type, variables),
46+
...beforeAndAfter(color, size, variables),
5647
},
5748
}),
5849
}

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

Lines changed: 7 additions & 5 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 => {
1719
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

Lines changed: 4 additions & 0 deletions
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,
@@ -21,6 +24,7 @@ export default {
2124
}: ComponentStyleFunctionParam<TextProps, TextVariables>): ICSSInJSStyle => {
2225
return {
2326
display: 'inline-block',
27+
...(color && { color: _.get(v.colors, color) }),
2428
...(truncated && truncateStyle),
2529
...(atMention === true && {
2630
color: v.atMentionOtherColor,

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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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 extracting the color names and values.
81+
*/
82+
export type ColorValues<T> = Partial<Record<ColorNames, T>>
83+
7484
/**
7585
* A type for a base colors.
7686
*/

0 commit comments

Comments
 (0)