-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: major refactoring for addon model
- Loading branch information
Showing
26 changed files
with
467 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.