Skip to content

Commit 79a0a36

Browse files
committed
feat: 🎸 warn user on clashing block names
1 parent 511b293 commit 79a0a36

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed

‎.storybook/put.stories.js

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ put('.red-border', {
1717
}
1818
});
1919

20+
put('.check-same-name-warning', {color: 'red'});
21+
put('.check-same-name-warning', {color: 'red'});
22+
2023
storiesOf('Addons/put()', module)
2124
.add('Default', () =>
2225
h('div', {className: 'red-border'}, 'Hello world')

‎.storybook/rule.stories.js

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ const className1 = rule({
1313
border: '1px solid red'
1414
}, 'RedBorder');
1515

16+
// Chech if generating multiple times same styles result in clash of CSS selector.
17+
rule({
18+
border: '1px solid red'
19+
}, 'Rule');
20+
rule({
21+
border: '1px solid red'
22+
}, 'Rule');
23+
1624
storiesOf('Addons/rule()', module)
1725
.add('Default', () =>
1826
h('div', {className: className1}, 'Hello world')

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ __Motto of `nano-css` is simple__: *create the smallest possible CSS-in-JS libra
3636
- [`atoms`](./docs/atoms.md) — [*demo!*](https://codesandbox.io/s/rlkxl6o9v4)
3737
- [`nesting`](./docs/nesting.md)
3838
- [`keyframes()`](./docs/keyframes.md)
39-
- [`hydrate`](./docs/hydrate.md)
39+
- [`hydrate()`](./docs/hydrate.md)
4040
- [`prefixer`](./docs/prefixer.md)
4141
- [`stylis`](./docs/stylis.md)
4242
- [`unitless`](./docs/unitless.md)

‎addon/rule.js

+24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,31 @@ exports.addon = function (renderer) {
55
require('./__dev__/warnOnMissingDependencies')('rule', renderer, ['put']);
66
}
77

8+
var blocks;
9+
10+
if (process.env.NODE_ENV !== 'production') {
11+
blocks = {};
12+
}
13+
814
renderer.rule = function (css, block) {
15+
// Warn user if CSS selectors clash.
16+
if (process.env.NODE_ENV !== 'production') {
17+
if (block) {
18+
if (typeof block !== 'string') {
19+
throw new TypeError(
20+
'nano-css block name must be a string. ' +
21+
'For example, use nano.rule({color: "red", "RedText").'
22+
);
23+
}
24+
25+
if (blocks[block]) {
26+
console.error('Block name "' + block + '" used more than once.');
27+
}
28+
29+
blocks[block] = 1;
30+
}
31+
}
32+
933
block = block || renderer.hash(css);
1034
block = renderer.pfx + block;
1135
renderer.put('.' + block, css);

‎docs/Addons.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ plenty more to chose from. Below is a list of addons shipped with `nano-css`.
2020
- [`atoms`](./atoms.md) — CSS shorthands for better DX
2121
- [`nesting`](./nesting.md) — better nesting features
2222
- [`keyframes()`](./keyframes.md) — adds `@keyframes` support
23-
- [`hydrate`](./hydrate.md) — adds support for re-hydration on client side
23+
- [`hydrate()`](./hydrate.md) — adds support for re-hydration on client side
2424
- [`prefixer`](./prefixer.md) — auto-prefixes your styles with correct vendor prefixes
2525
- [`stylis`](./stylis.md) — write CSS as strings
2626
- [`unitless`](./unitless.md) — automatically adds `px` to styles

‎docs/keyframes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const className = rule({
3838

3939
As a second argument, `keyframes` addon can accept a configuration object with the following keys:
4040

41-
- `prefixes` — optional, array of prefixes to add to `@keyframes` at-rule, defaults to `['-webkit-', '-moz-', '-o-', '']`.
41+
- `prefixes` — optional, array of prefixes, defaults to `['-webkit-', '-moz-', '-o-', '']`.
4242

4343

4444
## Installation

‎docs/virtual.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const classNames2 = nano.rule({
1717
border: '1px solid red',
1818
});
1919
// _b
20+
21+
<div className={classNames1} /> // <div class="_a _b _c" />
22+
<div className={classNames2} /> // <div class="_b" />
2023
```
2124

2225
## Installation

‎index.js

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ var hash = function (str) {
1313
exports.create = function (config) {
1414
config = config || {};
1515
var assign = config.assign || Object.assign;
16-
1716
var client = typeof window === 'object';
1817

1918
// Check if we are really in browser environment.

0 commit comments

Comments
 (0)