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

Commit 4f32f74

Browse files
author
Andrey Okonetchnikov
committed
feat: Typography component shows text styles and not just font sizes
BREAKING CHANGE: `FontSizeSwatch` is now `TextStyleSwatch` and accepts text style objects as `value`.
1 parent 2eaf22b commit 4f32f74

File tree

10 files changed

+135
-54
lines changed

10 files changed

+135
-54
lines changed

src/components/FontSizeSwatch.jsx

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/components/FontSizeSwatch.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/components/TextStyleSwatch.jsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from "react";
2+
import { css } from "theme-ui";
3+
import { valuePropType } from "../propTypes";
4+
5+
/**
6+
* A swatch to render a `textStyle` variant. Provide the sample text as `children`.
7+
* @param value
8+
* @param componentCSS
9+
* @param rest
10+
* @return React.Element
11+
* @constructor
12+
*/
13+
const TextStyleSwatch = ({ value, css: componentCSS, ...rest }) => (
14+
<p
15+
css={css({
16+
alignSelf: "center",
17+
m: 0,
18+
...value,
19+
...componentCSS
20+
})}
21+
{...rest}
22+
/>
23+
);
24+
25+
TextStyleSwatch.propTypes = {
26+
...valuePropType
27+
};
28+
29+
/** @component */
30+
export default TextStyleSwatch;

src/components/TextStyleSwatch.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```jsx harmony
2+
import theme from "../theme";
3+
4+
<TextStyleSwatch token={"heading"} value={theme.textStyles.heading}>
5+
Aa
6+
</TextStyleSwatch>;
7+
```

src/components/Typography.jsx

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
import React from "react";
2-
import { ThemeProvider, css } from "theme-ui";
2+
import { ThemeProvider } from "theme-ui";
33
import Stack from "stack-styled/emotion/Stack.js";
4-
import {
5-
FontSizeSwatch,
6-
Swatch,
7-
Swatches,
8-
SwatchToken,
9-
SwatchValue
10-
} from "../index";
4+
import { Swatch, Swatches, SwatchToken, TextStyleSwatch } from "../";
115

6+
/**
7+
* Typography component showcases all available text styles defined in `theme.textStyles`
8+
* object of [styled-system theme](https://styled-system.com/table#variants).
9+
* @param theme
10+
* @return React.Element
11+
* @constructor
12+
*/
1213
export default function Typography({ theme }) {
1314
return (
1415
<ThemeProvider theme={theme}>
1516
<Stack gap={5}>
16-
<Swatches theme={theme} items={theme.fontSizes}>
17+
<Swatches theme={theme} items={theme.textStyles || {}}>
1718
{(token, value) => (
1819
<Swatch token={token} value={value} key={token}>
1920
<Stack gap={2}>
2021
<div>
21-
<SwatchToken css={{ display: "inline-block" }}>
22+
<SwatchToken
23+
css={{ display: "inline-block", fontFamily: "monospace" }}
24+
>
2225
{token}
23-
</SwatchToken>{" "}
24-
<SwatchValue css={{ display: "inline-block" }}>
25-
({value})
26-
</SwatchValue>
26+
</SwatchToken>
2727
</div>
28-
<FontSizeSwatch
29-
token={token}
30-
value={value}
31-
css={css({ color: "text" })}
32-
>
28+
<TextStyleSwatch token={token} value={value}>
3329
The quick brown fox jumps over the lazy dog
34-
</FontSizeSwatch>
30+
</TextStyleSwatch>
3531
</Stack>
3632
</Swatch>
3733
)}

src/components/Typography.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,17 @@ import theme from "../theme";
33

44
<Typography theme={theme} />;
55
```
6+
7+
Starting from v5 of styled-system, you can co-locate variants inside your components.
8+
9+
Consider this is your `Text` component that defines text styles:
10+
11+
```js { "file": "../../examples/Text.js" }
12+
```
13+
14+
```jsx harmony
15+
import theme from "../theme";
16+
import { textStyles } from "../examples/Text";
17+
18+
<Typography theme={{ ...theme, textStyles }} />;
19+
```

src/examples/Text.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import styled from "@emotion/styled";
2+
import { css } from "@styled-system/css";
3+
import { typography, variant, compose } from "styled-system";
4+
5+
export const textStyles = {
6+
body: {
7+
fontFamily: "body",
8+
fontSize: "md",
9+
fontWeight: "normal",
10+
color: "text"
11+
},
12+
heading: {
13+
fontFamily: "heading",
14+
fontSize: "xl",
15+
fontWeight: "bold",
16+
color: "secondary"
17+
}
18+
};
19+
20+
const Text = styled.p(
21+
css({
22+
m: 0,
23+
lineHeight: 1.5
24+
}),
25+
compose(
26+
variant({
27+
prop: "textStyle",
28+
variants: textStyles
29+
}),
30+
typography
31+
)
32+
);
33+
34+
export default Text;

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export { default as Swatch } from "./components/Swatch";
33
export { default as SwatchToken } from "./components/SwatchToken";
44
export { default as SwatchValue } from "./components/SwatchValue";
55
export { default as ColorSwatch } from "./components/ColorSwatch";
6-
export { default as FontSizeSwatch } from "./components/FontSizeSwatch";
6+
export { default as TextStyleSwatch } from "./components/TextStyleSwatch";
77
export { default as PaletteSwatch } from "./components/PaletteSwatch";
88
export { default as SpacingSwatch } from "./components/SpacingSwatch";
99
export { default as Colors } from "./components/Colors";

src/theme.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ export default {
2525
heading: '"Avenir Next", sans-serif',
2626
monospace: "Menlo, monospace"
2727
},
28+
textStyles: {
29+
body: {
30+
fontFamily: "body",
31+
fontSize: "md",
32+
color: "text"
33+
},
34+
heading: {
35+
fontSize: "xl",
36+
fontFamily: "heading",
37+
color: "secondary"
38+
}
39+
},
2840
space: [
2941
0,
3042
"0.125rem", // 2px

styleguide.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
const fs = require("fs");
12
const path = require("path");
23
const pkg = require("./package.json");
34

45
module.exports = {
56
title: pkg.name,
7+
serverPort: 6061,
68
styleguideDir: path.join(__dirname, "docs"),
79
getComponentPathLine(componentPath) {
810
const name = path.basename(componentPath, ".jsx");
@@ -28,5 +30,24 @@ module.exports = {
2830
],
2931
ribbon: {
3032
url: "https://github.com/component-driven/react-design-tokens"
33+
},
34+
updateExample(props, exampleFilePath) {
35+
// props.settings are passed by any fenced code block, in this case
36+
const { settings, lang } = props;
37+
// "../mySourceCode.js"
38+
if (typeof settings.file === "string") {
39+
// "absolute path to mySourceCode.js"
40+
const filepath = path.resolve(exampleFilePath, settings.file);
41+
// displays the block as static code
42+
settings.static = true;
43+
// no longer needed
44+
delete settings.file;
45+
return {
46+
content: fs.readFileSync(filepath, "utf8"),
47+
settings,
48+
lang
49+
};
50+
}
51+
return props;
3152
}
3253
};

0 commit comments

Comments
 (0)