Skip to content

Commit

Permalink
make it work in node maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
mdarens committed Jan 14, 2016
1 parent 6b7247e commit 9d5cc7d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ const Example = (props) => (
);

```

Pass an object as a 3rd argument to any of these methods and they will add or append the styles to be injected to it as a `__cssText` property.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "buryem",
"version": "1.1.0",
"version": "1.2.0",
"description": "got style conflicts? bury 'em!",
"main": "lib/buryem.js",
"author": "Mark D'Arensbourg",
Expand Down
27 changes: 19 additions & 8 deletions src/buryem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ const oHash = (obj) => hash(JSON.stringify(obj));

const vendorPrefix = getVendorPrefix();
const insertedRuleMap = {};
const head = document.head || document.getElementsByTagName('head')[0];
const head = document ? (document.head || document.getElementsByTagName('head')[0]) : false;
let styleTag;

function appendStyle(cssText) {
function appendStyle(cssText, cssHolder) {
if (!head) {
if (cssHolder && typeof cssHolder === "object") {
if (cssHolder.__cssText) {
cssHolder.__cssText += cssText;
} else {
cssHolder.__cssText = cssText;
}
}
return;
}

if(!styleTag) {
styleTag = document.createElement('style');
Expand All @@ -23,9 +33,10 @@ function appendStyle(cssText) {
styleTag.appendChild(document.createTextNode(cssText));
}

return;
}

export const create = (styles, classNameSpace) => {
export const create = (styles, classNameSpace, cssHolder) => {
let cssText = '';
let ruleMap = {};
let ns = classNameSpace || '';
Expand All @@ -42,12 +53,12 @@ export const create = (styles, classNameSpace) => {
insertedRuleMap[selector] = true;
});

appendStyle(cssText);
appendStyle(cssText, cssHolder);

return ruleMap;
};

export const createKeyframes = (keyframes, classNameSpace) => {
export const createKeyframes = (keyframes, classNameSpace, cssHolder) => {
let ns = classNameSpace || '';
let animationName = `${ns}_${oHash(keyframes)}`;
if (!insertedRuleMap[animationName]) {
Expand All @@ -72,19 +83,19 @@ export const createKeyframes = (keyframes, classNameSpace) => {
cssText += `
}`;

appendStyle(cssText);
appendStyle(cssText, cssHolder);
}

insertedRuleMap[animationName] = true;

return animationName;
};

export const createAnimations = (animations, classNameSpace) => {
export const createAnimations = (animations, classNameSpace, cssHolder) => {
let animationMap = {};
let ns = classNameSpace || '';
Object.keys(animations).forEach((a) => {
animationMap[a] = createKeyframes(animations[a], ns);
animationMap[a] = createKeyframes(animations[a], ns, cssHolder);
});
return animationMap;
}
Expand Down

0 comments on commit 9d5cc7d

Please sign in to comment.