Skip to content

Commit

Permalink
Merge pull request #4 from nparashar150/utils/styleToString
Browse files Browse the repository at this point in the history
Feat: create function to convert JsObjectCss to CssString
  • Loading branch information
prabhatexit0 authored Sep 24, 2022
2 parents 6120b98 + 5c8bc5a commit 7ed9d9b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 96 deletions.
6 changes: 6 additions & 0 deletions lib/createElement.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { styleToString } from "./utils/styleToString";

/**
*
* @param {HTMLElement} element
Expand Down Expand Up @@ -25,6 +27,10 @@ export const createElement = ({
// if prop is className
_htmlElement.setAttribute("class", props[propName]);
}
if (propName.toLowerCase() === "style") {
// if prop is style
_htmlElement.setAttribute("style", styleToString(props[propName]));
}
// rest other propNames
_htmlElement?.setAttribute(propName, props[propName]);
});
Expand Down
19 changes: 19 additions & 0 deletions lib/utils/styleToString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// converts JS object to CSS string
export const styleToString = (style) => {
// if style is not an object then return empty string
if (Object.keys(style).length === 0) return "";

// reduce Object into single string of CSS
return Object.keys(style).reduce(
(acc, key) =>
acc +
key
.split(/(?=[A-Z])/) // split camelCase
.join("-") // join with hyphen (-)
.toLowerCase() + // convert to kebab-case
":" + // add colon after key
style[key] + // add value of style
";", // add semicolon at the end of each style
""
);
};
97 changes: 1 addition & 96 deletions public/js/own-react-bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/components/button/button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createElement } from "../../../lib/createElement";

export const button = (parentElement, props, innerText) => {
if (!parentElement) return;

// styling in JS object is converted to CSS string
const buttonStyles = {
backgroundColor: "#EEE",
color: "#1a1a1a",
padding: "10px",
border: "none",
borderRadius: "5px",
cursor: "pointer",
};

const button = createElement({
element: "button",
props: {
...props,
style: buttonStyles,
},
state: {},
children: innerText,
parentElement,
});

return button;
};
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createElement } from "../lib/createElement";
import { button } from "./components/button/button";

(function createDOMNode() {
const root = document.body;
Expand Down Expand Up @@ -96,5 +97,15 @@ import { createElement } from "../lib/createElement";
],
parentElement: divCreatedFromFunction,
});

button(
root,
{
onclick: () => {
alert("You have clicked me!!");
},
},
"Click Me"
);
})();
export { createElement };

0 comments on commit 7ed9d9b

Please sign in to comment.