|
1 |
| -# react-css-component |
2 |
| -Inject CSS with a Component |
| 1 | +# CSS as a Component |
| 2 | + |
| 3 | +A single React component to inject CSS with ease.<br> |
| 4 | +It works with SSR (even using React 16's [renderToNodeStream](https://reactjs.org/docs/react-dom-server.html#rendertonodestream)) and client-side rendering out-of-the-box. |
| 5 | + |
| 6 | +<img alt="TravisCI" src="https://travis-ci.org/rofrischmann/react-css-component.svg?branch=master"> <a href="https://codeclimate.com/github/rofrischmann/react-css-component/coverage"><img alt="Test Coverage" src="https://codeclimate.com/github/rofrischmann/react-css-component/badges/coverage.svg"></a> <img alt="npm version" src="https://badge.fury.io/js/react-css-component.svg"> <img alt="npm downloads" src="https://img.shields.io/npm/dm/react-css-component.svg"> <img alt="dependencies" src="https://david-dm.org/rofrischmann/react-css-component.svg"> |
| 7 | + |
| 8 | +## Support Me |
| 9 | +If you're using [Robin Frischmann](https://rofrischmann.de)'s work, please consider supporting his [Open Source Projects](https://github.com/rofrischmann) on [**Patreon**](https://www.patreon.com/rofrischmann). |
| 10 | + |
| 11 | +## Installation |
| 12 | +```sh |
| 13 | +# yarn |
| 14 | +yarn add react-css-component |
| 15 | + |
| 16 | +# npm |
| 17 | +npm i --save react-css-component |
| 18 | +``` |
| 19 | +It requires `react` and `prop-types` to be present. |
| 20 | + |
| 21 | +## Why? |
| 22 | +This package is the result of a [tweet](https://twitter.com/kentcdodds/status/972268883339108352) by [Kent C. Dodds](https://twitter.com/kentcdodds).<br> |
| 23 | +Creating resuable React components in general is pretty easy, but adding styling is not. The simplest way is to just use [inline style](https://reactjs.org/docs/dom-elements.html#style), which works pretty well for many basic components. Yet, it is very limited. Neither do we have have pseudo classes nor do we have media queries.<br> |
| 24 | +So, at some point, we need actual CSS. We could include a CSS file, but that would require an additional build step e.g. using [Webpack](https://webpack.js.org) in combination with its [css-loader](https://github.com/webpack-contrib/css-loader). While this would work, it's not very compelling to enforce a certain build tool, just to use a single component.<br> |
| 25 | +Alright, how about [CSS in JS](http://michelebertoli.github.io/css-in-js/)? Using plain JavaScript to style the component sounds great, as the component is written in JavaScript anyways. But, [most CSS in JS solutions are way to big to depend on](https://github.com/hellofresh/css-in-js-perf-tests#bundle-sizes). They're built for applications, not for independent reusable components. Yet, we can still benefit from writing our CSS in JavaScript. This package is the attempt to provide the smallest universal solution for inlining CSS in JavaScript possible. |
| 26 | + |
| 27 | +## How? |
| 28 | +Depending on whether [universal rendering](#universalrendering) (server-side rendering with client-side rehydration) or [client-side only rendering](#clientrendering) is used, the implementation uses a different logic. |
| 29 | + |
| 30 | +### Universal Rendering |
| 31 | +**Server Rendering**: The [UniversalStyle](#style) component renders a primitive *style* DOM element that uses `dangerouslySetInnerHTML` to inject a CSS string. |
| 32 | +**Client Rehydration**: At first, the [UniversalStyle](#style) component just gets rehydrated to match the server-side markup. But, as soon as it is about to unmount, the *style* element is copied to the `document.head` **once**. This will ensure it's existence just in case any other component using it's CSS is still visible. |
| 33 | + |
| 34 | +##### Caveat |
| 35 | +During server-side rendering, the [UniversalStyle](#style) component is not able to track it's own occurence, which may result in duplicate *style* elements. **This won't break anything, but also is not optimal**. To ensure that each [UniversalStyle](#style) instance is only rendered once, we need an unique cache on every render. This is achieved by passing a simple cache via React's context feature. Check the [StyleCacheProvider](#stylecacheprovider) component for more information. |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +### Client-Only Rendering |
| 40 | +If we only render on the client-side anyways, we can skip that complex flow and just use the [ClientStyle](#style).<br> |
| 41 | +It simply injects a *style* element into the `document.head` on instantiation and tracks its occurence using a global cache. |
| 42 | + |
| 43 | +## Style |
| 44 | +Both `UniversalStyle` and `ClientStyle` share the exact same component API.<br> |
| 45 | +This component is the core component and is used to inject the CSS. |
| 46 | + |
| 47 | +### Props |
| 48 | + |
| 49 | +| Parameter | Type | Description | |
| 50 | +| --- | --- | --- | |
| 51 | +| css | (*string*) | A CSS string that is rendered | |
| 52 | + |
| 53 | +### Import |
| 54 | +```javascript |
| 55 | +// universal rendering |
| 56 | +import { UniversalStyle as Style } from 'react-css-component' |
| 57 | + |
| 58 | +// client-only rendering |
| 59 | +import { ClientStyle as Style } from 'react-css-component' |
| 60 | +``` |
| 61 | + |
| 62 | +### Example |
| 63 | +```javascript |
| 64 | +const css = ` |
| 65 | + .button { |
| 66 | + background-color: darkblue; |
| 67 | + padding: 20px 10px; |
| 68 | + font-size: 16px; |
| 69 | + color: white |
| 70 | + } |
| 71 | +
|
| 72 | + .button:hover { |
| 73 | + background-color: blue |
| 74 | + } |
| 75 | +` |
| 76 | + |
| 77 | +// return an array to colocate style and the button |
| 78 | +const Button = () => [ |
| 79 | + <Style css={css} /> |
| 80 | + <button className="button">Click Me!</button> |
| 81 | +] |
| 82 | +``` |
| 83 | + |
| 84 | +## StyleCacheProvider |
| 85 | +The StyleCacheProvider is used to prevent duplication. It is not required, but recommended.<br> |
| 86 | +It should wrap your whole application and is only required once, no matter how many components use the [Style](#style) component.<br> |
| 87 | +It does not alter the resulting DOM structure as it simply renders its children.<br> |
| 88 | +It passes an empty cache object via React's context feature. |
| 89 | + |
| 90 | +> **Tip**: If you're using the [UniversalStyle](#style) component for a reusable shared component, make sure to inform your users about the caveat of not using this component. |
| 91 | +
|
| 92 | +### Example |
| 93 | +```javascript |
| 94 | +import { StyleCacheProvider } from 'react-css-component' |
| 95 | + |
| 96 | +const App = () => ( |
| 97 | + <StyleCacheProvider> |
| 98 | + <Button /> |
| 99 | + </StyleCacheProvider> |
| 100 | +) |
| 101 | +``` |
| 102 | + |
| 103 | +## License |
| 104 | +react-css-component is licensed under the [MIT License](http://opensource.org/licenses/MIT).<br> |
| 105 | +Documentation is licensed under [Creative Common License](http://creativecommons.org/licenses/by/4.0/).<br> |
| 106 | +Created with ♥ by [@rofrischmann](http://rofrischmann.de). |
0 commit comments