I’m testing how to apply CSS to the HTML that comes from WordPress.
The first way that may come to mind is to create a styled component and use a processor to add it:
const Styled1 = styled.a`
color: blue;
`;
export const blockButtonStyled1 = {
test: node =>
node.component === "a" &&
node.props.className.split(" ").includes("wp-block-button__link"),
process: node => {
node.component = Styled1;
return node;
}
};
The problem with this approach is that it makes impossible to add more styles to the wp-block-button__link
after the first processor.
For example, this won’t work because node.component
is not a
anymore (it is Styled1
):
const Styled2 = styled.a`
background-color: yellow;
`;
export const blockButtonStyled2 = {
test: node =>
node.component === "a" &&
node.props.className.split(" ").includes("wp-block-button__link"),
process: node => {
node.component = Styled2;
return node;
}
};
so you can either add the blue color or the yellow background, but not both.
To solve it, we can promote the usage of the css
prop:
export const blockButtonCssProp1 = {
test: node =>
node.component === "a" &&
node.props.className.split(" ").includes("wp-block-button__link"),
process: node => {
node.props.css = css`
${node.props.css};
color: blue;
`;
return node;
}
};
This processor adds the color blue, but the node.component
is not modified and can be found by another processor executed later:
export const blockButtonCssProp2 = {
test: node =>
node.component === "a" &&
node.props.className.split(" ").includes("wp-block-button__link"),
process: node => {
node.props.css = css`
${node.props.css};
background-color: yellow;
`;
return node;
}
};
Now, your wp-block-button__link
has both a blue color and a white background.
You can also control which CSS takes precedence by positioning the ${node.props.css}
before or after your CSS:
node.props.css = css`
${node.props.css};
background-color: yellow; // <-- this will overwrite node.props.css
`;
node.props.css = css`
background-color: yellow; // <-- this won't overwrite node.props.css
${node.props.css};
`;
Apart from that, I’m not sure how’s the best way to propose, if using node.props.css
inside the css props or using an array like this:
const blue = css`
color: blue;
`;
export const blockButtonCssProp1 = {
test: node =>
node.component === "a" &&
node.props.className.split(" ").includes("wp-block-button__link"),
process: node => {
node.props.css = [node.props.css, blue];
return node;
}
};