[RFC] Color prop for components where it can target multiple areas (content, background) #636
Description
[RFC] Color prop for components where it can target multiple areas (content, background)
Problem description
Let's take the example of Label
component. The color
prop for a label can have at least 2 targets:
- the color of the text
- the color of the label's background
How should we create props for this type of components?
Solution 1
Always pick an area that color goes to and change other colors using variables.
e.g.:
for Label
component, the color
prop changes the background color, and we're using color
variable to change the text color:
API:
<Label color='red' variables={{ color: '#fff' }} />
will create a label with white text on red background.
Note: We should rename color
variable to textColor
to avoid confusion.
+:
- simple solution
-:
color
variable receives a CSS color and does not go through our color palette mechanism (we'd need to implement that)- confusing that we use a prop and a variable to set colors for areas of the same component
Solution 2
color
prop receives an object as value in order to identify the area of the component it sets color for:
e.g.:
for Label
component, the color
prop changes the text and background colors, so we'd have something like this for the color
prop:
type LabelColorProp = {
text: string
background: string
} | string
API:
<Label color={{ text: 'red', background: 'green' }} />
will create a label with red text on green background.
<Label color='red' />
will create a label with red text on background with default color.
+:
- more intuitive than Solution 1
- leverages the color palette mechanism to get correct hex codes for colors
-:
- complex API
- complex solution
Solution 3
- always pick an area that color goes to
- change other area colors automatically or using a color scheme: refactor(Divider): remove
type
prop #558 (comment) - accept overrides using variables
e.g.:
for Label
component:
- the
color
prop changes the background color and determines what text color complements the text color (same as SUIR) based on a tool / predefined color scheme (see: refactor(Divider): removetype
prop #558 (comment)) - we're using
color
variable to override the text color set bycolor
prop automatically when setting background color
API
<Label color='red' />
will create a label with light colored text that fits best on a red background color.
<Label color='white' />
will create a label with dark colored text that fits best on a white background.
<Label color='white' variables={{ color: 'yellow' }} />
will create a label with yellow text on white background.
+:
- leverages the color palette mechanism to get correct hex codes for colors
- simple API
-:
- complex solution
- error prone (wrong colors in scheme) and hard to maintain
- can get confusing since variables would override props, we can consider other override mechanisms