Skip to content

Commit

Permalink
refactor: major refactoring for addon model
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 14, 2018
1 parent 9f2a4f1 commit 864f67b
Show file tree
Hide file tree
Showing 26 changed files with 467 additions and 252 deletions.
2 changes: 1 addition & 1 deletion .storybook/atoms.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {linkTo} = require('@storybook/addon-links');
const {create} = require('../lib');
const {addon} = require('../addon/atoms');

const renderer = create(h);
const renderer = create({h});
addon(renderer);
const {rule} = renderer;

Expand Down
2 changes: 1 addition & 1 deletion .storybook/decorator.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {linkTo} = require('@storybook/addon-links');

import {create} from '../lib';
import {addon} from '../addon/decorator';
const renderer = create();
const renderer = create({h});
addon(renderer);
const {css} = renderer;

Expand Down
2 changes: 1 addition & 1 deletion .storybook/jsx.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {action} = require('@storybook/addon-actions');
const {linkTo} = require('@storybook/addon-links');
const {create} = require('../lib');

const renderer = create(h);
const renderer = create({h});
const {jsx} = renderer;

const RedBorder = jsx('div', {
Expand Down
2 changes: 1 addition & 1 deletion .storybook/nesting.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {linkTo} = require('@storybook/addon-links');
const {create} = require('../lib');
const {addon} = require('../addon/nesting');

const renderer = create(h);
const renderer = create({h});
addon(renderer);
const {rule} = renderer;

Expand Down
26 changes: 26 additions & 0 deletions .storybook/put.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 renderer = create();
const {put} = renderer;

put('red-border', {
border: '1px solid red',
':hover': {
fontWeight: 'bold'
},
span: {
color: 'red'
}
});

storiesOf('put()', module)
.add('Default', () =>
h('div', {className: 'red-border'}, 'Hello world')
)
.add('Nesting', () =>
h('div', {className: 'red-border'}, 'Hello ', h('span', null, 'world'))
)
6 changes: 4 additions & 2 deletions .storybook/rule.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ 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('../lib');
const {create} = require('../index');
const {addon} = require('../addon/rule');

const renderer = create(h);
const renderer = create();
addon(renderer);
const {rule} = renderer;

const className1 = rule({
Expand Down
6 changes: 3 additions & 3 deletions .storybook/sheet.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ 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('../lib');
const {create} = require('../index');
const {addon} = require('../addon/sheet');

const renderer = create(h);
const renderer = create({h});
addon(renderer);
const {sheet} = renderer;

Expand All @@ -28,7 +28,7 @@ const styles2 = renderer.sheet({
},
});

storiesOf('Addons/sheet()', module)
storiesOf('sheet()', module)
.add('Default', () =>
h('div', null,
h('div', {className: styles.tomato}, 'Red'),
Expand Down
2 changes: 1 addition & 1 deletion .storybook/stable.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {linkTo} = require('@storybook/addon-links');
const {create} = require('../lib');
const {addon} = require('../addon/stable');

const renderer = create(h);
const renderer = create({h});
addon(renderer);
const {rule} = renderer;

Expand Down
25 changes: 18 additions & 7 deletions .storybook/styled.stories.js → .storybook/style.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ 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('../lib');
const {addon} = require('../addon/styled');
const {create} = require('../index');
const {addon: addonRule} = require('../addon/rule');
const {addon: addonJsx} = require('../addon/jsx');
const {addon: addonStyle} = require('../addon/style');

const renderer = create(h);
addon(renderer);
const {styled} = renderer;
const renderer = create({h});
addonRule(renderer);
addonJsx(renderer);
addonStyle(renderer);
const {style} = renderer;

const RedBorder = styled('div', {
const RedBorder = style('div', {
border: '1px solid red'
}, (props) => {
if (props.primary) {
Expand All @@ -19,7 +23,11 @@ const RedBorder = styled('div', {
}
});

storiesOf('Addons/styled()', module)
const RedBorderItalic = style(RedBorder, {
fontStyle: 'italic'
});

storiesOf('style()', module)
.add('Default', () =>
h(RedBorder, null, 'Hello world')
)
Expand All @@ -29,3 +37,6 @@ storiesOf('Addons/styled()', module)
.add('Inline styles', () =>
h(RedBorder, {style: {color: 'red'}, primary: true}, 'Hello world')
)
.add('Composability', () =>
h(RedBorderItalic, null, 'Hello world')
)
2 changes: 1 addition & 1 deletion .storybook/useStyles.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {action} = require('@storybook/addon-actions');
const {linkTo} = require('@storybook/addon-links');
const {create} = require('../lib');

const renderer = create(h);
const renderer = create({h});
require('../addon/sheet').addon(renderer);
require('../addon/useStyles').addon(renderer);
const {useStyles} = renderer;
Expand Down
2 changes: 1 addition & 1 deletion .storybook/withStyles.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {action} = require('@storybook/addon-actions');
const {linkTo} = require('@storybook/addon-links');
const {create} = require('../lib');

const renderer = create(h);
const renderer = create({h});
require('../addon/sheet').addon(renderer);
require('../addon/withStyles').addon(renderer);
const {withStyles} = renderer;
Expand Down
17 changes: 17 additions & 0 deletions addon/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

exports.addon = function (renderer) {
var cache = {};

renderer.cache = function (styles) {
if (!styles) return '';

var key = renderer.hash(styles);

if (!cache[key]) {
cache[key] = renderer.rule(styles, key);
}

return cache[key];
};
};
35 changes: 35 additions & 0 deletions addon/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var KEBAB_REGEX = /[A-Z]/g;

var hash = function (str) {
var hash = 5381, i = str.length;

while (i) hash = (hash * 33) ^ str.charCodeAt(--i);

return '_' + (hash >>> 0).toString(36);
};

exports.addon = function (renderer, config) {
config = config || {};
var extend = config.extend || Object.assign;

extend(renderer, {
raw: '',
cns: {},
pfx: '_',
client: typeof window === 'object',
extend: extend,
stringify: JSON.stringify,
decl: function (key, value) {
key = key.replace(KEBAB_REGEX, '-$&').toLowerCase();
return key + ':' + value + ';';
},
hash: function (obj) {
return hash(renderer.stringify(obj));
},
selector: function (parent, selector) {
return parent + (selector[0] === ':' ? '' : ' ') + selector;
},
}, config);
};
35 changes: 29 additions & 6 deletions addon/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
var cloneElement = require('react').cloneElement;

exports.addon = function (renderer) {
var transformStatic = function (fn, styles, block) {
block = block || fn.displayName || fn.name;

var render_ = fn.prototype.render;
var transformStatic = function (prototype, styles, block) {
var render_ = prototype.render;
var className = '';

fn.prototype.render = function() {
prototype.render = function() {
var element = render_.call(this);

if (element) {
Expand All @@ -30,9 +28,34 @@ exports.addon = function (renderer) {
};
};

/*
var transformDynamic = function (prototype, dynamicTemplate) {
var render_ = prototype.render;
prototype.render = function () {
var element = render_.apply(this, arguments);
var props = element.props;
var className =
(props.className || '') +
renderer.render(this.constructor, this, this[$$el], dynamicTemplate(this));
if (process.env.NODE_ENV === 'production') {
element.ref = ref;
props.className = className;
return element;
}
return cloneElement(element, Object.assign({}, props, {ref: ref, className: className}), props.children);
};
};
*/

renderer.css = function (a, b) {
return function (Klass) {
transformStatic(Klass, a, b);
var block = b || Klass.displayName || Klass.name;
var prototype = Klass.prototype;

transformStatic(prototype, a, block);
};
};
};
44 changes: 44 additions & 0 deletions addon/jsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

var addonCache = require('./cache').addon;

exports.addon = function (renderer) {
if (!renderer.cache) {
addonCache(renderer);
}

renderer.jsx = function (fn, styles, block) {
var className;
var isElement = typeof fn === 'string';

if (!block && !isElement)
block = fn.displayName || fn.name;

var Component = function (props) {
if (!className) {
className = renderer.rule(styles, block);
}

var copy = props;

if (process.env.NODE_ENV !== 'production') {
copy = Object.assign({}, props);
}

var dynamicClassName = renderer.cache(props.css);
delete copy.css;

copy.className = (props.className || '') + className + dynamicClassName;

return isElement ? renderer.h(fn, copy) : fn(copy);
};

if (process.env.NODE_EVN !== 'production') {
if (block) {
Component.displayName = 'jsx(' + block + ')';
}
}

return Component;
};
};
42 changes: 42 additions & 0 deletions addon/put.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

exports.addon = function (renderer) {
var putRaw;

if (renderer.client) {
var sheet = document.head.appendChild(document.createElement('style')).sheet;
putRaw = function (rawCss) {
sheet.insertRule(rawCss, 0);
};
} else {
putRaw = function (rawCss) {
renderer.raw += rawCss;
};
}

var put = function (selector, decls, atrule) {
var str = '';
var prop, value;

for (prop in decls) {
value = decls[prop];

if (value instanceof Object) {
if (prop[0] === '@') {
put(selector, value, prop);
} else {
put(renderer.selector(selector, prop), value, atrule);
}
} else {
str += renderer.decl(prop, value);
}
}

if (str) {
str = '.' + selector + '{' + str + '}';
putRaw(atrule ? atrule + '{' + str + '}' : str);
}
};

renderer.put = put;
};
Loading

0 comments on commit 864f67b

Please sign in to comment.