-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nparashar150/utils/styleToString
Feat: create function to convert JsObjectCss to CssString
- Loading branch information
Showing
5 changed files
with
65 additions
and
96 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
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 | ||
"" | ||
); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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; | ||
}; |
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