Skip to content

Commit

Permalink
feat: 🎸 improve hydrate addon
Browse files Browse the repository at this point in the history
Allow to hydrate arbitrary number of stylesheets, expose .hydrate()
method
  • Loading branch information
streamich committed Jul 10, 2018
1 parent 8fb46c0 commit 511b293
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
21 changes: 21 additions & 0 deletions .storybook/hydrate.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {createElement as h} from 'react';
import {storiesOf} from '@storybook/react';
const {action} = require('@storybook/addon-actions');
const {linkTo} = require('@storybook/addon-links');
const {create} = require('../index');
const {addon: addonHydrate} = require('../addon/hydrate');

const nano1 = create();
nano1.put('.hydrate-test', {color: 'red'});

const nano2 = create({
sh: nano1.sh
});
addonHydrate(nano2);
// nano2.hydrate(nano1.sh);
nano2.put('.hydrate-test', {color: 'red'});

storiesOf('Addons/hydrate()', module)
.add('Default', () =>
h('div', {}, 'Should log hydration info')
)
21 changes: 8 additions & 13 deletions addon/hydrate.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
'use strict';

exports.addon = function (renderer, stylesheet) {
exports.addon = function (renderer) {
if (process.env.NODE_ENV !== 'production') {
require('./__dev__/warnOnMissingDependencies')('hydrate', renderer, ['put']);
}

if (renderer.client) {
var hydrated = {};
stylesheet = stylesheet || renderer.sh;

if (!stylesheet) {
if (process.env.NODE_ENV !== 'production') {
console.error('Hydration style sheet was not found.');
}
var hydrated = {};

return;
}

var cssRules = stylesheet.sheet.cssRules;
renderer.hydrate = function (sh) {
var cssRules = sh.cssRules || sh.sheet.cssRules;

for (var i = 0; i < cssRules.length; i++)
hydrated[cssRules[i].selectorText] = 1;
};

if (renderer.client) {
if (renderer.sh) renderer.hydrate(renderer.sh);

var put = renderer.put;

Expand Down
15 changes: 15 additions & 0 deletions docs/hydrate.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ const nano = create({

That's it! `nano-css` will not inject CSS rules are already present in the style sheet.

You can also manually hydrate any stylesheet or external stylesheet you might have created using [`extract`](./extract.md) addon.

Let's say you have and external style sheet:

```html
<link rel="stylesheet" type="text/css" href="extracted.css" id="extracted-css">
```

You can hydrate it like so:

```js
nano.hydrate(document.getElementById('extracted-css'));
```

*P.S. Currently, does not hydrate media queries or animation keyframes.*

## Installation

Expand Down
4 changes: 3 additions & 1 deletion docs/put.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ It maps 1-to-1 to analogous CSS.

```css
.selector {
color: red
color: red;
}

.selector:hover {
Expand Down Expand Up @@ -92,3 +92,5 @@ and camel-case property syntax.
```

It also supports *Atoms* via the [`atoms`](./atoms.md) addon and "chaining" using the [`snake`](./snake.md) addon.
You might also want to take a look at [`stylis`](./stylis.md) addon that supports writing CSS as a string and
[`tachyons`](./tachyons.md) addon that allows to chain *tachyons*.
2 changes: 2 additions & 0 deletions docs/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const Button = style('button', {
background: props.primary ? 'blue' : 'grey',
fontSize: props.small ? '12px' : '16px'
}));

<Button>Click me!</Button>
```

`style()` has the following signature:
Expand Down

0 comments on commit 511b293

Please sign in to comment.