Skip to content

Commit

Permalink
feat: 🎸 warn user on clashing block names
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jul 10, 2018
1 parent 511b293 commit 79a0a36
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .storybook/put.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ put('.red-border', {
}
});

put('.check-same-name-warning', {color: 'red'});
put('.check-same-name-warning', {color: 'red'});

storiesOf('Addons/put()', module)
.add('Default', () =>
h('div', {className: 'red-border'}, 'Hello world')
Expand Down
8 changes: 8 additions & 0 deletions .storybook/rule.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const className1 = rule({
border: '1px solid red'
}, 'RedBorder');

// Chech if generating multiple times same styles result in clash of CSS selector.
rule({
border: '1px solid red'
}, 'Rule');
rule({
border: '1px solid red'
}, 'Rule');

storiesOf('Addons/rule()', module)
.add('Default', () =>
h('div', {className: className1}, 'Hello world')
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ __Motto of `nano-css` is simple__: *create the smallest possible CSS-in-JS libra
- [`atoms`](./docs/atoms.md) — [*demo!*](https://codesandbox.io/s/rlkxl6o9v4)
- [`nesting`](./docs/nesting.md)
- [`keyframes()`](./docs/keyframes.md)
- [`hydrate`](./docs/hydrate.md)
- [`hydrate()`](./docs/hydrate.md)
- [`prefixer`](./docs/prefixer.md)
- [`stylis`](./docs/stylis.md)
- [`unitless`](./docs/unitless.md)
Expand Down
24 changes: 24 additions & 0 deletions addon/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,31 @@ exports.addon = function (renderer) {
require('./__dev__/warnOnMissingDependencies')('rule', renderer, ['put']);
}

var blocks;

if (process.env.NODE_ENV !== 'production') {
blocks = {};
}

renderer.rule = function (css, block) {
// Warn user if CSS selectors clash.
if (process.env.NODE_ENV !== 'production') {
if (block) {
if (typeof block !== 'string') {
throw new TypeError(
'nano-css block name must be a string. ' +
'For example, use nano.rule({color: "red", "RedText").'
);
}

if (blocks[block]) {
console.error('Block name "' + block + '" used more than once.');
}

blocks[block] = 1;
}
}

block = block || renderer.hash(css);
block = renderer.pfx + block;
renderer.put('.' + block, css);
Expand Down
2 changes: 1 addition & 1 deletion docs/Addons.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plenty more to chose from. Below is a list of addons shipped with `nano-css`.
- [`atoms`](./atoms.md) — CSS shorthands for better DX
- [`nesting`](./nesting.md) — better nesting features
- [`keyframes()`](./keyframes.md) — adds `@keyframes` support
- [`hydrate`](./hydrate.md) — adds support for re-hydration on client side
- [`hydrate()`](./hydrate.md) — adds support for re-hydration on client side
- [`prefixer`](./prefixer.md) — auto-prefixes your styles with correct vendor prefixes
- [`stylis`](./stylis.md) — write CSS as strings
- [`unitless`](./unitless.md) — automatically adds `px` to styles
Expand Down
2 changes: 1 addition & 1 deletion docs/keyframes.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const className = rule({

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

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


## Installation
Expand Down
3 changes: 3 additions & 0 deletions docs/virtual.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const classNames2 = nano.rule({
border: '1px solid red',
});
// _b

<div className={classNames1} /> // <div class="_a _b _c" />
<div className={classNames2} /> // <div class="_b" />
```

## Installation
Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var hash = function (str) {
exports.create = function (config) {
config = config || {};
var assign = config.assign || Object.assign;

var client = typeof window === 'object';

// Check if we are really in browser environment.
Expand Down

0 comments on commit 79a0a36

Please sign in to comment.