Skip to content

Commit 1ac8324

Browse files
authored
feat: allow theming for text (#671)
* feat: allow theming for text
1 parent 2eb6518 commit 1ac8324

File tree

4 files changed

+67
-33
lines changed

4 files changed

+67
-33
lines changed

src/components/label/label.visualroute.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ export const component = ({ themes }) => (
1818
Hello
1919
</Label>
2020
</Spec>
21-
<ThemeProvider theme={themes.darkTheme}>
22-
<Spec label="when inverted">
23-
<Label tone="inverted">Hello</Label>
21+
<ThemeProvider theme={{ ...themes.darkTheme, colorSurface: 'purple' }}>
22+
<Spec label="when inverted" omitPropsList={true}>
23+
<ThemeProvider theme={themes.darkTheme}>
24+
<Label tone="inverted">Hello</Label>
25+
</ThemeProvider>
2426
</Spec>
2527
</ThemeProvider>
2628
</Suite>

src/components/typography/text/text.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const Headline = props => {
3030
const HeadlineElement = props.elementType;
3131
return (
3232
<HeadlineElement
33-
css={headlineStyles(props)}
33+
css={theme => headlineStyles(props, theme)}
3434
title={props.title}
3535
{...filterDataAttributes(props)}
3636
>
@@ -57,7 +57,7 @@ const Subheadline = props => {
5757
return (
5858
<SubheadlineElement
5959
title={props.title}
60-
css={subheadlineStyles(props)}
60+
css={theme => subheadlineStyles(props, theme)}
6161
{...filterDataAttributes(props)}
6262
>
6363
{props.intlMessage ? (
@@ -87,7 +87,11 @@ Subheadline.propTypes = {
8787
};
8888

8989
const Wrap = props => (
90-
<div css={wrapStyles()} title={props.title} {...filterDataAttributes(props)}>
90+
<div
91+
css={theme => wrapStyles(props, theme)}
92+
title={props.title}
93+
{...filterDataAttributes(props)}
94+
>
9195
{props.intlMessage ? (
9296
<FormattedMessage {...props.intlMessage} />
9397
) : (
@@ -106,7 +110,7 @@ Wrap.propTypes = {
106110
const Body = props =>
107111
props.isInline ? (
108112
<span
109-
css={bodyStyles(props)}
113+
css={theme => bodyStyles(props, theme)}
110114
title={props.title}
111115
{...filterDataAttributes(props)}
112116
>
@@ -118,7 +122,7 @@ const Body = props =>
118122
</span>
119123
) : (
120124
<p
121-
css={bodyStyles(props)}
125+
css={theme => bodyStyles(props, theme)}
122126
title={props.title}
123127
{...filterDataAttributes(props)}
124128
>
@@ -151,7 +155,7 @@ Body.propTypes = {
151155

152156
const Detail = props => (
153157
<small
154-
css={detailStyles(props)}
158+
css={theme => detailStyles(props, theme)}
155159
title={props.title}
156160
{...filterDataAttributes(props)}
157161
className={props.className}

src/components/typography/text/text.styles.js

+35-24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { css } from '@emotion/core';
22
import vars from '../../../../materials/custom-properties';
33

4-
const baseStyles = `
5-
font-family: ${vars.fontFamilyDefault};
6-
color: ${vars.colorSolid};
4+
const getBaseStyles = (props, theme) => {
5+
const overwrittenVars = {
6+
...vars,
7+
...theme,
8+
};
9+
10+
return `
11+
font-family: ${vars.fontFamilyDefault};
12+
color: ${overwrittenVars.colorSolid};
713
`;
14+
};
815

916
const truncate = `
1017
white-space: nowrap;
@@ -24,22 +31,26 @@ const inline = `
2431
display: inline-block;
2532
`;
2633

27-
const getTone = tone => {
34+
const getTone = (tone, theme) => {
35+
const overwrittenVars = {
36+
...vars,
37+
...theme,
38+
};
2839
switch (tone) {
2940
case 'information':
30-
return `color: ${vars.colorInfo};`;
41+
return `color: ${overwrittenVars.colorInfo};`;
3142
case 'secondary':
32-
return `color: ${vars.colorNeutral60};`;
43+
return `color: ${overwrittenVars.colorNeutral60};`;
3344
case 'positive':
34-
return `color: ${vars.colorPrimary25};`;
45+
return `color: ${overwrittenVars.colorPrimary25};`;
3546
case 'primary':
36-
return `color: ${vars.colorPrimary};`;
47+
return `color: ${overwrittenVars.colorPrimary};`;
3748
case 'negative':
38-
return `color: ${vars.colorError};`;
49+
return `color: ${overwrittenVars.colorError};`;
3950
case 'inverted':
40-
return `color: ${vars.colorSurface};`;
51+
return `color: ${overwrittenVars.colorSurface};`;
4152
case 'warning':
42-
return `color: ${vars.colorWarning};`;
53+
return `color: ${overwrittenVars.colorWarning};`;
4354
default:
4455
return ``;
4556
}
@@ -62,47 +73,47 @@ const getElementFontSize = elementType => {
6273
}
6374
};
6475

65-
export const bodyStyles = props => css`
66-
${baseStyles}
76+
export const bodyStyles = (props, theme) => css`
77+
${getBaseStyles(props, theme)}
6778
margin: 0;
6879
font-size: 1rem;
6980
${props.isBold && bold}
7081
${props.isItalic && italic}
71-
${props.tone && getTone(props.tone)}
82+
${props.tone && getTone(props.tone, theme)}
7283
${props.truncate && truncate}
7384
`;
7485

75-
export const headlineStyles = props => css`
76-
${baseStyles}
86+
export const headlineStyles = (props, theme) => css`
87+
${getBaseStyles(props, theme)}
7788
margin: 0;
7889
font-size: ${getElementFontSize(props.elementType)};
7990
font-weight: 300;
8091
${props.truncate && truncate}
8192
`;
8293

83-
export const subheadlineStyles = props => css`
84-
${baseStyles}
94+
export const subheadlineStyles = (props, theme) => css`
95+
${getBaseStyles(props, theme)}
8596
margin: 0;
8697
font-size: ${getElementFontSize(props.elementType)};
8798
font-weight: normal;
8899
${props.truncate && truncate}
89100
${props.isBold && bold}
90-
${props.tone && getTone(props.tone)}
101+
${props.tone && getTone(props.tone, theme)}
91102
`;
92103

93-
export const wrapStyles = () => css`
94-
${baseStyles}
104+
export const wrapStyles = (props, theme) => css`
105+
${getBaseStyles(props, theme)}
95106
font-size: 1rem;
96107
white-space: pre-wrap;
97108
`;
98109

99-
export const detailStyles = props => css`
100-
${baseStyles}
110+
export const detailStyles = (props, theme) => css`
111+
${getBaseStyles(props, theme)}
101112
display: block;
102113
font-size: 0.9231rem;
103114
${props.isInline && inline}
104115
${props.isBold && bold}
105116
${props.isItalic && italic}
106-
${props.tone && getTone(props.tone)}
117+
${props.tone && getTone(props.tone, theme)}
107118
${props.truncate && truncate}
108119
`;

src/components/typography/text/text.visualroute.js

+17
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,22 @@ export const component = ({ themes }) => (
202202
<Spec label="Detail (intl message)">
203203
<Text.Detail intlMessage={intlMessage} />
204204
</Spec>
205+
<ThemeProvider theme={themes.darkTheme}>
206+
<Spec label="Headline (dark theme)">
207+
<Text.Headline elementType="h1">Dark theme</Text.Headline>
208+
</Spec>
209+
<Spec label="Subheadline (dark theme)">
210+
<Text.Subheadline elementType="h4">Dark theme</Text.Subheadline>
211+
</Spec>
212+
<Spec label="Body (dark theme)">
213+
<Text.Body>Dark theme</Text.Body>
214+
</Spec>
215+
<Spec label="Detail (dark theme)">
216+
<Text.Detail>Dark theme</Text.Detail>
217+
</Spec>
218+
<Spec label="Wrap (dark theme)">
219+
<Text.Wrap>Dark theme</Text.Wrap>
220+
</Spec>
221+
</ThemeProvider>
205222
</Suite>
206223
);

0 commit comments

Comments
 (0)