Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor changes!! #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# http.editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ jspm_packages

.DS_Store
css-constructor.js

# VSCode settings folder
.vscode
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ export default class Hello extends React.Component {
`

render () {
return (<div> // 🔼 Attaches class to the highest element
<img src="https://github.com/siddharthkp.png"/>
<div id="handle">@siddharthkp</div>
</div>)
return (
<div> // 🔼 Attaches class to the highest element
<img src="https://github.com/siddharthkp.png"/>
<div id="handle">@siddharthkp</div>
</div>
)
}
};

Expand Down Expand Up @@ -77,7 +79,7 @@ export default class Hello extends React.Component {

#### Usage

1. `npm install css-constructor --save`
1. `npm install css-constructor`

2. `import css from 'css-constructor'`

Expand Down
42 changes: 22 additions & 20 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,25 @@ import stylis from 'stylis'
this function gets the parent class, name of the function -
render in this case and the descriptor for the function.
*/

let css = rawCSS => (parentClass, name, descriptor) => ({
...descriptor,
value: function() {
let originalProps

/* Totally stealing props by fake rendering the component */
let getProps = object => {
originalProps = object.props
return object
}
let rendered = descriptor.value.apply(getProps(this), arguments)

/* Replace props and return realCSS™ */
let realCSS = fillProps(rawCSS, originalProps)
let
originalProps,
getProps = object => { /* Totally stealing props by fake rendering the component */
originalProps = object.props
return object
},
rendered = descriptor.value.apply(getProps(this), arguments),
realCSS = fillProps(rawCSS, originalProps) /* Replace props and return realCSS™ */

/* Merge classNames */
const existingClassNames = rendered.props.className || ''
let className = `${existingClassNames} ${insertRules(realCSS)}`

/* Convert real CSS to javascripty CSS */
//let style = parseCss(realCSS);
// let style = parseCss(realCSS);

/* Merge styles into original props */
let newProps = { ...originalProps, className }
Expand All @@ -56,13 +54,18 @@ let css = rawCSS => (parentClass, name, descriptor) => ({
Does not evaluate conditions (yet)
color: {this.props.color || 'blue'}
*/

let fillProps = (rawCSS, props) => {
rawCSS = rawCSS[0] // template literal = array
let re = /{this.props.*}/g
let matches = rawCSS.match(re)
let
re = /{this.props.*}/g,
matches = rawCSS.match(re)
if (matches && matches.length) {
for (let match of matches) {
let keyword = match, replaceWord, propKeys
let
keyword = match,
replaceWord,
propKeys
keyword = keyword.replace('{this.props.', '')
keyword = keyword.substring(0, keyword.length - 1) // remove }
keyword = keyword.trim()
Expand All @@ -82,11 +85,10 @@ let fillProps = (rawCSS, props) => {
*/

let insertRules = realCSS => {
let style = getStyleElement()
/* Get unique classname */
let className = getHash(realCSS)
/* Convert nested CSS */
let styles = stylis(`.${className}`, realCSS)
let
style = getStyleElement(), /* Get unique classname */
className = getHash(realCSS), /* Convert nested CSS */
styles = stylis(`.${className}`, realCSS)
style.innerHTML += styles
return className
}
Expand Down
5 changes: 4 additions & 1 deletion example/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ let color = {
sampleColor: 'papayawhip'
}

ReactDOM.render(<Hello color={color} />, document.getElementById('container'))
ReactDOM.render(
<Hello color={color} />,
document.getElementById('container')
)
2 changes: 2 additions & 0 deletions example/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react'
import css from './css-constructor' // development - for me

export default class Hello extends React.Component {

/* javascript constructor */
constructor(props) {
super(props)
Expand Down Expand Up @@ -34,6 +35,7 @@ export default class Hello extends React.Component {
& {font-size: 18px;}
}
`

render() {
return (
<div className="existing">
Expand Down