|
1 | 1 | # react-sugar-styled
|
2 | 2 | 
|
3 |
| -> 🍬 Tools and magic numbers for styled-components theme used within roast-cms React components. |
| 3 | +> 🍬 Customizable and extendable theming dictionary for Styled Components. |
4 | 4 |
|
5 |
| -### What is this? |
| 5 | + |
6 | 6 |
|
7 |
| -This is a list of defaults (colours, sizes, fonts etc.) which all or most React components under `roast-cms` use with help from [https://github.com/styled-components/styled-components](styled-components). |
8 |
| - |
9 |
| -### What you will need. |
10 |
| -This package requires `styled-components`. |
| 7 | +- Create **more flexible designs** with React and Styled Components. |
| 8 | +- **Pre-built design system** that works across all your projects. |
| 9 | +- **Focus on design**, not variables. |
11 | 10 |
|
| 11 | +## Installation: |
12 | 12 | ```
|
13 |
| -# first you'll need the package: |
14 | 13 | yarn add @roast-cms/react-sugar-styled
|
15 | 14 | ```
|
| 15 | +Then, in your project: |
| 16 | +```javascript |
| 17 | +import styled, { ThemeProvider } from "styled-components" |
| 18 | +import { Sugar } from "@roast-cms/react-sugar-styled" |
| 19 | +// |
| 20 | +// you can access the dictionary values from Styled Components within |
| 21 | +// any component that's wrapped in <ThemeProvider /> |
| 22 | +const MyComponent = styled.div` |
| 23 | + color: ${props => props.theme.color.brand()}; |
| 24 | +` |
| 25 | +// |
| 26 | +// pass the dictionary Sugar() as a theme to your app: |
| 27 | +const App = props => |
| 28 | + <ThemeProvider theme={Sugar()}><MyComponent /></ThemeProvider> |
| 29 | +``` |
| 30 | + |
| 31 | +## Why? |
| 32 | +[Styled Components](https://github.com/styled-components/styled-components) lets you apply CSS to your React components with ease. Their [theming support](https://www.styled-components.com/docs/advanced#theming) adds a level of flexibility and organization to your application. You can make your components visually behave in an organized fashion, while remaining independent. |
| 33 | + |
| 34 | +Theming in this context works as defining a dictionary of values which could be reused across the children components. **However**, what those values are is a mystery until you finish building your project. |
| 35 | + |
| 36 | +`react-sugar-styled` provides you with a set of defaults that you can easily customize and extend. They include basics like colours, fonts, key sizes, and more. A set of convenience functions also helps you with responsive design (breakpoints), as well as size and colour modifications. |
| 37 | + |
| 38 | +## Theme globals. |
| 39 | +All of the values in the theme object are accessible across all your components which are children of `<ThemeProvider/>`. Here's what they do and how to access them: |
| 40 | + |
| 41 | +To access a value within your CSS (that you wrote with Styled Components) simply include `${props => props.theme.[OBJECT REFERENCE]}`, where **OBJECT REFERENCE** is one of the following: |
| 42 | + |
| 43 | +#### Colours |
| 44 | + |
| 45 | +**OBJECT REFERENCE** | Description | Usage Example |
| 46 | +--- | --- | --- |
| 47 | +`color.brand(alpha)` | An rgba value for your "special" theme colour. | `color: ${props => props.theme.color.brand()};` |
| 48 | +`color.foreground(alpha)` | An rgba value for your foreground colour, typically the colour of your body text. | `color: ${props => props.theme.color.foreground()};` |
| 49 | +`color.background(alpha)` | An rgba value for your background colour, typically the colour of your background. | `background: ${props => props.theme.color.background()};` |
| 50 | + |
| 51 | +#### Typography |
| 52 | + |
| 53 | +**OBJECT REFERENCE** | Description | Usage Example |
| 54 | +--- | --- | --- |
| 55 | +`typography.title.auto` | Return CSS that sets `color`, `font-family`, `letter-spacing`, `line-height`, `font-weight`, and `margin` designed for title/heading tags. | `h1 { ${props => props.theme.typography.title.auto} }` |
| 56 | +`typography.text.auto` | Same as above, but for body text. | `p { ${props => props.theme.typography.text.auto} }` |
| 57 | + |
| 58 | +#### Block measuring sizes |
| 59 | +**OBJECT REFERENCE** | Description | Usage Example |
| 60 | +--- | --- | --- |
| 61 | +`size.block.padding` | Returns a number meant to be used as `em` value for standard padding width | `padding: ${props => props.theme.size.block.padding}em;` |
| 62 | +`size.block.padding` | Returns a number meant to be used as `em` value for standard spacing between blocks | `margin: ${props => props.theme.size.block.spacing}em;` |
| 63 | +`size.block.border` | Returns a number meant to be used as `px` value for standard border widths | `border: ${props => props.theme.size.block.border}px solid;` |
| 64 | +`size.block.column.m` | Returns a number meant to be used as `px` value for a medium-width content column | `article { width: ${props => props.theme.size.column.m} }` |
| 65 | +`size.block.column.l` | Same as above, but for wider screens | `article { width: ${props => props.theme.size.column.l} }` |
| 66 | + |
| 67 | +#### Complete list can be found [here](/THEME_GLOBALS.md) |
| 68 | + |
| 69 | +## Customizing & extending the theme. |
| 70 | +Although you can immediately start designing your components using the default theme dictionary, you may want to customize it. Luckily there's an easy way to do this: |
16 | 71 |
|
17 |
| -### How to use. |
18 | 72 | ```javascript
|
19 |
| -import { ThemeProvider } from "styled-components" |
20 |
| -import { Sugar } from "../src/index" |
| 73 | +<ThemeProvider |
| 74 | + theme={Sugar({ |
| 75 | + color_brand: "rgb(189,67,54)", |
| 76 | + color_background: "rgb(44,44,44)", |
| 77 | + color_foreground: "rgb(224,213,255)", |
| 78 | + // |
| 79 | + font_heading: "'Yanone Kaffeesatz', sans-serif", |
| 80 | + font_heading_weight: 400, |
| 81 | + // |
| 82 | + font_body: "'Lobster Two', serif", |
| 83 | + // |
| 84 | + size_base: 28, |
| 85 | + size_column_medium: 700, |
| 86 | + size_column_large: 900, |
| 87 | + size_block_padding: 2, |
| 88 | + size_block_spacing: 1.5, |
| 89 | + size_block_border: 10, |
| 90 | + // |
| 91 | + effects_border_radius: 1 |
| 92 | + }) |
| 93 | + } |
| 94 | +> |
| 95 | +{/* components */} |
| 96 | +</ThemeProvider> |
| 97 | +``` |
21 | 98 |
|
22 |
| -const ThemedComponent = props => |
23 |
| - <ThemeProvider theme={Sugar}> |
24 |
| - Whatever. |
25 |
| - </ThemeProvider> |
| 99 | +You can also extend the theme with your own dictionary values or system using ES6 spread operator: |
| 100 | + |
| 101 | +```javascript |
| 102 | +<ThemeProvider |
| 103 | + theme={{ |
| 104 | + ...Sugar(), |
| 105 | + font_special: "'Indie Flower', cursive" |
| 106 | + }} |
| 107 | +> |
| 108 | +{/* components */} |
| 109 | +</ThemeProvider> |
26 | 110 | ```
|
27 | 111 |
|
28 |
| -### Contributing |
29 |
| -PRs and issue reports are welcome. Please submit all PRs to `develop` branch. To test, run `yarn start` |
| 112 | +## Contributions welcome! |
| 113 | +To get started with the code: clone the repo, run `yarn install` then `yarn start` and open up `http://localhost:3002` in your browser. |
0 commit comments