Skip to content

Commit 4b99f7a

Browse files
authored
Merge pull request #24 from streamich/feat/font
Feat/font
2 parents bdb5f3d + 73259ef commit 4b99f7a

12 files changed

Lines changed: 126 additions & 30 deletions

File tree

.storybook/googleFont.stories.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {createElement as h} from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
const {action} = require('@storybook/addon-actions');
4+
const {linkTo} = require('@storybook/addon-links');
5+
const {create} = require('../index');
6+
const {addon: addonRule} = require('../addon/rule');
7+
const {addon: addonGoogleFont} = require('../addon/googleFont');
8+
9+
const nano = create();
10+
addonRule(nano);
11+
addonGoogleFont(nano);
12+
13+
nano.googleFont('Roboto');
14+
nano.googleFont('Roboto Slab', 700);
15+
16+
const className1 = nano.rule({
17+
fontFamily: '"Roboto"'
18+
}, 'Roboto');
19+
20+
const className2 = nano.rule({
21+
fontFamily: '"Roboto Slab"'
22+
}, 'Roboto_Slab');
23+
24+
storiesOf('Addons/googleFont', module)
25+
.add('Roboto', () =>
26+
h('div', {className: className1}, 'Hello world')
27+
)
28+
.add('Roboto Slab, bold', () =>
29+
h('div', {className: className2}, 'Hello world')
30+
)

.storybook/important.stories.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ addonImportant(renderer);
1212
const {rule} = renderer;
1313

1414
const className1 = rule({
15-
border: '1px solid red'
15+
border: '1px solid red'
1616
}, 'RedBorderImportant');
1717

1818
storiesOf('Addons/!important', module)
19-
.add('Default', () =>
20-
h('div', {className: className1}, 'Hello world')
21-
)
19+
.add('Default', () =>
20+
h('div', {className: className1}, 'Hello world')
21+
)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ __Tiny [5<sup>th</sup> generation](https://github.com/streamich/freestyler/blob/
4242
- [`animate/*`](./docs/animations.md)
4343
- [`reset/*`](./docs/resets.md)
4444
- [`reset-font`](./docs/reset-font.md)
45+
- [`googleFont()`](./docs/googleFont.md)
4546
- [Server-side rendering](./docs/SSR.md)
4647

4748

addon/googleFont.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
function createUrl (font, weights, subsets) {
4+
var params = '?family=' + encodeURIComponent(font);
5+
6+
if (weights) {
7+
if (!(weights instanceof Array))
8+
weights = [weights];
9+
10+
params += ':' + weights.join(',');
11+
}
12+
13+
if (subsets) {
14+
if (!(subsets instanceof Array))
15+
subsets = [subsets];
16+
17+
params += '&subset=' + subsets.join(',');
18+
}
19+
20+
return 'https://fonts.googleapis.com/css' + params;
21+
}
22+
23+
exports.addon = function (renderer) {
24+
if (process.env.NODE_ENV !== 'production') {
25+
require('./__dev__/warnOnMissingDependencies')('hydrate', renderer, ['put']);
26+
}
27+
28+
if (renderer.client) {
29+
renderer.googleFont = function (font, weights, subsets) {
30+
var el = document.createElement('link');
31+
32+
el.href = createUrl(font, weights, subsets);
33+
el.rel = 'stylesheet';
34+
el.type = 'text/css';
35+
36+
document.head.appendChild(el);
37+
};
38+
} else {
39+
renderer.googleFont = function (font, weights, subsets) {
40+
renderer.raw = "@import url('" + createUrl(font, weights, subsets) + "');" + renderer.raw;
41+
};
42+
}
43+
};

addon/hydrate.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
'use strict';
22

3-
exports.addon = function (renderer, id) {
4-
id = id || 'nano-css';
5-
3+
exports.addon = function (renderer) {
64
if (process.env.NODE_ENV !== 'production') {
75
require('./__dev__/warnOnMissingDependencies')('hydrate', renderer, ['put']);
86
}
97

108
if (renderer.client) {
119
var hydrated = {};
12-
var stylesheet = document.getElementById(id);
10+
var stylesheet = renderer.sh;
1311

1412
if (!stylesheet) {
1513
if (process.env.NODE_ENV !== 'production') {
16-
console.error('Hydration stylesheet with id "' + id + '" was not found.');
14+
console.error('Hydration style sheet was not found.');
1715
}
1816

1917
return;

addon/keyframes.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ exports.addon = function (renderer) {
1010
document.head.appendChild(renderer.ksh = document.createElement('style'))
1111
}
1212

13-
var putAtrule = renderer.putAtrule;
13+
var putAt = renderer.putAt;
1414

15-
renderer.putAtrule = function (__, keyframes, prelude) {
15+
renderer.putAt = function (__, keyframes, prelude) {
1616
// @keyframes
1717
if (prelude[1] === 'k') {
1818
var str = '';
@@ -38,14 +38,14 @@ exports.addon = function (renderer) {
3838
return;
3939
}
4040

41-
putAtrule(__, keyframes, prelude);
41+
putAt(__, keyframes, prelude);
4242
};
4343

4444
renderer.keyframes = function (keyframes, block) {
4545
if (!block) block = renderer.hash(keyframes);
4646
block = renderer.pfx + block;
4747

48-
renderer.putAtrule('', keyframes, '@keyframes ' + block);
48+
renderer.putAt('', keyframes, '@keyframes ' + block);
4949

5050
return block;
5151
};

docs/Addons.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ plenty more to chose from. Below is a list of addons shipped with `nano-css`.
2828
- [`:global`](./global.md) &mdash; allows emitting global styles
2929
- [`animate/*`](./animations.md) &mdash; collection of animation styles
3030
- [`reset/*`](./resets.md) &mdash; collection of CSS resets
31-
- [`reset-font`](./docs/reset-font.md) &mdash; global styles for better font
31+
- [`reset-font`](./reset-font.md) &mdash; global styles for better font
32+
- [`googleFont()`](./googleFont.md) &mdash; loads custom fonts from *Google Fonts*
3233

3334
## Addon Installation
3435

docs/SSR.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ html += `<style>${nano.raw}</style>`;
1212
## Re-hydrating
1313

1414
`nano-css` can re-hydrate server-side generated CSS. To do that, you need to install [`hydrate` addon](hydrate.md);
15-
and set `nano-css` id on your `<style>` element.
15+
and provide style sheet you want to hydrate to the `nano-css` instance when creating it.
1616

1717
```js
18+
const nano = create({
19+
sh: typeof document === 'object' ? document.getElementById('nano-css') : null
20+
});
21+
1822
html += `<style id="nano-css">${nano.raw}</style>`;
1923
```

docs/googleFont.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# `googleFont()` Addon
2+
3+
Creates `googleFont()` method that loads fonts from *Google Fonts*. Signature:
4+
5+
```ts
6+
googleFont(name: string, widths?: number | number[], subsets?: string | string[])
7+
```
8+
9+
10+
## Example
11+
12+
Below example loads `Roboto Slab` font at `400` and `700` widths, including `cyrillic` characters.
13+
14+
```js
15+
nano.googleFont('Roboto Slab', [400, 700], 'cyrillic');
16+
```
17+
18+
19+
## Installation
20+
21+
Simply install `googleFont` addon. Read more about the [Addon Installation](./Addons.md#addon-installation).

docs/hydrate.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@ Re-hydrates CSS styles generated on the server. On the server add `nano-css` id
66
html += `<style id="nano-css">${nano.raw}</style>`;
77
```
88

9-
That's it! `hydrate` addon will find this stylesheet in the browser and will not emit CSS
10-
rules that were already generated by the server.
11-
12-
13-
## Configuration
14-
15-
You can set a custom id for you style sheet.
9+
And when creating `nano-css` instance provide that style sheet in configuration.
1610

1711
```js
18-
import {addon as addonHydrate} from 'nano-css/addon/hydrate';
19-
20-
addonHydrate(nano, 'custom-id');
12+
const nano = create({
13+
sh: typeof document === 'object' ? document.getElementById('nano-css') : null
14+
});
2115
```
2216

17+
That's it!
18+
2319

2420
## Installation
2521

0 commit comments

Comments
 (0)