Skip to content

Instantly share code, notes, and snippets.

@mauroquinteros
Created October 19, 2020 21:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauroquinteros/d60296696209ad01e5da0d5c0cae8f57 to your computer and use it in GitHub Desktop.
Save mauroquinteros/d60296696209ad01e5da0d5c0cae8f57 to your computer and use it in GitHub Desktop.
Styled Component with tagged template
function component(strings, ...values) {
return function (props) {
let newContent = strings.slice();
values.forEach((value, index) => {
newContent[index] += props[value];
});
return newContent.join("");
};
}
function render(component, container) {
container.innerHTML = component;
}
function createStyledApi(htmlTag) {
return (styles) => (content) => `
<${htmlTag} style="${styles}">${content}</${htmlTag}>
`;
}
const tags = ["h1", "h2", "h3", "p", "div"];
function createObjectStyled(tags) {
return tags.reduce((acc, tag) => {
let attr = tag;
let newObj = { ...acc };
newObj[attr] = createStyledApi(tag);
return newObj;
}, {});
}
const styledApi = createObjectStyled(tags);
console.log(styledApi);
// Creando los estilos
const TitleStyled = styledApi.h3`
color: orange;
font-family: system-ui;
font-size: 30px;
text-shadow: 1px 1px 0 black;
`;
// Agregando el contenido
const props = {
firstName: "Mauro",
lastName: "Quinteros",
};
const TitleContent = component`Hola ${"firstName"} ${"lastName"}`(props);
console.log(TitleContent);
// Combinando el contenido con los estilos
const TitleComponent = TitleStyled(TitleContent);
render(TitleComponent, window.container);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment