-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
397 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import styled, {KEYS, map, create} from '@reshadow/core'; | ||
import {createStyled, css} from '@reshadow/runtime'; | ||
import tags from '@reshadow/utils/html-tags'; | ||
|
||
export function jsx() { | ||
const args = Array.prototype.slice.call(arguments); | ||
const element = args[0]; | ||
if (typeof element === 'string' && !tags.has(element)) { | ||
args[0] = 'div'; | ||
} | ||
args[1] = map(element, args[1]); | ||
return React.createElement.apply(null, args); | ||
} | ||
|
||
export {css, create}; | ||
|
||
const reactStyled = createStyled(elem => { | ||
let style = styled[KEYS.__style__]; | ||
let result = styled(elem); | ||
if (style) { | ||
result = React.cloneElement(result, { | ||
style: Object.assign(style, result.props.style), | ||
}); | ||
} | ||
return result; | ||
}); | ||
|
||
reactStyled.jsx = jsx; | ||
|
||
export default reactStyled; |
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,36 @@ | ||
{ | ||
"name": "@reshadow/react", | ||
"version": "0.0.1-alpha.41", | ||
"description": "reshadow react", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/lttb/reshadow.git" | ||
}, | ||
"author": "Kenzhaev Artur <kenzhaev.artur@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/lttb/reshadow/issues" | ||
}, | ||
"homepage": "https://reshadow.dev", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"build": "../../build.sh && ./build.sh" | ||
}, | ||
"dependencies": { | ||
"@reshadow/core": "^0.0.1-alpha.41", | ||
"@reshadow/runtime": "^0.0.1-alpha.41", | ||
"@reshadow/utils": "^0.0.1-alpha.30" | ||
}, | ||
"optionalDependencies": { | ||
"react": ">=15" | ||
}, | ||
"devDependencies": { | ||
"enzyme": "^3.8.0", | ||
"enzyme-adapter-react-16": "^1.7.1", | ||
"react": "^16.7.0", | ||
"react-dom": "^16.7.0" | ||
} | ||
} |
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,65 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`react should apply dynamic styles 1`] = ` | ||
<button | ||
class="__button_xkvkx5" | ||
style="--xkvkx5_1:red" | ||
> | ||
click me | ||
</button> | ||
`; | ||
|
||
exports[`react should apply dynamic styles 2`] = `".__button_3rhptm{color:red;}.__button_xkvkx5{color:var(--xkvkx5_1);}"`; | ||
|
||
exports[`react should apply dynamic styles 3`] = ` | ||
<button | ||
class="__button_xkvkx5" | ||
style="--xkvkx5_1:green" | ||
> | ||
click me | ||
</button> | ||
`; | ||
|
||
exports[`react should apply dynamic styles 4`] = `".__button_3rhptm{color:red;}.__button_xkvkx5{color:var(--xkvkx5_1);}"`; | ||
|
||
exports[`react should apply external dynamic styles 1`] = ` | ||
<button | ||
class="__button_xkvkx5" | ||
style="--xkvkx5_1:red" | ||
> | ||
click me | ||
</button> | ||
`; | ||
|
||
exports[`react should apply external dynamic styles 2`] = `".__button_3rhptm{color:red;}.__button_xkvkx5{color:var(--xkvkx5_1);}"`; | ||
|
||
exports[`react should apply external dynamic styles 3`] = ` | ||
<button | ||
class="__button_xkvkx5" | ||
style="--xkvkx5_1:green" | ||
> | ||
click me | ||
</button> | ||
`; | ||
|
||
exports[`react should apply external dynamic styles 4`] = `".__button_3rhptm{color:red;}.__button_xkvkx5{color:var(--xkvkx5_1);}"`; | ||
|
||
exports[`react should apply external styles 1`] = ` | ||
<button | ||
class="__button_3rhptm" | ||
> | ||
click me | ||
</button> | ||
`; | ||
|
||
exports[`react should apply external styles 2`] = `".__button_3rhptm{color:red;}.__button_xkvkx5{color:var(--xkvkx5_1);}"`; | ||
|
||
exports[`react should apply styles 1`] = ` | ||
<button | ||
class="__button_3rhptm" | ||
> | ||
click me | ||
</button> | ||
`; | ||
|
||
exports[`react should apply styles 2`] = `".__button_3rhptm{color:red;}"`; |
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,78 @@ | ||
/** | ||
* @jest-environment enzyme | ||
*/ | ||
|
||
/** @jsx jsx */ | ||
import styled, {css, jsx} from '..'; | ||
import {render, shallow} from 'enzyme'; | ||
|
||
const getStyles = () => | ||
[...document.getElementsByTagName('style')] | ||
.map(x => x.textContent) | ||
.join(''); | ||
|
||
describe('react', () => { | ||
it('should apply styles', () => { | ||
const Button = ({children, ...props}) => styled` | ||
button { | ||
color: red; | ||
} | ||
`(<button>{children}</button>); | ||
|
||
const wrapper = render(<Button>click me</Button>); | ||
expect(wrapper).toMatchSnapshot(); | ||
expect(getStyles()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should apply dynamic styles', () => { | ||
const Button = ({children, color, ...props}) => styled` | ||
button { | ||
color: ${color}; | ||
} | ||
`(<button>{children}</button>); | ||
|
||
const wrapper = shallow(<Button color="red">click me</Button>); | ||
expect(wrapper.render()).toMatchSnapshot(); | ||
expect(getStyles()).toMatchSnapshot(); | ||
|
||
wrapper.setProps({color: 'green'}); | ||
expect(wrapper.render()).toMatchSnapshot(); | ||
expect(getStyles()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should apply external styles', () => { | ||
const styles = css` | ||
button { | ||
color: red; | ||
} | ||
`; | ||
|
||
const Button = ({children, color, ...props}) => styled(styles)( | ||
<button>{children}</button>, | ||
); | ||
|
||
const wrapper = render(<Button>click me</Button>); | ||
expect(wrapper).toMatchSnapshot(); | ||
expect(getStyles()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should apply external dynamic styles', () => { | ||
const styles = ({color}) => css` | ||
button { | ||
color: ${color}; | ||
} | ||
`; | ||
|
||
const Button = ({children, color, ...props}) => styled(styles({color}))( | ||
<button>{children}</button>, | ||
); | ||
|
||
const wrapper = shallow(<Button color="red">click me</Button>); | ||
expect(wrapper.render()).toMatchSnapshot(); | ||
expect(getStyles()).toMatchSnapshot(); | ||
|
||
wrapper.setProps({color: 'green'}); | ||
expect(wrapper.render()).toMatchSnapshot(); | ||
expect(getStyles()).toMatchSnapshot(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from '@reshadow/core'; | ||
export {default} from '@reshadow/core'; | ||
export * from '@reshadow/react'; | ||
export {default} from '@reshadow/react'; |
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,71 @@ | ||
import {use, set, create, __css__, KEYS} from '@reshadow/core'; | ||
import stringHash from 'string-hash'; | ||
import parse from './parse'; | ||
|
||
export {use}; | ||
|
||
const cache = {}; | ||
|
||
function css() { | ||
const str = arguments[0]; | ||
const hash = stringHash(str.raw.join('')).toString(36); | ||
let parsed; | ||
|
||
const vars = {}; | ||
for (let i = 1, len = arguments.length; i < len; i++) { | ||
const name = '--' + hash + '_' + i; | ||
vars[name] = arguments[i]; | ||
} | ||
|
||
if (cache[hash]) { | ||
parsed = cache[hash]; | ||
} else { | ||
const values = []; | ||
for (let name in vars) { | ||
values.push('var(' + name + ')'); | ||
} | ||
const code = String.raw(str, ...values); | ||
parsed = parse(code, hash); | ||
__css__(parsed.css, hash); | ||
cache[hash] = parsed; | ||
} | ||
cache[hash].tokens[KEYS.__style__] = vars; | ||
return cache[hash].tokens; | ||
} | ||
|
||
function createStyled(styled) { | ||
let styles = null; | ||
|
||
function taggedStyled() { | ||
const str = arguments[0]; | ||
if (!(str[0] && str.raw)) { | ||
return styled.apply(null, arguments); | ||
} | ||
|
||
const tokens = css.apply(null, arguments); | ||
|
||
if (styles) { | ||
styled(); | ||
} | ||
|
||
set([styles, tokens]); | ||
styles = null; | ||
|
||
return styled; | ||
} | ||
|
||
function carriedStyled() { | ||
const str = arguments[0]; | ||
if (str[0] && str.raw) { | ||
return taggedStyled.apply(null, arguments); | ||
} else { | ||
styles = create(Array.prototype.slice.call(arguments)); | ||
set([styles]); | ||
return taggedStyled; | ||
} | ||
} | ||
|
||
return carriedStyled; | ||
} | ||
|
||
export {createStyled, css}; |
Oops, something went wrong.