Skip to content

Commit b36b6eb

Browse files
committed
Update APIs
1 parent 42f65ee commit b36b6eb

File tree

7 files changed

+346
-159
lines changed

7 files changed

+346
-159
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Using ReactCSSOM means:
3131
- No limitations to CSS power
3232
- Easy to use with CSS Preprocessors (no more stuff needed)
3333
- Highly expressive
34-
- Lightweight, only 50 lines of js
34+
- Lightweight, only 100 lines of js
3535

3636
## Installation
3737

@@ -171,7 +171,7 @@ and here is the corresponding css, note the global selector:
171171

172172
### Additional API
173173

174-
ReactCSSOM also exposes 2 simple API to mount and unmount styles, here they are:
174+
ReactCSSOM also exposes 2 simple API to mount and unmount styles in the DOM, here they are:
175175

176176
```js
177177
import ReactCSSOM from 'react-cssom';

src/classInjector.js

-35
This file was deleted.

src/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import initClassInjector from './classInjector';
2-
3-
initClassInjector();
1+
import React from 'react';
2+
import inject from './injector';
43

54
const ReactCSSOM = {
5+
initInjector: () => {
6+
React.Component = inject(React.Component)();
7+
},
8+
inject,
69
mount: (css) => {
710
const head = document.head || document.getElementsByTagName('head')[0];
811
const style = document.createElement('style');

src/injector.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React from 'react';
2+
3+
const getReactCSSOMClass = (componentName) => `⚛${componentName}`;
4+
5+
function injectInElement(component, componentName) {
6+
let result = component;
7+
if (component) {
8+
const classToInject = getReactCSSOMClass(componentName);
9+
if (typeof component.type === 'string') {
10+
result = React.cloneElement(
11+
component,
12+
{
13+
className: `${classToInject} ${component.props.className || ''}`,
14+
}
15+
);
16+
} else if (typeof component.type === 'function') {
17+
result = (
18+
<div className={`${classToInject}`}>
19+
{component}
20+
</div>
21+
);
22+
}
23+
}
24+
return result;
25+
}
26+
27+
function injectInComponent(Component, componentName) {
28+
const componentPrototype = Component.prototype;
29+
const componentConstructor = Component;
30+
31+
//eslint-disable-next-line
32+
const newComponent = function ReactComponent(...params) {
33+
componentConstructor.apply(this, [...params]);
34+
const originalRender = this.render;
35+
this.render = () => (
36+
injectInElement(
37+
originalRender.apply(this),
38+
componentName || this.constructor.displayName || this.constructor.name
39+
)
40+
);
41+
};
42+
43+
//eslint-disable-next-line
44+
newComponent.prototype = componentPrototype;
45+
Object.keys(Component).forEach((x) => {
46+
newComponent[x] = Component[x];
47+
});
48+
49+
return newComponent;
50+
}
51+
52+
function injectInFunctionalComponent(component, componentName) {
53+
return (props) => injectInElement(
54+
component(props),
55+
componentName
56+
);
57+
}
58+
59+
export default function inject(component) {
60+
return (componentName) => {
61+
let result = null;
62+
if (
63+
typeof component === 'function' &&
64+
component.prototype &&
65+
component.prototype.isReactComponent
66+
) {
67+
result = injectInComponent(component, componentName);
68+
} else if (typeof component === 'function') {
69+
result = injectInFunctionalComponent(component, componentName);
70+
} else if (component !== null && typeof component === 'object') {
71+
result = injectInElement(component, componentName);
72+
}
73+
return result;
74+
};
75+
}

test/index.js renamed to test/DOMUtils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import expect from 'expect';
22
import { jsdom } from 'jsdom';
33
import ReactCSSOM from '../src/index';
44

5-
describe('Class', () => {
5+
describe('DOMUtils', () => {
66
afterEach(() => {
77
global.document = jsdom('<!doctype html><html><head></head><body></body></html>');
88
});

test/classInjector.js

-118
This file was deleted.

0 commit comments

Comments
 (0)